2a826251a2ae0825e73f535678e6002d1c9b89af
[runtime.git] / lib / silcutil / unix / silcunixthread.c
1 /*
2
3   silcunixthread.c
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2001 - 2008 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 "silcruntime.h"
21
22 /**************************** SILC Thread API *******************************/
23
24 typedef struct {
25   SilcThreadStart start_func;
26   void *context;
27 } *SilcThreadStartContext;
28
29 static void *silc_thread_start(void *context)
30 {
31   SilcThreadStartContext c = context;
32   SilcThreadStart start_func = c->start_func;
33   void *start_context = c->context;
34
35   silc_free(c);
36
37   silc_thread_tls_init();
38
39   return start_func(start_context);
40 }
41
42 SilcThread silc_thread_create(SilcThreadStart start_func, void *context,
43                               SilcBool waitable)
44 {
45 #ifdef SILC_THREADS
46   SilcThreadStartContext c;
47   pthread_attr_t attr;
48   pthread_t thread;
49   int ret;
50
51   SILC_LOG_DEBUG(("Creating new thread"));
52
53   if (!start_func)
54     return NULL;
55
56   c = silc_calloc(1, sizeof(*c));
57   if (!c)
58     return NULL;
59   c->start_func = start_func;
60   c->context = context;
61
62   if (pthread_attr_init(&attr)) {
63     silc_set_errno_posix(errno);
64     SILC_LOG_ERROR(("Thread error: %s", silc_errno_string(silc_errno)));
65     silc_free(c);
66     return NULL;
67   }
68
69   if (pthread_attr_setdetachstate(&attr,
70                                   waitable ? PTHREAD_CREATE_JOINABLE :
71                                   PTHREAD_CREATE_DETACHED)) {
72     silc_set_errno_posix(errno);
73     SILC_LOG_ERROR(("Thread error: %s", silc_errno_string(silc_errno)));
74     pthread_attr_destroy(&attr);
75     silc_free(c);
76     return NULL;
77   }
78
79   ret = pthread_create(&thread, &attr, silc_thread_start, c);
80   if (ret) {
81     silc_set_errno_posix(errno);
82     SILC_LOG_ERROR(("Thread error: %s", silc_errno_string(silc_errno)));
83     pthread_attr_destroy(&attr);
84     silc_free(c);
85     return NULL;
86   }
87
88   pthread_attr_destroy(&attr);
89
90   SILC_LOG_DEBUG(("Created thread %p", (SilcThread)thread));
91
92   return (SilcThread)thread;
93 #else
94   /* Call thread callback immediately */
95   (*start_func)(context);
96   return NULL;
97 #endif
98 }
99
100 void silc_thread_exit(void *exit_value)
101 {
102 #ifdef SILC_THREADS
103   pthread_exit(exit_value);
104 #endif
105 }
106
107 SilcThread silc_thread_self(void)
108 {
109 #ifdef SILC_THREADS
110   pthread_t self = pthread_self();
111   return (SilcThread)self;
112 #else
113   return NULL;
114 #endif
115 }
116
117 SilcBool silc_thread_wait(SilcThread thread, void **exit_value)
118 {
119 #ifdef SILC_THREADS
120   SILC_LOG_DEBUG(("Waiting for thread %p", thread));
121   if (!pthread_join(*(pthread_t *)thread, exit_value))
122     return TRUE;
123   return FALSE;
124 #else
125   return FALSE;
126 #endif
127 }
128
129 void silc_thread_yield(void)
130 {
131 #ifdef SILC_THREADS
132 #ifdef HAVE_SCHED_YIELD
133   sched_yield();
134 #endif /* HAVE_SCHED_YIELD */
135 #endif /* SILC_THREADS */
136 }
137
138 /***************************** SILC Mutex API *******************************/
139
140 /* SILC Mutex structure */
141 struct SilcMutexStruct {
142 #ifdef SILC_THREADS
143   pthread_mutex_t mutex;
144 #endif /* SILC_THREADS */
145   unsigned int locked : 1;
146 };
147
148 SilcBool silc_mutex_alloc(SilcMutex *mutex)
149 {
150 #ifdef SILC_THREADS
151   *mutex = silc_calloc(1, sizeof(**mutex));
152   if (*mutex == NULL)
153     return FALSE;
154   if (pthread_mutex_init(&(*mutex)->mutex, NULL)) {
155     silc_set_errno_posix(errno);
156     silc_free(*mutex);
157     return FALSE;
158   }
159   (*mutex)->locked = FALSE;
160   return TRUE;
161 #else
162   return FALSE;
163 #endif /* SILC_THREADS */
164 }
165
166 void silc_mutex_free(SilcMutex mutex)
167 {
168 #ifdef SILC_THREADS
169   if (mutex) {
170     pthread_mutex_destroy(&mutex->mutex);
171     silc_free(mutex);
172   }
173 #endif /* SILC_THREADS */
174 }
175
176 void silc_mutex_lock(SilcMutex mutex)
177 {
178 #ifdef SILC_THREADS
179   if (mutex) {
180     SILC_VERIFY(pthread_mutex_lock(&mutex->mutex) == 0);
181     mutex->locked = TRUE;
182   }
183 #endif /* SILC_THREADS */
184 }
185
186 void silc_mutex_unlock(SilcMutex mutex)
187 {
188 #ifdef SILC_THREADS
189   if (mutex) {
190     mutex->locked = FALSE;
191     SILC_VERIFY(pthread_mutex_unlock(&mutex->mutex) == 0);
192   }
193 #endif /* SILC_THREADS */
194 }
195
196 void silc_mutex_assert_locked(SilcMutex mutex)
197 {
198 #ifdef SILC_THREADS
199   if (mutex)
200     SILC_VERIFY(mutex->locked);
201 #endif /* SILC_THREADS */
202 }
203
204 /***************************** SILC Rwlock API ******************************/
205
206 /* SILC read/write lock structure */
207 struct SilcRwLockStruct {
208 #ifdef SILC_THREADS
209   pthread_rwlock_t rwlock;
210 #else
211   void *tmp;
212 #endif /* SILC_THREADS */
213 };
214
215 SilcBool silc_rwlock_alloc(SilcRwLock *rwlock)
216 {
217 #ifdef SILC_THREADS
218   *rwlock = silc_calloc(1, sizeof(**rwlock));
219   if (*rwlock == NULL)
220     return FALSE;
221   if (pthread_rwlock_init(&(*rwlock)->rwlock, NULL)) {
222     silc_set_errno_posix(errno);
223     silc_free(*rwlock);
224     return FALSE;
225   }
226   return TRUE;
227 #else
228   return FALSE;
229 #endif /* SILC_THREADS */
230 }
231
232 void silc_rwlock_free(SilcRwLock rwlock)
233 {
234 #ifdef SILC_THREADS
235   if (rwlock) {
236     pthread_rwlock_destroy(&rwlock->rwlock);
237     silc_free(rwlock);
238   }
239 #endif /* SILC_THREADS */
240 }
241
242 void silc_rwlock_rdlock(SilcRwLock rwlock)
243 {
244 #ifdef SILC_THREADS
245   if (rwlock)
246     pthread_rwlock_rdlock(&rwlock->rwlock);
247 #endif /* SILC_THREADS */
248 }
249
250 void silc_rwlock_wrlock(SilcRwLock rwlock)
251 {
252 #ifdef SILC_THREADS
253   if (rwlock)
254     SILC_VERIFY(pthread_rwlock_wrlock(&rwlock->rwlock) == 0);
255 #endif /* SILC_THREADS */
256 }
257
258 void silc_rwlock_unlock(SilcRwLock rwlock)
259 {
260 #ifdef SILC_THREADS
261   if (rwlock)
262     SILC_VERIFY(pthread_rwlock_unlock(&rwlock->rwlock) == 0);
263 #endif /* SILC_THREADS */
264 }
265
266 /****************************** SILC Cond API *******************************/
267
268 /* SILC Conditional Variable context */
269 struct SilcCondStruct {
270 #ifdef SILC_THREADS
271   pthread_cond_t cond;
272 #else
273   void *tmp;
274 #endif /* SILC_THREADS*/
275 };
276
277 SilcBool silc_cond_alloc(SilcCond *cond)
278 {
279 #ifdef SILC_THREADS
280   *cond = silc_calloc(1, sizeof(**cond));
281   if (*cond == NULL)
282     return FALSE;
283   if (pthread_cond_init(&(*cond)->cond, NULL)) {
284     silc_set_errno_posix(errno);
285     silc_free(*cond);
286     return FALSE;
287   }
288   return TRUE;
289 #else
290   return FALSE;
291 #endif /* SILC_THREADS*/
292 }
293
294 void silc_cond_free(SilcCond cond)
295 {
296 #ifdef SILC_THREADS
297   pthread_cond_destroy(&cond->cond);
298   silc_free(cond);
299 #endif /* SILC_THREADS*/
300 }
301
302 void silc_cond_signal(SilcCond cond)
303 {
304 #ifdef SILC_THREADS
305   pthread_cond_signal(&cond->cond);
306 #endif /* SILC_THREADS*/
307 }
308
309 void silc_cond_broadcast(SilcCond cond)
310 {
311 #ifdef SILC_THREADS
312   pthread_cond_broadcast(&cond->cond);
313 #endif /* SILC_THREADS*/
314 }
315
316 void silc_cond_wait(SilcCond cond, SilcMutex mutex)
317 {
318 #ifdef SILC_THREADS
319   pthread_cond_wait(&cond->cond, &mutex->mutex);
320 #endif /* SILC_THREADS*/
321 }
322
323 SilcBool silc_cond_timedwait(SilcCond cond, SilcMutex mutex,
324                              int timeout)
325 {
326 #ifdef SILC_THREADS
327   struct timespec t;
328   if (timeout) {
329     t.tv_sec = timeout / 1000;
330     t.tv_nsec = (timeout % 1000) * 1000;
331     return pthread_cond_timedwait(&cond->cond, &mutex->mutex, &t) == 0;
332   }
333
334   return pthread_cond_wait(&cond->cond, &mutex->mutex) == 0;
335 #else
336   return FALSE;
337 #endif /* SILC_THREADS*/
338 }
339
340 /************************** Thread-local Storage ****************************/
341
342 #if (defined(SILC_THREADS) && defined(HAVE_PTHREAD_KEY_CREATE) && \
343      defined(HAVE_PTHREAD_ONCE))
344
345 static SilcBool key_set = FALSE;
346 static pthread_key_t key;
347 static pthread_once_t key_once = PTHREAD_ONCE_INIT;
348
349 static void silc_thread_tls_destructor(void *context)
350 {
351   silc_free(context);
352 }
353
354 static void silc_thread_tls_alloc(void)
355 {
356   if (pthread_key_create(&key, silc_thread_tls_destructor))
357     SILC_LOG_ERROR(("Error creating Thread-local storage"));
358   key_set = TRUE;
359 }
360
361 SilcTls silc_thread_tls_init(void)
362 {
363   SilcTls tls;
364
365   pthread_once(&key_once, silc_thread_tls_alloc);
366
367   if (silc_thread_get_tls())
368     return silc_thread_get_tls();
369
370   /* Allocate Tls for the thread */
371   tls = silc_calloc(1, sizeof(*tls));
372   if (!tls) {
373     SILC_LOG_ERROR(("Error allocating Thread-local storage"));
374     return NULL;
375   }
376
377   pthread_setspecific(key, tls);
378   return tls;
379 }
380
381 SilcTls silc_thread_get_tls(void)
382 {
383   if (!key_set)
384     return NULL;
385   return pthread_getspecific(key);
386 }
387
388 #else
389
390 SilcTlsStruct tls;
391 SilcTls tls_ptr = NULL;
392
393 SilcTls silc_thread_tls_init(void)
394 {
395   if (silc_thread_get_tls())
396     return silc_thread_get_tls();
397
398   tls_ptr = &tls;
399   memset(tls_ptr, 0, sizeof(*tls_ptr));
400   return tls_ptr;
401 }
402
403 SilcTls silc_thread_get_tls(void)
404 {
405   return tls_ptr;
406 }
407
408 #endif