comparison src/thread/generic/SDL_syssem.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
29 29
30 30
31 #if SDL_THREADS_DISABLED 31 #if SDL_THREADS_DISABLED
32 32
33 SDL_sem * 33 SDL_sem *
34 SDL_CreateSemaphore (Uint32 initial_value) 34 SDL_CreateSemaphore(Uint32 initial_value)
35 { 35 {
36 SDL_SetError ("SDL not configured with thread support"); 36 SDL_SetError("SDL not configured with thread support");
37 return (SDL_sem *) 0; 37 return (SDL_sem *) 0;
38 } 38 }
39 39
40 void 40 void
41 SDL_DestroySemaphore (SDL_sem * sem) 41 SDL_DestroySemaphore(SDL_sem * sem)
42 { 42 {
43 return; 43 return;
44 } 44 }
45 45
46 int 46 int
47 SDL_SemTryWait (SDL_sem * sem) 47 SDL_SemTryWait(SDL_sem * sem)
48 { 48 {
49 SDL_SetError ("SDL not configured with thread support"); 49 SDL_SetError("SDL not configured with thread support");
50 return -1; 50 return -1;
51 } 51 }
52 52
53 int 53 int
54 SDL_SemWaitTimeout (SDL_sem * sem, Uint32 timeout) 54 SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
55 { 55 {
56 SDL_SetError ("SDL not configured with thread support"); 56 SDL_SetError("SDL not configured with thread support");
57 return -1; 57 return -1;
58 } 58 }
59 59
60 int 60 int
61 SDL_SemWait (SDL_sem * sem) 61 SDL_SemWait(SDL_sem * sem)
62 { 62 {
63 SDL_SetError ("SDL not configured with thread support"); 63 SDL_SetError("SDL not configured with thread support");
64 return -1; 64 return -1;
65 } 65 }
66 66
67 Uint32 67 Uint32
68 SDL_SemValue (SDL_sem * sem) 68 SDL_SemValue(SDL_sem * sem)
69 { 69 {
70 return 0; 70 return 0;
71 } 71 }
72 72
73 int 73 int
74 SDL_SemPost (SDL_sem * sem) 74 SDL_SemPost(SDL_sem * sem)
75 { 75 {
76 SDL_SetError ("SDL not configured with thread support"); 76 SDL_SetError("SDL not configured with thread support");
77 return -1; 77 return -1;
78 } 78 }
79 79
80 #else 80 #else
81 81
86 SDL_mutex *count_lock; 86 SDL_mutex *count_lock;
87 SDL_cond *count_nonzero; 87 SDL_cond *count_nonzero;
88 }; 88 };
89 89
90 SDL_sem * 90 SDL_sem *
91 SDL_CreateSemaphore (Uint32 initial_value) 91 SDL_CreateSemaphore(Uint32 initial_value)
92 { 92 {
93 SDL_sem *sem; 93 SDL_sem *sem;
94 94
95 sem = (SDL_sem *) SDL_malloc (sizeof (*sem)); 95 sem = (SDL_sem *) SDL_malloc(sizeof(*sem));
96 if (!sem) { 96 if (!sem) {
97 SDL_OutOfMemory (); 97 SDL_OutOfMemory();
98 return NULL; 98 return NULL;
99 } 99 }
100 sem->count = initial_value; 100 sem->count = initial_value;
101 sem->waiters_count = 0; 101 sem->waiters_count = 0;
102 102
103 sem->count_lock = SDL_CreateMutex (); 103 sem->count_lock = SDL_CreateMutex();
104 sem->count_nonzero = SDL_CreateCond (); 104 sem->count_nonzero = SDL_CreateCond();
105 if (!sem->count_lock || !sem->count_nonzero) { 105 if (!sem->count_lock || !sem->count_nonzero) {
106 SDL_DestroySemaphore (sem); 106 SDL_DestroySemaphore(sem);
107 return NULL; 107 return NULL;
108 } 108 }
109 109
110 return sem; 110 return sem;
111 } 111 }
112 112
113 /* WARNING: 113 /* WARNING:
114 You cannot call this function when another thread is using the semaphore. 114 You cannot call this function when another thread is using the semaphore.
115 */ 115 */
116 void 116 void
117 SDL_DestroySemaphore (SDL_sem * sem) 117 SDL_DestroySemaphore(SDL_sem * sem)
118 { 118 {
119 if (sem) { 119 if (sem) {
120 sem->count = 0xFFFFFFFF; 120 sem->count = 0xFFFFFFFF;
121 while (sem->waiters_count > 0) { 121 while (sem->waiters_count > 0) {
122 SDL_CondSignal (sem->count_nonzero); 122 SDL_CondSignal(sem->count_nonzero);
123 SDL_Delay (10); 123 SDL_Delay(10);
124 } 124 }
125 SDL_DestroyCond (sem->count_nonzero); 125 SDL_DestroyCond(sem->count_nonzero);
126 if (sem->count_lock) { 126 if (sem->count_lock) {
127 SDL_mutexP (sem->count_lock); 127 SDL_mutexP(sem->count_lock);
128 SDL_mutexV (sem->count_lock); 128 SDL_mutexV(sem->count_lock);
129 SDL_DestroyMutex (sem->count_lock); 129 SDL_DestroyMutex(sem->count_lock);
130 } 130 }
131 SDL_free (sem); 131 SDL_free(sem);
132 } 132 }
133 } 133 }
134 134
135 int 135 int
136 SDL_SemTryWait (SDL_sem * sem) 136 SDL_SemTryWait(SDL_sem * sem)
137 { 137 {
138 int retval; 138 int retval;
139 139
140 if (!sem) { 140 if (!sem) {
141 SDL_SetError ("Passed a NULL semaphore"); 141 SDL_SetError("Passed a NULL semaphore");
142 return -1; 142 return -1;
143 } 143 }
144 144
145 retval = SDL_MUTEX_TIMEDOUT; 145 retval = SDL_MUTEX_TIMEDOUT;
146 SDL_LockMutex (sem->count_lock); 146 SDL_LockMutex(sem->count_lock);
147 if (sem->count > 0) { 147 if (sem->count > 0) {
148 --sem->count; 148 --sem->count;
149 retval = 0; 149 retval = 0;
150 } 150 }
151 SDL_UnlockMutex (sem->count_lock); 151 SDL_UnlockMutex(sem->count_lock);
152 152
153 return retval; 153 return retval;
154 } 154 }
155 155
156 int 156 int
157 SDL_SemWaitTimeout (SDL_sem * sem, Uint32 timeout) 157 SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
158 { 158 {
159 int retval; 159 int retval;
160 160
161 if (!sem) { 161 if (!sem) {
162 SDL_SetError ("Passed a NULL semaphore"); 162 SDL_SetError("Passed a NULL semaphore");
163 return -1; 163 return -1;
164 } 164 }
165 165
166 /* A timeout of 0 is an easy case */ 166 /* A timeout of 0 is an easy case */
167 if (timeout == 0) { 167 if (timeout == 0) {
168 return SDL_SemTryWait (sem); 168 return SDL_SemTryWait(sem);
169 } 169 }
170 170
171 SDL_LockMutex (sem->count_lock); 171 SDL_LockMutex(sem->count_lock);
172 ++sem->waiters_count; 172 ++sem->waiters_count;
173 retval = 0; 173 retval = 0;
174 while ((sem->count == 0) && (retval != SDL_MUTEX_TIMEDOUT)) { 174 while ((sem->count == 0) && (retval != SDL_MUTEX_TIMEDOUT)) {
175 retval = SDL_CondWaitTimeout (sem->count_nonzero, 175 retval = SDL_CondWaitTimeout(sem->count_nonzero,
176 sem->count_lock, timeout); 176 sem->count_lock, timeout);
177 } 177 }
178 --sem->waiters_count; 178 --sem->waiters_count;
179 --sem->count; 179 --sem->count;
180 SDL_UnlockMutex (sem->count_lock); 180 SDL_UnlockMutex(sem->count_lock);
181 181
182 return retval; 182 return retval;
183 } 183 }
184 184
185 int 185 int
186 SDL_SemWait (SDL_sem * sem) 186 SDL_SemWait(SDL_sem * sem)
187 { 187 {
188 return SDL_SemWaitTimeout (sem, SDL_MUTEX_MAXWAIT); 188 return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT);
189 } 189 }
190 190
191 Uint32 191 Uint32
192 SDL_SemValue (SDL_sem * sem) 192 SDL_SemValue(SDL_sem * sem)
193 { 193 {
194 Uint32 value; 194 Uint32 value;
195 195
196 value = 0; 196 value = 0;
197 if (sem) { 197 if (sem) {
198 SDL_LockMutex (sem->count_lock); 198 SDL_LockMutex(sem->count_lock);
199 value = sem->count; 199 value = sem->count;
200 SDL_UnlockMutex (sem->count_lock); 200 SDL_UnlockMutex(sem->count_lock);
201 } 201 }
202 return value; 202 return value;
203 } 203 }
204 204
205 int 205 int
206 SDL_SemPost (SDL_sem * sem) 206 SDL_SemPost(SDL_sem * sem)
207 { 207 {
208 if (!sem) { 208 if (!sem) {
209 SDL_SetError ("Passed a NULL semaphore"); 209 SDL_SetError("Passed a NULL semaphore");
210 return -1; 210 return -1;
211 } 211 }
212 212
213 SDL_LockMutex (sem->count_lock); 213 SDL_LockMutex(sem->count_lock);
214 if (sem->waiters_count > 0) { 214 if (sem->waiters_count > 0) {
215 SDL_CondSignal (sem->count_nonzero); 215 SDL_CondSignal(sem->count_nonzero);
216 } 216 }
217 ++sem->count; 217 ++sem->count;
218 SDL_UnlockMutex (sem->count_lock); 218 SDL_UnlockMutex(sem->count_lock);
219 219
220 return 0; 220 return 0;
221 } 221 }
222 222
223 #endif /* SDL_THREADS_DISABLED */ 223 #endif /* SDL_THREADS_DISABLED */