5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2001 - 2007 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.
23 /**************************** SILC Thread API *******************************/
26 SilcThreadStart start_func;
28 } *SilcThreadStartContext;
30 static void *silc_thread_start(void *context)
32 SilcThreadStartContext c = context;
33 SilcThreadStart start_func = c->start_func;
34 void *start_context = c->context;
38 silc_thread_tls_init();
40 return start_func(start_context);
43 SilcThread silc_thread_create(SilcThreadStart start_func, void *context,
47 SilcThreadStartContext c;
52 SILC_LOG_DEBUG(("Creating new thread"));
57 c = silc_calloc(1, sizeof(*c));
60 c->start_func = start_func;
63 if (pthread_attr_init(&attr)) {
64 SILC_LOG_ERROR(("Thread error: %s", strerror(errno)));
69 if (pthread_attr_setdetachstate(&attr,
70 waitable ? PTHREAD_CREATE_JOINABLE :
71 PTHREAD_CREATE_DETACHED)) {
72 SILC_LOG_ERROR(("Thread error: %s", strerror(errno)));
73 pthread_attr_destroy(&attr);
78 ret = pthread_create(&thread, &attr, silc_thread_start, c);
80 SILC_LOG_ERROR(("Thread error: %s", strerror(errno)));
81 pthread_attr_destroy(&attr);
86 pthread_attr_destroy(&attr);
88 SILC_LOG_DEBUG(("Created thread %p", (SilcThread)thread));
90 return (SilcThread)thread;
92 /* Call thread callback immediately */
93 (*start_func)(context);
98 void silc_thread_exit(void *exit_value)
101 pthread_exit(exit_value);
105 SilcThread silc_thread_self(void)
108 pthread_t self = pthread_self();
109 return (SilcThread)self;
115 SilcBool silc_thread_wait(SilcThread thread, void **exit_value)
118 SILC_LOG_DEBUG(("Waiting for thread %p", thread));
119 if (!pthread_join(*(pthread_t *)thread, exit_value))
127 void silc_thread_yield(void)
130 #ifdef HAVE_SCHED_YIELD
132 #endif /* HAVE_SCHED_YIELD */
133 #endif /* SILC_THREADS */
136 /***************************** SILC Mutex API *******************************/
138 /* SILC Mutex structure */
139 struct SilcMutexStruct {
141 pthread_mutex_t mutex;
142 #endif /* SILC_THREADS */
143 unsigned int locked : 1;
146 SilcBool silc_mutex_alloc(SilcMutex *mutex)
149 *mutex = silc_calloc(1, sizeof(**mutex));
152 pthread_mutex_init(&(*mutex)->mutex, NULL);
153 (*mutex)->locked = FALSE;
157 #endif /* SILC_THREADS */
160 void silc_mutex_free(SilcMutex mutex)
164 pthread_mutex_destroy(&mutex->mutex);
167 #endif /* SILC_THREADS */
170 void silc_mutex_lock(SilcMutex mutex)
174 SILC_VERIFY(pthread_mutex_lock(&mutex->mutex) == 0);
175 mutex->locked = TRUE;
177 #endif /* SILC_THREADS */
180 void silc_mutex_unlock(SilcMutex mutex)
184 mutex->locked = FALSE;
185 SILC_VERIFY(pthread_mutex_unlock(&mutex->mutex) == 0);
187 #endif /* SILC_THREADS */
190 void silc_mutex_assert_locked(SilcMutex mutex)
194 SILC_VERIFY(mutex->locked);
195 #endif /* SILC_THREADS */
198 /***************************** SILC Rwlock API ******************************/
200 /* SILC read/write lock structure */
201 struct SilcRwLockStruct {
203 pthread_rwlock_t rwlock;
206 #endif /* SILC_THREADS */
209 SilcBool silc_rwlock_alloc(SilcRwLock *rwlock)
212 *rwlock = silc_calloc(1, sizeof(**rwlock));
215 pthread_rwlock_init(&(*rwlock)->rwlock, NULL);
219 #endif /* SILC_THREADS */
222 void silc_rwlock_free(SilcRwLock rwlock)
226 pthread_rwlock_destroy(&rwlock->rwlock);
229 #endif /* SILC_THREADS */
232 void silc_rwlock_rdlock(SilcRwLock rwlock)
236 pthread_rwlock_rdlock(&rwlock->rwlock);
237 #endif /* SILC_THREADS */
240 void silc_rwlock_wrlock(SilcRwLock rwlock)
244 SILC_VERIFY(pthread_rwlock_wrlock(&rwlock->rwlock) == 0);
245 #endif /* SILC_THREADS */
248 void silc_rwlock_unlock(SilcRwLock rwlock)
252 SILC_VERIFY(pthread_rwlock_unlock(&rwlock->rwlock) == 0);
253 #endif /* SILC_THREADS */
256 /****************************** SILC Cond API *******************************/
258 /* SILC Conditional Variable context */
259 struct SilcCondStruct {
264 #endif /* SILC_THREADS*/
267 SilcBool silc_cond_alloc(SilcCond *cond)
270 *cond = silc_calloc(1, sizeof(**cond));
273 pthread_cond_init(&(*cond)->cond, NULL);
277 #endif /* SILC_THREADS*/
280 void silc_cond_free(SilcCond cond)
283 pthread_cond_destroy(&cond->cond);
285 #endif /* SILC_THREADS*/
288 void silc_cond_signal(SilcCond cond)
291 pthread_cond_signal(&cond->cond);
292 #endif /* SILC_THREADS*/
295 void silc_cond_broadcast(SilcCond cond)
298 pthread_cond_broadcast(&cond->cond);
299 #endif /* SILC_THREADS*/
302 void silc_cond_wait(SilcCond cond, SilcMutex mutex)
305 pthread_cond_wait(&cond->cond, &mutex->mutex);
306 #endif /* SILC_THREADS*/
309 SilcBool silc_cond_timedwait(SilcCond cond, SilcMutex mutex,
315 t.tv_sec = timeout / 1000;
316 t.tv_nsec = (timeout % 1000) * 1000;
317 return pthread_cond_timedwait(&cond->cond, &mutex->mutex, &t) == 0;
320 return pthread_cond_wait(&cond->cond, &mutex->mutex) == 0;
323 #endif /* SILC_THREADS*/
326 /************************** Thread-local Storage ****************************/
328 #if (defined(SILC_THREADS) && defined(HAVE_PTHREAD_KEY_CREATE) && \
329 defined(HAVE_PTHREAD_ONCE))
331 static pthread_key_t key;
332 static pthread_once_t key_once;
334 static void silc_thread_tls_destructor(void *context)
339 static void silc_thread_tls_alloc(void)
341 if (pthread_key_create(&key, silc_thread_tls_destructor))
342 SILC_LOG_ERROR(("Error creating Thread-local storage"));
345 SilcTls silc_thread_tls_init(void)
349 pthread_once(&key_once, silc_thread_tls_alloc);
351 if (silc_thread_get_tls())
352 return silc_thread_get_tls();
354 /* Allocate Tls for the thread */
355 tls = silc_calloc(1, sizeof(*tls));
357 SILC_LOG_ERROR(("Error allocating Thread-local storage"));
361 pthread_setspecific(key, tls);
365 SilcTls silc_thread_get_tls(void)
367 return pthread_getspecific(key);
373 SilcTls tls_ptr = NULL;
375 SilcTls silc_thread_tls_init(void)
377 if (silc_thread_get_tls())
378 return silc_thread_get_tls();
381 memset(tls_ptr, 0, sizeof(*tls_ptr));
385 SilcTls silc_thread_get_tls(void)