fbfb0da254c697596f9f2c4ee75fbbaf52c47bac
[crypto.git] / lib / silcutil / unix / silcunixthread.c
1 /*
2
3   silcunixthread.c
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2001 - 2007 Pekka Riikonen
8
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.
12
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.
17
18 */
19 /* $Id$ */
20
21 #include "silc.h"
22
23 /**************************** SILC Thread API *******************************/
24
25 SilcThread silc_thread_create(SilcThreadStart start_func, void *context,
26                               SilcBool waitable)
27 {
28 #ifdef SILC_THREADS
29   pthread_attr_t attr;
30   pthread_t thread;
31   int ret;
32
33   SILC_LOG_DEBUG(("Creating new thread"));
34
35   if (!start_func)
36     return NULL;
37
38   if (pthread_attr_init(&attr)) {
39     SILC_LOG_ERROR(("Thread error: %s", strerror(errno)));
40     return NULL;
41   }
42
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);
48     return NULL;
49   }
50
51   ret = pthread_create(&thread, &attr, (void * (*)(void *))start_func,
52                        context);
53   if (ret) {
54     SILC_LOG_ERROR(("Thread error: %s", strerror(errno)));
55     pthread_attr_destroy(&attr);
56     return NULL;
57   }
58
59   pthread_attr_destroy(&attr);
60
61   SILC_LOG_DEBUG(("Created thread %p", (SilcThread)thread));
62
63   return (SilcThread)thread;
64 #else
65   /* Call thread callback immediately */
66   (*start_func)(context);
67   return NULL;
68 #endif
69 }
70
71 void silc_thread_exit(void *exit_value)
72 {
73 #ifdef SILC_THREADS
74   pthread_exit(exit_value);
75 #endif
76 }
77
78 SilcThread silc_thread_self(void)
79 {
80 #ifdef SILC_THREADS
81   pthread_t self = pthread_self();
82   return (SilcThread)self;
83 #else
84   return NULL;
85 #endif
86 }
87
88 SilcBool silc_thread_wait(SilcThread thread, void **exit_value)
89 {
90 #ifdef SILC_THREADS
91   SILC_LOG_DEBUG(("Waiting for thread %p", thread));
92   if (!pthread_join(*(pthread_t *)thread, exit_value))
93     return TRUE;
94   return FALSE;
95 #else
96   return FALSE;
97 #endif
98 }
99
100 void silc_thread_yield(void)
101 {
102 #ifdef SILC_THREADS
103 #ifdef HAVE_SCHED_YIELD
104   sched_yield();
105 #endif /* HAVE_SCHED_YIELD */
106 #endif /* SILC_THREADS */
107 }
108
109 /***************************** SILC Mutex API *******************************/
110
111 /* SILC Mutex structure */
112 struct SilcMutexStruct {
113 #ifdef SILC_THREADS
114   pthread_mutex_t mutex;
115 #endif /* SILC_THREADS */
116   unsigned int locked : 1;
117 };
118
119 SilcBool silc_mutex_alloc(SilcMutex *mutex)
120 {
121 #ifdef SILC_THREADS
122   *mutex = silc_calloc(1, sizeof(**mutex));
123   if (*mutex == NULL)
124     return FALSE;
125   pthread_mutex_init(&(*mutex)->mutex, NULL);
126   (*mutex)->locked = FALSE;
127   return TRUE;
128 #else
129   return FALSE;
130 #endif /* SILC_THREADS */
131 }
132
133 void silc_mutex_free(SilcMutex mutex)
134 {
135 #ifdef SILC_THREADS
136   if (mutex) {
137     pthread_mutex_destroy(&mutex->mutex);
138     silc_free(mutex);
139   }
140 #endif /* SILC_THREADS */
141 }
142
143 void silc_mutex_lock(SilcMutex mutex)
144 {
145 #ifdef SILC_THREADS
146   if (mutex) {
147     if (pthread_mutex_lock(&mutex->mutex))
148       SILC_ASSERT(FALSE);
149     mutex->locked = TRUE;
150   }
151 #endif /* SILC_THREADS */
152 }
153
154 void silc_mutex_unlock(SilcMutex mutex)
155 {
156 #ifdef SILC_THREADS
157   if (mutex) {
158     if (pthread_mutex_unlock(&mutex->mutex))
159       SILC_ASSERT(FALSE);
160     mutex->locked = FALSE;
161   }
162 #endif /* SILC_THREADS */
163 }
164
165 void silc_mutex_assert_locked(SilcMutex mutex)
166 {
167 #ifdef SILC_THREADS
168   if (mutex)
169     SILC_ASSERT(mutex->locked);
170 #endif /* SILC_THREADS */
171 }
172
173 /***************************** SILC Rwlock API ******************************/
174
175 /* SILC read/write lock structure */
176 struct SilcRwLockStruct {
177 #ifdef SILC_THREADS
178   pthread_rwlock_t rwlock;
179 #else
180   void *tmp;
181 #endif /* SILC_THREADS */
182 };
183
184 SilcBool silc_rwlock_alloc(SilcRwLock *rwlock)
185 {
186 #ifdef SILC_THREADS
187   *rwlock = silc_calloc(1, sizeof(**rwlock));
188   if (*rwlock == NULL)
189     return FALSE;
190   pthread_rwlock_init(&(*rwlock)->rwlock, NULL);
191   return TRUE;
192 #else
193   return FALSE;
194 #endif /* SILC_THREADS */
195 }
196
197 void silc_rwlock_free(SilcRwLock rwlock)
198 {
199 #ifdef SILC_THREADS
200   if (rwlock) {
201     pthread_rwlock_destroy(&rwlock->rwlock);
202     silc_free(rwlock);
203   }
204 #endif /* SILC_THREADS */
205 }
206
207 void silc_rwlock_rdlock(SilcRwLock rwlock)
208 {
209 #ifdef SILC_THREADS
210   if (rwlock)
211     pthread_rwlock_rdlock(&rwlock->rwlock);
212 #endif /* SILC_THREADS */
213 }
214
215 void silc_rwlock_wrlock(SilcRwLock rwlock)
216 {
217 #ifdef SILC_THREADS
218   if (rwlock)
219     pthread_rwlock_wrlock(&rwlock->rwlock);
220 #endif /* SILC_THREADS */
221 }
222
223 void silc_rwlock_unlock(SilcRwLock rwlock)
224 {
225 #ifdef SILC_THREADS
226   if (rwlock)
227     pthread_rwlock_unlock(&rwlock->rwlock);
228 #endif /* SILC_THREADS */
229 }
230
231 /****************************** SILC Cond API *******************************/
232
233 /* SILC Conditional Variable context */
234 struct SilcCondStruct {
235 #ifdef SILC_THREADS
236   pthread_cond_t cond;
237 #else
238   void *tmp;
239 #endif /* SILC_THREADS*/
240 };
241
242 SilcBool silc_cond_alloc(SilcCond *cond)
243 {
244 #ifdef SILC_THREADS
245   *cond = silc_calloc(1, sizeof(**cond));
246   if (*cond == NULL)
247     return FALSE;
248   pthread_cond_init(&(*cond)->cond, NULL);
249   return TRUE;
250 #else
251   return FALSE;
252 #endif /* SILC_THREADS*/
253 }
254
255 void silc_cond_free(SilcCond cond)
256 {
257 #ifdef SILC_THREADS
258   pthread_cond_destroy(&cond->cond);
259   silc_free(cond);
260 #endif /* SILC_THREADS*/
261 }
262
263 void silc_cond_signal(SilcCond cond)
264 {
265 #ifdef SILC_THREADS
266   pthread_cond_signal(&cond->cond);
267 #endif /* SILC_THREADS*/
268 }
269
270 void silc_cond_broadcast(SilcCond cond)
271 {
272 #ifdef SILC_THREADS
273   pthread_cond_broadcast(&cond->cond);
274 #endif /* SILC_THREADS*/
275 }
276
277 void silc_cond_wait(SilcCond cond, SilcMutex mutex)
278 {
279 #ifdef SILC_THREADS
280   pthread_cond_wait(&cond->cond, &mutex->mutex);
281 #endif /* SILC_THREADS*/
282 }
283
284 SilcBool silc_cond_timedwait(SilcCond cond, SilcMutex mutex,
285                              int timeout)
286 {
287 #ifdef SILC_THREADS
288   struct timespec t;
289   if (timeout) {
290     t.tv_sec = timeout / 1000;
291     t.tv_nsec = (timeout % 1000) * 1000;
292     return pthread_cond_timedwait(&cond->cond, &mutex->mutex, &t) == 0;
293   }
294
295   return pthread_cond_wait(&cond->cond, &mutex->mutex) == 0;
296 #else
297   return FALSE;
298 #endif /* SILC_THREADS*/
299 }