Sun Mar 11 15:22:42 CET 2007 Jochen Eisinger <coffee@silcnet.org>
[silc.git] / lib / silcutil / win32 / silcwin32thread.c
1 /*
2
3   silcwin32thread.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 #ifdef SILC_THREADS
26
27 /* Thread structure for WIN32 */
28 typedef struct {
29   HANDLE thread;
30   SilcThreadStart start_func;
31   void *context;
32   SilcBool waitable;
33 } *SilcWin32Thread;
34
35 static DWORD silc_thread_tls;
36
37 /* Actual routine that is called by WIN32 when the thread is created.
38    We will call the start_func from here. When this returns the thread
39    is destroyed. */
40
41 unsigned __stdcall silc_thread_win32_start(void *context)
42 {
43   SilcWin32Thread thread = (SilcWin32Thread)context;
44
45   TlsSetValue(silc_thread_tls, context);
46   silc_thread_exit(thread->start_func(thread->context));
47
48   return 0;
49 }
50 #endif
51
52 SilcThread silc_thread_create(SilcThreadStart start_func, void *context,
53                               SilcBool waitable)
54 {
55 #ifdef SILC_THREADS
56   SilcWin32Thread thread;
57   unsigned id;
58
59   SILC_LOG_DEBUG(("Creating new thread"));
60
61   thread = silc_calloc(1, sizeof(*thread));
62   thread->start_func = start_func;
63   thread->context = context;
64   thread->waitable = waitable;
65   thread->thread =
66     CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)silc_thread_win32_start,
67                  (void *)thread, 0, &id);
68
69   if (!thread->thread) {
70     SILC_LOG_ERROR(("Could not create new thread"));
71     silc_free(thread);
72     return NULL;
73   }
74
75   return (SilcThread)thread;
76 #else
77   /* Call thread callback immediately */
78   (*start_func)(context);
79   return NULL;
80 #endif
81 }
82
83 void silc_thread_exit(void *exit_value)
84 {
85 #ifdef SILC_THREADS
86   SilcWin32Thread thread = TlsGetValue(silc_thread_tls);
87
88   if (thread) {
89     /* If the thread is waitable the memory is freed only in silc_thread_wait
90        by another thread. If not waitable, free it now. */
91     if (!thread->waitable) {
92       TerminateThread(thread->thread, 0);
93       silc_free(thread);
94     }
95
96     TlsSetValue(silc_thread_tls, NULL);
97   }
98   ExitThread(0);
99 #endif
100 }
101
102 SilcThread silc_thread_self(void)
103 {
104 #ifdef SILC_THREADS
105   SilcWin32Thread self = TlsGetValue(silc_thread_tls);
106
107   if (!self) {
108     /* This should only happen for the main thread! */
109     HANDLE handle = GetCurrentThread ();
110     HANDLE process = GetCurrentProcess ();
111     self = silc_calloc(1, sizeof(*self));
112     DuplicateHandle(process, handle, process,
113                     &self->thread, 0, FALSE,
114                     DUPLICATE_SAME_ACCESS);
115     TlsSetValue(silc_thread_tls, self);
116   }
117
118   return (SilcThread)self;
119         #else
120   return NULL;
121 #endif
122 }
123
124 SilcBool silc_thread_wait(SilcThread thread, void **exit_value)
125 {
126 #ifdef SILC_THREADS
127   SilcWin32Thread self = (SilcWin32Thread)thread;
128
129   SILC_LOG_DEBUG(("Waiting for thread %p", self));
130
131   if (!self->waitable)
132     return FALSE;
133
134   /* The thread is waitable thus we will free all memory after the
135      WaitForSingleObject returns, the thread is destroyed after that. */
136   if (WaitForSingleObject(self->thread, 2500) == WAIT_TIMEOUT)
137     TerminateThread(self->thread, 0);
138
139   silc_free(self);
140   if (exit_value)
141     *exit_value = NULL;
142
143   return TRUE;
144 #else
145   return FALSE;
146 #endif
147 }
148
149
150 /***************************** SILC Mutex API *******************************/
151
152 /* SILC Mutex structure */
153 struct SilcMutexStruct {
154 #ifdef SILC_THREADS
155   CRITICAL_SECTION mutex;
156 #endif /* SILC_THREADS */
157   unsigned int locked : 1;
158 };
159
160 SilcBool silc_mutex_alloc(SilcMutex *mutex)
161 {
162 #ifdef SILC_THREADS
163   *mutex = silc_calloc(1, sizeof(**mutex));
164   if (!(*mutex))
165     return FALSE;
166   InitializeCriticalSection(&((*mutex)->mutex));
167   return TRUE;
168 #else
169   return FALSE;
170 #endif /* SILC_THREADS */
171 }
172
173 void silc_mutex_free(SilcMutex mutex)
174 {
175 #ifdef SILC_THREADS
176   if (mutex) {
177     DeleteCriticalSection(&mutex->mutex);
178     silc_free(mutex);
179   }
180 #endif /* SILC_THREADS */
181 }
182
183 void silc_mutex_lock(SilcMutex mutex)
184 {
185 #ifdef SILC_THREADS
186   if (mutex) {
187     EnterCriticalSection(&mutex->mutex);
188     SILC_ASSERT(mutex->locked == FALSE);
189     mutex->locked = TRUE;
190   }
191 #endif /* SILC_THREADS */
192 }
193
194 void silc_mutex_unlock(SilcMutex mutex)
195 {
196 #ifdef SILC_THREADS
197   if (mutex) {
198     SILC_ASSERT(mutex->locked == TRUE);
199     mutex->locked = FALSE;
200     LeaveCriticalSection(&mutex->mutex);
201   }
202 #endif /* SILC_THREADS */
203 }
204
205 void silc_mutex_assert_locked(SilcMutex mutex)
206 {
207 #ifdef SILC_THREADS
208   if (mutex)
209     SILC_ASSERT(mutex->locked);
210 #endif /* SILC_THREADS */
211 }
212
213
214 /***************************** SILC Rwlock API ******************************/
215
216 /* SILC read/write lock structure */
217 struct SilcRwLockStruct {
218 #ifdef SILC_THREADS
219   SilcMutex mutex;
220   SilcCond cond;
221 #endif /* SILC_THREADS */
222   unsigned int readers : 31;
223   unsigned int locked  : 1;
224 };
225
226 SilcBool silc_rwlock_alloc(SilcRwLock *rwlock)
227 {
228 #ifdef SILC_THREADS
229   *rwlock = silc_calloc(1, sizeof(**rwlock));
230   if (!(*rwlock))
231     return FALSE;
232   if (!silc_mutex_alloc(&(*rwlock)->mutex)) {
233     silc_free(*rwlock);
234     return FALSE;
235   }
236   if (!silc_cond_alloc(&(*rwlock)->cond)) {
237     silc_mutex_free((*rwlock)->mutex);
238     silc_free(*rwlock);
239     return FALSE;
240   }
241   return TRUE;
242 #else
243   return FALSE;
244 #endif /* SILC_THREADS */
245 }
246
247 void silc_rwlock_free(SilcRwLock rwlock)
248 {
249 #ifdef SILC_THREADS
250   if (mutex) {
251     silc_mutex_free(rwlock->mutex);
252     silc_cond_free(rwlock->cond);
253     silc_free(rwlock);
254   }
255 #endif /* SILC_THREADS */
256 }
257
258 void silc_rwlock_rdlock(SilcRwLock rwlock)
259 {
260 #ifdef SILC_THREADS
261   if (rwlock) {
262     silc_mutex_lock(rwlock->mutex);
263     rwlock->readers++;
264     silc_mutex_unlock(rwlock->mutex);
265   }
266 #endif /* SILC_THREADS */
267 }
268
269 void silc_rwlock_wrlock(SilcRwLock rwlock)
270 {
271 #ifdef SILC_THREADS
272   if (rwlock) {
273     silc_mutex_lock(rwlock->mutex);
274     while (rwlock->readers > 0)
275       silc_cond_wait(rwlock->cond, rwlock->mutex);
276     rwlock->locked = TRUE;
277   }
278 #endif /* SILC_THREADS */
279 }
280
281 void silc_rwlock_unlock(SilcRwLock rwlock)
282 {
283 #ifdef SILC_THREADS
284   if (rwlock) {
285     if (rwlock->locked) {
286       /* Unlock writer */
287       rwlock->locked = FALSE;
288       silc_mutex_unlock(rwlock->mutex);
289       return;
290     }
291
292     /* Unlock reader */
293     silc_mutex_lock(rwlock->mutex);
294     rwlock->readers--;
295     silc_cond_broadcast(rwlock->cond);
296     silc_mutex_unlock(rwlock->mutex);
297   }
298 #endif /* SILC_THREADS */
299 }
300
301
302 /**************************** SILC Cond API ******************************/
303
304 /* SILC Conditional Variable context */
305 struct SilcCondStruct {
306 #ifdef SILC_THREADS
307   HANDLE event;
308 #endif /* SILC_THREADS*/
309   unsigned int waiters : 23;
310   unsigned int signal  : 1;
311 };
312
313 SilcBool silc_cond_alloc(SilcCond *cond)
314 {
315 #ifdef SILC_THREADS
316   *cond = silc_calloc(1, sizeof(**cond));
317   if (*cond == NULL)
318     return FALSE;
319   (*cond)->event = CreateEvent(NULL, TRUE, FALSE, NULL);
320   return TRUE;
321 #else
322   return FALSE;
323 #endif /* SILC_THREADS*/
324 }
325
326 void silc_cond_free(SilcCond cond)
327 {
328 #ifdef SILC_THREADS
329   CloseHandle(cond->event);
330   silc_free(cond);
331 #endif /* SILC_THREADS*/
332 }
333
334 void silc_cond_signal(SilcCond cond)
335 {
336 #ifdef SILC_THREADS
337   cond->signal = TRUE;
338   SetEvent(cond->event);
339 #endif /* SILC_THREADS*/
340 }
341
342 void silc_cond_broadcast(SilcCond cond)
343 {
344 #ifdef SILC_THREADS
345   cond->signal = TRUE;
346   SetEvent(cond->event);
347 #endif /* SILC_THREADS*/
348 }
349
350 void silc_cond_wait(SilcCond cond, SilcMutex mutex)
351 {
352 #ifdef SILC_THREADS
353   silc_cond_timedwait(cond, mutex, NULL);
354 #endif /* SILC_THREADS*/
355 }
356
357 SilcBool silc_cond_timedwait(SilcCond cond, SilcMutex mutex,
358                              int timeout)
359 {
360 #ifdef SILC_THREADS
361   DWORD ret, t = INFINITE;
362
363   if (timeout)
364     t = timeout;
365
366   while (TRUE) {
367     cond->waiters++;
368     silc_mutex_unlock(mutex);
369
370     ret = WaitForSingleObject(cond->event, t);
371
372     silc_mutex_lock(mutex);
373     cond->waiters--;
374
375     if (ret != WAIT_OBJECT_0)
376       return FALSE;
377
378     if (cond->signal) {
379       cond->signal = FALSE;
380       ResetEvent(cond->event);
381       break;
382     }
383   }
384 #endif /* SILC_THREADS*/
385   return TRUE;
386 }