* configure.in (MONO_VERSION): Emit it into config.make. Don't build Consts.cs.
[mono.git] / mono / io-layer / threads.c
index 30e66ab3c6a2499764aae5d00372a6b4394a24dc..989c6e80eeb1cd81a9ec54916c4eb3ec240fc17c 100644 (file)
@@ -8,10 +8,6 @@
  */
 
 #include <config.h>
-#if HAVE_BOEHM_GC
-#include <mono/os/gc_wrapper.h>
-#include "mono/utils/mono-hash.h"
-#endif
 #include <stdio.h>
 #include <glib.h>
 #include <string.h>
@@ -20,6 +16,8 @@
 #include <sched.h>
 #include <sys/time.h>
 #include <errno.h>
+#include <sys/types.h>
+#include <unistd.h>
 
 #include <mono/io-layer/wapi.h>
 #include <mono/io-layer/wapi-private.h>
@@ -29,6 +27,7 @@
 #include <mono/io-layer/mono-mutex.h>
 #include <mono/io-layer/thread-private.h>
 #include <mono/io-layer/mono-spinlock.h>
+#include <mono/io-layer/mutex-private.h>
 
 #if HAVE_VALGRIND_MEMCHECK_H
 #include <valgrind/memcheck.h>
@@ -45,16 +44,11 @@ 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;
 
-#if HAVE_BOEHM_GC
-static MonoGHashTable *tls_gc_hash = NULL;
-#endif
-
-static void thread_close_private (gpointer handle);
-static void thread_own (gpointer handle);
+static void thread_close (gpointer handle, gpointer data);
+static gboolean thread_own (gpointer handle);
 
 struct _WapiHandleOps _wapi_thread_ops = {
-       NULL,                           /* close_shared */
-       thread_close_private,           /* close_private */
+       thread_close,                   /* close */
        NULL,                           /* signal */
        thread_own,                     /* own */
        NULL,                           /* is_owned */
@@ -76,81 +70,74 @@ static void thread_ops_init (void)
 #endif
 }
 
-static void thread_close_private (gpointer handle)
+static void thread_close (gpointer handle, gpointer data)
 {
-       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;
-       }
-       
+       struct _WapiHandle_thread *thread_handle = (struct _WapiHandle_thread *)data;
+
 #ifdef DEBUG
-       g_message(G_GNUC_PRETTY_FUNCTION
-                 ": closing thread handle %p with thread %p id %ld",
-                 handle, thread_handle->thread,
-                 thread_handle->thread->id);
+       g_message ("%s: closing thread handle %p", __func__, handle);
 #endif
 
-       if(thread_handle->thread!=NULL) {
-               _wapi_timed_thread_destroy (thread_handle->thread);
-       }
+       g_ptr_array_free (thread_handle->owned_mutexes, TRUE);
 }
 
-static void thread_own (gpointer handle)
+static gboolean thread_own (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;
-       }
+#ifdef DEBUG
+       g_message ("%s: owning thread handle %p", __func__, handle);
+#endif
 
-       if(thread_private_handle->joined==FALSE) {
-               _wapi_timed_thread_join (thread_private_handle->thread, NULL,
-                                        NULL);
-               thread_private_handle->joined=TRUE;
+       ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
+                                 (gpointer *)&thread_handle);
+       if (ok == FALSE) {
+               g_warning ("%s: error looking up thread handle %p", __func__,
+                          handle);
+               return(FALSE);
+       }
+       
+       if (thread_handle->joined == FALSE) {
+               _wapi_timed_thread_join (thread_handle->thread, NULL, NULL);
+               thread_handle->joined = TRUE;
        }
+
+       return(TRUE);
 }
 
 static void thread_exit(guint32 exitstatus, gpointer handle)
 {
        struct _WapiHandle_thread *thread_handle;
-       struct _WapiHandlePrivate_thread *thread_private_handle;
        gboolean ok;
        int thr_ret;
+       int i;
        
-       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_cleanup_push ((void(*)(void *))_wapi_handle_unlock_handle,
                              handle);
        thr_ret = _wapi_handle_lock_handle (handle);
        g_assert (thr_ret == 0);
+       
+       ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
+                                 (gpointer *)&thread_handle);
+       if (ok == FALSE) {
+               g_warning ("%s: error looking up thread handle %p", __func__,
+                          handle);
+               return;
+       }
+       
+       for (i = 0; i < thread_handle->owned_mutexes->len; i++) {
+               _wapi_mutex_abandon (g_ptr_array_index (thread_handle->owned_mutexes, i), getpid (), thread_handle->thread->id);
+       }
 
 #ifdef DEBUG
-       g_message (G_GNUC_PRETTY_FUNCTION
-                  ": Recording thread handle %p exit status", handle);
+       g_message ("%s: Recording thread handle %p exit status", __func__,
+                  handle);
 #endif
        
