2 Simple DirectMedia Layer
3 Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
21 #include "../../SDL_internal.h"
25 #include <semaphore.h>
29 #include "SDL_thread.h"
30 #include "SDL_timer.h"
32 /* Wrapper around POSIX 1003.1b semaphores */
34 #if defined(__MACOSX__) || defined(__IPHONEOS__)
35 /* Mac OS X doesn't support sem_getvalue() as of version 10.4 */
36 #include "../generic/SDL_syssem.c"
44 /* Create a semaphore, initialized with value */
46 SDL_CreateSemaphore(Uint32 initial_value)
48 SDL_sem *sem = (SDL_sem *) SDL_malloc(sizeof(SDL_sem));
50 if (sem_init(&sem->sem, 0, initial_value) < 0) {
51 SDL_SetError("sem_init() failed");
62 SDL_DestroySemaphore(SDL_sem * sem)
65 sem_destroy(&sem->sem);
71 SDL_SemTryWait(SDL_sem * sem)
76 return SDL_SetError("Passed a NULL semaphore");
78 retval = SDL_MUTEX_TIMEDOUT;
79 if (sem_trywait(&sem->sem) == 0) {
86 SDL_SemWait(SDL_sem * sem)
91 return SDL_SetError("Passed a NULL semaphore");
95 retval = sem_wait(&sem->sem);
96 } while (retval < 0 && errno == EINTR);
99 retval = SDL_SetError("sem_wait() failed");
105 SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
108 #ifdef HAVE_SEM_TIMEDWAIT
109 #ifndef HAVE_CLOCK_GETTIME
112 struct timespec ts_timeout;
118 return SDL_SetError("Passed a NULL semaphore");
121 /* Try the easy cases first */
123 return SDL_SemTryWait(sem);
125 if (timeout == SDL_MUTEX_MAXWAIT) {
126 return SDL_SemWait(sem);
129 #ifdef HAVE_SEM_TIMEDWAIT
130 /* Setup the timeout. sem_timedwait doesn't wait for
131 * a lapse of time, but until we reach a certain time.
132 * This time is now plus the timeout.
134 #ifdef HAVE_CLOCK_GETTIME
135 clock_gettime(CLOCK_REALTIME, &ts_timeout);
137 /* Add our timeout to current time */
138 ts_timeout.tv_nsec += (timeout % 1000) * 1000000;
139 ts_timeout.tv_sec += timeout / 1000;
141 gettimeofday(&now, NULL);
143 /* Add our timeout to current time */
144 ts_timeout.tv_sec = now.tv_sec + (timeout / 1000);
145 ts_timeout.tv_nsec = (now.tv_usec + (timeout % 1000) * 1000) * 1000;
148 /* Wrap the second if needed */
149 if (ts_timeout.tv_nsec > 1000000000) {
150 ts_timeout.tv_sec += 1;
151 ts_timeout.tv_nsec -= 1000000000;
156 retval = sem_timedwait(&sem->sem, &ts_timeout);
157 } while (retval < 0 && errno == EINTR);
160 if (errno == ETIMEDOUT) {
161 retval = SDL_MUTEX_TIMEDOUT;
163 SDL_SetError("sem_timedwait returned an error: %s", strerror(errno));
167 end = SDL_GetTicks() + timeout;
168 while ((retval = SDL_SemTryWait(sem)) == SDL_MUTEX_TIMEDOUT) {
169 if (SDL_TICKS_PASSED(SDL_GetTicks(), end)) {
174 #endif /* HAVE_SEM_TIMEDWAIT */
180 SDL_SemValue(SDL_sem * sem)
184 sem_getvalue(&sem->sem, &ret);
193 SDL_SemPost(SDL_sem * sem)
198 return SDL_SetError("Passed a NULL semaphore");
201 retval = sem_post(&sem->sem);
203 SDL_SetError("sem_post() failed");
208 #endif /* __MACOSX__ */
209 /* vi: set ts=4 sw=4 expandtab: */