Skip to content

Latest commit

 

History

History
414 lines (355 loc) · 7.91 KB

SDL_syssem.c

File metadata and controls

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