silc_snprintf name fixes.
[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
218 /****************************** SILC Cond API *******************************/
219
220 /* SILC Conditional Variable context */
221 struct SilcCondStruct {
222 #ifdef SILC_THREADS
223   RCondVar *cond;
224 #else
225   void *tmp;
226 #endif /* SILC_THREADS*/
227 };
228
229 SilcBool silc_cond_alloc(SilcCond *cond)
230 {
231 #ifdef SILC_THREADS
232   *cond = (SilcCond)silc_calloc(1, sizeof(**cond));
233   if (*cond == NULL)
234     return FALSE;
235   (*cond)->cond = new RCondVar();
236   if (!(*cond)->cond) {
237     silc_free(*cond);
238     return FALSE;
239   }
240   if ((*cond)->cond->CreateLocal() != KErrNone) {
241     delete (*cond)->cond;
242     silc_free(*cond);
243     return FALSE;
244   }
245   return TRUE;
246 #else
247   return FALSE;
248 #endif /* SILC_THREADS*/
249 }
250
251 void silc_cond_free(SilcCond cond)
252 {
253 #ifdef SILC_THREADS
254   cond->cond->Close();
255   delete cond->cond;
256   silc_free(cond);
257 #endif /* SILC_THREADS*/
258 }
259
260 void silc_cond_signal(SilcCond cond)
261 {
262 #ifdef SILC_THREADS
263   cond->cond->Signal();
264 #endif /* SILC_THREADS*/
265 }
266
267 void silc_cond_broadcast(SilcCond cond)
268 {
269 #ifdef SILC_THREADS
270   cond->cond->Broadcast();
271 #endif /* SILC_THREADS*/
272 }
273
274 void silc_cond_wait(SilcCond cond, SilcMutex mutex)
275 {
276 #ifdef SILC_THREADS
277   cond->cond->Wait(*mutex->mutex);
278 #endif /* SILC_THREADS*/
279 }
280
281 SilcBool silc_cond_timedwait(SilcCond cond, SilcMutex mutex,
282                              int timeout)
283 {
284 #ifdef SILC_THREADS
285   if (timeout)
286     return (cond->cond->TimedWait(*mutex->mutex, (TInt)timeout * 1000) ==
287             KErrNone);
288   return (cond->cond->Wait(*mutex->mutex) == KErrNone);
289 #else
290   return FALSE;
291 #endif /* SILC_THREADS*/
292 }