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 /* Semaphore functions using the OS/2 API */
27 #define INCL_DOSERRORS
28 #define INCL_DOSSEMAPHORES
31 #include "SDL_thread.h"
32 #include "SDL_timer.h"
35 struct SDL_semaphore {
42 /* Create a semaphore */
43 DECLSPEC SDL_sem * SDLCALL SDL_CreateSemaphore(Uint32 initial_value)
48 /* Allocate sem memory */
49 sem = (SDL_sem *)SDL_malloc(sizeof(*sem));
51 /* Create the mutex semaphore */
52 ulrc = DosCreateMutexSem(NULL,&(sem->id),0,TRUE);
54 SDL_SetError("Couldn't create semaphore");
59 DosCreateEventSem(NULL, &(sem->changed), 0, FALSE);
60 sem->value = initial_value;
61 DosReleaseMutexSem(sem->id);
69 /* Free the semaphore */
70 DECLSPEC void SDLCALL SDL_DestroySemaphore(SDL_sem *sem)
74 DosCloseEventSem(sem->changed);
75 DosCloseMutexSem(sem->id);
82 DECLSPEC int SDLCALL SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
87 SDL_SetError("Passed a NULL sem");
91 if ( timeout == SDL_MUTEX_MAXWAIT ) {
93 ulrc = DosRequestMutexSem(sem->id, SEM_INDEFINITE_WAIT);
95 /* if error waiting mutex */
96 SDL_SetError("DosRequestMutexSem() failed");
98 } else if (sem->value) {
100 DosReleaseMutexSem(sem->id);
104 DosResetEventSem(sem->changed, &ulPostCount);
105 DosReleaseMutexSem(sem->id);
106 /* continue waiting until somebody posts the semaphore */
107 DosWaitEventSem(sem->changed, SEM_INDEFINITE_WAIT);
113 ulrc = DosRequestMutexSem(sem->id, SEM_INDEFINITE_WAIT);
119 DosReleaseMutexSem(sem->id);
123 DosReleaseMutexSem(sem->id);
124 return SDL_MUTEX_TIMEDOUT;
128 SDL_SetError("DosRequestMutexSem() failed");
132 ulrc = DosRequestMutexSem(sem->id, SEM_INDEFINITE_WAIT);
134 /* if error waiting mutex */
135 SDL_SetError("DosRequestMutexSem() failed");
140 DosReleaseMutexSem(sem->id);
144 DosResetEventSem(sem->changed, &ulPostCount);
145 DosReleaseMutexSem(sem->id);
146 /* continue waiting until somebody posts the semaphore */
147 ulrc = DosWaitEventSem(sem->changed, timeout);
151 return SDL_MUTEX_TIMEDOUT;
158 DECLSPEC int SDLCALL SDL_SemTryWait(SDL_sem *sem)
160 return SDL_SemWaitTimeout(sem, 0);
163 DECLSPEC int SDLCALL SDL_SemWait(SDL_sem *sem)
165 return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT);
168 /* Returns the current count of the semaphore */
169 DECLSPEC Uint32 SDLCALL SDL_SemValue(SDL_sem *sem)
172 SDL_SetError("Passed a NULL sem");
178 DECLSPEC int SDLCALL SDL_SemPost(SDL_sem *sem)
181 SDL_SetError("Passed a NULL sem");
184 if ( DosRequestMutexSem(sem->id,SEM_INDEFINITE_WAIT) ) {
185 SDL_SetError("DosRequestMutexSem() failed");
189 DosPostEventSem(sem->changed);
190 DosReleaseMutexSem(sem->id);