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
24 #include "SDL_error.h"
25 #include "SDL_thread.h"
27 #define SDL_MUTEX_TIMEOUT -2
35 extern int CreateUnique(int (*aFunc
)(const TDesC
& aName
, void*, void*), void*, void*);
37 int NewSema(const TDesC
& aName
, void* aPtr1
, void* aPtr2
)
39 int value
= *((int*) aPtr2
);
40 return ((RSemaphore
*)aPtr1
)->CreateGlobal(aName
, value
);
43 /* Create a semaphore */
44 SDL_sem
*SDL_CreateSemaphore(Uint32 initial_value
)
47 int status
= CreateUnique(NewSema
, &s
, &initial_value
);
48 if(status
!= KErrNone
)
50 SDL_SetError("Couldn't create semaphore");
52 SDL_semaphore
* sem
= new SDL_semaphore
;
53 sem
->handle
= s
.Handle();
54 sem
->count
= initial_value
;
58 /* Free the semaphore */
59 void SDL_DestroySemaphore(SDL_sem
*sem
)
64 sema
.SetHandle(sem
->handle
);
65 sema
.Signal(sem
->count
);
72 void _WaitAll(SDL_sem
*sem
)
74 //since SemTryWait may changed the counter.
75 //this may not be atomic, but hopes it works.
77 sema
.SetHandle(sem
->handle
);
85 int SDL_SemWaitTimeout(SDL_sem
*sem
, Uint32 timeout
)
88 SDL_SetError("Passed a NULL sem");
92 if ( timeout
== SDL_MUTEX_MAXWAIT
)
95 return SDL_MUTEX_MAXWAIT
;
99 sema
.SetHandle(sem
->handle
);
100 if(KErrNone
== sema
.Wait(timeout
))
105 int SDL_SemTryWait(SDL_sem
*sem
)
111 return SDL_MUTEX_TIMEOUT
;
114 int SDL_SemWait(SDL_sem
*sem
)
116 return SDL_SemWaitTimeout(sem
, SDL_MUTEX_MAXWAIT
);
119 /* Returns the current count of the semaphore */
120 Uint32
SDL_SemValue(SDL_sem
*sem
)
123 SDL_SetError("Passed a NULL sem");
129 int SDL_SemPost(SDL_sem
*sem
)
132 SDL_SetError("Passed a NULL sem");
137 sema
.SetHandle(sem
->handle
);