Fri Sep 13 15:41:40 CEST 2002 Paolo Molaro <lupus@ximian.com>
[mono.git] / mono / io-layer / threads.c
index b53b31e2648b18220e920f94b109b223e7a929c9..133c2661b60a213ed028933d99d4af8a55381775 100644 (file)
@@ -1,4 +1,17 @@
+/*
+ * threads.c:  Thread handles
+ *
+ * Author:
+ *     Dick Porter (dick@ximian.com)
+ *
+ * (C) 2002 Ximian, Inc.
+ */
+
 #include <config.h>
+#if HAVE_BOEHM_GC
+#include <mono/os/gc_wrapper.h>
+#include "mono/utils/mono-hash.h"
+#endif
 #include <glib.h>
 #include <string.h>
 #include <pthread.h>
 #include <sys/time.h>
 #include <errno.h>
 
-#include "mono/io-layer/wapi.h"
-#include "wapi-private.h"
-#include "timed-thread.h"
-#include "wait-private.h"
-#include "handles-private.h"
-#include "misc-private.h"
+#include <mono/io-layer/wapi.h>
+#include <mono/io-layer/wapi-private.h>
+#include <mono/io-layer/timed-thread.h>
+#include <mono/io-layer/handles-private.h>
+#include <mono/io-layer/misc-private.h>
+#include <mono/io-layer/mono-mutex.h>
+#include <mono/io-layer/thread-private.h>
+#include <mono/io-layer/mono-spinlock.h>
 
-#include "pthread-compat.h"
-
-#define DEBUG
-
-typedef enum {
-       THREAD_STATE_START,
-       THREAD_STATE_EXITED,
-} WapiThreadState;
-
-struct _WapiHandle_thread
-{
-       WapiHandle handle;
-       WapiThreadState state;
-       TimedThread *thread;
-       guint32 exitstatus;
-};
+#undef DEBUG
+#undef TLS_DEBUG
+#undef TLS_PTHREAD_MUTEX
 
-static pthread_mutex_t thread_signal_mutex = PTHREAD_MUTEX_INITIALIZER;
-static pthread_cond_t thread_signal_cond = PTHREAD_COND_INITIALIZER;
 
 /* Hash threads with tids. I thought of using TLS for this, but that
  * would have to set the data in the new thread, which is more hassle
  */
-static pthread_once_t thread_hash_once = PTHREAD_ONCE_INIT;
-static pthread_mutex_t thread_hash_mutex = PTHREAD_MUTEX_INITIALIZER;
+static mono_once_t thread_hash_once = MONO_ONCE_INIT;
+static mono_mutex_t thread_hash_mutex = MONO_MUTEX_INITIALIZER;
 static GHashTable *thread_hash=NULL;
 