-       thread_handle->exitstatus=exitstatus;
-       thread_handle->state=THREAD_STATE_EXITED;
+       thread_handle->exitstatus = exitstatus;
+       thread_handle->state = THREAD_STATE_EXITED;
+
        _wapi_handle_set_signal_state (handle, TRUE, TRUE);
 
        thr_ret = _wapi_handle_unlock_handle (handle);
@@ -158,9 +145,8 @@ static void thread_exit(guint32 exitstatus, gpointer handle)
        pthread_cleanup_pop (0);
        
 #ifdef DEBUG
-       g_message(G_GNUC_PRETTY_FUNCTION
-                 ": Recording thread handle %p id %ld status as %d",
-                 handle, thread_private_handle->thread->id, exitstatus);
+       g_message("%s: Recording thread handle %p id %ld status as %d",
+                 __func__, handle, thread_handle->thread->id, exitstatus);
 #endif
 
        /* Remove this thread from the hash */
@@ -169,7 +155,7 @@ static void thread_exit(guint32 exitstatus, gpointer handle)
        thr_ret = mono_mutex_lock(&thread_hash_mutex);
        g_assert (thr_ret == 0);
        
-       g_hash_table_remove(thread_hash, &thread_private_handle->thread->id);
+       g_hash_table_remove (thread_hash, (gpointer)(thread_handle->thread->id));
 
        thr_ret = mono_mutex_unlock(&thread_hash_mutex);
        g_assert (thr_ret == 0);
