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 /* Semaphores in the BeOS environment */
26 #include <be/kernel/OS.h>
28 #include "SDL_thread.h"
31 struct SDL_semaphore {
35 /* Create a counting semaphore */
36 SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
40 sem = (SDL_sem *)SDL_malloc(sizeof(*sem));
42 sem->id = create_sem(initial_value, "SDL semaphore");
43 if ( sem->id < B_NO_ERROR ) {
44 SDL_SetError("create_sem() failed");
54 /* Free the semaphore */
55 void SDL_DestroySemaphore(SDL_sem *sem)
58 if ( sem->id >= B_NO_ERROR ) {
65 int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
71 SDL_SetError("Passed a NULL semaphore");
76 if ( timeout == SDL_MUTEX_MAXWAIT ) {
77 val = acquire_sem(sem->id);
79 timeout *= 1000; /* BeOS uses a timeout in microseconds */
80 val = acquire_sem_etc(sem->id, 1, B_RELATIVE_TIMEOUT, timeout);
89 retval = SDL_MUTEX_TIMEDOUT;
92 retval = SDL_MUTEX_TIMEDOUT;
95 SDL_SetError("acquire_sem() failed");
103 int SDL_SemTryWait(SDL_sem *sem)
105 return SDL_SemWaitTimeout(sem, 0);
108 int SDL_SemWait(SDL_sem *sem)
110 return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT);
113 /* Returns the current count of the semaphore */
114 Uint32 SDL_SemValue(SDL_sem *sem)
121 get_sem_count(sem->id, &count);
123 value = (Uint32)count;
129 /* Atomically increases the semaphore's count (not blocking) */
130 int SDL_SemPost(SDL_sem *sem)
133 SDL_SetError("Passed a NULL semaphore");
137 if ( release_sem(sem->id) != B_NO_ERROR ) {
138 SDL_SetError("release_sem() failed");