5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2001 - 2007 Pekka Riikonen
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.
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.
23 /**************************** SILC Thread API *******************************/
27 /* Thread structure for WIN32 */
30 SilcThreadStart start_func;
35 static DWORD silc_thread_tls;
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
41 unsigned __stdcall silc_thread_win32_start(void *context)
43 SilcWin32Thread thread = (SilcWin32Thread)context;
45 TlsSetValue(silc_thread_tls, context);
46 silc_thread_exit(thread->start_func(thread->context));
52 SilcThread silc_thread_create(SilcThreadStart start_func, void *context,
56 SilcWin32Thread thread;
59 SILC_LOG_DEBUG(("Creating new thread"));
61 thread = silc_calloc(1, sizeof(*thread));
62 thread->start_func = start_func;
63 thread->context = context;
64 thread->waitable = waitable;
66 CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)silc_thread_win32_start,
67 (void *)thread, 0, &id);
69 if (!thread->thread) {
70 SILC_LOG_ERROR(("Could not create new thread"));
75 return (SilcThread)thread;
77 /* Call thread callback immediately */
78 (*start_func)(context);
83 void silc_thread_exit(void *exit_value)
86 SilcWin32Thread thread = TlsGetValue(silc_thread_tls);
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);
96 TlsSetValue(silc_thread_tls, NULL);
102 SilcThread silc_thread_self(void)
105 SilcWin32Thread self = TlsGetValue(silc_thread_tls);
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);
118 return (SilcThread)self;
124 SilcBool silc_thread_wait(SilcThread thread, void **exit_value)
127 SilcWin32Thread self = (SilcWin32Thread)thread;
129 SILC_LOG_DEBUG(("Waiting for thread %p", self));
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);
149 void silc_thread_yield(void)
153 #endif /* SILC_THREADS */
157 /***************************** SILC Mutex API *******************************/
159 /* SILC Mutex structure */
160 struct SilcMutexStruct {
162 CRITICAL_SECTION mutex;
163 #endif /* SILC_THREADS */
164 unsigned int locked : 1;
167 SilcBool silc_mutex_alloc(SilcMutex *mutex)
170 *mutex = silc_calloc(1, sizeof(**mutex));
173 InitializeCriticalSection(&((*mutex)->mutex));
177 #endif /* SILC_THREADS */
180 void silc_mutex_free(SilcMutex mutex)
184 DeleteCriticalSection(&mutex->mutex);
187 #endif /* SILC_THREADS */
190 void silc_mutex_lock(SilcMutex mutex)
194 EnterCriticalSection(&mutex->mutex);
195 SILC_ASSERT(mutex->locked == FALSE);
196 mutex->locked = TRUE;
198 #endif /* SILC_THREADS */
201 void silc_mutex_unlock(SilcMutex mutex)
205 SILC_ASSERT(mutex->locked == TRUE);
206 mutex->locked = FALSE;
207 LeaveCriticalSection(&mutex->mutex);
209 #endif /* SILC_THREADS */
212 void silc_mutex_assert_locked(SilcMutex mutex)
216 SILC_ASSERT(mutex->locked);
217 #endif /* SILC_THREADS */
221 /***************************** SILC Rwlock API ******************************/
223 /* SILC read/write lock structure */
224 struct SilcRwLockStruct {
228 #endif /* SILC_THREADS */
229 unsigned int readers : 31;
230 unsigned int locked : 1;
233 SilcBool silc_rwlock_alloc(SilcRwLock *rwlock)
236 *rwlock = silc_calloc(1, sizeof(**rwlock));
239 if (!silc_mutex_alloc(&(*rwlock)->mutex)) {
243 if (!silc_cond_alloc(&(*rwlock)->cond)) {
244 silc_mutex_free((*rwlock)->mutex);
251 #endif /* SILC_THREADS */
254 void silc_rwlock_free(SilcRwLock rwlock)
258 silc_mutex_free(rwlock->mutex);
259 silc_cond_free(rwlock->cond);
262 #endif /* SILC_THREADS */
265 void silc_rwlock_rdlock(SilcRwLock rwlock)
269 silc_mutex_lock(rwlock->mutex);
271 silc_mutex_unlock(rwlock->mutex);
273 #endif /* SILC_THREADS */
276 void silc_rwlock_wrlock(SilcRwLock rwlock)
280 silc_mutex_lock(rwlock->mutex);
281 while (rwlock->readers > 0)
282 silc_cond_wait(rwlock->cond, rwlock->mutex);
283 rwlock->locked = TRUE;
285 #endif /* SILC_THREADS */
288 void silc_rwlock_unlock(SilcRwLock rwlock)
292 if (rwlock->locked) {
294 rwlock->locked = FALSE;
295 silc_mutex_unlock(rwlock->mutex);
300 silc_mutex_lock(rwlock->mutex);
302 silc_cond_broadcast(rwlock->cond);
303 silc_mutex_unlock(rwlock->mutex);
305 #endif /* SILC_THREADS */
309 /**************************** SILC Cond API ******************************/
311 /* SILC Conditional Variable context */
312 struct SilcCondStruct {
315 #endif /* SILC_THREADS*/
316 unsigned int waiters : 23;
317 unsigned int signal : 1;
320 SilcBool silc_cond_alloc(SilcCond *cond)
323 *cond = silc_calloc(1, sizeof(**cond));
326 (*cond)->event = CreateEvent(NULL, TRUE, FALSE, NULL);
330 #endif /* SILC_THREADS*/
333 void silc_cond_free(SilcCond cond)
336 CloseHandle(cond->event);
338 #endif /* SILC_THREADS*/
341 void silc_cond_signal(SilcCond cond)
345 SetEvent(cond->event);
346 #endif /* SILC_THREADS*/
349 void silc_cond_broadcast(SilcCond cond)
353 SetEvent(cond->event);
354 #endif /* SILC_THREADS*/
357 void silc_cond_wait(SilcCond cond, SilcMutex mutex)
360 silc_cond_timedwait(cond, mutex, NULL);
361 #endif /* SILC_THREADS*/
364 SilcBool silc_cond_timedwait(SilcCond cond, SilcMutex mutex,
368 DWORD ret, t = INFINITE;
375 silc_mutex_unlock(mutex);
377 ret = WaitForSingleObject(cond->event, t);
379 silc_mutex_lock(mutex);
382 if (ret != WAIT_OBJECT_0)
386 cond->signal = FALSE;
387 ResetEvent(cond->event);
391 #endif /* SILC_THREADS*/