Added SilcRwLock, a read/write lock API.
[silc.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
101 /***************************** SILC Mutex API *******************************/
102
103 /* SILC Mutex structure */
104 struct SilcMutexStruct {
105 #ifdef SILC_THREADS
106   pthread_mutex_t mutex;
107 #endif /* SILC_THREADS */
108   unsigned int locked : 1;
109 };
110
111 SilcBool silc_mutex_alloc(SilcMutex *mutex)
112 {
113 #ifdef SILC_THREADS
114   *mutex = silc_calloc(1, sizeof(**mutex));
115   if (*mutex == NULL)
116     return FALSE;
117   pthread_mutex_init(&(*mutex)->mutex, NULL);
118   (*mutex)->locked = FALSE;
119   return TRUE;
120 #else
121   return FALSE;
122 #endif /* SILC_THREADS */
123 }
124
125 void silc_mutex_free(SilcMutex mutex)
126 {
127 #ifdef SILC_THREADS
128   if (mutex) {
129     pthread_mutex_destroy(&mutex->mutex);
130     silc_free(mutex);
131   }
132 #endif /* SILC_THREADS */
133 }
134
135 void silc_mutex_lock(SilcMutex mutex)
136 {
137 #ifdef SILC_THREADS
138   if (mutex) {
139     if (pthread_mutex_lock(&mutex->mutex))
140       SILC_ASSERT(FALSE);
141     mutex->locked = TRUE;
142   }
143 #endif /* SILC_THREADS */
144 }
145
146 void silc_mutex_unlock(SilcMutex mutex)
147 {
148 #ifdef SILC_THREADS
149   if (mutex) {
150     if (pthread_mutex_unlock(&mutex->mutex))
151       SILC_ASSERT(FALSE);
152     mutex->locked = FALSE;
153   }
154 #endif /* SILC_THREADS */
155 }
156
157 void silc_mutex_assert_locked(SilcMutex mutex)
158 {
159 #ifdef SILC_THREADS
160   if (mutex)
161     SILC_ASSERT(mutex->locked);
162 #endif /* SILC_THREADS */
163 }
164
165 /***************************** SILC Rwlock API ******************************/
166
167 /* SILC read/write lock structure */
168 struct SilcRwLockStruct {
169 #ifdef SILC_THREADS
170   pthread_rwlock_t rwlock;
171 #else
172   void *tmp;
173 #endif /* SILC_THREADS */
174 };
175
176 SilcBool silc_rwlock_alloc(SilcRwLock *rwlock)
177 {
178 #ifdef SILC_THREADS
179   *rwlock = silc_calloc(1, sizeof(**rwlock));
180   if (*rwlock == NULL)
181     return FALSE;
182   pthread_rwlock_init(&(*rwlock)->rwlock, NULL);
183   return TRUE;
184 #else
185   return FALSE;
186 #endif /* SILC_THREADS */
187 }
188
189 void silc_rwlock_free(SilcRwLock rwlock)
190 {
191 #ifdef SILC_THREADS
192   if (rwlock) {
193     pthread_rwlock_destroy(&rwlock->rwlock);
194     silc_free(rwlock);
195   }
196 #endif /* SILC_THREADS */
197 }
198
199 void silc_rwlock_rdlock(SilcRwLock rwlock)
200 {
201 #ifdef SILC_THREADS
202   if (rwlock)
203     pthread_rwlock_rdlock(&rwlock->rwlock);
204 #endif /* SILC_THREADS */
205 }
206
207 void silc_rwlock_wrlock(SilcRwLock rwlock)
208 {
209 #ifdef SILC_THREADS
210   if (rwlock)
211     pthread_rwlock_wrlock(&rwlock->rwlock);
212 #endif /* SILC_THREADS */
213 }
214
215 void silc_rwlock_unlock(SilcRwLock rwlock)
216 {
217 #ifdef SILC_THREADS
218   if (rwlock)
219     pthread_rwlock_unlock(&rwlock->rwlock);
220 #endif /* SILC_THREADS */
221 }
222
223 /****************************** SILC Cond API *******************************/
224
225 /* SILC Conditional Variable context */
226 struct SilcCondStruct {
227 #ifdef SILC_THREADS
228   pthread_cond_t cond;
229 #else
230   void *tmp;
231 #endif /* SILC_THREADS*/
232 };
233
234 SilcBool silc_cond_alloc(SilcCond *cond)
235 {
236 #ifdef SILC_THREADS
237   *cond = silc_calloc(1, sizeof(**cond));
238   if (*cond == NULL)
239     return FALSE;
240   pthread_cond_init(&(*cond)->cond, NULL);
241   return TRUE;
242 #else
243   return FALSE;
244 #endif /* SILC_THREADS*/
245 }
246
247 void silc_cond_free(SilcCond cond)
248 {
249 #ifdef SILC_THREADS
250   pthread_cond_destroy(&cond->cond);
251   silc_free(cond);
252 #endif /* SILC_THREADS*/
253 }
254
255 void silc_cond_signal(SilcCond cond)
256 {
257 #ifdef SILC_THREADS
258   pthread_cond_signal(&cond->cond);
259 #endif /* SILC_THREADS*/
260 }
261
262 void silc_cond_broadcast(SilcCond cond)
263 {
264 #ifdef SILC_THREADS
265   pthread_cond_broadcast(&cond->cond);
266 #endif /* SILC_THREADS*/
267 }
268
269 void silc_cond_wait(SilcCond cond, SilcMutex mutex)
270 {
271 #ifdef SILC_THREADS
272   pthread_cond_wait(&cond->cond, &mutex->mutex);
273 #endif /* SILC_THREADS*/
274 }
275
276 SilcBool silc_cond_timedwait(SilcCond cond, SilcMutex mutex,
277                              int timeout)
278 {
279 #ifdef SILC_THREADS
280   struct timespec t;
281   if (timeout) {
282     t.tv_sec = timeout / 1000;
283     t.tv_nsec = (timeout % 1000) * 1000;
284     return pthread_cond_timedwait(&cond->cond, &mutex->mutex, &t) == 0;
285   }
286
287   return pthread_cond_wait(&cond->cond, &mutex->mutex) == 0;
288 #else
289   return FALSE;
290 #endif /* SILC_THREADS*/
291 }