Added SILC Rand API, SILC Global Variables API and silcruntime.h.in
[runtime.git] / lib / silcutil / win32 / silcwin32thread.c
index f8121fcbfc4a2d809b9e65e78486b73ef047e7f3..aa0cfaafd5068efc79e633635b02004bf263ccd5 100644 (file)
@@ -4,7 +4,7 @@
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 2001 - 2007 Pekka Riikonen
+  Copyright (C) 2001 - 2008 Pekka Riikonen
 
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   GNU General Public License for more details.
 
 */
-/* $Id$ */
 
-#include "silc.h"
+#include "silcruntime.h"
+
+/************************ Static utility functions **************************/
+
+static SilcTls silc_thread_tls_init_shared(SilcTls other);
 
 /**************************** SILC Thread API *******************************/
 
@@ -30,10 +33,9 @@ typedef struct {
   SilcThreadStart start_func;
   void *context;
   SilcBool waitable;
+  SilcTls tls;
 } *SilcWin32Thread;
 
-static DWORD silc_thread_tls;
-
 /* Actual routine that is called by WIN32 when the thread is created.
    We will call the start_func from here. When this returns the thread
    is destroyed. */
@@ -41,10 +43,18 @@ static DWORD silc_thread_tls;
 unsigned __stdcall silc_thread_win32_start(void *context)
 {
   SilcWin32Thread thread = (SilcWin32Thread)context;
+  SilcTls other = thread->tls, tls;
+
+  tls = silc_thread_tls_init_shared(other);
+  if (tls)
+    tls->platform_context = thread;
 
-  TlsSetValue(silc_thread_tls, context);
   silc_thread_exit(thread->start_func(thread->context));
 
+  if (tls->tls_variables)
+    silc_hash_table_free(tls->tls_variables);
+  silc_free(tls);
+
   return 0;
 }
 #endif
@@ -59,15 +69,19 @@ SilcThread silc_thread_create(SilcThreadStart start_func, void *context,
   SILC_LOG_DEBUG(("Creating new thread"));
 
   thread = silc_calloc(1, sizeof(*thread));
+  if (!thread)
+    return NULL;
   thread->start_func = start_func;
   thread->context = context;
   thread->waitable = waitable;
+  thread->tls = silc_thread_get_tls();
   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"));
+    silc_set_errno_reason(SILC_ERR, "Could not create new thread");
     silc_free(thread);
     return NULL;
   }
@@ -83,40 +97,41 @@ SilcThread silc_thread_create(SilcThreadStart start_func, void *context,
 void silc_thread_exit(void *exit_value)
 {
 #ifdef SILC_THREADS
-  SilcWin32Thread thread = TlsGetValue(silc_thread_tls);
+  SilcTls tls = silc_thread_get_tls();
+  SilcWin32Thread thread = tls->platform_context;
 
   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);
+
+  _endthreadex(0);
 #endif
 }
 
 SilcThread silc_thread_self(void)
 {
 #ifdef SILC_THREADS
-  SilcWin32Thread self = TlsGetValue(silc_thread_tls);
+  SilcTls tls = silc_thread_get_tls();
+  SilcWin32Thread self = tls->platform_context;
 
   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));
-    DuplicateHandle(process, handle, process,
-                   &self->thread, 0, FALSE,
-                   DUPLICATE_SAME_ACCESS);
-    TlsSetValue(silc_thread_tls, self);
+    if (self) {
+      DuplicateHandle(process, handle, process,
+                     &self->thread, 0, FALSE,
+                     DUPLICATE_SAME_ACCESS);
+      tls->platform_context = self;
+    }
   }
 
   return (SilcThread)self;
-       #else
+#else
   return NULL;
 #endif
 }
@@ -133,10 +148,9 @@ SilcBool silc_thread_wait(SilcThread thread, void **exit_value)
 
   /* 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;
 
@@ -254,7 +268,7 @@ SilcBool silc_rwlock_alloc(SilcRwLock *rwlock)
 void silc_rwlock_free(SilcRwLock rwlock)
 {
 #ifdef SILC_THREADS
-  if (mutex) {
+  if (rwlock) {
     silc_mutex_free(rwlock->mutex);
     silc_cond_free(rwlock->cond);
     silc_free(rwlock);
@@ -357,7 +371,7 @@ void silc_cond_broadcast(SilcCond cond)
 void silc_cond_wait(SilcCond cond, SilcMutex mutex)
 {
 #ifdef SILC_THREADS
-  silc_cond_timedwait(cond, mutex, NULL);
+  silc_cond_timedwait(cond, mutex, 0);
 #endif /* SILC_THREADS*/
 }
 
