Merge pull request #900 from Blewzman/FixAggregateExceptionGetBaseException
[mono.git] / mono / io-layer / wthreads.c
index e0b2103e10f71db815f61804ce485621bd5f6967..0c34ba189ff8daf59b5132ae466e1913ed68ff84 100644 (file)
 #include <mono/io-layer/wapi-private.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/mutex-private.h>
-#include <mono/io-layer/atomic.h>
 
 #include <mono/utils/mono-threads.h>
-#include <mono/utils/gc_wrapper.h>
+#include <mono/utils/atomic.h>
+#include <mono/utils/mono-mutex.h>
 
 #ifdef HAVE_VALGRIND_MEMCHECK_H
 #include <valgrind/memcheck.h>
 #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 pthread_key_t thread_hash_key;
-
-/* 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 */
        NULL,                           /* signal */
@@ -80,62 +67,27 @@ static void thread_ops_init (void)
 
 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);
 }
 
-/* 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)
+static gpointer
+get_current_thread_handle (void)
 {
-       struct _WapiHandle_thread *thread_handle;
-       gboolean ok;
-       int i;
-       pid_t pid = _wapi_getpid ();
-       pthread_t tid = pthread_self ();
-       
-       DEBUG ("%s: Thread %p abandoning held mutexes", __func__, handle);
+       MonoThreadInfo *info;
 
-       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 looking up thread handle %p", __func__,
-                          handle);
-               return;
-       }
-       
-       if (!pthread_equal (thread_handle->id, tid)) {
-               return;
-       }
-       
-       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);
-       }
+       info = mono_thread_info_current ();
+       g_assert (info);
+       return info->handle;
 }
 
-void _wapi_thread_set_termination_details (gpointer handle,
+static void
+_wapi_thread_set_termination_details (gpointer handle,
                                           guint32 exitstatus)
 {
        struct _WapiHandle_thread *thread_handle;
        gboolean ok;
-       int thr_ret;
+       int i, thr_ret;
+       pid_t pid = _wapi_getpid ();
+       pthread_t tid = pthread_self ();
        
        if (_wapi_handle_issignalled (handle) ||
            _wapi_handle_type (handle) == WAPI_HANDLE_UNUSED) {
@@ -147,32 +99,27 @@ void _wapi_thread_set_termination_details (gpointer handle,
 
        DEBUG ("%s: Thread %p terminating", __func__, handle);
 
-       _wapi_thread_abandon_mutexes (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);
+       g_assert (ok);
 
-               return;
+       DEBUG ("%s: Thread %p abandoning held mutexes", __func__, handle);
+
+       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);
        }
+       g_ptr_array_free (thread_handle->owned_mutexes, 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);
-       pthread_cleanup_pop (0);
        
        DEBUG("%s: Recording thread handle %p id %ld status as %d",
                  __func__, handle, thread_handle->id, exitstatus);
@@ -185,268 +132,64 @@ void _wapi_thread_signal_self (guint32 exitstatus)
 {
        gpointer handle;
        
-       handle = _wapi_thread_handle_from_id (pthread_self ());
-       if (handle == NULL) {
-               /* Something gone badly wrong... */
+       handle = get_current_thread_handle ();
+       if (handle == NULL)
                return;
-       }
        
        _wapi_thread_set_termination_details (handle, exitstatus);
 }
 
-/* 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)
+void
+wapi_thread_handle_set_exited (gpointer handle, guint32 exitstatus)
 {
        _wapi_thread_set_termination_details (handle, exitstatus);
-       
-       /* Call pthread_exit() to call destructors and really exit the
-        * thread
-        */
-       mono_gc_pthread_exit (NULL);
-}
-
-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)
-{
-       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 = mono_gc_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. */
-               mono_gc_pthread_exit (NULL);
-       }
-
-       DEBUG ("%s: started thread id %ld", __func__, thread->id);
-
-       /* We set it again here since passing &thread->id to pthread_create is racy
-          as the thread can start running before the value is set.*/
-       thread->id = pthread_self ();
-
-       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
 }
 
-/**
- * CreateThread:
- * @security: Ignored for now.
- * @stacksize: the size in bytes of the new thread's stack. Use 0 to
- * default to the normal stack size. (Ignored for now).
- * @start: The function that the new thread should start with
- * @param: The parameter to give to @start.
- * @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.  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.
+/*
+ * wapi_create_thread_handle:
  *
- * Return value: a new handle, or NULL
+ *   Create a thread handle for the current thread.
  */
