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 void silc_mutex_assert_locked(SilcMutex mutex)
207 SILC_VERIFY(mutex->locked);
208 #endif /* SILC_THREADS */
211 /***************************** SILC Rwlock API ******************************/
213 /* SILC read/write lock structure */
214 struct SilcRwLockStruct {
216 pthread_rwlock_t rwlock;
219 #endif /* SILC_THREADS */
222 SilcBool silc_rwlock_alloc(SilcRwLock *rwlock)
225 *rwlock = silc_calloc(1, sizeof(**rwlock));
228 if (pthread_rwlock_init(&(*rwlock)->rwlock, NULL)) {
229 silc_set_errno_posix(errno);
236 #endif /* SILC_THREADS */
239 void silc_rwlock_free(SilcRwLock rwlock)
243 pthread_rwlock_destroy(&rwlock->rwlock);
246 #endif /* SILC_THREADS */
249 void silc_rwlock_rdlock(SilcRwLock rwlock)
253 pthread_rwlock_rdlock(&rwlock->rwlock);
254 #endif /* SILC_THREADS */
257 void silc_rwlock_wrlock(SilcRwLock rwlock)
261 SILC_VERIFY(pthread_rwlock_wrlock(&rwlock->rwlock) == 0);
262 #endif /* SILC_THREADS */
265 void silc_rwlock_unlock(SilcRwLock rwlock)
269 SILC_VERIFY(pthread_rwlock_unlock(&rwlock->rwlock) == 0);
270 #endif /* SILC_THREADS */
273 /****************************** SILC Cond API *******************************/
275 /* SILC Conditional Variable context */
276 struct SilcCondStruct {
281 #endif /* SILC_THREADS*/
284 SilcBool silc_cond_alloc(SilcCond *cond)
287 *cond = silc_calloc(1, sizeof(**cond));
290 if (pthread_cond_init(&(*cond)->cond, NULL)) {
291 silc_set_errno_posix(errno);
298 #endif /* SILC_THREADS*/
301 void silc_cond_free(SilcCond cond)
304 pthread_cond_destroy(&cond->cond);
306 #endif /* SILC_THREADS*/
309 void silc_cond_signal(SilcCond cond)
312 pthread_cond_signal(&cond->cond);
313 #endif /* SILC_THREADS*/
316 void silc_cond_broadcast(SilcCond cond)
319 pthread_cond_broadcast(&cond->cond);
320 #endif /* SILC_THREADS*/
323 void silc_cond_wait(SilcCond cond, SilcMutex mutex)
326 pthread_cond_wait(&cond->cond, &mutex->mutex);
327 #endif /* SILC_THREADS*/
330 SilcBool silc_cond_timedwait(SilcCond cond, SilcMutex mutex,
336 t.tv_sec = timeout / 1000;
337 t.tv_nsec = (timeout % 1000) * 1000;
338 return pthread_cond_timedwait(&cond->cond, &mutex->mutex, &t) == 0;
341 return pthread_cond_wait(&cond->cond, &mutex->mutex) == 0;
344 #endif /* SILC_THREADS*/
347 /************************** Thread-local Storage ****************************/
349 #if (defined(SILC_THREADS) && defined(HAVE_PTHREAD_KEY_CREATE) && \
350 defined(HAVE_PTHREAD_ONCE))
352 static SilcBool key_set = FALSE;
353 static pthread_key_t key;
354 static pthread_once_t key_once = PTHREAD_ONCE_INIT;
356 static void silc_thread_tls_destructor(void *context)
358 SilcTls tls = context;
360 if (tls->tls_variables)
361 silc_hash_table_free(tls->tls_variables);
362 tls->tls_variables = NULL;
367 static void silc_thread_tls_alloc(void)
369 if (pthread_key_create(&key, silc_thread_tls_destructor))
370 SILC_LOG_ERROR(("Error creating Thread-local storage"));
374 SilcTls silc_thread_tls_init(void)
378 pthread_once(&key_once, silc_thread_tls_alloc);
380 if (silc_thread_get_tls())
381 return silc_thread_get_tls();
383 /* Allocate Tls for the thread */
384 tls = silc_calloc(1, sizeof(*tls));
386 SILC_LOG_ERROR(("Error allocating Thread-local storage"));
389 pthread_setspecific(key, tls);
391 /* Allocate global lock */
392 silc_mutex_alloc(&tls->lock);
397 static SilcTls silc_thread_tls_init_shared(SilcTls other)
401 pthread_once(&key_once, silc_thread_tls_alloc);
403 if (silc_thread_get_tls())
404 return silc_thread_get_tls();
406 /* Allocate Tls for the thread */
407 tls = silc_calloc(1, sizeof(*tls));
409 SILC_LOG_ERROR(("Error allocating Thread-local storage"));
412 pthread_setspecific(key, tls);
414 /* Take shared data */
415 tls->shared_data = 1;
416 tls->lock = other->lock;
417 tls->variables = other->variables;
422 SilcTls silc_thread_get_tls(void)
426 return pthread_getspecific(key);
429 void silc_thread_tls_uninit(void)
431 SilcTls tls = silc_thread_get_tls();
433 if (!tls || tls->shared_data)
436 /* Main thread cleanup */
437 if (tls->tls_variables)
438 silc_hash_table_free(tls->tls_variables);
440 silc_hash_table_free(tls->variables);
442 silc_mutex_free(tls->lock);
443 tls->variables = NULL;
446 pthread_setspecific(key, NULL);
452 SilcTls tls_ptr = NULL;
454 SilcTls silc_thread_tls_init(void)
456 if (silc_thread_get_tls())
457 return silc_thread_get_tls();
460 memset(tls_ptr, 0, sizeof(*tls_ptr));
462 atexit(silc_thread_tls_uninit);
467 static SilcTls silc_thread_tls_init_shared(SilcTls other)
469 return silc_thread_tls_init();
472 SilcTls silc_thread_get_tls(void)
477 void silc_thread_tls_uninit(void)
479 if (tls.tls_variables)
480 silc_hash_table_free(tls.tls_variables);
482 silc_hash_table_free(tls.variables);
484 silc_mutex_free(tls.lock);