Sun Mar 11 15:22:42 CET 2007 Jochen Eisinger <coffee@silcnet.org>
[silc.git] / lib / silcutil / symbian / silcsymbianthread.cpp
1 /*
2
3   silcsymbianthread.cpp
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2006 - 2007 Pekka Riikonen
8
9   This program is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; version 2 of the License.
12
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18 */
19
20 #include "silc.h"
21 #include <e32std.h>
22
23 /**************************** SILC Thread API *******************************/
24
25 /* Thread structure for Symbian */
26 typedef struct {
27 #ifdef SILC_THREADS
28   SilcThreadStart start_func;
29   void *context;
30   SilcBool waitable;
31 #else
32   void *tmp;
33 #endif
34 } *SilcSymbianThread;
35
36 /* The actual thread function */
37
38 static TInt silc_thread_start(TAny *context)
39 {
40 #ifdef SILC_THREADS
41   SilcSymbianThread tc = (SilcSymbianThread)context;
42   SilcThreadStart start_func = tc->start_func;
43   void *context = tc->context;
44   SilcBool waitable = tc->waitable;
45
46   silc_free(tc);
47
48   /* Call the thread function */
49   if (waitable)
50     silc_thread_exit(start_func(context));
51   else
52     start_func(context);
53
54 #endif
55   return KErrNone;
56 }
57
58 /* Executed new thread */
59
60 SilcThread silc_thread_create(SilcThreadStart start_func, void *context,
61                               bool waitable)
62 {
63 #ifdef SILC_THREADS
64   SilcSymbianThread tc;
65   RThread *thread;
66   TInt ret;
67   TBuf<32> name;
68
69   SILC_LOG_DEBUG(("Creating new thread"));
70
71   tc = (SilcSymbianThread)silc_calloc(1, sizeof(*thread));
72   if (!tc)
73     return NULL;
74   tc->start_func = start_func;
75   tc->context = context;
76   tc->waitable = waitable;
77
78   /* Allocate thread */
79   thread = new RThread();
80   if (!thread) {
81     silc_free(tc);
82     return NULL;
83   }
84
85   /* Create the thread */
86   name = (TText *)silc_time_string(0);
87   ret = thread->Create(name, silc_thread_start, 8192, 4096, 1024 * 1024,
88                        (TAny *)tc);
89   if (ret != KErrNone) {
90     SILC_LOG_ERROR(("Could not create new thread"));
91     delete thread;
92     silc_free(tc);
93     return NULL;
94   }
95
96   /* Start the thread */
97   thread->Resume();
98
99   /* Close our instance to the thread */
100   thread->Close();
101
102   return (SilcThread)thread;
103 #else
104   /* Call thread callback immediately */
105   (*start_func)(context);
106   return NULL;
107 #endif
108 }
109
110 /* Exits current thread */
111
112 void silc_thread_exit(void *exit_value)
113 {
114 #ifdef SILC_THREADS
115   RThread().Kill((Tint)exit_value);
116 #endif
117 }
118
119 /* Returns current thread context */
120
121 SilcThread silc_thread_self(void)
122 {
123 #ifdef SILC_THREADS
124   return (SilcThread)&RThread();
125 #else
126   return NULL;
127 #endif
128 }
129
130 /* Blocks calling thread to wait for `thread' to finish. */
131
132 SilcBool silc_thread_wait(SilcThread thread, void **exit_value)
133 {
134 #ifdef SILC_THREADS
135   TRequestStatus req;
136   RThread *t = (RThread *)thread;
137   t->Logon(req);
138   User::WaitForAnyRequest();
139   return TRUE;
140 #else
141   return FALSE;
142 #endif
143 }
144
145 /***************************** SILC Mutex API *******************************/
146
147 /* SILC Mutex structure */
148 struct SilcMutexStruct {
149 #ifdef SILC_THREADS
150   RMutex *mutex;
151 #endif /* SILC_THREADS */
152   unsigned int locked : 1;
153 };
154
155 SilcBool silc_mutex_alloc(SilcMutex *mutex)
156 {
157 #ifdef SILC_THREADS
158   *mutex = (SilcMutex)silc_calloc(1, sizeof(**mutex));
159   if (*mutex == NULL)
160     return FALSE;
161   (*mutex)->mutex = new RMutex();
162   if (!(*mutex)->mutex) {
163     silc_free(*mutex);
164     return FALSE;
165   }
166   if ((*mutex)->mutex->CreateLocal() != KErrNone) {
167     delete (*mutex)->mutex;
168     silc_free(*mutex);
169     return FALSE;
170   }
171   (*mutex)->locked = FALSE;
172   return TRUE;
173 #else
174   return FALSE;
175 #endif /* SILC_THREADS */
176 }
177
178 void silc_mutex_free(SilcMutex mutex)
179 {
180 #ifdef SILC_THREADS
181   if (mutex) {
182     mutex->mutex->Close();
183     delete mutex->mutex;
184     silc_free(mutex);
185   }
186 #endif /* SILC_THREADS */
187 }
188
189 void silc_mutex_lock(SilcMutex mutex)
190 {
191 #ifdef SILC_THREADS
192   if (mutex) {
193     mutex->mutex->Wait();
194     mutex->locked = TRUE;
195   }
196 #endif /* SILC_THREADS */
197 }
198
199 void silc_mutex_unlock(SilcMutex mutex)
200 {
201 #ifdef SILC_THREADS
202   if (mutex) {
203     mutex->mutex->Signal();
204     mutex->locked = FALSE;
205   }
206 #endif /* SILC_THREADS */
207 }
208
209 void silc_mutex_assert_locked(SilcMutex mutex)
210 {
211 #ifdef SILC_THREADS
212   if (mutex)
213     SILC_ASSERT(mutex->locked);
214 #endif /* SILC_THREADS */
215 }
216
217 /***************************** SILC Rwlock API *****************************/
218
219 /* SILC read/write lock structure */
220 struct SilcRwLockStruct {
221 #ifdef SILC_THREADS
222   SilcMutex mutex;
223   SilcCond cond;
224 #endif /* SILC_THREADS */
225   unsigned int readers : 31;
226   unsigned int locked  : 1;
227 };
228
229 SilcBool silc_rwlock_alloc(SilcRwLock *rwlock)
230 {
231 #ifdef SILC_THREADS
232   *rwlock = (SilcRwLock)silc_calloc(1, sizeof(**rwlock));
233   if (!(*rwlock))
234     return FALSE;
235   if (!silc_mutex_alloc(&(*rwlock)->mutex)) {
236     silc_free(*rwlock);
237     return FALSE;
238   }
239   if (!silc_cond_alloc(&(*rwlock)->cond)) {
240     silc_mutex_free((*rwlock)->mutex);
241     silc_free(*rwlock);
242     return FALSE;
243   }
244   return TRUE;
245 #else
246   return FALSE;
247 #endif /* SILC_THREADS */
248 }
249
250 void silc_rwlock_free(SilcRwLock rwlock)
251 {
252 #ifdef SILC_THREADS
253   if (mutex) {
254     silc_mutex_free(rwlock->mutex);
255     silc_cond_free(rwlock->cond);
256     silc_free(rwlock);
257   }
258 #endif /* SILC_THREADS */
259 }
260
261 void silc_rwlock_rdlock(SilcRwLock rwlock)
262 {
263 #ifdef SILC_THREADS
264   if (rwlock) {
265     silc_mutex_lock(rwlock->mutex);
266     rwlock->readers++;
267     silc_mutex_unlock(rwlock->mutex);
268   }
269 #endif /* SILC_THREADS */
270 }
271
272 void silc_rwlock_wrlock(SilcRwLock rwlock)
273 {
274 #ifdef SILC_THREADS
275   if (rwlock) {
276     silc_mutex_lock(rwlock->mutex);
277     while (rwlock->readers > 0)
278       silc_cond_wait(rwlock->cond, rwlock->mutex);
279     rwlock->locked = TRUE;
280   }
281 #endif /* SILC_THREADS */
282 }
283
284 void silc_rwlock_unlock(SilcRwLock rwlock)
285 {
286 #ifdef SILC_THREADS
287   if (rwlock) {
288     if (rwlock->locked) {
289       /* Unlock writer */
290       rwlock->locked = FALSE;
291       silc_mutex_unlock(rwlock->mutex);
292       return;
293     }
294
295     /* Unlock reader */
296     silc_mutex_lock(rwlock->mutex);
297     rwlock->readers--;
298     silc_cond_broadcast(rwlock->cond);
299     silc_mutex_unlock(rwlock->mutex);
300   }
301 #endif /* SILC_THREADS */
302 }
303
304 /****************************** SILC Cond API *******************************/
305
306 /* SILC Conditional Variable context */
307 struct SilcCondStruct {
308 #ifdef SILC_THREADS
309   RCondVar *cond;
310 #else
311   void *tmp;
312 #endif /* SILC_THREADS*/
313 };
314
315 SilcBool silc_cond_alloc(SilcCond *cond)
316 {
317 #ifdef SILC_THREADS
318   *cond = (SilcCond)silc_calloc(1, sizeof(**cond));
319   if (*cond == NULL)
320     return FALSE;
321   (*cond)->cond = new RCondVar();
322   if (!(*cond)->cond) {
323     silc_free(*cond);
324     return FALSE;
325   }
326   if ((*cond)->cond->CreateLocal() != KErrNone) {
327     delete (*cond)->cond;
328     silc_free(*cond);
329     return FALSE;
330   }
331   return TRUE;
332 #else
333   return FALSE;
334 #endif /* SILC_THREADS*/
335 }
336
337 void silc_cond_free(SilcCond cond)
338 {
339 #ifdef SILC_THREADS
340   cond->cond->Close();
341   delete cond->cond;
342   silc_free(cond);
343 #endif /* SILC_THREADS*/
344 }
345
346 void silc_cond_signal(SilcCond cond)
347 {
348 #ifdef SILC_THREADS
349   cond->cond->Signal();
350 #endif /* SILC_THREADS*/
351 }
352
353 void silc_cond_broadcast(SilcCond cond)
354 {
355 #ifdef SILC_THREADS
356   cond->cond->Broadcast();
357 #endif /* SILC_THREADS*/
358 }
359
360 void silc_cond_wait(SilcCond cond, SilcMutex mutex)
361 {
362 #ifdef SILC_THREADS
363   cond->cond->Wait(*mutex->mutex);
364 #endif /* SILC_THREADS*/
365 }
366
367 SilcBool silc_cond_timedwait(SilcCond cond, SilcMutex mutex,
368                              int timeout)
369 {
370 #ifdef SILC_THREADS
371   if (timeout)
372     return (cond->cond->TimedWait(*mutex->mutex, (TInt)timeout * 1000) ==
373             KErrNone);
374   return (cond->cond->Wait(*mutex->mutex) == KErrNone);
375 #else
376   return FALSE;
377 #endif /* SILC_THREADS*/
378 }