Added conditional variables.
[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 - 2006 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 #else
108   void *tmp;
109 #endif /* SILC_THREADS */
110 };
111
112 SilcBool silc_mutex_alloc(SilcMutex *mutex)
113 {
114 #ifdef SILC_THREADS
115   *mutex = silc_calloc(1, sizeof(**mutex));
116   if (*mutex == NULL)
117     return FALSE;
118   pthread_mutex_init(&(*mutex)->mutex, NULL);
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       assert(FALSE);
141   }
142 #endif /* SILC_THREADS */
143 }
144
145 void silc_mutex_unlock(SilcMutex mutex)
146 {
147 #ifdef SILC_THREADS
148   if (mutex) {
149     if (pthread_mutex_unlock(&mutex->mutex))
150       assert(FALSE);
151   }
152 #endif /* SILC_THREADS */
153 }
154
155
156 /**************************** SILC CondVar API ******************************/
157
158 /* SILC Conditional Variable context */
159 struct SilcCondVarStruct {
160 #ifdef SILC_THREADS
161   pthread_cond_t cond;
162 #else
163   void *tmp;
164 #endif /* SILC_THREADS*/
165 };
166
167 SilcBool silc_condvar_alloc(SilcCondVar *cond)
168 {
169 #ifdef SILC_THREADS
170   *cond = silc_calloc(1, sizeof(**cond));
171   if (*cond == NULL)
172     return FALSE;
173   pthread_cond_init(&(*cond)->cond, NULL);
174   return TRUE;
175 #else
176   return FALSE;
177 #endif /* SILC_THREADS*/
178 }
179
180 void silc_condvar_free(SilcCondVar cond)
181 {
182 #ifdef SILC_THREADS
183   pthread_cond_destroy(&cond->cond);
184   silc_free(cond);
185 #endif /* SILC_THREADS*/
186 }
187
188 void silc_condvar_signal(SilcCondVar cond)
189 {
190 #ifdef SILC_THREADS
191   pthread_cond_signal(&cond->cond);
192 #endif /* SILC_THREADS*/
193 }
194
195 void silc_condvar_broadcast(SilcCondVar cond)
196 {
197 #ifdef SILC_THREADS
198   pthread_cond_broadcast(&cond->cond);
199 #endif /* SILC_THREADS*/
200 }
201
202 void silc_condvar_wait(SilcCondVar cond, SilcMutex mutex)
203 {
204 #ifdef SILC_THREADS
205   pthread_cond_wait(&cond->cond, &mutex->mutex);
206 #endif /* SILC_THREADS*/
207 }
208
209 SilcBool silc_condvar_timedwait(SilcCondVar cond, SilcMutex mutex,
210                                 int timeout)
211 {
212 #ifdef SILC_THREADS
213   struct timespec t;
214   if (timeout) {
215     t.tv_sec = timeout / 1000;
216     t.tv_nsec = (timeout % 1000) * 1000;
217     return pthread_cond_timedwait(&cond->cond, &mutex->mutex, &t) == 0;
218   }
219
220   return pthread_cond_wait(&cond->cond, &mutex->mutex) == 0;
221 #endif /* SILC_THREADS*/
222 }