5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2001 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; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
20 /* These routines are based on GLib's WIN32 gthread implementation and
21 thus credits should go there. */
24 #include "silcincludes.h"
28 /* Thread structure for WIN32 */
31 SilcThreadStart start_func;
36 static DWORD silc_thread_tls;
38 /* Actual routine that is called by WIN32 when the thread is created.
39 We will call the start_func from here. When this returns the thread
42 unsigned __stdcall silc_thread_win32_start(void *context)
44 SilcWin32Thread thread = (SilcWin32Thread)context;
46 TlsSetValue(silc_thread_tls, context);
47 thread->start_func(thread->context);
48 silc_thread_exit(NULL);
54 SilcThread silc_thread_create(SilcThreadStart start_func, void *context,
58 SilcWin32Thread thread;
61 SILC_LOG_DEBUG(("Creating new thread"));
63 thread = silc_calloc(1, sizeof(*thread));
64 thread->start_func = start_func;
65 thread->context = context;
66 thread->waitable = waitable;
67 thread->thread = (HANDLE)_beginthreadex(NULL, 0, silc_thread_win32_start,
68 (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 CloseHandle(thread->thread);
96 TlsSetValue(silc_thread_tls, NULL);
103 SilcThread silc_thread_self(void)
106 SilcWin32Thread self = TlsGetValue(silc_thread_tls);
109 /* This should only happen for the main thread! */
110 HANDLE handle = GetCurrentThread ();
111 HANDLE process = GetCurrentProcess ();
112 self = silc_calloc(1, sizeof(*self));
113 DuplicateHandle(process, handle, process,
114 &self->thread, 0, FALSE,
115 DUPLICATE_SAME_ACCESS);
116 TlsSetValue(silc_thread_tls, self);
119 return (SilcThread)self;
125 bool silc_thread_wait(SilcThread thread, void **exit_value)
128 SilcWin32Thread self = (SilcWin32Thread)thread;
130 SILC_LOG_DEBUG(("Waiting for thread %p", self));
135 /* The thread is waitable thus we will free all memory after the
136 WaitForSingleObject returns, the thread is destroyed after that. */
137 WaitForSingleObject(self->thread, INFINITE);
138 CloseHandle(self->thread);