5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2001 - 2008 Pekka Riikonen
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.
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.
20 #include "silcruntime.h"
22 /************************ Static utility functions **************************/
24 static SilcTls silc_thread_tls_init_shared(SilcTls other);
26 /**************************** SILC Thread API *******************************/
30 SilcThreadStart start_func;
32 } *SilcThreadStartContext;
34 static void *silc_thread_start(void *context)
36 SilcThreadStartContext c = context;
37 SilcThreadStart start_func = c->start_func;
38 void *start_context = c->context;
39 SilcTls other = c->tls;
43 silc_thread_tls_init_shared(other);
45 return start_func(start_context);
48 SilcThread silc_thread_create(SilcThreadStart start_func, void *context,
52 SilcThreadStartContext c;
57 SILC_LOG_DEBUG(("Creating new thread"));
62 c = silc_calloc(1, sizeof(*c));
65 c->start_func = start_func;
67 c->tls = silc_thread_get_tls();
69 if (pthread_attr_init(&attr)) {
70 silc_set_errno_posix(errno);
71 SILC_LOG_ERROR(("Thread error: %s", silc_errno_string(silc_errno)));
76 if (pthread_attr_setdetachstate(&attr,
77 waitable ? PTHREAD_CREATE_JOINABLE :
78 PTHREAD_CREATE_DETACHED)) {
79 silc_set_errno_posix(errno);
80 SILC_LOG_ERROR(("Thread error: %s", silc_errno_string(silc_errno)));
81 pthread_attr_destroy(&attr);
86 ret = pthread_create(&thread, &attr, silc_thread_start, c);
88 silc_set_errno_posix(errno);
89 SILC_LOG_ERROR(("Thread error: %s", silc_errno_string(silc_errno)));
90 pthread_attr_destroy(&attr);
95 pthread_attr_destroy(&attr);
97 SILC_LOG_DEBUG(("Created thread %p", (SilcThread)thread));
99 return (SilcThread)thread;
101 /* Call thread callback immediately */
102 (*start_func)(context);
107 void silc_thread_exit(void *exit_value)
110 pthread_exit(exit_value);
114 SilcThread silc_thread_self(void)
117 pthread_t self = pthread_self();
118 return (SilcThread)self;
124 SilcBool silc_thread_wait(SilcThread thread, void **exit_value)
127 SILC_LOG_DEBUG(("Waiting for thread %p", thread));
128 if (!pthread_join(*(pthread_t *)thread, exit_value))
136 void silc_thread_yield(void)
139 #ifdef HAVE_SCHED_YIELD
141 #endif /* HAVE_SCHED_YIELD */
142 #endif /* SILC_THREADS */
145 /***************************** SILC Mutex API *******************************/
147 /* SILC Mutex structure */
148 struct SilcMutexStruct {
150 pthread_mutex_t mutex;
151 #endif /* SILC_THREADS */
152 unsigned int locked : 1;
155 SilcBool silc_mutex_alloc(SilcMutex *mutex)
158 *mutex = silc_calloc(1, sizeof(**mutex));
161 if (pthread_mutex_init(&(*mutex)->mutex, NULL)) {
162 silc_set_errno_posix(errno);
166 (*mutex)->locked = FALSE;
170 #endif /* SILC_THREADS */
173 void silc_mutex_free(SilcMutex mutex)
177 pthread_mutex_destroy(&mutex->mutex);
180 #endif /* SILC_THREADS */
183 void silc_mutex_lock(SilcMutex mutex)
187 SILC_VERIFY(pthread_mutex_lock(&mutex->mutex) == 0);
188 mutex->locked = TRUE;
190 #endif /* SILC_THREADS */
193 void silc_mutex_unlock(SilcMutex mutex)
197 mutex->locked = FALSE;
198 SILC_VERIFY(pthread_mutex_unlock(&mutex->mutex) == 0);
200 #endif /* SILC_THREADS */
203 SilcBool silc_mutex_trylock(SilcMutex mutex)
207 if (pthread_mutex_trylock(&mutex->mutex) == 0) {
208 mutex->locked = TRUE;
212 #endif /* SILC_THREADS */
216 void silc_mutex_assert_locked(SilcMutex mutex)
220 SILC_VERIFY(mutex->locked);
221 #endif /* SILC_THREADS */
224 /***************************** SILC Rwlock API ******************************/
226 /* SILC read/write lock structure */
227 struct SilcRwLockStruct {
229 pthread_rwlock_t rwlock;
232 #endif /* SILC_THREADS */
235 SilcBool silc_rwlock_alloc(SilcRwLock *rwlock)
238 *rwlock = silc_calloc(1, sizeof(**rwlock));
241 if (pthread_rwlock_init(&(*rwlock)->rwlock, NULL)) {
242 silc_set_errno_posix(errno);
249 #endif /* SILC_THREADS */
252 void silc_rwlock_free(SilcRwLock rwlock)
256 pthread_rwlock_destroy(&rwlock->rwlock);
259 #endif /* SILC_THREADS */
262 void silc_rwlock_rdlock(SilcRwLock rwlock)
266 pthread_rwlock_rdlock(&rwlock->rwlock);
267 #endif /* SILC_THREADS */
270 void silc_rwlock_wrlock(SilcRwLock rwlock)
274 SILC_VERIFY(pthread_rwlock_wrlock(&rwlock->rwlock) == 0);
275 #endif /* SILC_THREADS */
278 void silc_rwlock_unlock(SilcRwLock rwlock)
282 SILC_VERIFY(pthread_rwlock_unlock(&rwlock->rwlock) == 0);
283 #endif /* SILC_THREADS */
286 /****************************** SILC Cond API *******************************/
288 /* SILC Conditional Variable context */
289 struct SilcCondStruct {
294 #endif /* SILC_THREADS*/
297 SilcBool silc_cond_alloc(SilcCond *cond)
300 *cond = silc_calloc(1, sizeof(**cond));
303 if (pthread_cond_init(&(*cond)->cond, NULL)) {
304 silc_set_errno_posix(errno);
311 #endif /* SILC_THREADS*/
314 void silc_cond_free(SilcCond cond)
317 pthread_cond_destroy(&cond->cond);
319 #endif /* SILC_THREADS*/
322 void silc_cond_signal(SilcCond cond)
325 pthread_cond_signal(&cond->cond);
326 #endif /* SILC_THREADS*/
329 void silc_cond_broadcast(SilcCond cond)
332 pthread_cond_broadcast(&cond->cond);
333 #endif /* SILC_THREADS*/
336 void silc_cond_wait(SilcCond cond, SilcMutex mutex)
339 pthread_cond_wait(&cond->cond, &mutex->mutex);
340 #endif /* SILC_THREADS*/
343 SilcBool silc_cond_timedwait(SilcCond cond, SilcMutex mutex,
349 t.tv_sec = timeout / 1000;
350 t.tv_nsec = (timeout % 1000) * 1000;
351 return pthread_cond_timedwait(&cond->cond, &mutex->mutex, &t) == 0;
354 return pthread_cond_wait(&cond->cond, &mutex->mutex) == 0;
357 #endif /* SILC_THREADS*/
360 /************************** Thread-local Storage ****************************/
362 #if (defined(SILC_THREADS) && defined(HAVE_PTHREAD_KEY_CREATE) && \
363 defined(HAVE_PTHREAD_ONCE))
365 static SilcBool key_set = FALSE;
366 static pthread_key_t key;
367 static pthread_once_t key_once = PTHREAD_ONCE_INIT;
369 static void silc_thread_tls_destructor(void *context)
371 SilcTls tls = context;
373 if (tls->tls_variables)
374 silc_hash_table_free(tls->tls_variables);
375 tls->tls_variables = NULL;
380 static void silc_thread_tls_alloc(void)
382 if (pthread_key_create(&key, silc_thread_tls_destructor))
383 SILC_LOG_ERROR(("Error creating Thread-local storage"));
387 SilcTls silc_thread_tls_init(void)
391 pthread_once(&key_once, silc_thread_tls_alloc);
393 if (silc_thread_get_tls())
394 return silc_thread_get_tls();
396 /* Allocate Tls for the thread */
397 tls = silc_calloc(1, sizeof(*tls));
399 SILC_LOG_ERROR(("Error allocating Thread-local storage"));
402 pthread_setspecific(key, tls);
404 /* Allocate global lock */
405 silc_mutex_alloc(&tls->lock);
410 static SilcTls silc_thread_tls_init_shared(SilcTls other)
414 pthread_once(&key_once, silc_thread_tls_alloc);
416 if (silc_thread_get_tls())
417 return silc_thread_get_tls();
419 /* Allocate Tls for the thread */
420 tls = silc_calloc(1, sizeof(*tls));
422 SILC_LOG_ERROR(("Error allocating Thread-local storage"));
425 pthread_setspecific(key, tls);
427 /* Take shared data */
428 tls->shared_data = 1;
429 tls->lock = other->lock;
430 tls->variables = other->variables;
435 SilcTls silc_thread_get_tls(void)
439 return pthread_getspecific(key);
442 void silc_thread_tls_uninit(void)
444 SilcTls tls = silc_thread_get_tls();
446 if (!tls || tls->shared_data)
449 /* Main thread cleanup */
450 if (tls->tls_variables)
451 silc_hash_table_free(tls->tls_variables);
453 silc_hash_table_free(tls->variables);
455 silc_mutex_free(tls->lock);
456 tls->variables = NULL;
459 pthread_setspecific(key, NULL);
465 SilcTls tls_ptr = NULL;
467 SilcTls silc_thread_tls_init(void)
469 if (silc_thread_get_tls())
470 return silc_thread_get_tls();
473 memset(tls_ptr, 0, sizeof(*tls_ptr));
475 atexit(silc_thread_tls_uninit);
480 static SilcTls silc_thread_tls_init_shared(SilcTls other)
482 return silc_thread_tls_init();
485 SilcTls silc_thread_get_tls(void)
490 void silc_thread_tls_uninit(void)
492 if (tls.tls_variables)
493 silc_hash_table_free(tls.tls_variables);
495 silc_hash_table_free(tls.variables);
497 silc_mutex_free(tls.lock);