-gpointer CreateThread(WapiSecurityAttributes *security G_GNUC_UNUSED, guint32 stacksize,
-                     WapiThreadStart start, gpointer param, guint32 create,
-                     gsize *tid) 
+gpointer
+wapi_create_thread_handle (void)
 {
-       struct _WapiHandle_thread thread_handle = {0}, *thread_handle_p;
-       pthread_attr_t attr;
+       struct _WapiHandle_thread thread_handle = {0}, *thread;
        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);
+       int res;
+
        mono_once (&thread_ops_once, thread_ops_init);
-       
-       if (start == NULL) {
-               return(NULL);
-       }
 
-       thread_handle.state = THREAD_STATE_START;
        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);
+               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;
-       }
+       res = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
+                                                          (gpointer *)&thread);
+       g_assert (res);
 
-       /* Hold a reference while the thread is active, because we use
+       thread->id = pthread_self ();
+
+       /*
+        * 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>
-        * set the size to 2M-4k.  I don't know why it's short by 4k, so
-        * I'm leaving it as 2M until I'm told differently.)
-        */
-       thr_ret = pthread_attr_init(&attr);
-       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) {
-                       stacksize = 1 << 20;
-               } else {
-                       stacksize = (SIZEOF_VOID_P / 4) * 1024 * 1024;
-               }
-#else
-               stacksize = (SIZEOF_VOID_P / 4) * 1024 * 1024;
-#endif
-       }
-
-#ifdef PTHREAD_STACK_MIN
-       if (stacksize < PTHREAD_STACK_MIN)
-               stacksize = PTHREAD_STACK_MIN;
-#endif
-
-#ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE
-       thr_ret = pthread_attr_setstacksize(&attr, stacksize);
-       g_assert (thr_ret == 0);
-#endif
 
-       MONO_SEM_INIT (&thread_handle_p->suspend_sem, 0);
-       thread_handle_p->handle = handle;
-       
-
-       ret = mono_threads_pthread_create (&thread_handle_p->id, &attr,
-                                                                          thread_start_routine, (void *)thread_handle_p);
-
-       if (ret != 0) {
-               DEBUG ("%s: Thread create error: %s", __func__,
-                          strerror(ret));
-
-               /* Two, because of the reference we took above */
-               unrefs = 2;
-               
-               goto cleanup;
-       }
-       ct_ret = handle;
-       
-       DEBUG("%s: Started thread handle %p ID %ld", __func__, handle,
-                 thread_handle_p->id);
+       DEBUG ("%s: started thread id %ld", __func__, thread->id);
        
-       if (tid != NULL) {
-#ifdef PTHREAD_POINTER_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_p->id;
-#endif
-       }
+       return handle;
+}
 
-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);
+void
+wapi_ref_thread_handle (gpointer handle)
+{
+       _wapi_handle_ref (handle);
 }
 
 /* The only time this function is called when tid != pthread_self ()
@@ -456,23 +199,10 @@ cleanup:
  */
 gpointer _wapi_thread_handle_from_id (pthread_t tid)
 {
-       gpointer ret;
-
-       if (pthread_equal (tid, pthread_self ()) &&
-           (ret = pthread_getspecific (thread_hash_key)) != NULL) {
-               /* We know the handle */
-
-               DEBUG ("%s: Returning %p for self thread %ld from TLS",
-                          __func__, ret, tid);
-               
-               return(ret);
-       }
-       
-       DEBUG ("%s: Returning NULL for unknown or non-self thread %ld",
-                  __func__, tid);
-               
-
-       return(NULL);
+       if (pthread_equal (tid, pthread_self ()))
+               return get_current_thread_handle ();
+       else
+               return NULL;
 }
 
 static gboolean find_thread_by_id (gpointer handle, gpointer user_data)
