Apply anotherguest's fixes.
[SDL.s60v3.git] / src / thread / symbian / SDL_syssem.cpp
blobb74fde8a9ee298f22bf4c18053fbf1bc3b4efe72
1 /*
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
19 Sam Lantinga
20 slouken@devolution.com
23 #include <e32std.h>
24 #include "SDL_error.h"
25 #include "SDL_thread.h"
27 #define SDL_MUTEX_TIMEOUT -2
29 struct SDL_semaphore
31 int handle;
32 int count;
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)
46 RSemaphore s;
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;
55 return(sem);
58 /* Free the semaphore */
59 void SDL_DestroySemaphore(SDL_sem *sem)
61 if ( sem )
63 RSemaphore sema;
64 sema.SetHandle(sem->handle);
65 sema.Signal(sem->count);
66 sema.Close();
67 delete sem;
68 sem = NULL;
72 void _WaitAll(SDL_sem *sem)
74 //since SemTryWait may changed the counter.
75 //this may not be atomic, but hopes it works.
76 RSemaphore sema;
77 sema.SetHandle(sem->handle);
78 sema.Wait();
79 while(sem->count < 0)
81 sema.Wait();
85 int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
87 if ( ! sem ) {
88 SDL_SetError("Passed a NULL sem");
89 return -1;
92 if ( timeout == SDL_MUTEX_MAXWAIT )
94 _WaitAll(sem);
95 return SDL_MUTEX_MAXWAIT;
98 RSemaphore sema;
99 sema.SetHandle(sem->handle);
100 if(KErrNone == sema.Wait(timeout))
101 return 0;
102 return -1;
105 int SDL_SemTryWait(SDL_sem *sem)
107 if(sem->count > 0)
109 sem->count--;
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)
122 if ( ! sem ) {
123 SDL_SetError("Passed a NULL sem");
124 return 0;
126 return sem->count;
129 int SDL_SemPost(SDL_sem *sem)
131 if ( ! sem ) {
132 SDL_SetError("Passed a NULL sem");
133 return -1;
135 sem->count++;
136 RSemaphore sema;
137 sema.SetHandle(sem->handle);
138 sema.Signal();
139 return 0;