@@ -391,3 +405,134 @@ SilcBool silc_cond_timedwait(SilcCond cond, SilcMutex mutex,
 #endif /* SILC_THREADS*/
   return TRUE;
 }
+
+/************************** Thread-local Storage ****************************/
+
+#ifdef SILC_THREADS
+
+static DWORD silc_tls;
+SilcBool silc_tls_set = FALSE;
+
+SilcTls silc_thread_tls_init(void)
+{
+  SilcTls tls;
+
+  if (!silc_tls_set) {
+    silc_tls = TlsAlloc();
+    if (silc_tls == TLS_OUT_OF_INDEXES) {
+      SILC_LOG_ERROR(("Error creating Thread-local storage"));
+      return NULL;
+    }
+
+    silc_tls_set = TRUE;
+  }
+
+  if (silc_thread_get_tls())
+    return silc_thread_get_tls();
+
+  /* Allocate Tls for the thread */
+  tls = silc_calloc(1, sizeof(*tls));
+  if (!tls) {
+    SILC_LOG_ERROR(("Error allocating Thread-local storage"));
+    return NULL;
+  }
+  TlsSetValue(silc_tls, tls);
+
+  /* Allocate global lock */
+  silc_mutex_alloc(&tls->lock);
+
+  return tls;
+}
+
+static SilcTls silc_thread_tls_init_shared(SilcTls other)
+{
+  SilcTls tls;
+
+  if (!silc_tls_set) {
+    silc_tls = TlsAlloc();
+    if (silc_tls == TLS_OUT_OF_INDEXES) {
+      SILC_LOG_ERROR(("Error creating Thread-local storage"));
+      return NULL;
+    }
+
+    silc_tls_set = TRUE;
+  }
+
+  if (silc_thread_get_tls())
+    return silc_thread_get_tls();
+
+  /* Allocate Tls for the thread */
+  tls = silc_calloc(1, sizeof(*tls));
+  if (!tls) {
+    SILC_LOG_ERROR(("Error allocating Thread-local storage"));
+    return NULL;
+  }
+  TlsSetValue(silc_tls, tls);
+
+  /* Take shared data */
+  tls->shared_data = 1;
+  tls->lock = other->lock;
+  tls->variables = other->variables;
+
+  return tls;
+}
+
+SilcTls silc_thread_get_tls(void)
+{
+  return (SilcTls)TlsGetValue(silc_tls);
+}
+
+void silc_thread_tls_uninit(void)
+{
+  SilcTls tls = silc_thread_get_tls();
+
+  if (!tls || tls->shared_data)
+    return;
+
+  if (tls->tls_variables)
+    silc_hash_table_free(tls->tls_variables);
+  if (tls->variables)
+    silc_hash_table_free(tls->variables);
+  if (tls->lock)
+    silc_mutex_free(tls->lock);
+
+  tls->variables = NULL;
+  tls->lock = NULL;
+}
+
+#else
+
+SilcTlsStruct tls;
+SilcTls tls_ptr = NULL;
+
+SilcTls silc_thread_tls_init(void)
+{
+  if (silc_thread_get_tls())
+    return silc_thread_get_tls();
+
+  tls_ptr = &tls;
+  memset(tls_ptr, 0, sizeof(*tls_ptr));
+  return tls_ptr;
+}
+
+static SilcTls silc_thread_tls_init_shared(SilcTls other)
+{
+  return silc_thread_tls_init();
+}
+
+SilcTls silc_thread_get_tls(void)
+{
+  return tls_ptr;
+}
+
+void silc_thread_tls_uninit(void)
+{
+  if (tls.tls_variables)
+    silc_hash_table_free(tls.tls_variables);
+  if (tls.variables)
+    silc_hash_table_free(tls.variables);
+  if (tls.lock)
+    silc_mutex_free(tls.lock);
+}
+
+#endif /* SILC_THREADS */