Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
231 lines (197 loc) · 4.67 KB

SDL_syssem.c

File metadata and controls

231 lines (197 loc) · 4.67 KB
 
Apr 26, 2001
Apr 26, 2001
1
2
/*
SDL - Simple DirectMedia Layer
Jan 24, 2010
Jan 24, 2010
3
Copyright (C) 1997-2010 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
*/
Feb 21, 2006
Feb 21, 2006
22
#include "SDL_config.h"
Apr 26, 2001
Apr 26, 2001
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include "SDL_thread.h"
#include "SDL_timer.h"
#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"
Jul 10, 2006
Jul 10, 2006
39
40
41
struct SDL_semaphore
{
int id;
Apr 26, 2001
Apr 26, 2001
42
43
44
};
/* Not defined by many operating systems, use configure to detect */
Feb 16, 2006
Feb 16, 2006
45
/*
Apr 26, 2001
Apr 26, 2001
46
47
48
49
50
51
52
#if !defined(HAVE_SEMUN)
union semun {
int val;
struct semid_ds *buf;
ushort *array;
};
#endif
Feb 16, 2006
Feb 16, 2006
53
*/
Apr 26, 2001
Apr 26, 2001
54
55
static struct sembuf op_trywait[2] = {
Jul 10, 2006
Jul 10, 2006
56
{0, -1, (IPC_NOWAIT | SEM_UNDO)} /* Decrement semaphore, no block */
Apr 26, 2001
Apr 26, 2001
57
};
Aug 27, 2008
Aug 27, 2008
58
Apr 26, 2001
Apr 26, 2001
59
static struct sembuf op_wait[2] = {
Jul 10, 2006
Jul 10, 2006
60
{0, -1, SEM_UNDO} /* Decrement semaphore */
Apr 26, 2001
Apr 26, 2001
61
};
Aug 27, 2008
Aug 27, 2008
62
Apr 26, 2001
Apr 26, 2001
63
static struct sembuf op_post[1] = {
Jul 10, 2006
Jul 10, 2006
64
{0, 1, (IPC_NOWAIT | SEM_UNDO)} /* Increment semaphore */
Apr 26, 2001
Apr 26, 2001
65
66
67
};
/* Create a blockable semaphore */
Jul 10, 2006
Jul 10, 2006
68
69
SDL_sem *
SDL_CreateSemaphore(Uint32 initial_value)
Apr 26, 2001
Apr 26, 2001
70
{
Jul 10, 2006
Jul 10, 2006
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
extern int _creating_thread_lock; /* SDL_threads.c */
SDL_sem *sem;
union semun init;
sem = (SDL_sem *) SDL_malloc(sizeof(*sem));
if (sem == NULL) {
SDL_OutOfMemory();
return (NULL);
}
sem->id = semget(IPC_PRIVATE, 1, (0600 | IPC_CREAT));
if (sem->id < 0) {
SDL_SetError("Couldn't create semaphore");
SDL_free(sem);
return (NULL);
}
init.val = initial_value; /* Initialize semaphore */
semctl(sem->id, 0, SETVAL, init);
return (sem);
Apr 26, 2001
Apr 26, 2001
89
90
}
Jul 10, 2006
Jul 10, 2006
91
92
void
SDL_DestroySemaphore(SDL_sem * sem)
Apr 26, 2001
Apr 26, 2001
93
{
Jul 10, 2006
Jul 10, 2006
94
if (sem) {
Feb 21, 2006
Feb 21, 2006
95
#ifdef __IRIX__
Jul 10, 2006
Jul 10, 2006
96
semctl(sem->id, 0, IPC_RMID);
Apr 26, 2001
Apr 26, 2001
97
#else
Jul 10, 2006
Jul 10, 2006
98
99
100
union semun dummy;
dummy.val = 0;
semctl(sem->id, 0, IPC_RMID, dummy);
Apr 26, 2001
Apr 26, 2001
101
#endif
Jul 10, 2006
Jul 10, 2006
102
103
SDL_free(sem);
}
Apr 26, 2001
Apr 26, 2001
104
105
}
Jul 10, 2006
Jul 10, 2006
106
107
int
SDL_SemTryWait(SDL_sem * sem)
Apr 26, 2001
Apr 26, 2001
108
{
Jul 10, 2006
Jul 10, 2006
109
int retval;
Apr 26, 2001
Apr 26, 2001
110
Jul 10, 2006
Jul 10, 2006
111
112
113
114
if (!sem) {
SDL_SetError("Passed a NULL semaphore");
return -1;
}
Apr 26, 2001
Apr 26, 2001
115
Jul 10, 2006
Jul 10, 2006
116
retval = 0;
Apr 26, 2001
Apr 26, 2001
117
tryagain:
Jul 10, 2006
Jul 10, 2006
118
119
120
121
122
123
124
if (semop(sem->id, op_trywait, 1) < 0) {
if (errno == EINTR) {
goto tryagain;
}
retval = SDL_MUTEX_TIMEDOUT;
}
return retval;
Apr 26, 2001
Apr 26, 2001
125
126
}
Jul 10, 2006
Jul 10, 2006
127
128
int
SDL_SemWait(SDL_sem * sem)
Apr 26, 2001
Apr 26, 2001
129
{
Jul 10, 2006
Jul 10, 2006
130
int retval;
Apr 26, 2001
Apr 26, 2001
131
Jul 10, 2006
Jul 10, 2006
132
133
134
135
if (!sem) {
SDL_SetError("Passed a NULL semaphore");
return -1;
}
Apr 26, 2001
Apr 26, 2001
136
Jul 10, 2006
Jul 10, 2006
137
retval = 0;
Apr 26, 2001
Apr 26, 2001
138
tryagain:
Jul 10, 2006
Jul 10, 2006
139
140
141
142
143
144
145
146
if (semop(sem->id, op_wait, 1) < 0) {
if (errno == EINTR) {
goto tryagain;
}
SDL_SetError("Semaphore operation error");
retval = -1;
}
return retval;
Apr 26, 2001
Apr 26, 2001
147
148
}
Jul 10, 2006
Jul 10, 2006
149
150
int
SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
Apr 26, 2001
Apr 26, 2001
151
{
Jul 10, 2006
Jul 10, 2006
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
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);
Aug 27, 2008
Aug 27, 2008
175
} while (SDL_GetTicks() < timeout);
Jul 10, 2006
Jul 10, 2006
176
177
return retval;
Apr 26, 2001
Apr 26, 2001
178
179
}
Jul 10, 2006
Jul 10, 2006
180
181
Uint32
SDL_SemValue(SDL_sem * sem)
Apr 26, 2001
Apr 26, 2001
182
{
Jul 10, 2006
Jul 10, 2006
183
184
185
186
187
188
int semval;
Uint32 value;
value = 0;
if (sem) {
tryagain:
Feb 21, 2006
Feb 21, 2006
189
#ifdef __IRIX__
Jul 10, 2006
Jul 10, 2006
190
semval = semctl(sem->id, 0, GETVAL);
Apr 26, 2001
Apr 26, 2001
191
#else
Jul 10, 2006
Jul 10, 2006
192
193
194
195
196
{
union semun arg;
arg.val = 0;
semval = semctl(sem->id, 0, GETVAL, arg);
}
Apr 26, 2001
Apr 26, 2001
197
#endif
Jul 10, 2006
Jul 10, 2006
198
199
200
201
202
203
204
205
206
if (semval < 0) {
if (errno == EINTR) {
goto tryagain;
}
} else {
value = (Uint32) semval;
}
}
return value;
Apr 26, 2001
Apr 26, 2001
207
208
}
Jul 10, 2006
Jul 10, 2006
209
210
int
SDL_SemPost(SDL_sem * sem)
Apr 26, 2001
Apr 26, 2001
211
{
Jul 10, 2006
Jul 10, 2006
212
int retval;
Apr 26, 2001
Apr 26, 2001
213
Jul 10, 2006
Jul 10, 2006
214
215
216
217
if (!sem) {
SDL_SetError("Passed a NULL semaphore");
return -1;
}
Apr 26, 2001
Apr 26, 2001
218
Jul 10, 2006
Jul 10, 2006
219
retval = 0;
Apr 26, 2001
Apr 26, 2001
220
tryagain:
Jul 10, 2006
Jul 10, 2006
221
222
223
224
225
226
227
228
if (semop(sem->id, op_post, 1) < 0) {
if (errno == EINTR) {
goto tryagain;
}
SDL_SetError("Semaphore operation error");
retval = -1;
}
return retval;
Apr 26, 2001
Apr 26, 2001
229
}
Jul 10, 2006
Jul 10, 2006
230
231
/* vi: set ts=4 sw=4 expandtab: */