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