-static void thread_close(WapiHandle *handle);
-static gboolean thread_wait(WapiHandle *handle, WapiHandle *signal,
-                           guint32 ms);
-static guint32 thread_wait_multiple(gpointer data);
-
-static struct _WapiHandleOps thread_ops = {
-       thread_close,                   /* close */
-       NULL,                           /* getfiletype */
-       NULL,                           /* readfile */
-       NULL,                           /* writefile */
-       NULL,                           /* seek */
-       NULL,                           /* setendoffile */
-       NULL,                           /* getfilesize */
-       thread_wait,                    /* wait */
-       thread_wait_multiple,           /* wait_multiple */
+#if HAVE_BOEHM_GC
+static MonoGHashTable *tls_gc_hash = NULL;
+#endif
+
+static void thread_close_private (gpointer handle);
+static void thread_own (gpointer handle);
+
+struct _WapiHandleOps _wapi_thread_ops = {
+       NULL,                           /* close_shared */
+       thread_close_private,           /* close_private */
        NULL,                           /* signal */
+       thread_own,                     /* own */
+       NULL,                           /* is_owned */
 };
 
-static void thread_close(WapiHandle *handle)
-{
-       struct _WapiHandle_thread *thread_handle=(struct _WapiHandle_thread *)handle;
-       
-#ifdef DEBUG
-       g_message(G_GNUC_PRETTY_FUNCTION
-                 ": closing thread handle %p with thread %p id %ld",
-                 thread_handle, thread_handle->thread,
-                 thread_handle->thread->id);
-#endif
+static mono_once_t thread_ops_once=MONO_ONCE_INIT;
 
-       g_free(thread_handle->thread);
+static void thread_ops_init (void)
+{
+       _wapi_handle_register_capabilities (WAPI_HANDLE_THREAD,
+                                           WAPI_HANDLE_CAP_WAIT);
 }
 
-static gboolean thread_wait(WapiHandle *handle, WapiHandle *signal, guint32 ms)
+static void thread_close_private (gpointer handle)
 {
-       struct _WapiHandle_thread *thread_handle=(struct _WapiHandle_thread *)handle;
-       int ret;
-       
-       /* A thread can never become unsignalled after it was
-        * signalled, so we can signal this handle now without
-        * worrying about lost wakeups
-        */
-       if(signal!=NULL) {
-               signal->ops->signal(signal);
+       struct _WapiHandlePrivate_thread *thread_handle;
+       gboolean ok;
+       
+       ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD, NULL,
+                               (gpointer *)&thread_handle);
+       if(ok==FALSE) {
+               g_warning (G_GNUC_PRETTY_FUNCTION
+                          ": error looking up thread handle %p", handle);
+               return;
        }
        
-       if(handle->signalled==TRUE) {
-               /* Already signalled, so return straight away */
-#ifdef DEBUG
-               g_message(G_GNUC_PRETTY_FUNCTION ": thread handle %p already signalled, returning now", handle);
-#endif
-
-               return(TRUE);
-       }
-
 #ifdef DEBUG
        g_message(G_GNUC_PRETTY_FUNCTION
-                 ": waiting for %d ms for thread handle %p with id %ld", ms,
-                 thread_handle, thread_handle->thread->id);
+                 ": closing thread handle %p with thread %p id %ld",
+                 handle, thread_handle->thread,
+                 thread_handle->thread->id);
 #endif
 
-       if(ms==INFINITE) {
-               ret=_wapi_timed_thread_join(thread_handle->thread, NULL, NULL);
-       } else {
-               struct timespec timeout;
-
-               _wapi_calc_timeout(&timeout, ms);
-       
-               ret=_wapi_timed_thread_join(thread_handle->thread, &timeout,
-                                           NULL);
-       }
-       
-       if(ret==0) {
-               /* Thread joined */
-               return(TRUE);
-       } else {
-               /* ret might be ETIMEDOUT for timeout, or other for error */
-               return(FALSE);
+       if(thread_handle->thread!=NULL) {
+               _wapi_timed_thread_destroy (thread_handle->thread);
        }
 }
 
-static guint32 thread_wait_multiple(gpointer data)
+static void thread_own (gpointer handle)
 {
-       WaitQueueItem *item=(WaitQueueItem *)data;
-       int ret;
-       guint32 numhandles, count;
-       struct timespec timeout;
-       
-       numhandles=item->handles[WAPI_HANDLE_THREAD]->len;
-       
-#ifdef DEBUG
-       g_message(G_GNUC_PRETTY_FUNCTION
-                 ": waiting on %d thread handles for %d ms", numhandles,
-                 item->timeout);
-#endif
-
-       /* First, check if any of the handles are already
-        * signalled. If waitall is specified we only return if all
-        * handles have been signalled.
-        */
-       count=_wapi_handle_count_signalled(item, WAPI_HANDLE_THREAD);
-
-#ifdef DEBUG
-       g_message(G_GNUC_PRETTY_FUNCTION
-                 ": Preliminary check found %d handles signalled", count);
-#endif
-
-       if((item->waitall==TRUE && count==numhandles) || 
-          (item->waitall==FALSE && count>0)) {
-               goto success;
-       }
-       
-       /* OK, we need to wait for some */
-       if(item->timeout!=INFINITE) {
-               _wapi_calc_timeout(&timeout, item->timeout);
-       }
-       
-       /* We can restart from here without resetting the timeout,
-        * because it is calculated from absolute time, not an offset
-        */
-again:
-       pthread_mutex_lock(&thread_signal_mutex);
-       if(item->timeout==INFINITE) {
-               ret=pthread_cond_wait(&thread_signal_cond,
-                                     &thread_signal_mutex);
-       } else {
-               ret=pthread_cond_timedwait(&thread_signal_cond,
-                                          &thread_signal_mutex,
-                                          &timeout);
+       struct _WapiHandle_thread *thread_handle;
+       struct _WapiHandlePrivate_thread *thread_private_handle;
+       gboolean ok;
+       
+       ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
+                               (gpointer *)&thread_handle,
+                               (gpointer *)&thread_private_handle);
+       if(ok==FALSE) {
+               g_warning (G_GNUC_PRETTY_FUNCTION
+                          ": error looking up thread handle %p", handle);
+               return;
        }
-       pthread_mutex_unlock(&thread_signal_mutex);
-
-       if(ret==ETIMEDOUT) {
-               /* Check signalled state here, just in case a thread
-                * exited between the first check and the cond wait.
-                * We return the number of signalled handles, which
-                * may be fewer than the total.
-                */
-#ifdef DEBUG
-               g_message(G_GNUC_PRETTY_FUNCTION ": Wait timed out");
-#endif
 
-               count=_wapi_handle_count_signalled(item, WAPI_HANDLE_THREAD);
-               goto success;
+       if(thread_private_handle->joined==FALSE) {
+               _wapi_timed_thread_join (thread_private_handle->thread, NULL,
+                                        NULL);
+               thread_private_handle->joined=TRUE;
        }
-       
-#ifdef DEBUG
-       g_message(G_GNUC_PRETTY_FUNCTION ": Thread exited, checking status");
-#endif
-
-       /* Another thread exited, so see if it was one we are
-        * interested in
-        */
-       count=_wapi_handle_count_signalled(item, WAPI_HANDLE_THREAD);
-
-#ifdef DEBUG
-       g_message(G_GNUC_PRETTY_FUNCTION
-                 ": Check after thread exit found %d handles signalled",
-                 count);
-#endif
+}
 
-       if((item->waitall==TRUE && count==numhandles) ||
-          (item->waitall==FALSE && count>0)) {
-               goto success;
+static void thread_exit(guint32 exitstatus, gpointer handle)
+{
+       struct _WapiHandle_thread *thread_handle;
+       struct _WapiHandlePrivate_thread *thread_private_handle;
+       gboolean ok;
+       
+       ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
+                               (gpointer *)&thread_handle,
+                               (gpointer *)&thread_private_handle);
+       if(ok==FALSE) {
+               g_warning (G_GNUC_PRETTY_FUNCTION
+                          ": error looking up thread handle %p", handle);
+               return;
        }
 
-       /* Either we have waitall set with more handles to wait for,
-        * or the thread that exited wasn't interesting to us
-        */
+       _wapi_handle_lock_handle (handle);
+
 #ifdef DEBUG
-       g_message(G_GNUC_PRETTY_FUNCTION ": Waiting a bit longer");
+       g_message (G_GNUC_PRETTY_FUNCTION
+                  ": Recording thread handle %p exit status", handle);
 #endif
-
-       goto again;
-
-success:
-       item->waited[WAPI_HANDLE_THREAD]=TRUE;
-       item->waitcount[WAPI_HANDLE_THREAD]=count;
        
-       return(count);
-}
-
-static void thread_exit(guint32 exitstatus, gpointer userdata)
-{
-       struct _WapiHandle_thread *thread_handle=(struct _WapiHandle_thread *)userdata;
-
        thread_handle->exitstatus=exitstatus;
        thread_handle->state=THREAD_STATE_EXITED;
-       thread_handle->handle.signalled=TRUE;
+       _wapi_handle_set_signal_state (handle, TRUE, TRUE);
+
+       _wapi_handle_unlock_handle (handle);
        
 #ifdef DEBUG
        g_message(G_GNUC_PRETTY_FUNCTION
                  ": Recording thread handle %p id %ld status as %d",
-                 thread_handle, thread_handle->thread->id, exitstatus);
+                 handle, thread_private_handle->thread->id, exitstatus);
 #endif
 
        /* Remove this thread from the hash */
-       pthread_mutex_lock(&thread_hash_mutex);
-       g_hash_table_remove(thread_hash, &thread_handle->thread->id);
-       pthread_mutex_unlock(&thread_hash_mutex);
-       
-       /* Signal any thread waiting on thread exit */
-       pthread_mutex_lock(&thread_signal_mutex);
-       pthread_cond_broadcast(&thread_signal_cond);
-       pthread_mutex_unlock(&thread_signal_mutex);
+       mono_mutex_lock(&thread_hash_mutex);
+       g_hash_table_remove(thread_hash, &thread_private_handle->thread->id);
+       mono_mutex_unlock(&thread_hash_mutex);
+
+       /* The thread is no longer active, so unref it */
+       _wapi_handle_unref (handle);
 }
 
 static void thread_hash_init(void)
@@ -267,60 +174,114 @@ static void thread_hash_init(void)
  *
  * Return value: a new handle, or NULL
  */
-WapiHandle *CreateThread(WapiSecurityAttributes *security G_GNUC_UNUSED, guint32 stacksize G_GNUC_UNUSED,
-                        WapiThreadStart start, gpointer param, guint32 create G_GNUC_UNUSED,
-                        guint32 *tid) 
+gpointer CreateThread(WapiSecurityAttributes *security G_GNUC_UNUSED, guint32 stacksize G_GNUC_UNUSED,
+                     WapiThreadStart start, gpointer param, guint32 create G_GNUC_UNUSED,
+                     guint32 *tid) 
 {
        struct _WapiHandle_thread *thread_handle;
-       WapiHandle *handle;
+       struct _WapiHandlePrivate_thread *thread_private_handle;
+       gpointer handle;
+       gboolean ok;
        int ret;
        
-       pthread_once(&thread_hash_once, thread_hash_init);
+       mono_once(&thread_hash_once, thread_hash_init);
+       mono_once (&thread_ops_once, thread_ops_init);
        
        if(start==NULL) {
                return(NULL);
        }
        
-       thread_handle=(struct _WapiHandle_thread *)g_new0(struct _WapiHandle_thread, 1);
+       handle=_wapi_handle_new (WAPI_HANDLE_THREAD);
+       if(handle==_WAPI_HANDLE_INVALID) {
+               g_warning (G_GNUC_PRETTY_FUNCTION
+                          ": error creating thread handle");
+               return(NULL);
+       }
+
+       _wapi_handle_lock_handle (handle);
+       
+       ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
+                               (gpointer *)&thread_handle,
+                               (gpointer *)&thread_private_handle);
+       if(ok==FALSE) {
+               g_warning (G_GNUC_PRETTY_FUNCTION
+                          ": error looking up thread handle %p", handle);
+               _wapi_handle_unlock_handle (handle);
+               return(NULL);
+       }
+
+       /* Hold a reference while the thread is active, because we use
+        * the handle to store thread exit information
+        */
+       _wapi_handle_ref (handle);
+
        thread_handle->state=THREAD_STATE_START;
        
        /* Lock around the thread create, so that the new thread cant
         * race us to look up the thread handle in GetCurrentThread()
         */
-       pthread_mutex_lock(&thread_hash_mutex);
+       mono_mutex_lock(&thread_hash_mutex);
        
-       ret=_wapi_timed_thread_create(&thread_handle->thread, NULL, start,
-                                     thread_exit, param, thread_handle);
+       ret=_wapi_timed_thread_create(&thread_private_handle->thread, NULL,
+                                     start, thread_exit, param, handle);
        if(ret!=0) {
 #ifdef DEBUG
                g_message(G_GNUC_PRETTY_FUNCTION ": Thread create error: %s",
                          strerror(ret));
 #endif
-               pthread_mutex_unlock(&thread_hash_mutex);
-               g_free(thread_handle);
+               mono_mutex_unlock(&thread_hash_mutex);
+               _wapi_handle_unlock_handle (handle);
+               _wapi_handle_unref (handle);
+               
+               /* And again, because of the reference we took above */
+               _wapi_handle_unref (handle);
                return(NULL);
        }
 
-       g_hash_table_insert(thread_hash, &thread_handle->thread->id,
-                           thread_handle);
-       pthread_mutex_unlock(&thread_hash_mutex);
-
-       handle=(WapiHandle *)thread_handle;
-       _WAPI_HANDLE_INIT(handle, WAPI_HANDLE_THREAD, thread_ops);
+       g_hash_table_insert(thread_hash, &thread_private_handle->thread->id,
+                           handle);
+       mono_mutex_unlock(&thread_hash_mutex);
        
 #ifdef DEBUG
        g_message(G_GNUC_PRETTY_FUNCTION
-                 ": Started thread handle %p thread %p ID %ld", thread_handle,
-                 thread_handle->thread, thread_handle->thread->id);
+                 ": Started thread handle %p thread %p ID %ld", handle,
+                 thread_private_handle->thread,
+                 thread_private_handle->thread->id);
 #endif
        
        if(tid!=NULL) {
-               *tid=thread_handle->thread->id;
+               *tid=thread_private_handle->thread->id;
        }
+
+       _wapi_handle_unlock_handle (handle);
        
        return(handle);
 }
 
