2009-04-02 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / io-layer / threads.c
index 4ae22f0b16d6a5e1368f2243b44cdf420dd29f4d..29cf8561c243093f411b85c432fa47292b5ef050 100644 (file)
@@ -4,13 +4,14 @@
  * Author:
  *     Dick Porter (dick@ximian.com)
  *
- * (C) 2002 Ximian, Inc.
+ * (C) 2002-2006 Ximian, Inc.
  */
 
 #include <config.h>
 #include <stdio.h>
 #include <glib.h>
 #include <string.h>
+#include <mono/utils/gc_wrapper.h>
 #include <pthread.h>
 #include <signal.h>
 #include <sched.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 <mono/io-layer/mutex-private.h>
+#include <mono/io-layer/atomic.h>
 
-#if HAVE_VALGRIND_MEMCHECK_H
+#ifdef HAVE_VALGRIND_MEMCHECK_H
 #include <valgrind/memcheck.h>
 #endif
 
 #undef DEBUG
 #undef TLS_DEBUG
 
+#if 0
+#define WAIT_DEBUG(code) do { code } while (0)
+#else
+#define WAIT_DEBUG(code) do { } while (0)
+#endif
 
 /* 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 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 pthread_key_t thread_hash_key;
 
-static gboolean thread_own (gpointer handle);
+/* This key is used with attached threads and a destructor to signal
+ * when attached threads exit, as they don't have the thread_exit()
+ * infrastructure
+ */
+static pthread_key_t thread_attached_key;
 
 struct _WapiHandleOps _wapi_thread_ops = {
-       NULL,                           /* close_shared */
+       NULL,                           /* close */
        NULL,                           /* signal */
-       thread_own,                     /* own */
+       NULL,                           /* own */
        NULL,                           /* is_owned */
+       NULL,                           /* special_wait */
+       NULL                            /* prewait */
 };
 
 static mono_once_t thread_ops_once=MONO_ONCE_INIT;
 
-#ifdef WITH_INCLUDED_LIBGC
-static void gc_init (void);
-#endif
-
 static void thread_ops_init (void)
 {
        _wapi_handle_register_capabilities (WAPI_HANDLE_THREAD,
                                            WAPI_HANDLE_CAP_WAIT);
+}
 
-#ifdef WITH_INCLUDED_LIBGC
-       gc_init ();
-#endif
+void _wapi_thread_cleanup (void)
+{
+       int ret;
+       
+       ret = pthread_key_delete (thread_hash_key);
+       g_assert (ret == 0);
+       
+       ret = pthread_key_delete (thread_attached_key);
+       g_assert (ret == 0);
 }
 
