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 Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with this library; if not, write to the Free
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 slouken@devolution.com
26 Epoc version by Markus Mertama (w@iki.fi)
31 "@(#) $Id: SDL_syssem.c,v 1.1.2.4 2000/06/22 15:24:48 hercules Exp $";
34 /* Semaphore functions using the Win32 API */
40 #include "SDL_error.h"
41 #include "SDL_thread.h"
44 #define SDL_MUTEX_TIMEOUT -2
53 extern TInt CreateUnique(TInt (*aFunc)(const TDesC& aName, TAny*, TAny*), TAny*, TAny*);
55 extern TInt NewThread(const TDesC& aName, TAny* aPtr1, TAny* aPtr2);
58 TInt NewSema(const TDesC& aName, TAny* aPtr1, TAny* aPtr2)
60 TInt value = *((TInt*) aPtr2);
61 return ((RSemaphore*)aPtr1)->CreateGlobal(aName, value);
64 /* Create a semaphore */
65 SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
68 TInt status = CreateUnique(NewSema, &s, &initial_value);
69 if(status != KErrNone)
71 SDL_SetError("Couldn't create semaphore");
73 SDL_semaphore* sem = new /*(ELeave)*/ SDL_semaphore;
74 sem->handle = s.Handle();
75 sem->count = initial_value;
79 /* Free the semaphore */
80 void SDL_DestroySemaphore(SDL_sem *sem)
85 sema.SetHandle(sem->handle);
98 TInfo(TInt aTime, TInt aHandle) :
99 iTime(aTime), iHandle(aHandle), iVal(0) {}
107 TBool ThreadRun(TAny* aInfo)
109 TInfo* info = STATIC_CAST(TInfo*, aInfo);
110 User::After(info->iTime);
112 sema.SetHandle(info->iHandle);
114 info->iVal = SDL_MUTEX_TIMEOUT;
121 void _WaitAll(SDL_sem *sem)
123 //since SemTryWait may changed the counter.
124 //this may not be atomic, but hopes it works.
126 sema.SetHandle(sem->handle);
128 while(sem->count < 0)
134 int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
137 SDL_SetError("Passed a NULL sem");
141 if ( timeout == SDL_MUTEX_MAXWAIT )
144 return SDL_MUTEX_MAXWAIT;
150 sema.SetHandle(sem->handle);
151 if(KErrNone == sema.Wait(timeout))
157 TInfo* info = new (ELeave)TInfo(timeout, sem->handle);
159 TInt status = CreateUnique(NewThread, &thread, info);
161 if(status != KErrNone)
168 if(thread.ExitType() == EExitPending)
170 thread.Kill(SDL_MUTEX_TIMEOUT);
179 int SDL_SemTryWait(SDL_sem *sem)
185 return SDL_MUTEX_TIMEOUT;
188 int SDL_SemWait(SDL_sem *sem)
190 return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT);
193 /* Returns the current count of the semaphore */
194 Uint32 SDL_SemValue(SDL_sem *sem)
197 SDL_SetError("Passed a NULL sem");
203 int SDL_SemPost(SDL_sem *sem)
206 SDL_SetError("Passed a NULL sem");
211 sema.SetHandle(sem->handle);