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 /* RISC OS semiphores based on linux code */
30 #include "SDL_timer.h"
31 #include "SDL_thread.h"
32 #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");
79 #include <unistd.h> /* For getpid() */
81 #include <semaphore.h>
83 struct SDL_semaphore {
88 /* Create a semaphore, initialized with value */
89 SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
91 SDL_sem *sem = (SDL_sem *) SDL_malloc(sizeof(SDL_sem));
93 if ( sem_init(&sem->sem_data, 0, initial_value) < 0 ) {
94 SDL_SetError("sem_init() failed");
98 sem->sem = &sem->sem_data;
106 void SDL_DestroySemaphore(SDL_sem *sem)
109 sem_destroy(sem->sem);
114 int SDL_SemTryWait(SDL_sem *sem)
119 SDL_SetError("Passed a NULL semaphore");
122 retval = SDL_MUTEX_TIMEDOUT;
123 if ( sem_trywait(sem->sem) == 0 ) {
129 int SDL_SemWait(SDL_sem *sem)
134 SDL_SetError("Passed a NULL semaphore");
138 while ( ((retval = sem_wait(sem->sem)) == -1) && (errno == EINTR) ) {}
140 SDL_SetError("sem_wait() failed");
145 int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
150 SDL_SetError("Passed a NULL semaphore");
154 /* Try the easy cases first */
155 if ( timeout == 0 ) {
156 return SDL_SemTryWait(sem);
158 if ( timeout == SDL_MUTEX_MAXWAIT ) {
159 return SDL_SemWait(sem);
162 /* Ack! We have to busy wait... */
163 timeout += SDL_GetTicks();
165 retval = SDL_SemTryWait(sem);
170 } while ( SDL_GetTicks() < timeout );
175 Uint32 SDL_SemValue(SDL_sem *sem)
179 sem_getvalue(sem->sem, &ret);
187 int SDL_SemPost(SDL_sem *sem)
192 SDL_SetError("Passed a NULL semaphore");
196 retval = sem_post(sem->sem);
198 SDL_SetError("sem_post() failed");
203 #endif /* !SDL_THREADS_DISABLED */