3 silcsymbianthread.cpp
\r
5 Author: Pekka Riikonen <priikone@silcnet.org>
\r
7 Copyright (C) 2006 Pekka Riikonen
\r
9 This program is free software; you can redistribute it and/or modify
\r
10 it under the terms of the GNU General Public License as published by
\r
11 the Free Software Foundation; version 2 of the License.
\r
13 This program is distributed in the hope that it will be useful,
\r
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
16 GNU General Public License for more details.
\r
23 /**************************** SILC Thread API *******************************/
\r
25 /* Thread structure for Symbian */
\r
29 SilcThreadStart start_func;
\r
35 } *SilcSymbianThread;
\r
37 /* The actual thread function */
\r
39 TInt silc_thread_epoc_start(TAny *context)
\r
42 SilcSymbianThread thread = (SilcSymbianThread)context;
\r
45 ret = thread->start_func(thread->context);
\r
46 silc_thread_exit(ret);
\r
52 SilcThread silc_thread_create(SilcThreadStart start_func, void *context,
\r
56 SilcSymbianThread thread;
\r
60 SILC_LOG_DEBUG(("Creating new thread"));
\r
62 thread = (SilcSymbianThread)silc_calloc(1, sizeof(*thread));
\r
65 thread->start_func = start_func;
\r
66 thread->context = context;
\r
67 thread->waitable = waitable;
\r
69 /* Create the thread */
\r
70 /* XXX Unique name should be given for the thread */
\r
71 thread->thread = new RThread();
\r
72 if (!thread->thread) {
\r
77 name = (TText *)"silc" + time(NULL);
\r
78 ret = thread->thread->Create(name, silc_thread_epoc_start,
\r
79 8192, 4096, 1024 * 1024, (TAny *)thread);
\r
80 if (ret != KErrNone) {
\r
81 SILC_LOG_ERROR(("Could not create new thread"));
\r
82 delete thread->thread;
\r
86 thread->thread->Resume();
\r
88 return (SilcThread)thread;
\r
90 /* Call thread callback immediately */
\r
91 (*start_func)(context);
\r
96 void silc_thread_exit(void *exit_value)
\r
103 SilcThread silc_thread_self(void)
\r
105 #ifdef SILC_THREADS
\r
113 SilcBool silc_thread_wait(SilcThread thread, void **exit_value)
\r
115 #ifdef SILC_THREADS
\r
123 /***************************** SILC Mutex API *******************************/
\r
125 /* SILC Mutex structure */
\r
126 struct SilcMutexStruct {
\r
127 #ifdef SILC_THREADS
\r
129 #endif /* SILC_THREADS */
\r
130 unsigned int locked : 1;
\r
133 SilcBool silc_mutex_alloc(SilcMutex *mutex)
\r
135 #ifdef SILC_THREADS
\r
136 *mutex = (SilcMutex)silc_calloc(1, sizeof(**mutex));
\r
137 if (*mutex == NULL)
\r
139 (*mutex)->mutex = new RMutex();
\r
140 if (!(*mutex)->mutex) {
\r
144 if ((*mutex)->mutex->CreateLocal() != KErrNone) {
\r
145 delete (*mutex)->mutex;
\r
149 (*mutex)->locked = FALSE;
\r
153 #endif /* SILC_THREADS */
\r
156 void silc_mutex_free(SilcMutex mutex)
\r
158 #ifdef SILC_THREADS
\r
160 mutex->mutex->Close();
\r
161 delete mutex->mutex;
\r
164 #endif /* SILC_THREADS */
\r
167 void silc_mutex_lock(SilcMutex mutex)
\r
169 #ifdef SILC_THREADS
\r
171 mutex->mutex->Wait();
\r
172 mutex->locked = TRUE;
\r
174 #endif /* SILC_THREADS */
\r
177 void silc_mutex_unlock(SilcMutex mutex)
\r
179 #ifdef SILC_THREADS
\r
181 mutex->mutex->Signal();
\r
182 mutex->locked = FALSE;
\r
184 #endif /* SILC_THREADS */
\r
187 void silc_mutex_assert_locked(SilcMutex mutex)
\r
189 #ifdef SILC_THREADS
\r
191 SILC_ASSERT(mutex->locked);
\r
192 #endif /* SILC_THREADS */
\r
196 /****************************** SILC Cond API *******************************/
\r
198 /* SILC Conditional Variable context */
\r
199 struct SilcCondStruct {
\r
200 #ifdef SILC_THREADS
\r
204 #endif /* SILC_THREADS*/
\r
207 SilcBool silc_cond_alloc(SilcCond *cond)
\r
209 #ifdef SILC_THREADS
\r
210 *cond = (SilcCond)silc_calloc(1, sizeof(**cond));
\r
213 (*cond)->cond = new RCondVar();
\r
214 if (!(*cond)->cond) {
\r
218 if ((*cond)->cond->CreateLocal() != KErrNone) {
\r
219 delete (*cond)->cond;
\r
226 #endif /* SILC_THREADS*/
\r
229 void silc_cond_free(SilcCond cond)
\r
231 #ifdef SILC_THREADS
\r
232 cond->cond->Close();
\r
235 #endif /* SILC_THREADS*/
\r
238 void silc_cond_signal(SilcCond cond)
\r
240 #ifdef SILC_THREADS
\r
241 cond->cond->Signal();
\r
242 #endif /* SILC_THREADS*/
\r
245 void silc_cond_broadcast(SilcCond cond)
\r
247 #ifdef SILC_THREADS
\r
248 cond->cond->Broadcast();
\r
249 #endif /* SILC_THREADS*/
\r
252 void silc_cond_wait(SilcCond cond, SilcMutex mutex)
\r
254 #ifdef SILC_THREADS
\r
255 cond->cond->Wait(*mutex->mutex);
\r
256 #endif /* SILC_THREADS*/
\r
259 SilcBool silc_cond_timedwait(SilcCond cond, SilcMutex mutex,
\r
262 #ifdef SILC_THREADS
\r
264 return (cond->cond->TimedWait(*mutex->mutex, (TInt)timeout * 1000) ==
\r
266 return (cond->cond->Wait(*mutex->mutex) == KErrNone);
\r
269 #endif /* SILC_THREADS*/
\r