@@ -181,7 +167,7 @@ static void thread_exit(guint32 exitstatus, gpointer handle)
 
 static void thread_hash_init(void)
 {
-       thread_hash=g_hash_table_new(g_int_hash, g_int_equal);
+       thread_hash = g_hash_table_new (NULL, NULL);
 }
 
 /**
@@ -194,7 +180,9 @@ static void thread_hash_init(void)
  * @create: If 0, the new thread is ready to run immediately.  If
  * %CREATE_SUSPENDED, the new thread will be in the suspended state,
  * requiring a ResumeThread() call to continue running.
- * @tid: If non-NULL, the ID of the new thread is stored here.
+ * @tid: If non-NULL, the ID of the new thread is stored here.  NB
+ * this is defined as a DWORD (ie 32bit) in the MS API, but we need to
+ * cope with 64 bit IDs for s390x and amd64.
  *
  * Creates a new threading handle.
  *
@@ -202,10 +190,9 @@ static void thread_hash_init(void)
  */
 gpointer CreateThread(WapiSecurityAttributes *security G_GNUC_UNUSED, guint32 stacksize,
                      WapiThreadStart start, gpointer param, guint32 create,
-                     guint32 *tid) 
+                     gsize *tid) 
 {
-       struct _WapiHandle_thread *thread_handle;
-       struct _WapiHandlePrivate_thread *thread_private_handle;
+       struct _WapiHandle_thread thread_handle = {0}, *thread_handle_p;
        pthread_attr_t attr;
        gpointer handle;
        gboolean ok;
@@ -214,18 +201,23 @@ gpointer CreateThread(WapiSecurityAttributes *security G_GNUC_UNUSED, guint32 st
        int i, unrefs = 0;
        gpointer ct_ret = NULL;
        
-       mono_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) {
+       if (start == NULL) {
                return(NULL);
        }
+
+       thread_handle.state = THREAD_STATE_START;
+       thread_handle.owner_pid = getpid ();
+       thread_handle.owned_mutexes = g_ptr_array_new ();
        
-       handle=_wapi_handle_new (WAPI_HANDLE_THREAD);
-       if(handle==_WAPI_HANDLE_INVALID) {
-               g_warning (G_GNUC_PRETTY_FUNCTION
-                          ": error creating thread handle");
-               return(NULL);
+       handle = _wapi_handle_new (WAPI_HANDLE_THREAD, &thread_handle);
+       if (handle == _WAPI_HANDLE_INVALID) {
+               g_warning ("%s: error creating thread handle", __func__);
+               SetLastError (ERROR_GEN_FAILURE);
+               
+               return (NULL);
        }
 
        pthread_cleanup_push ((void(*)(void *))_wapi_handle_unlock_handle,
@@ -233,12 +225,13 @@ gpointer CreateThread(WapiSecurityAttributes *security G_GNUC_UNUSED, guint32 st
        thr_ret = _wapi_handle_lock_handle (handle);
        g_assert (thr_ret == 0);
        
-       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);
+       ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
+                                 (gpointer *)&thread_handle_p);
+       if (ok == FALSE) {
+               g_warning ("%s: error looking up thread handle %p", __func__,
+                          handle);
+               SetLastError (ERROR_GEN_FAILURE);
+               
                goto cleanup;
        }
 
@@ -246,8 +239,6 @@ gpointer CreateThread(WapiSecurityAttributes *security G_GNUC_UNUSED, guint32 st
         * 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()
@@ -266,16 +257,20 @@ gpointer CreateThread(WapiSecurityAttributes *security G_GNUC_UNUSED, guint32 st
        g_assert (thr_ret == 0);
        
        /* defaults of 2Mb for 32bits and 4Mb for 64bits */
+       /* temporarily changed to use 1 MB: this allows more threads
+        * to be used, as well as using less virtual memory and so
+        * more is available for the GC heap.
+        */
        if (stacksize == 0){
 #if HAVE_VALGRIND_MEMCHECK_H
-               if (RUNNING_ON_VALGRIND)
+               if (RUNNING_ON_VALGRIND) {
                        stacksize = 1 << 20;
-               else
-                       stacksize = (SIZEOF_VOID_P / 2) * 1024 * 1024;
+               } else {
+                       stacksize = (SIZEOF_VOID_P / 4) * 1024 * 1024;
+               }
 #else
-               stacksize = (SIZEOF_VOID_P / 2) * 1024 * 1024;
+               stacksize = (SIZEOF_VOID_P / 4) * 1024 * 1024;
 #endif
-               
        }
 
 #ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE
@@ -283,35 +278,41 @@ gpointer CreateThread(WapiSecurityAttributes *security G_GNUC_UNUSED, guint32 st
        g_assert (thr_ret == 0);
 #endif
 
-       ret=_wapi_timed_thread_create(&thread_private_handle->thread, &attr,
-                                     create, start, thread_exit, param,
-                                     handle);
-       if(ret!=0) {
+       ret = _wapi_timed_thread_create (&thread_handle_p->thread, &attr,
+                                        create, start, thread_exit, param,
+                                        handle);
+       if (ret != 0) {
 #ifdef DEBUG
-               g_message(G_GNUC_PRETTY_FUNCTION ": Thread create error: %s",
-                         strerror(ret));
+               g_message ("%s: Thread create error: %s", __func__,
+                          strerror(ret));
 #endif
+
                /* Two, because of the reference we took above */
                unrefs = 2;
+               
                goto thread_hash_cleanup;
        }
        ct_ret = handle;
        
-       g_hash_table_insert(thread_hash, &thread_private_handle->thread->id,
-                           handle);
+       g_hash_table_insert (thread_hash,
+                            (gpointer)(thread_handle_p->thread->id),
+                            handle);
        
 #ifdef DEBUG
-       g_message(G_GNUC_PRETTY_FUNCTION
-                 ": Started thread handle %p thread %p ID %ld", handle,
-                 thread_private_handle->thread,
-                 thread_private_handle->thread->id);
+       g_message("%s: Started thread handle %p thread %p ID %ld", __func__,
+                 handle, thread_handle_p->thread,
+                 thread_handle_p->thread->id);
 #endif
        
-       if(tid!=NULL) {
+       if (tid != NULL) {
 #ifdef PTHREAD_POINTER_ID
-               *tid=GPOINTER_TO_UINT(thread_private_handle->thread->id);
+               /* Don't use GPOINTER_TO_UINT here, it can't cope with
+                * sizeof(void *) > sizeof(uint) when a cast to uint
+                * would overflow
+                */
+               *tid = (gsize)(thread_handle_p->thread->id);
 #else
-               *tid=thread_private_handle->thread->id;
+               *tid = thread_handle_p->thread->id;
 #endif
        }
 
@@ -319,7 +320,7 @@ thread_hash_cleanup:
        thr_ret = mono_mutex_unlock (&thread_hash_mutex);
        g_assert (thr_ret == 0);
        pthread_cleanup_pop (0);
-       
+
 cleanup:
        thr_ret = _wapi_handle_unlock_handle (handle);
        g_assert (thr_ret == 0);
@@ -331,39 +332,50 @@ cleanup:
        for (i = 0; i < unrefs; i++) {
                _wapi_handle_unref (handle);
        }
-
+       
        return(ct_ret);
 }
 
-gpointer OpenThread (guint32 access G_GNUC_UNUSED, gboolean inherit G_GNUC_UNUSED, guint32 tid)
+gpointer _wapi_thread_handle_from_id (pthread_t tid)
 {
        gpointer ret=NULL;
        int thr_ret;
-       
-       mono_once(&thread_hash_once, thread_hash_init);
-       mono_once (&thread_ops_once, thread_ops_init);
-       
-#ifdef DEBUG
-       g_message (G_GNUC_PRETTY_FUNCTION ": looking up thread %d", tid);
-#endif
 
        pthread_cleanup_push ((void(*)(void *))mono_mutex_unlock_in_cleanup,
                              (void *)&thread_hash_mutex);
        thr_ret = mono_mutex_lock(&thread_hash_mutex);
        g_assert (thr_ret == 0);
        
-       ret=g_hash_table_lookup(thread_hash, &tid);
+       ret = g_hash_table_lookup (thread_hash, (gpointer)(tid));
 
        thr_ret = mono_mutex_unlock(&thread_hash_mutex);
        g_assert (thr_ret == 0);
        pthread_cleanup_pop (0);
+
+       return(ret);
+}
+
+/* NB tid is 32bit in MS API, but we need 64bit on amd64 and s390x
+ * (and probably others)
+ */
+gpointer OpenThread (guint32 access G_GNUC_UNUSED, gboolean inherit G_GNUC_UNUSED, gsize tid)
+{
+       gpointer ret=NULL;
        
+       mono_once (&thread_hash_once, thread_hash_init);
+       mono_once (&thread_ops_once, thread_ops_init);
+       
+#ifdef DEBUG
+       g_message ("%s: looking up thread %"G_GSIZE_FORMAT, __func__, tid);
+#endif
+
+       ret = _wapi_thread_handle_from_id ((pthread_t)tid);
        if(ret!=NULL) {
                _wapi_handle_ref (ret);
        }
        
 #ifdef DEBUG
-       g_message (G_GNUC_PRETTY_FUNCTION ": returning thread handle %p", ret);
+       g_message ("%s: returning thread handle %p", __func__, ret);
 #endif
        
        return(ret);
@@ -396,43 +408,39 @@ void ExitThread(guint32 exitcode)
 gboolean GetExitCodeThread(gpointer handle, guint32 *exitcode)
 {
        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);
+       ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
+                                 (gpointer *)&thread_handle);
+       if (ok == FALSE) {
+               g_warning ("%s: error looking up thread handle %p", __func__,
+                          handle);
+               return (FALSE);
        }
        
 #ifdef DEBUG
-       g_message(G_GNUC_PRETTY_FUNCTION
-                 ": Finding exit status for thread handle %p id %ld", handle,
-                 thread_private_handle->thread->id);
+       g_message ("%s: Finding exit status for thread handle %p id %ld",
+                  __func__, handle, thread_handle->thread->id);
 #endif
 
-       if(exitcode==NULL) {
+       if (exitcode == NULL) {
 #ifdef DEBUG
-               g_message(G_GNUC_PRETTY_FUNCTION
-                         ": Nowhere to store exit code");
+               g_message ("%s: Nowhere to store exit code", __func__);
 #endif
                return(FALSE);
        }
        
-       if(thread_handle->state!=THREAD_STATE_EXITED) {
+       if (thread_handle->state != THREAD_STATE_EXITED) {
 #ifdef DEBUG
-               g_message(G_GNUC_PRETTY_FUNCTION
-                         ": Thread still active (state %d, exited is %d)",
-                         thread_handle->state, THREAD_STATE_EXITED);
+               g_message ("%s: Thread still active (state %d, exited is %d)",
+                          __func__, thread_handle->state,
+                          THREAD_STATE_EXITED);
 #endif
-               *exitcode=STILL_ACTIVE;
+               *exitcode = STILL_ACTIVE;
                return(TRUE);
        }
        
-       *exitcode=thread_handle->exitstatus;
+       *exitcode = thread_handle->exitstatus;
        
        return(TRUE);
 }
@@ -443,23 +451,29 @@ gboolean GetExitCodeThread(gpointer handle, guint32 *exitcode)
  * Looks up the thread ID of the current thread.  This ID can be
  * passed to OpenThread() to create a new handle on this thread.
  *
- * Return value: the thread ID.
+ * Return value: the thread ID.  NB this is defined as DWORD (ie 32
+ * bit) in the MS API, but we need to cope with 64 bit IDs for s390x
+ * and amd64.  This doesn't really break the API, it just embraces and
+ * extends it on 64bit platforms :)
  */
-guint32 GetCurrentThreadId(void)
+gsize GetCurrentThreadId(void)
 {
-       pthread_t tid=pthread_self();
+       pthread_t tid = pthread_self();
        
 #ifdef PTHREAD_POINTER_ID
-       return(GPOINTER_TO_UINT(tid));
+       /* Don't use GPOINTER_TO_UINT here, it can't cope with
+        * sizeof(void *) > sizeof(uint) when a cast to uint would
+        * overflow
+        */
+       return((gsize)tid);
 #else
        return(tid);
 #endif
 }
 
-static gpointer thread_attach(guint32 *tid)
+static gpointer thread_attach(gsize *tid)
 {
-       struct _WapiHandle_thread *thread_handle;
-       struct _WapiHandlePrivate_thread *thread_private_handle;
+       struct _WapiHandle_thread thread_handle = {0}, *thread_handle_p;
        gpointer handle;
        gboolean ok;
        int ret;
@@ -467,27 +481,33 @@ static gpointer thread_attach(guint32 *tid)
        int i, unrefs = 0;
        gpointer ta_ret = NULL;
        
-       mono_once(&thread_hash_once, thread_hash_init);
+       mono_once (&thread_hash_once, thread_hash_init);
        mono_once (&thread_ops_once, thread_ops_init);
 
-       handle=_wapi_handle_new (WAPI_HANDLE_THREAD);
-       if(handle==_WAPI_HANDLE_INVALID) {
-               g_warning (G_GNUC_PRETTY_FUNCTION
-                          ": error creating thread handle");
-               return(NULL);
+       thread_handle.state = THREAD_STATE_START;
+       thread_handle.owner_pid = getpid ();
+       thread_handle.owned_mutexes = g_ptr_array_new ();
+
+       handle = _wapi_handle_new (WAPI_HANDLE_THREAD, &thread_handle);
+       if (handle == _WAPI_HANDLE_INVALID) {
+               g_warning ("%s: error creating thread handle", __func__);
+               
+               SetLastError (ERROR_GEN_FAILURE);
+               return (NULL);
        }
 
        pthread_cleanup_push ((void(*)(void *))_wapi_handle_unlock_handle,
                              handle);
        thr_ret = _wapi_handle_lock_handle (handle);
        g_assert (thr_ret == 0);
-
-       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);
+       
+       ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
+                                 (gpointer *)&thread_handle_p);
+       if (ok == FALSE) {
+               g_warning ("%s: error looking up thread handle %p", __func__,
+                          handle);
+               
+               SetLastError (ERROR_GEN_FAILURE);
                goto cleanup;
        }
 
@@ -495,9 +515,7 @@ static gpointer thread_attach(guint32 *tid)
         * 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()
         */
@@ -506,35 +524,40 @@ static gpointer thread_attach(guint32 *tid)
        thr_ret = mono_mutex_lock(&thread_hash_mutex);
        g_assert (thr_ret == 0);
 
-       ret=_wapi_timed_thread_attach(&thread_private_handle->thread,
-                                     thread_exit, handle);
-       if(ret!=0) {
+       ret = _wapi_timed_thread_attach (&thread_handle_p->thread, thread_exit,
+                                        handle);
+       if (ret != 0) {
 #ifdef DEBUG
-               g_message(G_GNUC_PRETTY_FUNCTION ": Thread attach error: %s",
-                         strerror(ret));
+               g_message ("%s: Thread attach error: %s", __func__,
+                          strerror(ret));
 #endif
+
                /* Two, because of the reference we took above */
                unrefs = 2;
-
+               
                goto thread_hash_cleanup;
        }
        ta_ret = handle;
        
-       g_hash_table_insert(thread_hash, &thread_private_handle->thread->id,
-                           handle);
+       g_hash_table_insert (thread_hash,
+                            (gpointer)(thread_handle_p->thread->id),
+                            handle);
 
 #ifdef DEBUG
-       g_message(G_GNUC_PRETTY_FUNCTION
-                 ": Attached thread handle %p thread %p ID %ld", handle,
-                 thread_private_handle->thread,
-                 thread_private_handle->thread->id);
+       g_message("%s: Attached thread handle %p thread %p ID %ld", __func__,
+                 handle, thread_handle_p->thread,
+                 thread_handle_p->thread->id);
 #endif
 
-       if(tid!=NULL) {
+       if (tid != NULL) {
 #ifdef PTHREAD_POINTER_ID
-               *tid=GPOINTER_TO_UINT(thread_private_handle->thread->id);
+               /* Don't use GPOINTER_TO_UINT here, it can't cope with
+                * sizeof(void *) > sizeof(uint) when a cast to uint
+                * would overflow
+                */
+               *tid = (gsize)(thread_handle_p->thread->id);
 #else
-               *tid=thread_private_handle->thread->id;
+               *tid = thread_handle_p->thread->id;
 #endif
        }
 
@@ -542,12 +565,12 @@ thread_hash_cleanup:
        thr_ret = mono_mutex_unlock (&thread_hash_mutex);
        g_assert (thr_ret == 0);
        pthread_cleanup_pop (0);
-       
+
 cleanup:
        thr_ret = _wapi_handle_unlock_handle (handle);
        g_assert (thr_ret == 0);
        pthread_cleanup_pop (0);
-
+       
        /* Must not call _wapi_handle_unref() with the handle already
         * locked
         */
@@ -572,25 +595,14 @@ cleanup:
 gpointer GetCurrentThread(void)
 {
        gpointer ret=NULL;
-       guint32 tid;
-       int thr_ret;
+       gsize tid;
        
        mono_once(&thread_hash_once, thread_hash_init);
        mono_once (&thread_ops_once, thread_ops_init);
        
-       tid=GetCurrentThreadId();
-       
-       pthread_cleanup_push ((void(*)(void *))mono_mutex_unlock_in_cleanup,
-                             (void *)&thread_hash_mutex);
-       thr_ret = mono_mutex_lock(&thread_hash_mutex);
-       g_assert (thr_ret == 0);
-
-       ret=g_hash_table_lookup(thread_hash, &tid);
-
-       thr_ret = mono_mutex_unlock(&thread_hash_mutex);
-       g_assert (thr_ret == 0);
-       pthread_cleanup_pop (0);
+       tid = GetCurrentThreadId();
        
+       ret = _wapi_thread_handle_from_id ((pthread_t)tid);
        if (!ret) {
                ret = thread_attach (NULL);
        }
@@ -610,29 +622,32 @@ gpointer GetCurrentThread(void)
 guint32 ResumeThread(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);
+       ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
+                                 (gpointer *)&thread_handle);
+       if (ok == FALSE) {
+               g_warning ("%s: error looking up thread handle %p", __func__,
+                          handle);
+               
+               return (0xFFFFFFFF);
+       }
+       
+       if (thread_handle->thread == NULL) {
                return(0xFFFFFFFF);
        }
 
 #ifdef WITH_INCLUDED_LIBGC
-       if (thread_private_handle->thread->suspend_count <= 1)
-               _wapi_timed_thread_resume (thread_private_handle->thread);
+       if (thread_handle->thread->suspend_count <= 1)
+               _wapi_timed_thread_resume (thread_handle->thread);
        
-       return --thread_private_handle->thread->suspend_count;
+       return (--thread_handle->thread->suspend_count));
 #else
        /* This is still a kludge that only copes with starting a
         * thread that was suspended on create, so don't bother with
         * the suspend count crap yet
         */
-       _wapi_timed_thread_resume (thread_private_handle->thread);
+       _wapi_timed_thread_resume (thread_handle->thread);
        return(0xFFFFFFFF);
 #endif
 }
@@ -650,30 +665,36 @@ guint32 SuspendThread(gpointer handle)
 {
 #ifdef WITH_INCLUDED_LIBGC
        struct _WapiHandle_thread *thread_handle;
-       struct _WapiHandlePrivate_thread *thread_private_handle;
        gpointer current;
        gboolean ok;
 
        current = GetCurrentThread ();
-       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);
+       ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
+                                 (gpointer *)&thread_handle);
+       if (ok == FALSE) {
+               g_warning ("%s: error looking up thread handle %p", __func__,
+                          handle);
+               return (0xFFFFFFFF);
+       }
+       
+       if (thread_handle->thread == NULL) {
                return(0xFFFFFFFF);
        }
 
-       if (!thread_private_handle->thread->suspend_count) {
+       if (!thread_handle->thread->suspend_count) {
                if (handle == current)
-                       _wapi_timed_thread_suspend (thread_private_handle->thread);
+                       _wapi_timed_thread_suspend (thread_handle->thread);
                else {
-                       pthread_kill (thread_private_handle->thread->id, SIGPWR);
-                       MONO_SEM_WAIT (&thread_private_handle->thread->suspended_sem);
+                       pthread_kill (thread_handle->thread->id, SIGPWR);
+                       while (MONO_SEM_WAIT (&thread_handle->thread->suspended_sem) != 0) {
+                               if (errno != EINTR) {
+                                       return(0xFFFFFFFF);
+                               }
+                       }
                }
        }
 
-       return thread_private_handle->thread->suspend_count++;
+       return (thread_handle->thread->suspend_count++);
 #else
        return(0xFFFFFFFF);
 #endif
@@ -693,6 +714,12 @@ static pthread_key_t TLS_keys[TLS_MINIMUM_AVAILABLE];
 static gboolean TLS_used[TLS_MINIMUM_AVAILABLE]={FALSE};
 static guint32 TLS_spinlock=0;
 
+guint32
+mono_pthread_key_for_tls (guint32 idx)
+{
+       return (guint32)TLS_keys [idx];
+}
+
 /**
  * TlsAlloc:
  *
@@ -719,8 +746,7 @@ guint32 TlsAlloc(void)
                        MONO_SPIN_UNLOCK (TLS_spinlock);
        
 #ifdef TLS_DEBUG
-                       g_message (G_GNUC_PRETTY_FUNCTION ": returning key %d",
-                                  i);
+                       g_message ("%s: returning key %d", __func__, i);
 #endif
                        
                        return(i);
@@ -730,7 +756,7 @@ guint32 TlsAlloc(void)
        MONO_SPIN_UNLOCK (TLS_spinlock);
        
 #ifdef TLS_DEBUG
-       g_message (G_GNUC_PRETTY_FUNCTION ": out of indices");
+       g_message ("%s: out of indices", __func__);
 #endif
                        
        
@@ -753,7 +779,7 @@ gboolean TlsFree(guint32 idx)
        int thr_ret;
        
 #ifdef TLS_DEBUG
-       g_message (G_GNUC_PRETTY_FUNCTION ": freeing key %d", idx);
+       g_message ("%s: freeing key %d", __func__, idx);
 #endif
 
        MONO_SPIN_LOCK (TLS_spinlock);
@@ -768,10 +794,6 @@ gboolean TlsFree(guint32 idx)
        thr_ret = pthread_key_delete(TLS_keys[idx]);
        g_assert (thr_ret == 0);
        
-#if HAVE_BOEHM_GC
-       mono_g_hash_table_remove (tls_gc_hash, MAKE_GC_ID (idx));
-#endif
-
        MONO_SPIN_UNLOCK (TLS_spinlock);
        
        return(TRUE);
@@ -792,13 +814,13 @@ gpointer TlsGetValue(guint32 idx)
        gpointer ret;
        
 #ifdef TLS_DEBUG
-       g_message (G_GNUC_PRETTY_FUNCTION ": looking up key %d", idx);
+       g_message ("%s: looking up key %d", __func__, idx);
 #endif
        
        ret=pthread_getspecific(TLS_keys[idx]);
 
 #ifdef TLS_DEBUG
-       g_message (G_GNUC_PRETTY_FUNCTION ": returning %p", ret);
+       g_message ("%s: returning %p", __func__, ret);
 #endif
        
        return(ret);
@@ -818,15 +840,14 @@ gboolean TlsSetValue(guint32 idx, gpointer value)
        int ret;
 
 #ifdef TLS_DEBUG
-       g_message (G_GNUC_PRETTY_FUNCTION ": setting key %d to %p", idx,
-                  value);
+       g_message ("%s: setting key %d to %p", __func__, idx, value);
 #endif
        
        MONO_SPIN_LOCK (TLS_spinlock);
        
        if(TLS_used[idx]==FALSE) {
 #ifdef TLS_DEBUG
-               g_message (G_GNUC_PRETTY_FUNCTION ": key %d unused", idx);
+               g_message ("%s: key %d unused", __func__, idx);
 #endif
 
                MONO_SPIN_UNLOCK (TLS_spinlock);
@@ -837,8 +858,8 @@ gboolean TlsSetValue(guint32 idx, gpointer value)
        ret=pthread_setspecific(TLS_keys[idx], value);
        if(ret!=0) {
 #ifdef TLS_DEBUG
-               g_message (G_GNUC_PRETTY_FUNCTION
-                          ": pthread_setspecific error: %s", strerror (ret));
+               g_message ("%s: pthread_setspecific error: %s", __func__,
+                          strerror (ret));
 #endif
 
                MONO_SPIN_UNLOCK (TLS_spinlock);
@@ -846,38 +867,42 @@ gboolean TlsSetValue(guint32 idx, gpointer value)
                return(FALSE);
        }
        
-#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
-
        MONO_SPIN_UNLOCK (TLS_spinlock);
        
        return(TRUE);
 }
 
 /**
- * Sleep:
+ * SleepEx:
  * @ms: The time in milliseconds to suspend for
+ * @alertable: if TRUE, the wait can be interrupted by an APC call
  *
  * Suspends execution of the current thread for @ms milliseconds.  A
  * value of zero causes the thread to relinquish its time slice.  A
  * value of %INFINITE causes an infinite delay.
  */
-void Sleep(guint32 ms)
+guint32 SleepEx(guint32 ms, gboolean alertable)
 {
        struct timespec req, rem;
        int ms_quot, ms_rem;
        int ret;
+       gpointer current_thread = NULL;
        
 #ifdef DEBUG
-       g_message(G_GNUC_PRETTY_FUNCTION ": Sleeping for %d ms", ms);
+       g_message("%s: Sleeping for %d ms", __func__, ms);
 #endif
 
+       if (alertable) {
+               current_thread = GetCurrentThread ();
+               if (_wapi_thread_apc_pending (current_thread)) {
+                       _wapi_thread_dispatch_apc_queue (current_thread);
+                       return WAIT_IO_COMPLETION;
+               }
+       }
+       
        if(ms==0) {
                sched_yield();
-               return;
+               return 0;
        }
        
        /* FIXME: check for INFINITE and sleep forever */
@@ -889,105 +914,132 @@ void Sleep(guint32 ms)
        
 again:
        ret=nanosleep(&req, &rem);
+
+       if (alertable && _wapi_thread_apc_pending (current_thread)) {
+               _wapi_thread_dispatch_apc_queue (current_thread);
+               return WAIT_IO_COMPLETION;
+       }
+       
        if(ret==-1) {
                /* Sleep interrupted with rem time remaining */
 #ifdef DEBUG
                guint32 rems=rem.tv_sec*1000 + rem.tv_nsec/1000000;
                
-               g_message(G_GNUC_PRETTY_FUNCTION ": Still got %d ms to go",
-                         rems);
+               g_message("%s: Still got %d ms to go", __func__, rems);
 #endif
                req=rem;
                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);
+       return 0;
 }
 
-gboolean
-BindIoCompletionCallback (gpointer handle,
-                         WapiOverlappedCB callback,
-                         guint64 flags)
+void Sleep(guint32 ms)
 {
-       WapiHandleType type;
-       
-       type = _wapi_handle_type (handle);
-       if (type == WAPI_HANDLE_FILE || type == WAPI_HANDLE_PIPE)
-               return _wapi_io_add_callback (handle, callback, flags);
-
-       SetLastError (ERROR_NOT_SUPPORTED);
-       return FALSE;
+       SleepEx(ms, FALSE);
 }
 
 guint32 QueueUserAPC (WapiApcProc apc_callback, gpointer handle, 
                                        gpointer param)
 {
        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(0);
+       
+       ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
+                                 (gpointer *)&thread_handle);
+       if (ok == FALSE) {
+               g_warning ("%s: error looking up thread handle %p", __func__,
+                          handle);
+               return (0);
        }
-
-       _wapi_timed_thread_queue_apc (thread_private_handle->thread, 
-                                                       apc_callback, param);
+       
+       _wapi_timed_thread_queue_apc (thread_handle->thread, apc_callback,
+                                     param);
        return(1);
 }
 
 gboolean _wapi_thread_cur_apc_pending (void)
 {
-       return _wapi_thread_apc_pending (GetCurrentThread ());
+       return(_wapi_thread_apc_pending (GetCurrentThread ()));
 }
 
 gboolean _wapi_thread_apc_pending (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);
+       if (ok == FALSE) {
+               g_warning ("%s: error looking up thread handle %p", __func__,
+                          handle);
+               return (FALSE);
+       }
+       
+       return(_wapi_timed_thread_apc_pending (thread_handle->thread));
+}
 
-       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);
+gboolean _wapi_thread_dispatch_apc_queue (gpointer handle)
+{
+       struct _WapiHandle_thread *thread_handle;
+       gboolean ok;
+       
+       ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
+                                 (gpointer *)&thread_handle);
+       if (ok == FALSE) {
+               g_warning ("%s: error looking up thread handle %p", __func__,
+                          handle);
+               return (0);
+       }
+       
+       _wapi_timed_thread_dispatch_apc_queue (thread_handle->thread);
+       return(1);
+}
+
+void _wapi_thread_own_mutex (pthread_t tid, gpointer mutex)
+{
+       struct _WapiHandle_thread *thread_handle;
+       gboolean ok;
+       gpointer thread;
+
+       thread = _wapi_thread_handle_from_id (tid);
+       if (thread == NULL) {
+               g_warning ("%s: error looking up thread by ID", __func__);
+               return;
        }
 
-       return _wapi_timed_thread_apc_pending (thread_private_handle->thread);
+       ok = _wapi_lookup_handle (thread, WAPI_HANDLE_THREAD,
+                                 (gpointer *)&thread_handle);
+       if (ok == FALSE) {
+               g_warning ("%s: error looking up thread handle %p", __func__,
+                          thread);
+               return;
+       }
+
+       g_ptr_array_add (thread_handle->owned_mutexes, mutex);
 }
 
-gboolean _wapi_thread_dispatch_apc_queue (gpointer handle)
+void _wapi_thread_disown_mutex (pthread_t tid, gpointer mutex)
 {
        struct _WapiHandle_thread *thread_handle;
-       struct _WapiHandlePrivate_thread *thread_private_handle;
        gboolean ok;
+       gpointer thread;
+
+       thread = _wapi_thread_handle_from_id (tid);
+       if (thread == NULL) {
+               g_warning ("%s: error looking up thread by ID", __func__);
+               return;
+       }
 
-       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(0);
+       ok = _wapi_lookup_handle (thread, WAPI_HANDLE_THREAD,
+                                 (gpointer *)&thread_handle);
+       if (ok == FALSE) {
+               g_warning ("%s: error looking up thread handle %p", __func__,
+                          thread);
+               return;
        }
 
-       _wapi_timed_thread_dispatch_apc_queue (thread_private_handle->thread);
-       return(1);
+       g_ptr_array_remove (thread_handle->owned_mutexes, mutex);
 }
 
 
@@ -997,26 +1049,24 @@ gboolean _wapi_thread_dispatch_apc_queue (gpointer handle)
 static void GC_suspend_handler (int sig)
 {
        struct _WapiHandle_thread *thread_handle;
-       struct _WapiHandlePrivate_thread *thread_private_handle;
        gpointer handle;
        gboolean ok;
 
        handle = GetCurrentThread ();
-       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);
+       ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
+                                 (gpointer *)&thread_handle);
+       if (ok == FALSE) {
+               g_warning ("%s: error looking up thread handle %p", __func__,
+                          handle);
                return;
        }
+       
+       thread_handle->thread->stack_ptr = &ok;
+       MONO_SEM_POST (&thread_handle->thread->suspended_sem);
 
-       thread_private_handle->thread->stack_ptr = &ok;
-       MONO_SEM_POST (&thread_private_handle->thread->suspended_sem);
-
-       _wapi_timed_thread_suspend (thread_private_handle->thread);
+       _wapi_timed_thread_suspend (thread_handle->thread);
 
-       thread_private_handle->thread->stack_ptr = NULL;
+       thread_handle->thread->stack_ptr = NULL;
 }
 
 static void gc_init (void)
@@ -1030,19 +1080,17 @@ static void gc_init (void)
 void mono_wapi_push_thread_stack (gpointer handle, gpointer stack_ptr)
 {
        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);
+       
+       ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
+                                 (gpointer *)&thread_handle);
+       if (ok == FALSE) {
+               g_warning ("%s: error looking up thread handle %p", __func__,
+                          handle);
                return;
        }
-
-       GC_push_all_stack (thread_private_handle->thread->stack_ptr, stack_ptr);
+       
+       GC_push_all_stack (thread_handle->thread->stack_ptr, stack_ptr);
 }
 
 #endif /* WITH_INCLUDED_LIBGC */