-static gboolean thread_own (gpointer handle)
+/* Called by thread_exit(), but maybe indirectly by
+ * mono_thread_manage() via mono_thread_signal_self() too
+ */
+static void _wapi_thread_abandon_mutexes (gpointer handle)
 {
-       struct _WapiHandleShared shared_handle;
        struct _WapiHandle_thread *thread_handle;
        gboolean ok;
+       int i;
+       pid_t pid = _wapi_getpid ();
+       pthread_t tid = pthread_self ();
        
 #ifdef DEBUG
-       g_message ("%s: owning thread handle %p", __func__, handle);
+       g_message ("%s: Thread %p abandoning held mutexes", __func__, handle);
 #endif
 
-       ok = _wapi_copy_handle (handle, WAPI_HANDLE_THREAD,
-                               &shared_handle);
+       if (handle == NULL) {
+               handle = _wapi_thread_handle_from_id (pthread_self ());
+               if (handle == NULL) {
+                       /* Something gone badly wrong... */
+                       return;
+               }
+       }
+       
+       ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
+                                 (gpointer *)&thread_handle);
        if (ok == FALSE) {
-               g_warning ("%s: error copying thread handle %p", __func__,
+               g_warning ("%s: error looking up thread handle %p", __func__,
                           handle);
-               return(FALSE);
+               return;
        }
-       thread_handle = &shared_handle.u.thread;
-
-       if (thread_handle->owner_pid != getpid()) {
-#ifdef DEBUG
-               g_message ("%s: can't join thread, %d not owner process %d",
-                          __func__, getpid(), thread_handle->owner_pid);
-#endif
-               /* FIXME: might need to return TRUE here so that other
-                * processes can WaitFor thread handles
-                */
-               return(FALSE);
+       
+       if (!pthread_equal (thread_handle->id, tid)) {
+               return;
        }
        
-       if (thread_handle->joined == FALSE) {
-               _wapi_timed_thread_join (thread_handle->thread, NULL, NULL);
-               thread_handle->joined = TRUE;
-
-               ok = _wapi_replace_handle (handle, WAPI_HANDLE_THREAD,
-                                          &shared_handle);
-               if (ok == FALSE) {
-                       SetLastError (ERROR_OUTOFMEMORY);
-                       return (FALSE);
-               }
+       for (i = 0; i < thread_handle->owned_mutexes->len; i++) {
+               gpointer mutex = g_ptr_array_index (thread_handle->owned_mutexes, i);
+               
+               _wapi_mutex_abandon (mutex, pid, tid);
+               _wapi_thread_disown_mutex (mutex);
        }
-
-       return(TRUE);
 }
 
-static void thread_exit(guint32 exitstatus, gpointer handle)
+void _wapi_thread_set_termination_details (gpointer handle,
+                                          guint32 exitstatus)
 {
-       struct _WapiHandleShared shared_handle;
        struct _WapiHandle_thread *thread_handle;
        gboolean ok;
        int thr_ret;
-
-       pthread_cleanup_push ((void(*)(void *))_wapi_handle_unlock_handle,
-                             handle);
-       thr_ret = _wapi_handle_lock_handle (handle);
-       g_assert (thr_ret == 0);
        
-       ok = _wapi_copy_handle (handle, WAPI_HANDLE_THREAD,
-                               &shared_handle);
-       if (ok == FALSE) {
-               g_warning ("%s: error copying thread handle %p", __func__,
-                          handle);
+       if (_wapi_handle_issignalled (handle) ||
+           _wapi_handle_type (handle) == WAPI_HANDLE_UNUSED) {
+               /* We must have already deliberately finished with
+                * this thread, so don't do any more now
+                */
                return;
        }
-       thread_handle = &shared_handle.u.thread;
-       
-       _wapi_mutex_check_abandoned (getpid (), thread_handle->thread->id);
 
 #ifdef DEBUG
-       g_message ("%s: Recording thread handle %p exit status", __func__,
-                  handle);
+       g_message ("%s: Thread %p terminating", __func__, handle);
 #endif
-       
-       thread_handle->exitstatus = exitstatus;
-       thread_handle->state = THREAD_STATE_EXITED;
 
-       ok = _wapi_replace_handle (handle, WAPI_HANDLE_THREAD, &shared_handle);
+       _wapi_thread_abandon_mutexes (handle);
+       
+       ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
+                                 (gpointer *)&thread_handle);
        if (ok == FALSE) {
-               SetLastError (ERROR_OUTOFMEMORY);
+               g_warning ("%s: error looking up thread handle %p", __func__,
+                          handle);
+
                return;
        }
        
-       _wapi_shared_handle_set_signal_state (handle, TRUE);
+       pthread_cleanup_push ((void(*)(void *))_wapi_handle_unlock_handle,
+                             handle);
+       thr_ret = _wapi_handle_lock_handle (handle);
+       g_assert (thr_ret == 0);
+       
+       thread_handle->exitstatus = exitstatus;
+       thread_handle->state = THREAD_STATE_EXITED;
+       MONO_SEM_DESTROY (&thread_handle->suspend_sem);
+       g_ptr_array_free (thread_handle->owned_mutexes, TRUE);
+
+       _wapi_handle_set_signal_state (handle, TRUE, TRUE);
 
        thr_ret = _wapi_handle_unlock_handle (handle);
        g_assert (thr_ret == 0);
@@ -159,28 +174,124 @@ static void thread_exit(guint32 exitstatus, gpointer handle)
        
 #ifdef DEBUG
        g_message("%s: Recording thread handle %p id %ld status as %d",
-                 __func__, handle, thread_handle->thread->id, exitstatus);
+                 __func__, handle, thread_handle->id, exitstatus);
 #endif
+       
+       /* The thread is no longer active, so unref it */
+       _wapi_handle_unref (handle);
+}
 
-       /* Remove this thread from the hash */
-       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);
+void _wapi_thread_signal_self (guint32 exitstatus)
+{
+       gpointer handle;
+       
+       handle = _wapi_thread_handle_from_id (pthread_self ());
+       if (handle == NULL) {
+               /* Something gone badly wrong... */
+               return;
+       }
        
-       g_hash_table_remove (thread_hash, GUINT_TO_POINTER (thread_handle->thread->id));
+       _wapi_thread_set_termination_details (handle, exitstatus);
+}
 
-       thr_ret = mono_mutex_unlock(&thread_hash_mutex);
-       g_assert (thr_ret == 0);
-       pthread_cleanup_pop (0);
+/* Called by the thread creation code as a thread is finishing up, and
+ * by ExitThread()
+*/
+static void thread_exit (guint32 exitstatus, gpointer handle) G_GNUC_NORETURN;
+static void thread_exit (guint32 exitstatus, gpointer handle)
+{
+       _wapi_thread_set_termination_details (handle, exitstatus);
+       
+       /* Call pthread_exit() to call destructors and really exit the
+        * thread
+        */
+       pthread_exit (NULL);
+}
 
