Added preliminary Symbian support.
[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 #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
166 /****************************** SILC Cond API *******************************/
167
168 /* SILC Conditional Variable context */
169 struct SilcCondStruct {
170 #ifdef SILC_THREADS
171   pthread_cond_t cond;
172 #else
173   void *tmp;
174 #endif /* SILC_THREADS*/
175 };
176
177 SilcBool silc_cond_alloc(SilcCond *cond)
178 {
179 #ifdef SILC_THREADS
180   *cond = silc_calloc(1, sizeof(**cond));
181   if (*cond == NULL)
182     return FALSE;
183   pthread_cond_init(&(*cond)->cond, NULL);
184   return TRUE;
185 #else
186   return FALSE;
187 #endif /* SILC_THREADS*/
188 }
189
190 void silc_cond_free(SilcCond cond)
191 {
192 #ifdef SILC_THREADS
193   pthread_cond_destroy(&cond->cond);
194   silc_free(cond);
195 #endif /* SILC_THREADS*/
196 }
197
198 void silc_cond_signal(SilcCond cond)
199 {
200 #ifdef SILC_THREADS
201   pthread_cond_signal(&cond->cond);
202 #endif /* SILC_THREADS*/
203 }
204
205 void silc_cond_broadcast(SilcCond cond)
206 {
207 #ifdef SILC_THREADS
208   pthread_cond_broadcast(&cond->cond);
209 #endif /* SILC_THREADS*/
210 }
211
212 void silc_cond_wait(SilcCond cond, SilcMutex mutex)
213 {
214 #ifdef SILC_THREADS
215   pthread_cond_wait(&cond->cond, &mutex->mutex);
216 #endif /* SILC_THREADS*/
217 }
218
219 SilcBool silc_cond_timedwait(SilcCond cond, SilcMutex mutex,
220                              int timeout)
221 {
222 #ifdef SILC_THREADS
223   struct timespec t;
224   if (timeout) {
225     t.tv_sec = timeout / 1000;
226     t.tv_nsec = (timeout % 1000) * 1000;
227     return pthread_cond_timedwait(&cond->cond, &mutex->mutex, &t) == 0;
228   }
229
230   return pthread_cond_wait(&cond->cond, &mutex->mutex) == 0;
231 #else
232   return FALSE;
233 #endif /* SILC_THREADS*/
234 }