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 /**************************** SILC Thread API *******************************/
25 SilcThreadStart start_func;
27 } *SilcThreadStartContext;
29 static void *silc_thread_start(void *context)
31 SilcThreadStartContext c = context;
32 SilcThreadStart start_func = c->start_func;
33 void *start_context = c->context;
37 silc_thread_tls_init();
39 return start_func(start_context);
42 SilcThread silc_thread_create(SilcThreadStart start_func, void *context,
46 SilcThreadStartContext c;
51 SILC_LOG_DEBUG(("Creating new thread"));
56 c = silc_calloc(1, sizeof(*c));
59 c->start_func = start_func;
62 if (pthread_attr_init(&attr)) {
63 silc_set_errno_posix(errno);
64 SILC_LOG_ERROR(("Thread error: %s", silc_errno_string(silc_errno)));
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);
79 ret = pthread_create(&thread, &attr, silc_thread_start, c);
81 silc_set_errno_posix(errno);
82 SILC_LOG_ERROR(("Thread error: %s", silc_errno_string(silc_errno)));
83 pthread_attr_destroy(&attr);
88 pthread_attr_destroy(&attr);
90 SILC_LOG_DEBUG(("Created thread %p", (SilcThread)thread));
92 return (SilcThread)thread;
94 /* Call thread callback immediately */
95 (*start_func)(context);
100 void silc_thread_exit(void *exit_value)
103 pthread_exit(exit_value);
107 SilcThread silc_thread_self(void)
110 pthread_t self = pthread_self();
111 return (SilcThread)self;
117 SilcBool silc_thread_wait(SilcThread thread, void **exit_value)
120 SILC_LOG_DEBUG(("Waiting for thread %p", thread));
121 if (!pthread_join(*(pthread_t *)thread, exit_value))
129 void silc_thread_yield(void)
132 #ifdef HAVE_SCHED_YIELD
134 #endif /* HAVE_SCHED_YIELD */
135 #endif /* SILC_THREADS */
138 /***************************** SILC Mutex API *******************************/
140 /* SILC Mutex structure */
141 struct SilcMutexStruct {
143 pthread_mutex_t mutex;
144 #endif /* SILC_THREADS */
145 unsigned int locked : 1;
148 SilcBool silc_mutex_alloc(SilcMutex *mutex)
151 *mutex = silc_calloc(1, sizeof(**mutex));
154 if (pthread_mutex_init(&(*mutex)->mutex, NULL)) {
155 silc_set_errno_posix(errno);
159 (*mutex)->locked = FALSE;
163 #endif /* SILC_THREADS */
166 void silc_mutex_free(SilcMutex mutex)
170 pthread_mutex_destroy(&mutex->mutex);
173 #endif /* SILC_THREADS */
176 void silc_mutex_lock(SilcMutex mutex)
180 SILC_VERIFY(pthread_mutex_lock(&mutex->mutex) == 0);
181 mutex->locked = TRUE;
183 #endif /* SILC_THREADS */
186 void silc_mutex_unlock(SilcMutex mutex)
190 mutex->locked = FALSE;
191 SILC_VERIFY(pthread_mutex_unlock(&mutex->mutex) == 0);
193 #endif /* SILC_THREADS */
196 void silc_mutex_assert_locked(SilcMutex mutex)
200 SILC_VERIFY(mutex->locked);
201 #endif /* SILC_THREADS */
204 /***************************** SILC Rwlock API ******************************/
206 /* SILC read/write lock structure */
207 struct SilcRwLockStruct {
209 pthread_rwlock_t rwlock;
212 #endif /* SILC_THREADS */
215 SilcBool silc_rwlock_alloc(SilcRwLock *rwlock)
218 *rwlock = silc_calloc(1, sizeof(**rwlock));
221 if (pthread_rwlock_init(&(*rwlock)->rwlock, NULL)) {
222 silc_set_errno_posix(errno);
229 #endif /* SILC_THREADS */
232 void silc_rwlock_free(SilcRwLock rwlock)
236 pthread_rwlock_destroy(&rwlock->rwlock);
239 #endif /* SILC_THREADS */
242 void silc_rwlock_rdlock(SilcRwLock rwlock)
246 pthread_rwlock_rdlock(&rwlock->rwlock);
247 #endif /* SILC_THREADS */
250 void silc_rwlock_wrlock(SilcRwLock rwlock)
254 SILC_VERIFY(pthread_rwlock_wrlock(&rwlock->rwlock) == 0);
255 #endif /* SILC_THREADS */
258 void silc_rwlock_unlock(SilcRwLock rwlock)
262 SILC_VERIFY(pthread_rwlock_unlock(&rwlock->rwlock) == 0);
263 #endif /* SILC_THREADS */
266 /****************************** SILC Cond API *******************************/
268 /* SILC Conditional Variable context */
269 struct SilcCondStruct {
274 #endif /* SILC_THREADS*/
277 SilcBool silc_cond_alloc(SilcCond *cond)
280 *cond = silc_calloc(1, sizeof(**cond));
283 if (pthread_cond_init(&(*cond)->cond, NULL)) {
284 silc_set_errno_posix(errno);
291 #endif /* SILC_THREADS*/
294 void silc_cond_free(SilcCond cond)
297 pthread_cond_destroy(&cond->cond);
299 #endif /* SILC_THREADS*/
302 void silc_cond_signal(SilcCond cond)
305 pthread_cond_signal(&cond->cond);
306 #endif /* SILC_THREADS*/
309 void silc_cond_broadcast(SilcCond cond)
312 pthread_cond_broadcast(&cond->cond);
313 #endif /* SILC_THREADS*/
316 void silc_cond_wait(SilcCond cond, SilcMutex mutex)
319 pthread_cond_wait(&cond->cond, &mutex->mutex);
320 #endif /* SILC_THREADS*/
323 SilcBool silc_cond_timedwait(SilcCond cond, SilcMutex mutex,
329 t.tv_sec = timeout / 1000;
330 t.tv_nsec = (timeout % 1000) * 1000;
331 return pthread_cond_timedwait(&cond->cond, &mutex->mutex, &t) == 0;
334 return pthread_cond_wait(&cond->cond, &mutex->mutex) == 0;
337 #endif /* SILC_THREADS*/
340 /************************** Thread-local Storage ****************************/
342 #if (defined(SILC_THREADS) && defined(HAVE_PTHREAD_KEY_CREATE) && \
343 defined(HAVE_PTHREAD_ONCE))
345 static SilcBool key_set = FALSE;
346 static pthread_key_t key;
347 static pthread_once_t key_once = PTHREAD_ONCE_INIT;
349 static void silc_thread_tls_destructor(void *context)
354 static void silc_thread_tls_alloc(void)
356 if (pthread_key_create(&key, silc_thread_tls_destructor))
357 SILC_LOG_ERROR(("Error creating Thread-local storage"));
361 SilcTls silc_thread_tls_init(void)
365 pthread_once(&key_once, silc_thread_tls_alloc);
367 if (silc_thread_get_tls())
368 return silc_thread_get_tls();
370 /* Allocate Tls for the thread */
371 tls = silc_calloc(1, sizeof(*tls));
373 SILC_LOG_ERROR(("Error allocating Thread-local storage"));
377 pthread_setspecific(key, tls);
381 SilcTls silc_thread_get_tls(void)
385 return pthread_getspecific(key);
391 SilcTls tls_ptr = NULL;
393 SilcTls silc_thread_tls_init(void)
395 if (silc_thread_get_tls())
396 return silc_thread_get_tls();
399 memset(tls_ptr, 0, sizeof(*tls_ptr));
403 SilcTls silc_thread_get_tls(void)