5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2001 - 2005 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.
21 #include "silcincludes.h"
25 /* Thread structure for WIN32 */
28 SilcThreadStart start_func;
33 static DWORD silc_thread_tls;
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;
43 TlsSetValue(silc_thread_tls, context);
44 silc_thread_exit(thread->start_func(thread->context));
50 SilcThread silc_thread_create(SilcThreadStart start_func, void *context,
54 SilcWin32Thread thread;
57 SILC_LOG_DEBUG(("Creating new thread"));
59 thread = silc_calloc(1, sizeof(*thread));
60 thread->start_func = start_func;
61 thread->context = context;
62 thread->waitable = waitable;
64 CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)silc_thread_win32_start,
65 (void *)thread, 0, &id);
67 if (!thread->thread) {
68 SILC_LOG_ERROR(("Could not create new thread"));
73 return (SilcThread)thread;
75 /* Call thread callback immediately */
76 (*start_func)(context);
81 void silc_thread_exit(void *exit_value)
84 SilcWin32Thread thread = TlsGetValue(silc_thread_tls);
87 /* If the thread is waitable the memory is freed only in silc_thread_wait
88 by another thread. If not waitable, free it now. */
89 if (!thread->waitable) {
90 TerminateThread(thread->thread, 0);
94 TlsSetValue(silc_thread_tls, NULL);
100 SilcThread silc_thread_self(void)
103 SilcWin32Thread self = TlsGetValue(silc_thread_tls);
106 /* This should only happen for the main thread! */
107 HANDLE handle = GetCurrentThread ();
108 HANDLE process = GetCurrentProcess ();
109 self = silc_calloc(1, sizeof(*self));
110 DuplicateHandle(process, handle, process,
111 &self->thread, 0, FALSE,
112 DUPLICATE_SAME_ACCESS);
113 TlsSetValue(silc_thread_tls, self);
116 return (SilcThread)self;
122 bool silc_thread_wait(SilcThread thread, void **exit_value)
125 SilcWin32Thread self = (SilcWin32Thread)thread;
127 SILC_LOG_DEBUG(("Waiting for thread %p", self));
132 /* The thread is waitable thus we will free all memory after the
133 WaitForSingleObject returns, the thread is destroyed after that. */
134 if (WaitForSingleObject(self->thread, 2500) == WAIT_TIMEOUT)
135 TerminateThread(self->thread, 0);