comparison src/thread/epoc/SDL_syssem.cpp @ 173:83018110dce8

Added initial support for EPOC/Symbian OS (thanks Hannu!)
author Sam Lantinga <slouken@libsdl.org>
date Tue, 11 Sep 2001 20:38:49 +0000
parents
children e8157fcb3114
comparison
equal deleted inserted replaced
172:37e3ca9254c7 173:83018110dce8
1 /*
2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997, 1998, 1999, 2000 Sam Lantinga
4
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.
9
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.
14
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
18
19 Sam Lantinga
20 slouken@devolution.com
21 */
22
23 /*
24 SDL_syssem.cpp
25
26 Epoc version by Markus Mertama (w@iki.fi)
27 */
28
29 #ifdef SAVE_RCSID
30 static char rcsid =
31 "@(#) $Id$";
32 #endif
33
34 /* Semaphore functions using the Win32 API */
35
36 //#include <stdio.h>
37 //#include <stdlib.h>
38 #include <e32std.h>
39
40 #include "SDL_error.h"
41 #include "SDL_thread.h"
42
43
44 #define SDL_MUTEX_TIMEOUT -2
45
46 struct SDL_semaphore
47 {
48 TInt handle;
49 TInt count;
50 };
51
52 extern TInt CreateUnique(TInt (*aFunc)(const TDesC& aName, TAny*, TAny*), TAny*, TAny*);
53 extern TInt NewThread(const TDesC& aName, TAny* aPtr1, TAny* aPtr2);
54
55 TInt NewSema(const TDesC& aName, TAny* aPtr1, TAny* aPtr2)
56 {
57 TInt value = *((TInt*) aPtr2);
58 return ((RSemaphore*)aPtr1)->CreateGlobal(aName, value);
59 }
60
61 /* Create a semaphore */
62 SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
63 {
64 RSemaphore s;
65 TInt status = CreateUnique(NewSema, &s, &initial_value);
66 if(status != KErrNone)
67 {
68 SDL_SetError("Couldn't create semaphore");
69 }
70 SDL_semaphore* sem = new /*(ELeave)*/ SDL_semaphore;
71 sem->handle = s.Handle();
72 sem->count = initial_value;
73 return(sem);
74 }
75
76 /* Free the semaphore */
77 void SDL_DestroySemaphore(SDL_sem *sem)
78 {
79 if ( sem )
80 {
81 RSemaphore sema;
82 sema.SetHandle(sem->handle);
83 sema.Signal(sema.Count());
84 sema.Close();
85 delete sem;
86 sem = NULL;
87 }
88 }
89
90
91 struct TInfo
92 {
93 TInfo(TInt aTime, TInt aHandle) :
94 iTime(aTime), iHandle(aHandle), iVal(0) {}
95 TInt iTime;
96 TInt iHandle;
97 TInt iVal;
98 };
99
100 TBool ThreadRun(TAny* aInfo)
101 {
102 TInfo* info = STATIC_CAST(TInfo*, aInfo);
103 User::After(info->iTime);
104 RSemaphore sema;
105 sema.SetHandle(info->iHandle);
106 sema.Signal();
107 info->iVal = SDL_MUTEX_TIMEOUT;
108 return 0;
109 }
110
111
112
113
114 void _WaitAll(SDL_sem *sem)
115 {
116 //since SemTryWait may changed the counter.
117 //this may not be atomic, but hopes it works.
118 RSemaphore sema;
119 sema.SetHandle(sem->handle);
120 sema.Wait();
121 while(sem->count < 0)
122 {
123 sema.Wait();
124 }
125 }
126
127 int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
128 {
129 if ( ! sem ) {
130 SDL_SetError("Passed a NULL sem");
131 return -1;
132 }
133
134 if ( timeout == SDL_MUTEX_MAXWAIT )
135 {
136 _WaitAll(sem);
137 return SDL_MUTEX_MAXWAIT;
138 }
139
140
141 RThread thread;
142
143 TInfo* info = new (ELeave)TInfo(timeout, sem->handle);
144
145 TInt status = CreateUnique(NewThread, &thread, info);
146
147 if(status != KErrNone)
148 return status;
149
150 thread.Resume();
151
152 _WaitAll(sem);
153
154 if(thread.ExitType() == EExitPending)
155 {
156 thread.Kill(SDL_MUTEX_TIMEOUT);
157 }
158
159 thread.Close();
160
161 return info->iVal;
162 }
163
164 int SDL_SemTryWait(SDL_sem *sem)
165 {
166 if(sem->count > 0)
167 {
168 sem->count--;
169 }
170 return SDL_MUTEX_TIMEOUT;
171 }
172
173 int SDL_SemWait(SDL_sem *sem)
174 {
175 return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT);
176 }
177
178 /* Returns the current count of the semaphore */
179 Uint32 SDL_SemValue(SDL_sem *sem)
180 {
181 if ( ! sem ) {
182 SDL_SetError("Passed a NULL sem");
183 return 0;
184 }
185 return sem->count;
186 }
187
188 int SDL_SemPost(SDL_sem *sem)
189 {
190 if ( ! sem ) {
191 SDL_SetError("Passed a NULL sem");
192 return -1;
193 }
194 sem->count++;
195 RSemaphore sema;
196 sema.SetHandle(sem->handle);
197 sema.Signal();
198 return 0;
199 }