+gpointer OpenThread (guint32 access G_GNUC_UNUSED, gboolean inherit G_GNUC_UNUSED, guint32 tid)
+{
+       gpointer ret=NULL;
+       
+#ifdef DEBUG
+       g_message (G_GNUC_PRETTY_FUNCTION ": looking up thread %d", tid);
+#endif
+
+       mono_mutex_lock(&thread_hash_mutex);
+       
+       ret=g_hash_table_lookup(thread_hash, &tid);
+       mono_mutex_unlock(&thread_hash_mutex);
+       
+       if(ret!=NULL) {
+               _wapi_handle_ref (ret);
+       }
+       
+#ifdef DEBUG
+       g_message (G_GNUC_PRETTY_FUNCTION ": returning thread handle %p", ret);
+#endif
+       
+       return(ret);
+}
+
 /**
  * ExitThread:
  * @exitcode: Sets the thread's exit code, which can be read from
@@ -332,6 +293,10 @@ WapiHandle *CreateThread(WapiSecurityAttributes *security G_GNUC_UNUSED, guint32
  */
 void ExitThread(guint32 exitcode)
 {
+       /* No thread created yet.  */
+       if (thread_hash == NULL)
+               exit(exitcode);
+
        _wapi_timed_thread_exit(exitcode);
 }
 
