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 Win32 API */
26 #define WIN32_LEAN_AND_MEAN
29 #include "SDL_thread.h"
30 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
31 #include "win_ce_semaphore.h"
35 struct SDL_semaphore {
36 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
41 Uint32 volatile count;
45 /* Create a semaphore */
46 SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
50 /* Allocate sem memory */
51 sem = (SDL_sem *)SDL_malloc(sizeof(*sem));
53 /* Create the semaphore, with max value 32K */
54 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
55 sem->id = CreateSemaphoreCE(NULL, initial_value, 32*1024, NULL);
57 sem->id = CreateSemaphore(NULL, initial_value, 32*1024, NULL);
59 sem->count = initial_value;
61 SDL_SetError("Couldn't create semaphore");
71 /* Free the semaphore */
72 void SDL_DestroySemaphore(SDL_sem *sem)
76 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
77 CloseSynchHandle(sem->id);
87 int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
93 SDL_SetError("Passed a NULL sem");
97 if ( timeout == SDL_MUTEX_MAXWAIT ) {
98 dwMilliseconds = INFINITE;
100 dwMilliseconds = (DWORD)timeout;
102 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
103 switch (WaitForSemaphoreCE(sem->id, dwMilliseconds)) {
105 switch (WaitForSingleObject(sem->id, dwMilliseconds)) {
108 InterlockedDecrement(&sem->count);
112 retval = SDL_MUTEX_TIMEDOUT;
115 SDL_SetError("WaitForSingleObject() failed");
122 int SDL_SemTryWait(SDL_sem *sem)
124 return SDL_SemWaitTimeout(sem, 0);
127 int SDL_SemWait(SDL_sem *sem)
129 return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT);
132 /* Returns the current count of the semaphore */
133 Uint32 SDL_SemValue(SDL_sem *sem)
136 SDL_SetError("Passed a NULL sem");
142 int SDL_SemPost(SDL_sem *sem)
145 SDL_SetError("Passed a NULL sem");
148 /* Increase the counter in the first place, because
149 * after a successful release the semaphore may
150 * immediately get destroyed by another thread which
151 * is waiting for this semaphore.
153 InterlockedIncrement(&sem->count);
154 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
155 if ( ReleaseSemaphoreCE(sem->id, 1, NULL) == FALSE ) {
157 if ( ReleaseSemaphore(sem->id, 1, NULL) == FALSE ) {
159 InterlockedDecrement(&sem->count); /* restore */
160 SDL_SetError("ReleaseSemaphore() failed");