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
22 #include "SDL_config.h"
24 /* An implementation of semaphores using mutexes and condition variables */
26 #include "SDL_timer.h"
27 #include "SDL_thread.h"
28 #include "SDL_systhread_c.h"
31 #if SDL_THREADS_DISABLED
33 SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
35 SDL_SetError("SDL not configured with thread support");
39 void SDL_DestroySemaphore(SDL_sem *sem)
44 int SDL_SemTryWait(SDL_sem *sem)
46 SDL_SetError("SDL not configured with thread support");
50 int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
52 SDL_SetError("SDL not configured with thread support");
56 int SDL_SemWait(SDL_sem *sem)
58 SDL_SetError("SDL not configured with thread support");
62 Uint32 SDL_SemValue(SDL_sem *sem)
67 int SDL_SemPost(SDL_sem *sem)
69 SDL_SetError("SDL not configured with thread support");
79 SDL_mutex *count_lock;
80 SDL_cond *count_nonzero;
83 SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
87 sem = (SDL_sem *)SDL_malloc(sizeof(*sem));
92 sem->count = initial_value;
93 sem->waiters_count = 0;
95 sem->count_lock = SDL_CreateMutex();
96 sem->count_nonzero = SDL_CreateCond();
97 if ( ! sem->count_lock || ! sem->count_nonzero ) {
98 SDL_DestroySemaphore(sem);
106 You cannot call this function when another thread is using the semaphore.
108 void SDL_DestroySemaphore(SDL_sem *sem)
111 sem->count = 0xFFFFFFFF;
112 while ( sem->waiters_count > 0) {
113 SDL_CondSignal(sem->count_nonzero);
116 SDL_DestroyCond(sem->count_nonzero);
117 if ( sem->count_lock ) {
118 SDL_mutexP(sem->count_lock);
119 SDL_mutexV(sem->count_lock);
120 SDL_DestroyMutex(sem->count_lock);
126 int SDL_SemTryWait(SDL_sem *sem)
131 SDL_SetError("Passed a NULL semaphore");
135 retval = SDL_MUTEX_TIMEDOUT;
136 SDL_LockMutex(sem->count_lock);
137 if ( sem->count > 0 ) {
141 SDL_UnlockMutex(sem->count_lock);
146 int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
151 SDL_SetError("Passed a NULL semaphore");
155 /* A timeout of 0 is an easy case */
156 if ( timeout == 0 ) {
157 return SDL_SemTryWait(sem);
160 SDL_LockMutex(sem->count_lock);
161 ++sem->waiters_count;
163 while ( (sem->count == 0) && (retval != SDL_MUTEX_TIMEDOUT) ) {
164 retval = SDL_CondWaitTimeout(sem->count_nonzero,
165 sem->count_lock, timeout);
167 --sem->waiters_count;
171 SDL_UnlockMutex(sem->count_lock);
176 int SDL_SemWait(SDL_sem *sem)
178 return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT);
181 Uint32 SDL_SemValue(SDL_sem *sem)
187 SDL_LockMutex(sem->count_lock);
189 SDL_UnlockMutex(sem->count_lock);
194 int SDL_SemPost(SDL_sem *sem)
197 SDL_SetError("Passed a NULL semaphore");
201 SDL_LockMutex(sem->count_lock);
202 if ( sem->waiters_count > 0 ) {
203 SDL_CondSignal(sem->count_nonzero);
206 SDL_UnlockMutex(sem->count_lock);
211 #endif /* SDL_THREADS_DISABLED */