-       /* The thread is no longer active, so unref it */
-       _wapi_handle_unref (handle);
+static void thread_attached_exit (gpointer handle)
+{
+       /* Drop the extra reference we take in thread_attach, now this
+        * thread is dead
+        */
+       
+       _wapi_thread_set_termination_details (handle, 0);
 }
 
 static void thread_hash_init(void)
 {
-       thread_hash = g_hash_table_new (NULL, NULL);
+       int thr_ret;
+       
+       thr_ret = pthread_key_create (&thread_hash_key, NULL);
+       g_assert (thr_ret == 0);
+
+       thr_ret = pthread_key_create (&thread_attached_key,
+                                     thread_attached_exit);
+       g_assert (thr_ret == 0);
+}
+
+static void _wapi_thread_suspend (struct _WapiHandle_thread *thread)
+{
+       g_assert (pthread_equal (thread->id, pthread_self ()));
+       
+       while (MONO_SEM_WAIT (&thread->suspend_sem) != 0 &&
+              errno == EINTR);
+}
+
+static void _wapi_thread_resume (struct _WapiHandle_thread *thread)
+{
+       MONO_SEM_POST (&thread->suspend_sem);
+}
+
+static void *thread_start_routine (gpointer args) G_GNUC_NORETURN;
+static void *thread_start_routine (gpointer args)
+{
+       struct _WapiHandle_thread *thread = (struct _WapiHandle_thread *)args;
+       int thr_ret;
+       
+       thr_ret = pthread_detach (pthread_self ());
+       g_assert (thr_ret == 0);
+
+       thr_ret = pthread_setspecific (thread_hash_key,
+                                      (void *)thread->handle);
+       if (thr_ret != 0) {
+               /* This is only supposed to happen when Mono is
+                  shutting down.  We cannot assert on it, though,
+                  because we must not depend on metadata, which is
+                  where the shutdown code is.
+
+                  This is a race condition which arises because
+                  pthreads don't allow creation of suspended threads.
+                  Once Mono is set to shut down no new thread is
+                  allowed to start, even though threads may still be
+                  created.  We emulate suspended threads in this
+                  function by calling _wapi_thread_suspend() below.
+
+                  So it can happen that even though Mono is already
+                  shutting down we still end up here, and at this
+                  point the thread_hash_key might already be
+                  destroyed. */
+               pthread_exit (NULL);
+       }
+
+       thread->id = pthread_self();
+
+#ifdef DEBUG
+       g_message ("%s: started thread id %ld", __func__, thread->id);
+#endif
+
+       if (thread->create_flags & CREATE_SUSPENDED) {
+               _wapi_thread_suspend (thread);
+       }
+       
+       thread_exit (thread->start_routine (thread->start_arg),
+                    thread->handle);
+
+#ifndef __GNUC__
+       /* Even though we tell gcc that this function doesn't return,
+        * other compilers won't see that.
+        */
+       return(NULL);
+#endif
 }
 
 /**
@@ -193,7 +304,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.
  *
@@ -201,15 +314,15 @@ 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 _WapiHandleShared shared_handle;
-       struct _WapiHandle_thread thread_handle = {0};
+       struct _WapiHandle_thread thread_handle = {0}, *thread_handle_p;
        pthread_attr_t attr;
        gpointer handle;
        gboolean ok;
        int ret;
        int thr_ret;
+       int i, unrefs = 0;
        gpointer ct_ret = NULL;
        
        mono_once (&thread_hash_once, thread_hash_init);
@@ -220,7 +333,38 @@ gpointer CreateThread(WapiSecurityAttributes *security G_GNUC_UNUSED, guint32 st
        }
 
        thread_handle.state = THREAD_STATE_START;
-       thread_handle.owner_pid = getpid();
+       thread_handle.owned_mutexes = g_ptr_array_new ();
+       thread_handle.create_flags = create;
+       thread_handle.start_routine = start;
+       thread_handle.start_arg = param;
+       
+       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_p);
+       if (ok == FALSE) {
+               g_warning ("%s: error looking up thread handle %p", __func__,
+                          handle);
+               SetLastError (ERROR_GEN_FAILURE);
+               
+               goto cleanup;
+       }
+
+       /* Hold a reference while the thread is active, because we use
+        * the handle to store thread exit information
+        */
+       _wapi_handle_ref (handle);
        
        /* Set a 2M stack size.  This is the default on Linux, but BSD
         * needs it.  (The original bug report from Martin Dvorak <md@9ll.cz>
@@ -231,9 +375,9 @@ 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.
+       /* 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
@@ -245,112 +389,154 @@ gpointer CreateThread(WapiSecurityAttributes *security G_GNUC_UNUSED, guint32 st
 #else
                stacksize = (SIZEOF_VOID_P / 4) * 1024 * 1024;
 #endif
-               
        }
 
 #ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE
        thr_ret = pthread_attr_setstacksize(&attr, stacksize);
        g_assert (thr_ret == 0);
 #endif
-       
-       /* Lock around the thread create, so that the new thread cant
-        * race us to look up the thread handle in GetCurrentThread()
-        */
-       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);
-       
-       handle = _wapi_handle_new (WAPI_HANDLE_THREAD, &thread_handle);
-       if (handle == _WAPI_HANDLE_INVALID) {
-               g_warning ("%s: error creating thread handle", __func__);
-               goto thread_hash_cleanup;
-       }
-       ct_ret = handle;
 
-       ret = _wapi_timed_thread_create (&thread_handle.thread, &attr,
-                                        create, start, thread_exit, param,
-                                        handle);
+       MONO_SEM_INIT (&thread_handle_p->suspend_sem, 0);
+       thread_handle_p->handle = handle;
+       
+       ret = pthread_create (&thread_handle_p->id, &attr,
+                             thread_start_routine, (void *)thread_handle_p);
        if (ret != 0) {
 #ifdef DEBUG
                g_message ("%s: Thread create error: %s", __func__,
                           strerror(ret));
 #endif
-               _wapi_handle_unref (handle);
-               goto thread_hash_cleanup;
-       }
 
-       /* Need to update the handle with the state info that
-        * _wapi_timed_thread_create created, after the initial handle
-        * info was copied into shared memory
-        */
-       ok = _wapi_copy_handle (handle, WAPI_HANDLE_THREAD, &shared_handle);
-       if (ok == FALSE) {
-               g_warning ("%s: error copying thread handle %p", __func__,
-                          handle);
-               _wapi_handle_unref (handle);
-               goto thread_hash_cleanup;
-       }
-       shared_handle.u.thread.thread = thread_handle.thread;
-       ok = _wapi_replace_handle (handle, WAPI_HANDLE_THREAD, &shared_handle);
-       if (ok == FALSE) {
-               SetLastError (ERROR_OUTOFMEMORY);
-               _wapi_handle_unref (handle);
-               goto thread_hash_cleanup;
+               /* Two, because of the reference we took above */
+               unrefs = 2;
+               
+               goto cleanup;
        }
