{
SilcWin32Thread thread = (SilcWin32Thread)context;
- TlsSetValue(silc_thread_tls, context);
+ silc_thread_tls = TlsAlloc();
+ if (silc_thread_tls != TLS_OUT_OF_INDEXES)
+ TlsSetValue(silc_thread_tls, context);
+
silc_thread_exit(thread->start_func(thread->context));
return 0;
thread->context = context;
thread->waitable = waitable;
thread->thread =
- CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)silc_thread_win32_start,
- (void *)thread, 0, &id);
+ _beginthreadex(NULL, 0, (LPTHREAD_START_ROUTINE)silc_thread_win32_start,
+ (void *)thread, 0, &id);
if (!thread->thread) {
SILC_LOG_ERROR(("Could not create new thread"));
if (thread) {
/* If the thread is waitable the memory is freed only in silc_thread_wait
by another thread. If not waitable, free it now. */
- if (!thread->waitable) {
- TerminateThread(thread->thread, 0);
+ if (!thread->waitable)
silc_free(thread);
- }
-
- TlsSetValue(silc_thread_tls, NULL);
}
- ExitThread(0);
+
+ TlsFree(silc_thread_tls);
+ _endthreadex(0);
#endif
}
SilcWin32Thread self = TlsGetValue(silc_thread_tls);
if (!self) {
- /* This should only happen for the main thread! */
+ /* This should only happen for the main thread. */
HANDLE handle = GetCurrentThread ();
HANDLE process = GetCurrentProcess ();
self = silc_calloc(1, sizeof(*self));
/* The thread is waitable thus we will free all memory after the
WaitForSingleObject returns, the thread is destroyed after that. */
- if (WaitForSingleObject(self->thread, 2500) == WAIT_TIMEOUT)
- TerminateThread(self->thread, 0);
+ WaitForSingleObject(self->thread, INFINITE);
+ CloseHandle(self->thread);
- silc_free(self);
if (exit_value)
*exit_value = NULL;