2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2009 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
25 #include "SDL_config.h"
27 /* An implementation of semaphores using mutexes and condition variables */
29 #include "SDL_timer.h"
30 #include "SDL_thread.h"
31 #include "SDL_systhread_c.h"
34 #if SDL_THREADS_DISABLED
36 SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
38 SDL_SetError("SDL not configured with thread support");
42 void SDL_DestroySemaphore(SDL_sem *sem)
47 int SDL_SemTryWait(SDL_sem *sem)
49 SDL_SetError("SDL not configured with thread support");
53 int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
55 SDL_SetError("SDL not configured with thread support");
59 int SDL_SemWait(SDL_sem *sem)
61 SDL_SetError("SDL not configured with thread support");
65 Uint32 SDL_SemValue(SDL_sem *sem)
70 int SDL_SemPost(SDL_sem *sem)
72 SDL_SetError("SDL not configured with thread support");
85 SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
87 return (SDL_sem *)sem_create(initial_value);
91 You cannot call this function when another thread is using the semaphore.
93 void SDL_DestroySemaphore(SDL_sem *sem)
96 SDL_SetError("Passed a NULL semaphore");
100 sem_destroy(&sem->sem);
103 int SDL_SemTryWait(SDL_sem *sem)
108 SDL_SetError("Passed a NULL semaphore");
112 retval = sem_trywait(&sem->sem);
113 if (retval==0) return 0;
114 else return SDL_MUTEX_TIMEDOUT;
119 int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
124 SDL_SetError("Passed a NULL semaphore");
128 /* A timeout of 0 is an easy case */
129 if ( timeout == 0 ) {
130 return SDL_SemTryWait(sem);
133 retval = sem_wait_timed(&sem->sem,timeout);
134 if (retval==-1) retval= SDL_MUTEX_TIMEDOUT;
139 int SDL_SemWait(SDL_sem *sem)
144 SDL_SetError("Passed a NULL semaphore");
148 while ( ((retval = sem_wait(&sem->sem)) == -1) && (errno == EINTR) ) {}
152 Uint32 SDL_SemValue(SDL_sem *sem)
155 SDL_SetError("Passed a NULL semaphore");
159 return sem_count(&sem->sem);
162 int SDL_SemPost(SDL_sem *sem)
165 SDL_SetError("Passed a NULL semaphore");
169 sem_signal(&sem->sem);
173 #endif /* SDL_THREADS_DISABLED */