-       
-       /* Hold a reference while the thread is active, because we use
-        * the handle to store thread exit information
-        */
-       _wapi_handle_ref (handle);
-       
-       g_hash_table_insert (thread_hash, GUINT_TO_POINTER (thread_handle.thread->id), handle);
+       ct_ret = handle;
        
 #ifdef DEBUG
-       g_message("%s: Started thread handle %p thread %p ID %ld", __func__,
-                 handle, thread_handle.thread, thread_handle.thread->id);
+       g_message("%s: Started thread handle %p ID %ld", __func__, handle,
+                 thread_handle_p->id);
 #endif
        
        if (tid != NULL) {
 #ifdef PTHREAD_POINTER_ID
-               *tid = GPOINTER_TO_UINT (thread_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->id);
 #else
-               *tid = thread_handle.thread->id;
+               *tid = thread_handle_p->id;
 #endif
        }
 
-thread_hash_cleanup:
-       thr_ret = mono_mutex_unlock (&thread_hash_mutex);
+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 shared handles
+        * already locked
+        */
+       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)
+/* The only time this function is called when tid != pthread_self ()
+ * is from OpenThread (), so we can fast-path most cases by just
+ * looking up the handle in TLS.  OpenThread () must cope with a NULL
+ * return and do a handle search in that case.
+ */
+gpointer _wapi_thread_handle_from_id (pthread_t tid)
 {
-       gpointer ret=NULL;
-       int thr_ret;
+       gpointer ret;
+
+       if (pthread_equal (tid, pthread_self ()) &&
+           (ret = pthread_getspecific (thread_hash_key)) != NULL) {
+               /* We know the handle */
+
+#ifdef DEBUG
+               g_message ("%s: Returning %p for self thread %ld from TLS",
+                          __func__, ret, tid);
+#endif
+               
+               return(ret);
+       }
        
-       mono_once(&thread_hash_once, thread_hash_init);
-       mono_once (&thread_ops_once, thread_ops_init);
+#ifdef DEBUG
+       g_message ("%s: Returning NULL for unknown or non-self thread %ld",
+                  __func__, tid);
+#endif
+               
+
+       return(NULL);
+}
+
+static gboolean find_thread_by_id (gpointer handle, gpointer user_data)
+{
+       pthread_t tid = (pthread_t)user_data;
+       struct _WapiHandle_thread *thread_handle;
+       gboolean ok;
        
+       /* Ignore threads that have already exited (ie they are signalled) */
+       if (_wapi_handle_issignalled (handle) == FALSE) {
+               ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
+                                         (gpointer *)&thread_handle);
+               if (ok == FALSE) {
+                       /* It's possible that the handle has vanished
+                        * during the _wapi_search_handle before it
+                        * gets here, so don't spam the console with
+                        * warnings.
+                        */
+                       return(FALSE);
+               }
+               
 #ifdef DEBUG
-       g_message ("%s: looking up thread %d", __func__, tid);
+               g_message ("%s: looking at thread %ld from process %d", __func__, thread_handle->id, 0);
 #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);
+               if (pthread_equal (thread_handle->id, tid)) {
+#ifdef DEBUG
+                       g_message ("%s: found the thread we are looking for",
+                                  __func__);
+#endif
+                       return(TRUE);
+               }
+       }
+       
+#ifdef DEBUG
+       g_message ("%s: not found %ld, returning FALSE", __func__, tid);
+#endif
        
-       ret = g_hash_table_lookup (thread_hash, GUINT_TO_POINTER (tid));
+       return(FALSE);
+}
 
-       thr_ret = mono_mutex_unlock(&thread_hash_mutex);
-       g_assert (thr_ret == 0);
-       pthread_cleanup_pop (0);
+/* 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);
        
-       if(ret!=NULL) {
+#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) {
+               /* We need to search for this thread */
+               ret = _wapi_search_handle (WAPI_HANDLE_THREAD, find_thread_by_id, (gpointer)tid, NULL, FALSE/*TRUE*/);  /* FIXME: have a proper look at this, me might not need to set search_shared = TRUE */
+       } else {
+               /* if _wapi_search_handle() returns a found handle, it
+                * refs it itself
+                */
                _wapi_handle_ref (ret);
        }
        
@@ -372,7 +558,14 @@ gpointer OpenThread (guint32 access G_GNUC_UNUSED, gboolean inherit G_GNUC_UNUSE
  */
 void ExitThread(guint32 exitcode)
 {
-       _wapi_timed_thread_exit(exitcode);
+       gpointer thread = _wapi_thread_handle_from_id (pthread_self ());
+       
+       if (thread != NULL) {
+               thread_exit(exitcode, thread);
+       } else {
+               /* Just blow this thread away */
+               pthread_exit (NULL);
+       }
 }
 
 /**
@@ -387,10 +580,20 @@ void ExitThread(guint32 exitcode)
  */
 gboolean GetExitCodeThread(gpointer handle, guint32 *exitcode)
 {
+       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 (FALSE);
+       }
+       
 #ifdef DEBUG
        g_message ("%s: Finding exit status for thread handle %p id %ld",
-                  __func__, handle,
-                  WAPI_SHARED_HANDLE_TYPED_DATA(handle, thread).thread->id);
+                  __func__, handle, thread_handle->id);
 #endif
 
        if (exitcode == NULL) {
@@ -400,18 +603,17 @@ gboolean GetExitCodeThread(gpointer handle, guint32 *exitcode)
                return(FALSE);
        }
        
