Mercurial > sdl-ios-xcode
annotate src/thread/linux/SDL_syssem.c @ 1300:fd068ab116ee
Removed some old Mac OS X cruft.
Fixes Bugzilla #64.
--ryan.
author | Ryan C. Gordon <icculus@icculus.org> |
---|---|
date | Mon, 30 Jan 2006 18:56:30 +0000 |
parents | b8d311d90021 |
children | c9b51268668f |
rev | line source |
---|---|
0 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
769
b8d311d90021
Updated copyright information for 2004 (Happy New Year!)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
3 Copyright (C) 1997-2004 Sam Lantinga |
0 | 4 |
5 This library is free software; you can redistribute it and/or | |
6 modify it under the terms of the GNU Library General Public | |
7 License as published by the Free Software Foundation; either | |
8 version 2 of the License, or (at your option) any later version. | |
9 | |
10 This library is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 Library General Public License for more details. | |
14 | |
15 You should have received a copy of the GNU Library General Public | |
16 License along with this library; if not, write to the Free | |
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | |
19 Sam Lantinga | |
252
e8157fcb3114
Updated the source with the correct e-mail address
Sam Lantinga <slouken@libsdl.org>
parents:
94
diff
changeset
|
20 slouken@libsdl.org |
0 | 21 */ |
22 | |
23 #ifdef SAVE_RCSID | |
24 static char rcsid = | |
25 "@(#) $Id$"; | |
26 #endif | |
27 | |
28 #include <stdlib.h> | |
29 #include "SDL_error.h" | |
30 #include "SDL_thread.h" | |
31 #include "SDL_timer.h" | |
32 | |
33 #ifdef linux | |
34 /* Look to see if glibc is available, and if so, what version */ | |
35 #include <features.h> | |
36 | |
37 #if (__GLIBC__ == 2) && (__GLIBC_MINOR__ == 0) | |
38 #warning Working around a bug in glibc 2.0 pthreads | |
39 #undef SDL_USE_PTHREADS | |
40 /* The bug is actually a problem where threads are suspended, but don't | |
41 wake up when the thread manager sends them a signal. This is a problem | |
42 with thread creation too, but it happens less often. :-/ | |
43 We avoid this by using System V IPC for semaphores. | |
44 */ | |
45 #endif /* glibc 2.0 */ | |
46 #endif /* linux */ | |
47 | |
48 #ifdef SDL_USE_PTHREADS | |
49 | |
50 #ifdef SDL_NO_PTHREAD_SEMAPHORES | |
94
ae6e6b73333f
Cleaned up the OpenBSD port, thanks to Peter Valchev
Sam Lantinga <slouken@lokigames.com>
parents:
18
diff
changeset
|
51 #include "generic/SDL_syssem.c" |
0 | 52 #else |
53 | |
54 #include <stdio.h> | |
55 #include <stdlib.h> | |
56 #include <unistd.h> /* For getpid() */ | |
18
d9e3595b63d5
Added pthread.h - necessary on some configurations
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
57 #include <pthread.h> |
0 | 58 #include <semaphore.h> |
59 | |
60 /* Wrapper around POSIX 1003.1b semaphores */ | |
61 | |
62 #ifdef MACOSX | |
63 #define USE_NAMED_SEMAPHORES | |
64 #endif /* MACOSX */ | |
65 | |
66 struct SDL_semaphore { | |
67 sem_t *sem; | |
68 #ifndef USE_NAMED_SEMAPHORES | |
69 sem_t sem_data; | |
70 #endif | |
71 }; | |
72 | |
73 /* Create a semaphore, initialized with value */ | |
74 SDL_sem *SDL_CreateSemaphore(Uint32 initial_value) | |
75 { | |
76 SDL_sem *sem = (SDL_sem *) malloc(sizeof(SDL_sem)); | |
77 if ( sem ) { | |
78 #ifdef USE_NAMED_SEMAPHORES | |
79 static int semnum = 0; | |
80 char name[32]; | |
81 | |
82 sprintf(name, "/SDL_sem-%d-%4.4d", getpid(), semnum++); | |
83 sem->sem = sem_open(name, O_CREAT, 0600, initial_value); | |
84 if ( sem->sem == (sem_t *)SEM_FAILED ) { | |
85 SDL_SetError("sem_open(%s) failed", name); | |
86 free(sem); | |
87 sem = NULL; | |
88 } else { | |
89 sem_unlink(name); | |
90 } | |
91 #else | |
92 if ( sem_init(&sem->sem_data, 0, initial_value) < 0 ) { | |
93 SDL_SetError("sem_init() failed"); | |
94 free(sem); | |
95 sem = NULL; | |
96 } else { | |
97 sem->sem = &sem->sem_data; | |
98 } | |
99 #endif /* USE_NAMED_SEMAPHORES */ | |
100 } else { | |
101 SDL_OutOfMemory(); | |
102 } | |
103 return sem; | |
104 } | |
105 | |
106 void SDL_DestroySemaphore(SDL_sem *sem) | |
107 { | |
108 if ( sem ) { | |
109 #ifdef USE_NAMED_SEMAPHORES | |
110 sem_close(sem->sem); | |
111 #else | |
112 sem_destroy(sem->sem); | |
113 #endif | |
114 free(sem); | |
115 } | |
116 } | |
117 | |
118 int SDL_SemTryWait(SDL_sem *sem) | |
119 { | |
120 int retval; | |
121 | |
122 if ( ! sem ) { | |
123 SDL_SetError("Passed a NULL semaphore"); | |
124 return -1; | |
125 } | |
126 retval = SDL_MUTEX_TIMEDOUT; | |
127 if ( sem_trywait(sem->sem) == 0 ) { | |
128 retval = 0; | |
129 } | |
130 return retval; | |
131 } | |
132 | |
133 int SDL_SemWait(SDL_sem *sem) | |
134 { | |
135 int retval; | |
136 | |
137 if ( ! sem ) { | |
138 SDL_SetError("Passed a NULL semaphore"); | |
139 return -1; | |
140 } | |
141 | |
142 retval = sem_wait(sem->sem); | |
143 if ( retval < 0 ) { | |
144 SDL_SetError("sem_wait() failed"); | |
145 } | |
146 return retval; | |
147 } | |
148 | |
149 int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout) | |
150 { | |
151 int retval; | |
152 | |
153 if ( ! sem ) { | |
154 SDL_SetError("Passed a NULL semaphore"); | |
155 return -1; | |
156 } | |
157 | |
158 /* Try the easy cases first */ | |
159 if ( timeout == 0 ) { | |
160 return SDL_SemTryWait(sem); | |
161 } | |
162 if ( timeout == SDL_MUTEX_MAXWAIT ) { | |
163 return SDL_SemWait(sem); | |
164 } | |
165 | |
166 /* Ack! We have to busy wait... */ | |
167 timeout += SDL_GetTicks(); | |
168 do { | |
169 retval = SDL_SemTryWait(sem); | |
170 if ( retval == 0 ) { | |
171 break; | |
172 } | |
173 SDL_Delay(1); | |
174 } while ( SDL_GetTicks() < timeout ); | |
175 | |
176 return retval; | |
177 } | |
178 | |
179 Uint32 SDL_SemValue(SDL_sem *sem) | |
180 { | |
181 int ret = 0; | |
182 if ( sem ) { | |
183 sem_getvalue(sem->sem, &ret); | |
184 if ( ret < 0 ) { | |
185 ret = 0; | |
186 } | |
187 } | |
188 return (Uint32)ret; | |
189 } | |
190 | |
191 int SDL_SemPost(SDL_sem *sem) | |
192 { | |
193 int retval; | |
194 | |
195 if ( ! sem ) { | |
196 SDL_SetError("Passed a NULL semaphore"); | |
197 return -1; | |
198 } | |
199 | |
200 retval = sem_post(sem->sem); | |
201 if ( retval < 0 ) { | |
202 SDL_SetError("sem_post() failed"); | |
203 } | |
204 return retval; | |
205 } | |
206 | |
207 #endif /* NO_PTHREAD_SEMAPHORES */ | |
208 | |
209 #else /* System V IPC implementation */ | |
210 | |
211 #include <stdio.h> | |
212 #include <stdlib.h> | |
213 #include <sys/types.h> | |
214 #include <sys/ipc.h> | |
215 #include <sys/sem.h> | |
216 #include <errno.h> | |
217 | |
218 #include "SDL_error.h" | |
219 #include "SDL_thread.h" | |
220 | |
221 | |
222 struct SDL_semaphore { | |
223 int id; | |
224 }; | |
225 | |
226 /* Not defined by many operating systems, use configure to detect */ | |
227 #if !defined(HAVE_SEMUN) | |
228 union semun { | |
229 int val; | |
230 struct semid_ds *buf; | |
231 ushort *array; | |
232 }; | |
233 #endif | |
234 | |
235 static struct sembuf op_trywait[2] = { | |
236 { 0, -1, (IPC_NOWAIT|SEM_UNDO) } /* Decrement semaphore, no block */ | |
237 }; | |
238 static struct sembuf op_wait[2] = { | |
239 { 0, -1, SEM_UNDO } /* Decrement semaphore */ | |
240 }; | |
241 static struct sembuf op_post[1] = { | |
242 { 0, 1, (IPC_NOWAIT|SEM_UNDO) } /* Increment semaphore */ | |
243 }; | |
244 | |
245 /* Create a blockable semaphore */ | |
246 SDL_sem *SDL_CreateSemaphore(Uint32 initial_value) | |
247 { | |
248 extern int _creating_thread_lock; /* SDL_threads.c */ | |
249 SDL_sem *sem; | |
250 union semun init; | |
251 key_t key; | |
252 | |
253 sem = (SDL_sem *)malloc(sizeof(*sem)); | |
254 if ( sem == NULL ) { | |
255 SDL_OutOfMemory(); | |
256 return(NULL); | |
257 } | |
258 /* This flag is true if we are creating the thread manager sem, | |
259 which is never freed. This allows us to reuse the same sem. | |
260 */ | |
261 if ( _creating_thread_lock ) { | |
262 key = 'S'+'D'+'L'; | |
263 } else { | |
264 key = IPC_PRIVATE; | |
265 } | |
266 /* Keep trying to create sem while we don't own the requested key */ | |
267 do { | |
268 if ( key != IPC_PRIVATE ) { | |
269 ++key; | |
270 } | |
271 sem->id = semget(key, 1, (0600|IPC_CREAT)); | |
272 } while ((sem->id < 0) && (key != IPC_PRIVATE) && (errno == EACCES)); | |
273 | |
274 /* Report the error if we eventually failed */ | |
275 if ( sem->id < 0 ) { | |
276 SDL_SetError("Couldn't create semaphore"); | |
277 free(sem); | |
278 return(NULL); | |
279 } | |
280 init.val = initial_value; /* Initialize semaphore */ | |
281 semctl(sem->id, 0, SETVAL, init); | |
282 return(sem); | |
283 } | |
284 | |
285 void SDL_DestroySemaphore(SDL_sem *sem) | |
286 { | |
287 if ( sem ) { | |
288 #ifdef _SGI_SOURCE | |
289 semctl(sem->id, 0, IPC_RMID); | |
290 #else | |
291 union semun dummy; | |
292 dummy.val = 0; | |
293 semctl(sem->id, 0, IPC_RMID, dummy); | |
294 #endif | |
295 free(sem); | |
296 } | |
297 } | |
298 | |
299 int SDL_SemTryWait(SDL_sem *sem) | |
300 { | |
301 int retval; | |
302 | |
303 if ( ! sem ) { | |
304 SDL_SetError("Passed a NULL semaphore"); | |
305 return -1; | |
306 } | |
307 | |
308 retval = 0; | |
309 tryagain: | |
310 if ( semop(sem->id, op_trywait, 1) < 0 ) { | |
311 if ( errno == EINTR ) { | |
312 goto tryagain; | |
313 } | |
314 retval = SDL_MUTEX_TIMEDOUT; | |
315 } | |
316 return retval; | |
317 } | |
318 | |
319 int SDL_SemWait(SDL_sem *sem) | |
320 { | |
321 int retval; | |
322 | |
323 if ( ! sem ) { | |
324 SDL_SetError("Passed a NULL semaphore"); | |
325 return -1; | |
326 } | |
327 | |
328 retval = 0; | |
329 tryagain: | |
330 if ( semop(sem->id, op_wait, 1) < 0 ) { | |
331 if ( errno == EINTR ) { | |
332 goto tryagain; | |
333 } | |
334 SDL_SetError("Semaphore operation error"); | |
335 retval = -1; | |
336 } | |
337 return retval; | |
338 } | |
339 | |
340 int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout) | |
341 { | |
342 int retval; | |
343 | |
344 if ( ! sem ) { | |
345 SDL_SetError("Passed a NULL semaphore"); | |
346 return -1; | |
347 } | |
348 | |
349 /* Try the easy cases first */ | |
350 if ( timeout == 0 ) { | |
351 return SDL_SemTryWait(sem); | |
352 } | |
353 if ( timeout == SDL_MUTEX_MAXWAIT ) { | |
354 return SDL_SemWait(sem); | |
355 } | |
356 | |
357 /* Ack! We have to busy wait... */ | |
358 timeout += SDL_GetTicks(); | |
359 do { | |
360 retval = SDL_SemTryWait(sem); | |
361 if ( retval == 0 ) { | |
362 break; | |
363 } | |
364 SDL_Delay(1); | |
365 } while ( SDL_GetTicks() < timeout ); | |
366 | |
367 return retval; | |
368 } | |
369 | |
370 Uint32 SDL_SemValue(SDL_sem *sem) | |
371 { | |
372 int semval; | |
373 Uint32 value; | |
374 | |
375 value = 0; | |
376 if ( sem ) { | |
377 tryagain: | |
378 #ifdef _SGI_SOURCE | |
379 semval = semctl(sem->id, 0, GETVAL); | |
380 #else | |
381 { | |
382 union semun arg; | |
383 arg.val = 0; | |
384 semval = semctl(sem->id, 0, GETVAL, arg); | |
385 } | |
386 #endif | |
387 if ( semval < 0 ) { | |
388 if ( errno == EINTR ) { | |
389 goto tryagain; | |
390 } | |
391 } else { | |
392 value = (Uint32)semval; | |
393 } | |
394 } | |
395 return value; | |
396 } | |
397 | |
398 int SDL_SemPost(SDL_sem *sem) | |
399 { | |
400 int retval; | |
401 | |
402 if ( ! sem ) { | |
403 SDL_SetError("Passed a NULL semaphore"); | |
404 return -1; | |
405 } | |
406 | |
407 retval = 0; | |
408 tryagain: | |
409 if ( semop(sem->id, op_post, 1) < 0 ) { | |
410 if ( errno == EINTR ) { | |
411 goto tryagain; | |
412 } | |
413 SDL_SetError("Semaphore operation error"); | |
414 retval = -1; | |
415 } | |
416 return retval; | |
417 } | |
418 | |
419 #endif /* SDL_USE_PTHREADS */ |