@@ -345,14 +310,25 @@ void ExitThread(guint32 exitcode)
  *
  * Return value: %TRUE, or %FALSE on error.
  */
-gboolean GetExitCodeThread(WapiHandle *handle, guint32 *exitcode)
+gboolean GetExitCodeThread(gpointer handle, guint32 *exitcode)
 {
-       struct _WapiHandle_thread *thread_handle=(struct _WapiHandle_thread *)handle;
+       struct _WapiHandle_thread *thread_handle;
+       struct _WapiHandlePrivate_thread *thread_private_handle;
+       gboolean ok;
+       
+       ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
+                               (gpointer *)&thread_handle,
+                               (gpointer *)&thread_private_handle);
+       if(ok==FALSE) {
+               g_warning (G_GNUC_PRETTY_FUNCTION
+                          ": error looking up thread handle %p", handle);
+               return(FALSE);
+       }
        
 #ifdef DEBUG
        g_message(G_GNUC_PRETTY_FUNCTION
                  ": Finding exit status for thread handle %p id %ld", handle,
-                 thread_handle->thread->id);
+                 thread_private_handle->thread->id);
 #endif
 
        if(exitcode==NULL) {
@@ -404,18 +380,18 @@ guint32 GetCurrentThreadId(void)
  * (Unknown whether Windows has a possible failure here.  It may be
  * necessary to implement the pseudohandle-constant behaviour).
  */
-WapiHandle *GetCurrentThread(void)
+gpointer GetCurrentThread(void)
 {
-       WapiHandle *ret=NULL;
+       gpointer ret=NULL;
        guint32 tid;
        
        tid=GetCurrentThreadId();
        
-       pthread_mutex_lock(&thread_hash_mutex);
+       mono_mutex_lock(&thread_hash_mutex);
 
        ret=g_hash_table_lookup(thread_hash, &tid);
        
-       pthread_mutex_unlock(&thread_hash_mutex);
+       mono_mutex_unlock(&thread_hash_mutex);
        
        return(ret);
 }
@@ -429,7 +405,7 @@ WapiHandle *GetCurrentThread(void)
  *
  * Return value: the previous suspend count, or 0xFFFFFFFF on error.
  */
-guint32 ResumeThread(WapiHandle *handle G_GNUC_UNUSED)
+guint32 ResumeThread(gpointer handle G_GNUC_UNUSED)
 {
        return(0xFFFFFFFF);
 }
@@ -443,7 +419,7 @@ guint32 ResumeThread(WapiHandle *handle G_GNUC_UNUSED)
  *
  * Return value: the previous suspend count, or 0xFFFFFFFF on error.
  */
-guint32 SuspendThread(WapiHandle *handle G_GNUC_UNUSED)
+guint32 SuspendThread(gpointer handle G_GNUC_UNUSED)
 {
        return(0xFFFFFFFF);
 }
@@ -460,7 +436,11 @@ guint32 SuspendThread(WapiHandle *handle G_GNUC_UNUSED)
 
 static pthread_key_t TLS_keys[TLS_MINIMUM_AVAILABLE];
 static gboolean TLS_used[TLS_MINIMUM_AVAILABLE]={FALSE};
-static pthread_mutex_t TLS_mutex=PTHREAD_MUTEX_INITIALIZER;
+#ifdef TLS_PTHREAD_MUTEX
+static mono_mutex_t TLS_mutex=MONO_MUTEX_INITIALIZER;
+#else
+static guint32 TLS_spinlock=0;
+#endif
 
 /**
  * TlsAlloc:
@@ -476,24 +456,48 @@ guint32 TlsAlloc(void)
 {
        guint32 i;
        
-       pthread_mutex_lock(&TLS_mutex);
+#ifdef TLS_PTHREAD_MUTEX
+       mono_mutex_lock(&TLS_mutex);
+#else
+       MONO_SPIN_LOCK (TLS_spinlock);
+#endif
        
        for(i=0; i<TLS_MINIMUM_AVAILABLE; i++) {
                if(TLS_used[i]==FALSE) {
                        TLS_used[i]=TRUE;
                        pthread_key_create(&TLS_keys[i], NULL);
 
-                       pthread_mutex_unlock(&TLS_mutex);
+#ifdef TLS_PTHREAD_MUTEX
+                       mono_mutex_unlock(&TLS_mutex);
+#else
+                       MONO_SPIN_UNLOCK (TLS_spinlock);
+#endif
+       
+#ifdef TLS_DEBUG
+                       g_message (G_GNUC_PRETTY_FUNCTION ": returning key %d",
+                                  i);
+#endif
                        
                        return(i);
                }
        }
 
-       pthread_mutex_unlock(&TLS_mutex);
+#ifdef TLS_PTHREAD_MUTEX
+       mono_mutex_unlock(&TLS_mutex);
+#else
+       MONO_SPIN_UNLOCK (TLS_spinlock);
+#endif
+       
+#ifdef TLS_DEBUG
+       g_message (G_GNUC_PRETTY_FUNCTION ": out of indices");
+#endif
+                       
        
        return(TLS_OUT_OF_INDEXES);
 }
 
+#define MAKE_GC_ID(idx) (GUINT_TO_POINTER((idx)|(GetCurrentThreadId()<<8)))
+
 /**
  * TlsFree:
  * @idx: The TLS index to free
@@ -505,17 +509,37 @@ guint32 TlsAlloc(void)
  */
 gboolean TlsFree(guint32 idx)
 {
-       pthread_mutex_lock(&TLS_mutex);
+#ifdef TLS_DEBUG
+       g_message (G_GNUC_PRETTY_FUNCTION ": freeing key %d", idx);
+#endif
+
+#ifdef TLS_PTHREAD_MUTEX
+       mono_mutex_lock(&TLS_mutex);
+#else
+       MONO_SPIN_LOCK (TLS_spinlock);
+#endif
        
        if(TLS_used[idx]==FALSE) {
-               pthread_mutex_unlock(&TLS_mutex);
+#ifdef TLS_PTHREAD_MUTEX
+               mono_mutex_unlock(&TLS_mutex);
+#else
+               MONO_SPIN_UNLOCK (TLS_spinlock);
+#endif
                return(FALSE);
        }
        
        TLS_used[idx]=FALSE;
        pthread_key_delete(TLS_keys[idx]);
        
-       pthread_mutex_unlock(&TLS_mutex);
+#if HAVE_BOEHM_GC
+       mono_g_hash_table_remove (tls_gc_hash, MAKE_GC_ID (idx));
+#endif
+
+#ifdef TLS_PTHREAD_MUTEX
+       mono_mutex_unlock(&TLS_mutex);
+#else
+       MONO_SPIN_UNLOCK (TLS_spinlock);
+#endif
        
        return(TRUE);
 }
@@ -534,16 +558,40 @@ gpointer TlsGetValue(guint32 idx)
 {
        gpointer ret;
        
-       pthread_mutex_lock(&TLS_mutex);
+#ifdef TLS_DEBUG
+       g_message (G_GNUC_PRETTY_FUNCTION ": looking up key %d", idx);
+#endif
+
+#ifdef TLS_PTHREAD_MUTEX
+       mono_mutex_lock(&TLS_mutex);
+#else
+       MONO_SPIN_LOCK (TLS_spinlock);
+#endif
        
        if(TLS_used[idx]==FALSE) {
-               pthread_mutex_unlock(&TLS_mutex);
+#ifdef TLS_DEBUG
+               g_message (G_GNUC_PRETTY_FUNCTION ": key %d unused", idx);
+#endif
+
+#ifdef TLS_PTHREAD_MUTEX
+               mono_mutex_unlock(&TLS_mutex);
+#else
+               MONO_SPIN_UNLOCK (TLS_spinlock);
+#endif
                return(NULL);
        }
        
        ret=pthread_getspecific(TLS_keys[idx]);
+
+#ifdef TLS_DEBUG
+       g_message (G_GNUC_PRETTY_FUNCTION ": returning %p", ret);
+#endif
        
-       pthread_mutex_unlock(&TLS_mutex);
+#ifdef TLS_PTHREAD_MUTEX
+       mono_mutex_unlock(&TLS_mutex);
+#else
+       MONO_SPIN_UNLOCK (TLS_spinlock);
+#endif
        
        return(ret);
 }
@@ -560,21 +608,57 @@ gpointer TlsGetValue(guint32 idx)
 gboolean TlsSetValue(guint32 idx, gpointer value)
 {
        int ret;
+
+#ifdef TLS_DEBUG
+       g_message (G_GNUC_PRETTY_FUNCTION ": setting key %d to %p", idx,
+                  value);
+#endif
        
-       pthread_mutex_lock(&TLS_mutex);
+#ifdef TLS_PTHREAD_MUTEX
+       mono_mutex_lock(&TLS_mutex);
+#else
+       MONO_SPIN_LOCK (TLS_spinlock);
+#endif
        
        if(TLS_used[idx]==FALSE) {
-               pthread_mutex_unlock(&TLS_mutex);
+#ifdef TLS_DEBUG
+               g_message (G_GNUC_PRETTY_FUNCTION ": key %d unused", idx);
+#endif
+
+#ifdef TLS_PTHREAD_MUTEX
+               mono_mutex_unlock(&TLS_mutex);
+#else
+               MONO_SPIN_UNLOCK (TLS_spinlock);
+#endif
                return(FALSE);
        }
        
        ret=pthread_setspecific(TLS_keys[idx], value);
        if(ret!=0) {
-               pthread_mutex_unlock(&TLS_mutex);
+#ifdef TLS_DEBUG
+               g_message (G_GNUC_PRETTY_FUNCTION
+                          ": pthread_setspecific error: %s", strerror (ret));
+#endif
+
+#ifdef TLS_PTHREAD_MUTEX
+               mono_mutex_unlock(&TLS_mutex);
+#else
+               MONO_SPIN_UNLOCK (TLS_spinlock);
+#endif
                return(FALSE);
        }
        
-       pthread_mutex_unlock(&TLS_mutex);
+#if HAVE_BOEHM_GC
+       if (!tls_gc_hash)
+               tls_gc_hash = mono_g_hash_table_new(g_direct_hash, g_direct_equal);
+       mono_g_hash_table_insert (tls_gc_hash, MAKE_GC_ID (idx), value);
+#endif
+
+#ifdef TLS_PTHREAD_MUTEX
+       mono_mutex_unlock(&TLS_mutex);
+#else
+       MONO_SPIN_UNLOCK (TLS_spinlock);
+#endif
        
        return(TRUE);
 }
@@ -606,14 +690,14 @@ void Sleep(guint32 ms)
        divvy=div((int)ms, 1000);
        
        req.tv_sec=divvy.quot;
-       req.tv_nsec=divvy.rem*1000;
+       req.tv_nsec=divvy.rem*1000000;
        
 again:
        ret=nanosleep(&req, &rem);
        if(ret==-1) {
                /* Sleep interrupted with rem time remaining */
 #ifdef DEBUG
-               guint32 rems=rem.tv_sec*1000 + rem.tv_nsec/1000;
+               guint32 rems=rem.tv_sec*1000 + rem.tv_nsec/1000000;
                
                g_message(G_GNUC_PRETTY_FUNCTION ": Still got %d ms to go",
                          rems);
@@ -622,3 +706,13 @@ again:
                goto again;
        }
 }
+
+/* FIXME: implement alertable */
+void SleepEx(guint32 ms, gboolean alertable)
+{
+       if(alertable==TRUE) {
+               g_warning(G_GNUC_PRETTY_FUNCTION ": alertable not implemented");
+       }
+       
+       Sleep(ms);
+}