-       if (WAPI_SHARED_HANDLE_TYPED_DATA(handle, thread).state != THREAD_STATE_EXITED) {
+       if (thread_handle->state != THREAD_STATE_EXITED) {
 #ifdef DEBUG
                g_message ("%s: Thread still active (state %d, exited is %d)",
-                          __func__,
-                          WAPI_SHARED_HANDLE_TYPED_DATA(handle, thread).state,
+                          __func__, thread_handle->state,
                           THREAD_STATE_EXITED);
 #endif
                *exitcode = STILL_ACTIVE;
                return(TRUE);
        }
        
-       *exitcode = WAPI_SHARED_HANDLE_TYPED_DATA(handle, thread).exitstatus;
+       *exitcode = thread_handle->exitstatus;
        
        return(TRUE);
 }
@@ -422,107 +624,120 @@ 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();
        
 #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 _WapiHandleShared shared_handle;
-       struct _WapiHandle_thread thread_handle = {0};
+       struct _WapiHandle_thread thread_handle = {0}, *thread_handle_p;
        gpointer handle;
        gboolean ok;
-       int ret;
        int thr_ret;
-       gpointer ta_ret = NULL;
        
        mono_once (&thread_hash_once, thread_hash_init);
        mono_once (&thread_ops_once, thread_ops_init);
 
        thread_handle.state = THREAD_STATE_START;
-       thread_handle.owner_pid = getpid();
-
-       /* Lock around the thread create, so that the new thread cant
-        * race us to look up the thread handle in GetCurrentThread()
-        */
-       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);
+       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__);
-               goto thread_hash_cleanup;
-       }
-       ta_ret = handle;
-
-       ret = _wapi_timed_thread_attach (&thread_handle.thread, thread_exit,
-                                        handle);
-       if (ret != 0) {
-#ifdef DEBUG
-               g_message ("%s: Thread attach error: %s", __func__,
-                          strerror(ret));
-#endif
-
-               _wapi_handle_unref (handle);
-               goto thread_hash_cleanup;
+               
+               SetLastError (ERROR_GEN_FAILURE);
+               return (NULL);
        }
 
-       /* Need to update the handle with the state info that
-        * _wapi_timed_thread_create created, after the initial handle
-        * info was copied into shared memory
-        */
-       ok = _wapi_copy_handle (handle, WAPI_HANDLE_THREAD, &shared_handle);
+       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_p);
        if (ok == FALSE) {
-               g_warning ("%s: error copying thread handle %p", __func__,
+               g_warning ("%s: error looking up thread handle %p", __func__,
                           handle);
-               _wapi_handle_unref (handle);
-               goto thread_hash_cleanup;
-       }
-       shared_handle.u.thread.thread = thread_handle.thread;
-       ok = _wapi_replace_handle (handle, WAPI_HANDLE_THREAD, &shared_handle);
-       if (ok == FALSE) {
-               SetLastError (ERROR_OUTOFMEMORY);
-               _wapi_handle_unref (handle);
-               goto thread_hash_cleanup;
+               
+               SetLastError (ERROR_GEN_FAILURE);
+               goto cleanup;
        }
 
        /* Hold a reference while the thread is active, because we use
         * the handle to store thread exit information
         */
        _wapi_handle_ref (handle);
-       
-       g_hash_table_insert (thread_hash, GUINT_TO_POINTER (thread_handle.thread->id), handle);
 
+       /* suspend_sem is not used for attached threads, but
+        * thread_exit() might try to destroy it
+        */
+       MONO_SEM_INIT (&thread_handle_p->suspend_sem, 0);
+       thread_handle_p->handle = handle;
+       thread_handle_p->id = pthread_self ();
+
+       thr_ret = pthread_setspecific (thread_hash_key, (void *)handle);
+       g_assert (thr_ret == 0);
+
+       thr_ret = pthread_setspecific (thread_attached_key, (void *)handle);
+       g_assert (thr_ret == 0);
+       
 #ifdef DEBUG
-       g_message("%s: Attached thread handle %p thread %p ID %ld", __func__,
-                 handle, thread_handle.thread, thread_handle.thread->id);
+       g_message("%s: Attached thread handle %p ID %ld", __func__, handle,
+                 thread_handle_p->id);
 #endif
 
        if (tid != NULL) {
 #ifdef PTHREAD_POINTER_ID
-               *tid = GPOINTER_TO_UINT(thread_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->id);
 #else
-               *tid = thread_handle.thread->id;
+               *tid = thread_handle_p->id;
 #endif
        }
 
-thread_hash_cleanup:
-       thr_ret = mono_mutex_unlock (&thread_hash_mutex);
+cleanup:
+       thr_ret = _wapi_handle_unlock_handle (handle);
        g_assert (thr_ret == 0);
        pthread_cleanup_pop (0);
        
-       return(ta_ret);
+       return(handle);
+}
+
+gpointer _wapi_thread_duplicate ()
+{
+       gpointer ret = NULL;
+       
+       mono_once (&thread_hash_once, thread_hash_init);
+       mono_once (&thread_ops_once, thread_ops_init);
+       
+       ret = _wapi_thread_handle_from_id (pthread_self ());
+       if (!ret) {
+               ret = thread_attach (NULL);
+       } else {
+               _wapi_handle_ref (ret);
+       }
+       
+       return(ret);
 }
 
 /**
@@ -538,31 +753,10 @@ thread_hash_cleanup:
  */
 gpointer GetCurrentThread(void)
 {
-       gpointer ret=NULL;
-       guint32 tid;
-       int thr_ret;
-       
        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, GUINT_TO_POINTER (tid));
