2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997, 1998, 1999, 2000 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
23 /* Semaphore functions using the Win32 API */
29 #include "SDL_error.h"
30 #include "SDL_thread.h"
32 #define SDL_MUTEX_TIMEOUT -2
41 extern TInt
CreateUnique(TInt (*aFunc
)(const TDesC
& aName
, TAny
*, TAny
*), TAny
*, TAny
*);
43 TInt
NewSema(const TDesC
& aName
, TAny
* aPtr1
, TAny
* aPtr2
)
45 TInt value
= *((TInt
*) aPtr2
);
46 return ((RSemaphore
*)aPtr1
)->CreateGlobal(aName
, value
);
49 /* Create a semaphore */
50 SDL_sem
*SDL_CreateSemaphore(Uint32 initial_value
)
53 TInt status
= CreateUnique(NewSema
, &s
, &initial_value
);
54 if(status
!= KErrNone
)
56 SDL_SetError("Couldn't create semaphore");
58 SDL_semaphore
* sem
= new /*(ELeave)*/ SDL_semaphore
;
59 sem
->handle
= s
.Handle();
60 sem
->count
= initial_value
;
64 /* Free the semaphore */
65 void SDL_DestroySemaphore(SDL_sem
*sem
)
70 sema
.SetHandle(sem
->handle
);
79 void _WaitAll(SDL_sem
*sem
)
81 //since SemTryWait may changed the counter.
82 //this may not be atomic, but hopes it works.
84 sema
.SetHandle(sem
->handle
);
92 int SDL_SemWaitTimeout(SDL_sem
*sem
, Uint32 timeout
)
95 SDL_SetError("Passed a NULL sem");
99 if ( timeout
== SDL_MUTEX_MAXWAIT
)
102 return SDL_MUTEX_MAXWAIT
;
106 sema
.SetHandle(sem
->handle
);
107 if(KErrNone
== sema
.Wait(timeout
))
112 int SDL_SemTryWait(SDL_sem
*sem
)
118 return SDL_MUTEX_TIMEOUT
;
121 int SDL_SemWait(SDL_sem
*sem
)
123 return SDL_SemWaitTimeout(sem
, SDL_MUTEX_MAXWAIT
);
126 /* Returns the current count of the semaphore */
127 Uint32
SDL_SemValue(SDL_sem
*sem
)
130 SDL_SetError("Passed a NULL sem");
136 int SDL_SemPost(SDL_sem
*sem
)
139 SDL_SetError("Passed a NULL sem");
144 sema
.SetHandle(sem
->handle
);