@@ -515,12 +245,12 @@ gpointer OpenThread (guint32 access G_GNUC_UNUSED, gboolean inherit G_GNUC_UNUSE
 {
        gpointer ret=NULL;
        
-       mono_once (&thread_hash_once, thread_hash_init);
        mono_once (&thread_ops_once, thread_ops_init);
        
        DEBUG ("%s: looking up thread %"G_GSIZE_FORMAT, __func__, tid);
 
-       ret = _wapi_thread_handle_from_id ((pthread_t)tid);
+       if (pthread_equal ((pthread_t)tid, pthread_self ()))
+               ret = get_current_thread_handle ();
        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 */
@@ -536,71 +266,6 @@ gpointer OpenThread (guint32 access G_GNUC_UNUSED, gboolean inherit G_GNUC_UNUSE
        return(ret);
 }
 
-/**
- * ExitThread:
- * @exitcode: Sets the thread's exit code, which can be read from
- * another thread with GetExitCodeThread().
- *
- * Terminates the calling thread.  A thread can also exit by returning
- * from its start function. When the last thread in a process
- * terminates, the process itself terminates.
- */
-void ExitThread(guint32 exitcode)
-{
-       gpointer thread = _wapi_thread_handle_from_id (pthread_self ());
-       
-       if (thread != NULL) {
-               thread_exit(exitcode, thread);
-       } else {
-               /* Just blow this thread away */
-               mono_gc_pthread_exit (NULL);
-       }
-}
-
-/**
- * GetExitCodeThread:
- * @handle: The thread handle to query
- * @exitcode: The thread @handle exit code is stored here
- *
- * Finds the exit code of @handle, and stores it in @exitcode.  If the
- * thread @handle is still running, the value stored is %STILL_ACTIVE.
- *
- * Return value: %TRUE, or %FALSE on error.
- */
-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);
-       }
-       
-       DEBUG ("%s: Finding exit status for thread handle %p id %ld",
-                  __func__, handle, thread_handle->id);
-
-       if (exitcode == NULL) {
-               DEBUG ("%s: Nowhere to store exit code", __func__);
-               return(FALSE);
-       }
-       
-       if (thread_handle->state != THREAD_STATE_EXITED) {
-               DEBUG ("%s: Thread still active (state %d, exited is %d)",
-                          __func__, thread_handle->state,
-                          THREAD_STATE_EXITED);
-               *exitcode = STILL_ACTIVE;
-               return(TRUE);
-       }
-       
-       *exitcode = thread_handle->exitstatus;
-       
-       return(TRUE);
-}
-
 /**
  * GetCurrentThreadId:
  *
@@ -614,175 +279,16 @@ gboolean GetExitCodeThread(gpointer handle, guint32 *exitcode)
  */
 gsize GetCurrentThreadId(void)
 {
-       pthread_t tid = pthread_self();
-       
-#ifdef PTHREAD_POINTER_ID
-       /* 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(gsize *tid)
-{
-       struct _WapiHandle_thread thread_handle = {0}, *thread_handle_p;
-       gpointer handle;
-       gboolean ok;
-       int thr_ret;
-       
-       mono_once (&thread_hash_once, thread_hash_init);
-       mono_once (&thread_ops_once, thread_ops_init);
+       MonoNativeThreadId id;
 
-       thread_handle.state = THREAD_STATE_START;
-       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_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);
-
-       /* 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);
-       
-       DEBUG("%s: Attached thread handle %p ID %ld", __func__, handle,
-                 thread_handle_p->id);
-
-       if (tid != NULL) {
-#ifdef PTHREAD_POINTER_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_p->id;
-#endif
-       }
-
-cleanup:
-       thr_ret = _wapi_handle_unlock_handle (handle);
-       g_assert (thr_ret == 0);
-       pthread_cleanup_pop (0);
-       
-       return(handle);
+       id = mono_native_thread_id_get ();
+       return MONO_NATIVE_THREAD_ID_TO_UINT (id);
 }
 
 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);
-}
-
-/**
- * GetCurrentThread:
- *
- * Looks up the handle associated with the current thread.  Under
- * Windows this is a pseudohandle, and must be duplicated with
- * DuplicateHandle() for some operations.
- *
- * Return value: The current thread handle, or %NULL on failure.
- * (Unknown whether Windows has a possible failure here.  It may be
- * necessary to implement the pseudohandle-constant behaviour).
- */
-gpointer GetCurrentThread(void)
-{
-       mono_once(&thread_hash_once, thread_hash_init);
-       mono_once (&thread_ops_once, thread_ops_init);
-       
-       return(_WAPI_THREAD_CURRENT);
-}
-
-/**
- * ResumeThread:
- * @handle: the thread handle to resume
- *
- * Decrements the suspend count of thread @handle. A thread can only
- * run if its suspend count is zero.
- *
- * Return value: the previous suspend count, or 0xFFFFFFFF on error.
- */
-guint32 ResumeThread(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 (0xFFFFFFFF);
-       }
-
-       /* 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_thread_resume (thread_handle);
-       return(0xFFFFFFFF);
-}
-
-/**
- * SuspendThread:
- * @handle: the thread handle to suspend
- *
- * Increments the suspend count of thread @handle. A thread can only
- * run if its suspend count is zero.
- *
- * Return value: the previous suspend count, or 0xFFFFFFFF on error.
- */
-guint32 SuspendThread(gpointer handle)
-{
-       return(0xFFFFFFFF);
+       g_assert_not_reached ();
+       return NULL;
 }
 
 /**
@@ -804,7 +310,7 @@ guint32 SleepEx(guint32 ms, gboolean alertable)
        DEBUG("%s: Sleeping for %d ms", __func__, ms);
 
        if (alertable) {
-               current_thread = _wapi_thread_handle_from_id (pthread_self ());
+               current_thread = get_current_thread_handle ();
                if (current_thread == NULL) {
                        SetLastError (ERROR_INVALID_HANDLE);
                        return(WAIT_FAILED);
@@ -858,7 +364,7 @@ void Sleep(guint32 ms)
 
 gboolean _wapi_thread_cur_apc_pending (void)
 {
-       gpointer thread = _wapi_thread_handle_from_id (pthread_self ());
+       gpointer thread = get_current_thread_handle ();
        
        if (thread == NULL) {
                SetLastError (ERROR_INVALID_HANDLE);
@@ -906,30 +412,32 @@ gboolean _wapi_thread_dispatch_apc_queue (gpointer handle)
 }
 
 /*
- * 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 
+ * wapi_interrupt_self:
+ *
+ * If this function 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.
+ * This function is async-signal-safe.
  */
-guint32 QueueUserAPC (WapiApcProc apc_callback, gpointer handle, 
-                     gpointer param)
+void
+wapi_thread_interrupt_self (void)
 {
+       HANDLE handle;
        struct _WapiHandle_thread *thread_handle;
        gboolean ok;
        
+       handle = get_current_thread_handle ();
+       g_assert (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 (0);
+               return;
        }
 
-       g_assert (thread_handle->id == GetCurrentThreadId ());
        /* No locking/memory barriers are needed here */
        thread_handle->has_apc = TRUE;
-       return(1);
 }
 
 /*
@@ -947,13 +455,18 @@ guint32 QueueUserAPC (WapiApcProc apc_callback, gpointer handle,
  * FIXME: get rid of QueueUserAPC and thread->has_apc, SleepEx seems to require it.
  */
 void wapi_interrupt_thread (gpointer thread_handle)
+{
+       gpointer wait_handle;
+
+       wait_handle = wapi_prepare_interrupt_thread (thread_handle);
+       wapi_finish_interrupt_thread (wait_handle);
+}
+
+gpointer wapi_prepare_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);
@@ -970,14 +483,23 @@ void wapi_interrupt_thread (gpointer thread_handle)
                                                                                                                 INTERRUPTION_REQUESTED_HANDLE, wait_handle);
                if (prev_handle == INTERRUPTION_REQUESTED_HANDLE)
                        /* Already interrupted */
