Drop credits litter.
[SDL.s60v3.git] / src / thread / symbian / SDL_syssem.cpp
blobe4c935cbad51f7a2be17861b81570b8f4974b145
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 /* Semaphore functions using the Win32 API */
25 //#include <stdio.h>
26 //#include <stdlib.h>
27 #include <e32std.h>
29 #include "SDL_error.h"
30 #include "SDL_thread.h"
32 #define SDL_MUTEX_TIMEOUT -2
34 struct SDL_semaphore
36 TInt handle;
37 TInt count;
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)
52 RSemaphore s;
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;
61 return(sem);
64 /* Free the semaphore */
65 void SDL_DestroySemaphore(SDL_sem *sem)
67 if ( sem )
69 RSemaphore sema;
70 sema.SetHandle(sem->handle);
71 while(--sem->count)
72 sema.Signal();
73 sema.Close();
74 delete sem;
75 sem = NULL;
79 void _WaitAll(SDL_sem *sem)
81 //since SemTryWait may changed the counter.
82 //this may not be atomic, but hopes it works.
83 RSemaphore sema;
84 sema.SetHandle(sem->handle);
85 sema.Wait();
86 while(sem->count < 0)
88 sema.Wait();
92 int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
94 if ( ! sem ) {
95 SDL_SetError("Passed a NULL sem");
96 return -1;
99 if ( timeout == SDL_MUTEX_MAXWAIT )
101 _WaitAll(sem);
102 return SDL_MUTEX_MAXWAIT;
105 RSemaphore sema;
106 sema.SetHandle(sem->handle);
107 if(KErrNone == sema.Wait(timeout))
108 return 0;
109 return -1;
112 int SDL_SemTryWait(SDL_sem *sem)
114 if(sem->count > 0)
116 sem->count--;
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)
129 if ( ! sem ) {
130 SDL_SetError("Passed a NULL sem");
131 return 0;
133 return sem->count;
136 int SDL_SemPost(SDL_sem *sem)
138 if ( ! sem ) {
139 SDL_SetError("Passed a NULL sem");
140 return -1;
142 sem->count++;
143 RSemaphore sema;
144 sema.SetHandle(sem->handle);
145 sema.Signal();
146 return 0;