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 *******************************/
25 SilcThread silc_thread_create(SilcThreadStart start_func, void *context,
33 SILC_LOG_DEBUG(("Creating new thread"));
38 if (pthread_attr_init(&attr)) {
39 SILC_LOG_ERROR(("Thread error: %s", strerror(errno)));
43 if (pthread_attr_setdetachstate(&attr,
44 waitable ? PTHREAD_CREATE_JOINABLE :
45 PTHREAD_CREATE_DETACHED)) {
46 SILC_LOG_ERROR(("Thread error: %s", strerror(errno)));
47 pthread_attr_destroy(&attr);
51 ret = pthread_create(&thread, &attr, (void * (*)(void *))start_func,
54 SILC_LOG_ERROR(("Thread error: %s", strerror(errno)));
55 pthread_attr_destroy(&attr);
59 pthread_attr_destroy(&attr);
61 SILC_LOG_DEBUG(("Created thread %p", (SilcThread)thread));
63 return (SilcThread)thread;
65 /* Call thread callback immediately */
66 (*start_func)(context);
71 void silc_thread_exit(void *exit_value)
74 pthread_exit(exit_value);
78 SilcThread silc_thread_self(void)
81 pthread_t self = pthread_self();
82 return (SilcThread)self;
88 SilcBool silc_thread_wait(SilcThread thread, void **exit_value)
91 SILC_LOG_DEBUG(("Waiting for thread %p", thread));
92 if (!pthread_join(*(pthread_t *)thread, exit_value))
101 /***************************** SILC Mutex API *******************************/
103 /* SILC Mutex structure */
104 struct SilcMutexStruct {
106 pthread_mutex_t mutex;
107 #endif /* SILC_THREADS */
108 unsigned int locked : 1;
111 SilcBool silc_mutex_alloc(SilcMutex *mutex)
114 *mutex = silc_calloc(1, sizeof(**mutex));
117 pthread_mutex_init(&(*mutex)->mutex, NULL);
118 (*mutex)->locked = FALSE;
122 #endif /* SILC_THREADS */
125 void silc_mutex_free(SilcMutex mutex)
129 pthread_mutex_destroy(&mutex->mutex);
132 #endif /* SILC_THREADS */
135 void silc_mutex_lock(SilcMutex mutex)
139 if (pthread_mutex_lock(&mutex->mutex))
141 mutex->locked = TRUE;
143 #endif /* SILC_THREADS */
146 void silc_mutex_unlock(SilcMutex mutex)
150 if (pthread_mutex_unlock(&mutex->mutex))
152 mutex->locked = FALSE;
154 #endif /* SILC_THREADS */
157 void silc_mutex_assert_locked(SilcMutex mutex)
161 SILC_ASSERT(mutex->locked);
162 #endif /* SILC_THREADS */
165 /***************************** SILC Rwlock API ******************************/
167 /* SILC read/write lock structure */
168 struct SilcRwLockStruct {
170 pthread_rwlock_t rwlock;
173 #endif /* SILC_THREADS */
176 SilcBool silc_rwlock_alloc(SilcRwLock *rwlock)
179 *rwlock = silc_calloc(1, sizeof(**rwlock));
182 pthread_rwlock_init(&(*rwlock)->rwlock, NULL);
186 #endif /* SILC_THREADS */
189 void silc_rwlock_free(SilcRwLock rwlock)
193 pthread_rwlock_destroy(&rwlock->rwlock);
196 #endif /* SILC_THREADS */
199 void silc_rwlock_rdlock(SilcRwLock rwlock)
203 pthread_rwlock_rdlock(&rwlock->rwlock);
204 #endif /* SILC_THREADS */
207 void silc_rwlock_wrlock(SilcRwLock rwlock)
211 pthread_rwlock_wrlock(&rwlock->rwlock);
212 #endif /* SILC_THREADS */
215 void silc_rwlock_unlock(SilcRwLock rwlock)
219 pthread_rwlock_unlock(&rwlock->rwlock);
220 #endif /* SILC_THREADS */
223 /****************************** SILC Cond API *******************************/
225 /* SILC Conditional Variable context */
226 struct SilcCondStruct {
231 #endif /* SILC_THREADS*/
234 SilcBool silc_cond_alloc(SilcCond *cond)
237 *cond = silc_calloc(1, sizeof(**cond));
240 pthread_cond_init(&(*cond)->cond, NULL);
244 #endif /* SILC_THREADS*/
247 void silc_cond_free(SilcCond cond)
250 pthread_cond_destroy(&cond->cond);
252 #endif /* SILC_THREADS*/
255 void silc_cond_signal(SilcCond cond)
258 pthread_cond_signal(&cond->cond);
259 #endif /* SILC_THREADS*/
262 void silc_cond_broadcast(SilcCond cond)
265 pthread_cond_broadcast(&cond->cond);
266 #endif /* SILC_THREADS*/
269 void silc_cond_wait(SilcCond cond, SilcMutex mutex)
272 pthread_cond_wait(&cond->cond, &mutex->mutex);
273 #endif /* SILC_THREADS*/
276 SilcBool silc_cond_timedwait(SilcCond cond, SilcMutex mutex,
282 t.tv_sec = timeout / 1000;
283 t.tv_nsec = (timeout % 1000) * 1000;
284 return pthread_cond_timedwait(&cond->cond, &mutex->mutex, &t) == 0;
287 return pthread_cond_wait(&cond->cond, &mutex->mutex) == 0;
290 #endif /* SILC_THREADS*/