-                       return;
+                       return 0;
                if (prev_handle == wait_handle)
                        break;
 
                /* Try again */
        }
 
-       WAIT_DEBUG (printf ("%p: state -> INTERRUPTED.\n", thread_handle->id););
+       WAIT_DEBUG (printf ("%p: state -> INTERRUPTED.\n", thread->id););
+
+       return wait_handle;
+}
+
+void wapi_finish_interrupt_thread (gpointer wait_handle)
+{
+       pthread_cond_t *cond;
+       mono_mutex_t *mutex;
+       guint32 idx;
 
        if (!wait_handle)
                /* Not waiting */
@@ -1010,42 +532,15 @@ void wapi_interrupt_thread (gpointer thread_handle)
  */
 void wapi_self_interrupt (void)
 {
-       struct _WapiHandle_thread *thread;
-       gboolean ok;
-       gpointer prev_handle, wait_handle;
+       gpointer wait_handle;
        gpointer thread_handle;
 
-
        thread_handle = OpenThread (0, 0, GetCurrentThreadId ());
-       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 */
-                       goto cleanup;
-               /*We did not get interrupted*/
-               if (prev_handle == wait_handle)
-                       break;
-
-               /* Try again */
-       }
-
-       if (wait_handle) {
+       wait_handle = wapi_prepare_interrupt_thread (thread_handle);
+       if (wait_handle)
                /* ref added by set_wait_handle */
                _wapi_handle_unref (wait_handle);
-       }
 
-cleanup:
        _wapi_handle_unref (thread_handle);
 }
 
@@ -1190,7 +685,7 @@ void _wapi_thread_own_mutex (gpointer mutex)
        gboolean ok;
        gpointer thread;
        
-       thread = _wapi_thread_handle_from_id (pthread_self ());
+       thread = get_current_thread_handle ();
        if (thread == NULL) {
                g_warning ("%s: error looking up thread by ID", __func__);
                return;
@@ -1215,7 +710,7 @@ void _wapi_thread_disown_mutex (gpointer mutex)
        gboolean ok;
        gpointer thread;
 
-       thread = _wapi_thread_handle_from_id (pthread_self ());
+       thread = get_current_thread_handle ();
        if (thread == NULL) {
                g_warning ("%s: error looking up thread by ID", __func__);
                return;