-
-       thr_ret = mono_mutex_unlock(&thread_hash_mutex);
-       g_assert (thr_ret == 0);
-       pthread_cleanup_pop (0);
-       
-       if (!ret) {
-               ret = thread_attach (NULL);
-       }
-
-       return(ret);
+       return(_WAPI_THREAD_CURRENT);
 }
 
 /**
@@ -576,23 +770,24 @@ gpointer GetCurrentThread(void)
  */
 guint32 ResumeThread(gpointer handle)
 {
-       if (WAPI_SHARED_HANDLE_TYPED_DATA(handle, thread).thread == NULL) {
-               return(0xFFFFFFFF);
+       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 (0xFFFFFFFF);
        }
 
-#ifdef WITH_INCLUDED_LIBGC
-       if (WAPI_SHARED_HANDLE_DATA(handle, thread).thread->suspend_count <= 1)
-               _wapi_timed_thread_resume (WAPI_SHARED_HANDLE_TYPED_DATA(handle, thread).thread);
-       
-       return (--(WAPI_SHARED_HANDLE_TYPED_DATA(handle, thread).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 (WAPI_SHARED_HANDLE_TYPED_DATA(handle, thread).thread);
+       _wapi_thread_resume (thread_handle);
        return(0xFFFFFFFF);
-#endif
 }
 
 /**
@@ -606,32 +801,7 @@ guint32 ResumeThread(gpointer handle)
  */
 guint32 SuspendThread(gpointer handle)
 {
-#ifdef WITH_INCLUDED_LIBGC
-       gpointer current;
-
-       current = GetCurrentThread ();
-
-       if (WAPI_SHARED_HANDLE_DATA(handle, thread).thread == NULL) {
-               return(0xFFFFFFFF);
-       }
-
-       if (!WAPI_SHARED_HANDLE_DATA(handle, thread).thread->suspend_count) {
-               if (handle == current)
-                       _wapi_timed_thread_suspend (WAPI_SHARED_HANDLE_DATA(handle, thread).thread);
-               else {
-                       pthread_kill (WAPI_SHARED_HANDLE_DATA(handle, thread).thread->id, SIGPWR);
-                       while (MONO_SEM_WAIT (&WAPI_SHARED_HANDLE_DATA(handle, thread).thread->suspended_sem) != 0) {
-                               if (errno != EINTR) {
-                                       return(0xFFFFFFFF);
-                               }
-                       }
-               }
-       }
-
-       return (WAPI_SHARED_HANDLE_DATA(handle, thread).thread->suspend_count++);
-#else
        return(0xFFFFFFFF);
-#endif
 }
 
 /*
@@ -827,7 +997,12 @@ guint32 SleepEx(guint32 ms, gboolean alertable)
 #endif
 
        if (alertable) {
-               current_thread = GetCurrentThread ();
+               current_thread = _wapi_thread_handle_from_id (pthread_self ());
+               if (current_thread == NULL) {
+                       SetLastError (ERROR_INVALID_HANDLE);
+                       return(WAIT_FAILED);
+               }
+               
                if (_wapi_thread_apc_pending (current_thread)) {
                        _wapi_thread_dispatch_apc_queue (current_thread);
                        return WAIT_IO_COMPLETION;
@@ -873,58 +1048,333 @@ void Sleep(guint32 ms)
        SleepEx(ms, FALSE);
 }
 
-guint32 QueueUserAPC (WapiApcProc apc_callback, gpointer handle, 
-                                       gpointer param)
-{
-       _wapi_timed_thread_queue_apc (WAPI_SHARED_HANDLE_TYPED_DATA(handle, thread).thread, apc_callback, param);
-       return(1);
-}
-
 gboolean _wapi_thread_cur_apc_pending (void)
 {
-       return(_wapi_thread_apc_pending (GetCurrentThread ()));
+       gpointer thread = _wapi_thread_handle_from_id (pthread_self ());
+       
+       if (thread == NULL) {
+               SetLastError (ERROR_INVALID_HANDLE);
+               return(FALSE);
+       }
+       
+       return(_wapi_thread_apc_pending (thread));
 }
 
 gboolean _wapi_thread_apc_pending (gpointer handle)
 {
-       return(_wapi_timed_thread_apc_pending (WAPI_SHARED_HANDLE_TYPED_DATA(handle, thread).thread));
+       struct _WapiHandle_thread *thread;
+       gboolean ok;
+       
+       ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
+                                 (gpointer *)&thread);
+       if (ok == FALSE) {
+#ifdef DEBUG
+               /* This might happen at process shutdown, as all
+                * thread handles are forcibly closed.  If a thread
+                * still has an alertable wait the final
+                * _wapi_thread_apc_pending check will probably fail
+                * to find the handle
+                */
+               g_warning ("%s: error looking up thread handle %p", __func__,
+                          handle);
+#endif
+               return (FALSE);
+       }
+       
+       return(thread->has_apc || thread->wait_handle == INTERRUPTION_REQUESTED_HANDLE);
 }
 
 gboolean _wapi_thread_dispatch_apc_queue (gpointer handle)
 {
-       _wapi_timed_thread_dispatch_apc_queue (WAPI_SHARED_HANDLE_TYPED_DATA(handle, thread).thread);
+       /* We don't support calling APC functions */
+       struct _WapiHandle_thread *thread;
+       gboolean ok;
+       
+       ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
+                                 (gpointer *)&thread);
+       g_assert (ok);
+
+       thread->has_apc = FALSE;
+
+       return(TRUE);
+}
+
+/*
+ * In this implementation, APC_CALLBACK is ignored.
+ * if HANDLE refers to the current thread, the only effect this function has 
+ * that if called from a signal handler, and the thread was waiting when receiving 
+ * the signal, the wait will be broken after the signal handler returns.
+ * In this case, this function is async-signal-safe.
+ */
+guint32 QueueUserAPC (WapiApcProc apc_callback, gpointer handle, 
+                     gpointer param)
+{
+       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);
+       }
+
+       g_assert (thread_handle->id == GetCurrentThreadId ());
+       /* No locking/memory barriers are needed here */
+       thread_handle->has_apc = TRUE;
        return(1);
 }
 
