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 /* Actual routine that is called by WIN32 when the thread is created.
36 We will call the start_func from here. When this returns the thread
39 unsigned __stdcall silc_thread_win32_start(void *context)
41 SilcWin32Thread thread = (SilcWin32Thread)context;
44 tls = silc_thread_tls_init();
46 tls->platform_context = thread;
48 silc_thread_exit(thread->start_func(thread->context));
56 SilcThread silc_thread_create(SilcThreadStart start_func, void *context,
60 SilcWin32Thread thread;
63 SILC_LOG_DEBUG(("Creating new thread"));
65 thread = silc_calloc(1, sizeof(*thread));
68 thread->start_func = start_func;
69 thread->context = context;
70 thread->waitable = waitable;
72 _beginthreadex(NULL, 0, (LPTHREAD_START_ROUTINE)silc_thread_win32_start,
73 (void *)thread, 0, &id);
75 if (!thread->thread) {
76 SILC_LOG_ERROR(("Could not create new thread"));
77 silc_set_errno_reason(SILC_ERR, "Could not create new thread");
82 return (SilcThread)thread;
84 /* Call thread callback immediately */
85 (*start_func)(context);
90 void silc_thread_exit(void *exit_value)
93 SilcTls tls = silc_thread_get_tls();
94 SilcWin32Thread thread = tls->platform_context;
97 /* If the thread is waitable the memory is freed only in silc_thread_wait
98 by another thread. If not waitable, free it now. */
99 if (!thread->waitable)
107 SilcThread silc_thread_self(void)
110 SilcTls tls = silc_thread_get_tls();
111 SilcWin32Thread self = tls->platform_context;
114 /* This should only happen for the main thread. */
115 HANDLE handle = GetCurrentThread ();
116 HANDLE process = GetCurrentProcess ();
117 self = silc_calloc(1, sizeof(*self));
119 DuplicateHandle(process, handle, process,
120 &self->thread, 0, FALSE,
121 DUPLICATE_SAME_ACCESS);
122 tls->platform_context = self;
126 return (SilcThread)self;
132 SilcBool silc_thread_wait(SilcThread thread, void **exit_value)
135 SilcWin32Thread self = (SilcWin32Thread)thread;
137 SILC_LOG_DEBUG(("Waiting for thread %p", self));
142 /* The thread is waitable thus we will free all memory after the
143 WaitForSingleObject returns, the thread is destroyed after that. */
144 WaitForSingleObject(self->thread, INFINITE);
145 CloseHandle(self->thread);
156 void silc_thread_yield(void)
160 #endif /* SILC_THREADS */
164 /***************************** SILC Mutex API *******************************/
166 /* SILC Mutex structure */
167 struct SilcMutexStruct {
169 CRITICAL_SECTION mutex;
170 #endif /* SILC_THREADS */
171 unsigned int locked : 1;
174 SilcBool silc_mutex_alloc(SilcMutex *mutex)
177 *mutex = silc_calloc(1, sizeof(**mutex));
180 InitializeCriticalSection(&((*mutex)->mutex));
184 #endif /* SILC_THREADS */
187 void silc_mutex_free(SilcMutex mutex)
191 DeleteCriticalSection(&mutex->mutex);
194 #endif /* SILC_THREADS */
197 void silc_mutex_lock(SilcMutex mutex)
201 EnterCriticalSection(&mutex->mutex);
202 SILC_ASSERT(mutex->locked == FALSE);
203 mutex->locked = TRUE;
205 #endif /* SILC_THREADS */
208 void silc_mutex_unlock(SilcMutex mutex)
212 SILC_ASSERT(mutex->locked == TRUE);
213 mutex->locked = FALSE;
214 LeaveCriticalSection(&mutex->mutex);
216 #endif /* SILC_THREADS */
219 void silc_mutex_assert_locked(SilcMutex mutex)
223 SILC_ASSERT(mutex->locked);
224 #endif /* SILC_THREADS */
228 /***************************** SILC Rwlock API ******************************/
230 /* SILC read/write lock structure */
231 struct SilcRwLockStruct {
235 #endif /* SILC_THREADS */
236 unsigned int readers : 31;
237 unsigned int locked : 1;
240 SilcBool silc_rwlock_alloc(SilcRwLock *rwlock)
243 *rwlock = silc_calloc(1, sizeof(**rwlock));
246 if (!silc_mutex_alloc(&(*rwlock)->mutex)) {
250 if (!silc_cond_alloc(&(*rwlock)->cond)) {
251 silc_mutex_free((*rwlock)->mutex);
258 #endif /* SILC_THREADS */
261 void silc_rwlock_free(SilcRwLock rwlock)
265 silc_mutex_free(rwlock->mutex);
266 silc_cond_free(rwlock->cond);
269 #endif /* SILC_THREADS */
272 void silc_rwlock_rdlock(SilcRwLock rwlock)
276 silc_mutex_lock(rwlock->mutex);
278 silc_mutex_unlock(rwlock->mutex);
280 #endif /* SILC_THREADS */
283 void silc_rwlock_wrlock(SilcRwLock rwlock)
287 silc_mutex_lock(rwlock->mutex);
288 while (rwlock->readers > 0)
289 silc_cond_wait(rwlock->cond, rwlock->mutex);
290 rwlock->locked = TRUE;
292 #endif /* SILC_THREADS */
295 void silc_rwlock_unlock(SilcRwLock rwlock)
299 if (rwlock->locked) {
301 rwlock->locked = FALSE;
302 silc_mutex_unlock(rwlock->mutex);
307 silc_mutex_lock(rwlock->mutex);
309 silc_cond_broadcast(rwlock->cond);
310 silc_mutex_unlock(rwlock->mutex);
312 #endif /* SILC_THREADS */
316 /**************************** SILC Cond API ******************************/
318 /* SILC Conditional Variable context */
319 struct SilcCondStruct {
322 #endif /* SILC_THREADS*/
323 unsigned int waiters : 23;
324 unsigned int signal : 1;
327 SilcBool silc_cond_alloc(SilcCond *cond)
330 *cond = silc_calloc(1, sizeof(**cond));
333 (*cond)->event = CreateEvent(NULL, TRUE, FALSE, NULL);
337 #endif /* SILC_THREADS*/
340 void silc_cond_free(SilcCond cond)
343 CloseHandle(cond->event);
345 #endif /* SILC_THREADS*/
348 void silc_cond_signal(SilcCond cond)
352 SetEvent(cond->event);
353 #endif /* SILC_THREADS*/
356 void silc_cond_broadcast(SilcCond cond)
360 SetEvent(cond->event);
361 #endif /* SILC_THREADS*/
364 void silc_cond_wait(SilcCond cond, SilcMutex mutex)
367 silc_cond_timedwait(cond, mutex, 0);
368 #endif /* SILC_THREADS*/
371 SilcBool silc_cond_timedwait(SilcCond cond, SilcMutex mutex,
375 DWORD ret, t = INFINITE;
382 silc_mutex_unlock(mutex);
384 ret = WaitForSingleObject(cond->event, t);
386 silc_mutex_lock(mutex);
389 if (ret != WAIT_OBJECT_0)
393 cond->signal = FALSE;
394 ResetEvent(cond->event);
398 #endif /* SILC_THREADS*/
402 /************************** Thread-local Storage ****************************/
406 static DWORD silc_tls;
407 SilcBool silc_tls_set = FALSE;
409 SilcTls silc_thread_tls_init(void)
414 silc_tls = TlsAlloc();
415 if (silc_tls == TLS_OUT_OF_INDEXES) {
416 SILC_LOG_ERROR(("Error creating Thread-local storage"));
423 if (silc_thread_get_tls())
424 return silc_thread_get_tls();
426 /* Allocate Tls for the thread */
427 tls = silc_calloc(1, sizeof(*tls));
429 SILC_LOG_ERROR(("Error allocating Thread-local storage"));
433 TlsSetValue(silc_tls, tls);
437 SilcTls silc_thread_get_tls(void)
439 return (SilcTls)TlsGetValue(silc_tls);
445 SilcTls tls_ptr = NULL;
447 SilcTls silc_thread_tls_init(void)
449 if (silc_thread_get_tls())
450 return silc_thread_get_tls();
453 memset(tls_ptr, 0, sizeof(*tls_ptr));
457 SilcTls silc_thread_get_tls(void)
462 #endif /* SILC_THREADS */