comparison src/thread/generic/SDL_sysmutex.c @ 1668:4da1ee79c9af SDL-1.3

more tweaking indent options
author Sam Lantinga <slouken@libsdl.org>
date Mon, 29 May 2006 04:04:35 +0000
parents 782fd950bd46
children
comparison
equal deleted inserted replaced
1667:1fddae038bc8 1668:4da1ee79c9af
34 SDL_sem *sem; 34 SDL_sem *sem;
35 }; 35 };
36 36
37 /* Create a mutex */ 37 /* Create a mutex */
38 SDL_mutex * 38 SDL_mutex *
39 SDL_CreateMutex (void) 39 SDL_CreateMutex(void)
40 { 40 {
41 SDL_mutex *mutex; 41 SDL_mutex *mutex;
42 42
43 /* Allocate mutex memory */ 43 /* Allocate mutex memory */
44 mutex = (SDL_mutex *) SDL_malloc (sizeof (*mutex)); 44 mutex = (SDL_mutex *) SDL_malloc(sizeof(*mutex));
45 if (mutex) { 45 if (mutex) {
46 /* Create the mutex semaphore, with initial value 1 */ 46 /* Create the mutex semaphore, with initial value 1 */
47 mutex->sem = SDL_CreateSemaphore (1); 47 mutex->sem = SDL_CreateSemaphore(1);
48 mutex->recursive = 0; 48 mutex->recursive = 0;
49 mutex->owner = 0; 49 mutex->owner = 0;
50 if (!mutex->sem) { 50 if (!mutex->sem) {
51 SDL_free (mutex); 51 SDL_free(mutex);
52 mutex = NULL; 52 mutex = NULL;
53 } 53 }
54 } else { 54 } else {
55 SDL_OutOfMemory (); 55 SDL_OutOfMemory();
56 } 56 }
57 return mutex; 57 return mutex;
58 } 58 }
59 59
60 /* Free the mutex */ 60 /* Free the mutex */
61 void 61 void
62 SDL_DestroyMutex (SDL_mutex * mutex) 62 SDL_DestroyMutex(SDL_mutex * mutex)
63 { 63 {
64 if (mutex) { 64 if (mutex) {
65 if (mutex->sem) { 65 if (mutex->sem) {
66 SDL_DestroySemaphore (mutex->sem); 66 SDL_DestroySemaphore(mutex->sem);
67 } 67 }
68 SDL_free (mutex); 68 SDL_free(mutex);
69 } 69 }
70 } 70 }
71 71
72 /* Lock the semaphore */ 72 /* Lock the semaphore */
73 int 73 int
74 SDL_mutexP (SDL_mutex * mutex) 74 SDL_mutexP(SDL_mutex * mutex)
75 { 75 {
76 #if SDL_THREADS_DISABLED 76 #if SDL_THREADS_DISABLED
77 return 0; 77 return 0;
78 #else 78 #else
79 Uint32 this_thread; 79 Uint32 this_thread;
80 80
81 if (mutex == NULL) { 81 if (mutex == NULL) {
82 SDL_SetError ("Passed a NULL mutex"); 82 SDL_SetError("Passed a NULL mutex");
83 return -1; 83 return -1;
84 } 84 }
85 85
86 this_thread = SDL_ThreadID (); 86 this_thread = SDL_ThreadID();
87 if (mutex->owner == this_thread) { 87 if (mutex->owner == this_thread) {
88 ++mutex->recursive; 88 ++mutex->recursive;
89 } else { 89 } else {
90 /* The order of operations is important. 90 /* The order of operations is important.
91 We set the locking thread id after we obtain the lock 91 We set the locking thread id after we obtain the lock
92 so unlocks from other threads will fail. 92 so unlocks from other threads will fail.
93 */ 93 */
94 SDL_SemWait (mutex->sem); 94 SDL_SemWait(mutex->sem);
95 mutex->owner = this_thread; 95 mutex->owner = this_thread;
96 mutex->recursive = 0; 96 mutex->recursive = 0;
97 } 97 }
98 98
99 return 0; 99 return 0;
100 #endif /* SDL_THREADS_DISABLED */ 100 #endif /* SDL_THREADS_DISABLED */
101 } 101 }
102 102
103 /* Unlock the mutex */ 103 /* Unlock the mutex */
104 int 104 int
105 SDL_mutexV (SDL_mutex * mutex) 105 SDL_mutexV(SDL_mutex * mutex)
106 { 106 {
107 #if SDL_THREADS_DISABLED 107 #if SDL_THREADS_DISABLED
108 return 0; 108 return 0;
109 #else 109 #else
110 if (mutex == NULL) { 110 if (mutex == NULL) {
111 SDL_SetError ("Passed a NULL mutex"); 111 SDL_SetError("Passed a NULL mutex");
112 return -1; 112 return -1;
113 } 113 }
114 114
115 /* If we don't own the mutex, we can't unlock it */ 115 /* If we don't own the mutex, we can't unlock it */
116 if (SDL_ThreadID () != mutex->owner) { 116 if (SDL_ThreadID() != mutex->owner) {
117 SDL_SetError ("mutex not owned by this thread"); 117 SDL_SetError("mutex not owned by this thread");
118 return -1; 118 return -1;
119 } 119 }
120 120
121 if (mutex->recursive) { 121 if (mutex->recursive) {
122 --mutex->recursive; 122 --mutex->recursive;
125 First reset the owner so another thread doesn't lock 125 First reset the owner so another thread doesn't lock
126 the mutex and set the ownership before we reset it, 126 the mutex and set the ownership before we reset it,
127 then release the lock semaphore. 127 then release the lock semaphore.
128 */ 128 */
129 mutex->owner = 0; 129 mutex->owner = 0;
130 SDL_SemPost (mutex->sem); 130 SDL_SemPost(mutex->sem);
131 } 131 }
132 return 0; 132 return 0;
133 #endif /* SDL_THREADS_DISABLED */ 133 #endif /* SDL_THREADS_DISABLED */
134 } 134 }
135 135