+/*
+ * wapi_interrupt_thread:
+ *
+ *   This is not part of the WIN32 API.
+ * The state of the thread handle HANDLE is set to 'interrupted' which means that
+ * if the thread calls one of the WaitFor functions, the function will return with 
+ * WAIT_IO_COMPLETION instead of waiting. Also, if the thread was waiting when
+ * this function was called, the wait will be broken.
+ * It is possible that the wait functions return WAIT_IO_COMPLETION, but the
+ * target thread didn't receive the interrupt signal yet, in this case it should
+ * call the wait function again. This essentially means that the target thread will
+ * busy wait until it is ready to process the interruption.
+ * FIXME: get rid of QueueUserAPC and thread->has_apc, SleepEx seems to require it.
+ */
+void wapi_interrupt_thread (gpointer thread_handle)
+{
+       struct _WapiHandle_thread *thread;
+       gboolean ok;
+       gpointer prev_handle, wait_handle;
+       guint32 idx;
+       pthread_cond_t *cond;
+       mono_mutex_t *mutex;
+       
+       ok = _wapi_lookup_handle (thread_handle, WAPI_HANDLE_THREAD,
+                                 (gpointer *)&thread);
+       g_assert (ok);
+
+       while (TRUE) {
+               wait_handle = thread->wait_handle;
+
+               /* 
+                * Atomically obtain the handle the thread is waiting on, and
+                * change it to a flag value.
+                */
+               prev_handle = InterlockedCompareExchangePointer (&thread->wait_handle,
+                                                                                                                INTERRUPTION_REQUESTED_HANDLE, wait_handle);
+               if (prev_handle == INTERRUPTION_REQUESTED_HANDLE)
+                       /* Already interrupted */
+                       return;
+               if (prev_handle == wait_handle)
+                       break;
+
+               /* Try again */
+       }
+
+       WAIT_DEBUG (printf ("%p: state -> INTERRUPTED.\n", thread_handle->id););
+
+       if (!wait_handle)
+               /* Not waiting */
+               return;
+
+       /* If we reach here, then wait_handle is set to the flag value, 
+        * which means that the target thread is either
+        * - before the first CAS in timedwait, which means it won't enter the
+        * wait.
+        * - it is after the first CAS, so it is already waiting, or it will 
+        * enter the wait, and it will be interrupted by the broadcast.
+        */
+       idx = GPOINTER_TO_UINT(wait_handle);
+       cond = &_WAPI_PRIVATE_HANDLES(idx).signal_cond;
+       mutex = &_WAPI_PRIVATE_HANDLES(idx).signal_mutex;
+
+       mono_mutex_lock (mutex);
+       mono_cond_broadcast (cond);
+       mono_mutex_unlock (mutex);
+
+       /* ref added by set_wait_handle */
+       _wapi_handle_unref (wait_handle);
+}
+
+/*
+ * wapi_clear_interruption:
+ *
+ *   This is not part of the WIN32 API. 
+ * Clear the 'interrupted' state of the calling thread.
+ */
+void wapi_clear_interruption (void)
+{
+       struct _WapiHandle_thread *thread;
+       gboolean ok;
+       gpointer prev_handle;
+       gpointer thread_handle;
+
+       thread_handle = OpenThread (0, 0, GetCurrentThreadId ());
+       ok = _wapi_lookup_handle (thread_handle, WAPI_HANDLE_THREAD,
+                                                         (gpointer *)&thread);
+       g_assert (ok);
 
+       prev_handle = InterlockedCompareExchangePointer (&thread->wait_handle,
+                                                                                                        NULL, INTERRUPTION_REQUESTED_HANDLE);
+       if (prev_handle == INTERRUPTION_REQUESTED_HANDLE)
+               WAIT_DEBUG (printf ("%p: state -> NORMAL.\n", GetCurrentThreadId ()););
 
-#ifdef WITH_INCLUDED_LIBGC
+       _wapi_handle_unref (thread_handle);
+}
 
