2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2012 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "SDL_config.h"
25 #include <semaphore.h>
29 #include "SDL_thread.h"
30 #include "SDL_timer.h"
32 /* Wrapper around POSIX 1003.1b semaphores */
35 /* Mac OS X doesn't support sem_getvalue() as of version 10.4 */
36 #include "../generic/SDL_syssem.c"
39 struct SDL_semaphore {
43 /* Create a semaphore, initialized with value */
44 SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
46 SDL_sem *sem = (SDL_sem *) SDL_malloc(sizeof(SDL_sem));
48 if ( sem_init(&sem->sem, 0, initial_value) < 0 ) {
49 SDL_SetError("sem_init() failed");
59 void SDL_DestroySemaphore(SDL_sem *sem)
62 sem_destroy(&sem->sem);
67 int SDL_SemTryWait(SDL_sem *sem)
72 SDL_SetError("Passed a NULL semaphore");
75 retval = SDL_MUTEX_TIMEDOUT;
76 if ( sem_trywait(&sem->sem) == 0 ) {
82 int SDL_SemWait(SDL_sem *sem)
87 SDL_SetError("Passed a NULL semaphore");
91 while ( ((retval = sem_wait(&sem->sem)) == -1) && (errno == EINTR) ) {}
93 SDL_SetError("sem_wait() failed");
98 int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
101 #ifdef HAVE_SEM_TIMEDWAIT
103 struct timespec ts_timeout;
109 SDL_SetError("Passed a NULL semaphore");
113 /* Try the easy cases first */
114 if ( timeout == 0 ) {
115 return SDL_SemTryWait(sem);
117 if ( timeout == SDL_MUTEX_MAXWAIT ) {
118 return SDL_SemWait(sem);
121 #ifdef HAVE_SEM_TIMEDWAIT
122 /* Setup the timeout. sem_timedwait doesn't wait for
123 * a lapse of time, but until we reach a certain time.
124 * This time is now plus the timeout.
126 gettimeofday(&now, NULL);
128 /* Add our timeout to current time */
129 now.tv_usec += (timeout % 1000) * 1000;
130 now.tv_sec += timeout / 1000;
132 /* Wrap the second if needed */
133 if ( now.tv_usec >= 1000000 ) {
134 now.tv_usec -= 1000000;
138 /* Convert to timespec */
139 ts_timeout.tv_sec = now.tv_sec;
140 ts_timeout.tv_nsec = now.tv_usec * 1000;
144 retval = sem_timedwait(&sem->sem, &ts_timeout);
145 while (retval == -1 && errno == EINTR);
148 SDL_SetError(strerror(errno));
150 end = SDL_GetTicks() + timeout;
151 while ((retval = SDL_SemTryWait(sem)) == SDL_MUTEX_TIMEDOUT) {
152 if ((SDL_GetTicks() - end) >= 0) {
157 #endif /* HAVE_SEM_TIMEDWAIT */
162 Uint32 SDL_SemValue(SDL_sem *sem)
166 sem_getvalue(&sem->sem, &ret);
174 int SDL_SemPost(SDL_sem *sem)
179 SDL_SetError("Passed a NULL semaphore");
183 retval = sem_post(&sem->sem);
185 SDL_SetError("sem_post() failed");
190 #endif /* __MACOSX__ */