-static void GC_suspend_handler (int sig)
+char* wapi_current_thread_desc ()
 {
+       struct _WapiHandle_thread *thread;
+       int i;
+       gboolean ok;
        gpointer handle;
+       gpointer thread_handle;
+       GString* text;
+       char *res;
+
+       thread_handle = OpenThread (0, 0, GetCurrentThreadId ());
+       ok = _wapi_lookup_handle (thread_handle, WAPI_HANDLE_THREAD,
+                                                         (gpointer *)&thread);
+       if (!ok)
+               return g_strdup_printf ("thread handle %p state : lookup failure", thread_handle);
+
+       handle = thread->wait_handle;
+       text = g_string_new (0);
+       g_string_append_printf (text, "thread handle %p state : ", thread_handle);
+
+       if (!handle)
+               g_string_append_printf (text, "not waiting");
+       else if (handle == INTERRUPTION_REQUESTED_HANDLE)
+               g_string_append_printf (text, "interrupted state");
+       else
+               g_string_append_printf (text, "waiting on %p : %s ", handle, _wapi_handle_typename[_wapi_handle_type (handle)]);
+       g_string_append_printf (text, " owns (");
+       for (i = 0; i < thread->owned_mutexes->len; i++) {
+               gpointer mutex = g_ptr_array_index (thread->owned_mutexes, i);
+               if (i > 0)
+                       g_string_append_printf (text, ", %p", mutex);
+               else
+                       g_string_append_printf (text, "%p", mutex);
+       }
+       g_string_append_printf (text, ")");
 
-       handle = GetCurrentThread ();
-
-       WAPI_SHARED_HANDLE_DATA(handle, thread).thread->stack_ptr = &ok;
-       MONO_SEM_POST (&WAPI_SHARED_HANDLE_DATA(handle, thread).thread->suspended_sem);
+       res = text->str;
+       g_string_free (text, FALSE);
+       return res;
+}
 
-       _wapi_timed_thread_suspend (WAPI_SHARED_HANDLE_DATA(handle, thread).thread);
+/**
+ * wapi_thread_set_wait_handle:
+ *
+ *   Set the wait handle for the current thread to HANDLE. Return TRUE on success, FALSE
+ * if the thread is in interrupted state, and cannot start waiting.
+ */
+gboolean wapi_thread_set_wait_handle (gpointer handle)
+{
+       struct _WapiHandle_thread *thread;
+       gboolean ok;
+       gpointer prev_handle;
+       gpointer thread_handle;
+
+       thread_handle = OpenThread (0, 0, GetCurrentThreadId ());
+       ok = _wapi_lookup_handle (thread_handle, WAPI_HANDLE_THREAD,
+                                                         (gpointer *)&thread);
+       g_assert (ok);
+
+       prev_handle = InterlockedCompareExchangePointer (&thread->wait_handle,
+                                                                                                        handle, NULL);
+       _wapi_handle_unref (thread_handle);
+
+       if (prev_handle == NULL) {
+               /* thread->wait_handle acts as an additional reference to the handle */
+               _wapi_handle_ref (handle);
+
+               WAIT_DEBUG (printf ("%p: state -> WAITING.\n", GetCurrentThreadId ()););
+       } else {
+               g_assert (prev_handle == INTERRUPTION_REQUESTED_HANDLE);
+               WAIT_DEBUG (printf ("%p: unable to set state to WAITING.\n", GetCurrentThreadId ()););
+       }
 
-       WAPI_SHARED_HANDLE_DATA(handle, thread).thread->stack_ptr = NULL;
+       return prev_handle == NULL;
 }
 
-static void gc_init (void)
+/**
+ * wapi_thread_clear_wait_handle:
+ *
+ *   Clear the wait handle of the current thread.
+ */
+void wapi_thread_clear_wait_handle (gpointer handle)
 {
-       struct sigaction act;
+       struct _WapiHandle_thread *thread;
+       gboolean ok;
+       gpointer prev_handle;
+       gpointer thread_handle;
+
+       thread_handle = OpenThread (0, 0, GetCurrentThreadId ());
+       ok = _wapi_lookup_handle (thread_handle, WAPI_HANDLE_THREAD,
+                                                         (gpointer *)&thread);
+       g_assert (ok);
+
+       prev_handle = InterlockedCompareExchangePointer (&thread->wait_handle,
+                                                                                                        NULL, handle);
+
+       if (prev_handle == handle) {
+               _wapi_handle_unref (handle);
+               WAIT_DEBUG (printf ("%p: state -> NORMAL.\n", GetCurrentThreadId ()););
+       } else {
+               g_assert (prev_handle == INTERRUPTION_REQUESTED_HANDLE);
+               WAIT_DEBUG (printf ("%p: finished waiting.\n", GetCurrentThreadId ()););
+       }
 
-       act.sa_handler = GC_suspend_handler;
-       g_assert (sigaction (SIGPWR, &act, NULL) == 0);
+       _wapi_handle_unref (thread_handle);
 }
 
-void mono_wapi_push_thread_stack (gpointer handle, gpointer stack_ptr)
+void _wapi_thread_own_mutex (gpointer mutex)
 {
-       GC_push_all_stack (WAPI_SHARED_HANDLE_DATA(handle, thread).thread->stack_ptr, stack_ptr);
+       struct _WapiHandle_thread *thread_handle;
+       gboolean ok;
+       gpointer thread;
+       
+       thread = _wapi_thread_handle_from_id (pthread_self ());
+       if (thread == NULL) {
+               g_warning ("%s: error looking up thread by ID", __func__);
+               return;
+       }
+
+       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_handle_ref (mutex);
+       
+       g_ptr_array_add (thread_handle->owned_mutexes, mutex);
 }
 
-#endif /* WITH_INCLUDED_LIBGC */
+void _wapi_thread_disown_mutex (gpointer mutex)
+{
+       struct _WapiHandle_thread *thread_handle;
+       gboolean ok;
+       gpointer thread;
+
+       thread = _wapi_thread_handle_from_id (pthread_self ());
+       if (thread == NULL) {
+               g_warning ("%s: error looking up thread by ID", __func__);
+               return;
+       }
+
+       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_handle_unref (mutex);
+       
+       g_ptr_array_remove (thread_handle->owned_mutexes, mutex);
+}