Merge pull request #2810 from kumpera/fix_hazard_free
[mono.git] / mono / utils / mono-threads.c
index c0bfbec293daabd03924a2664d9cda4d5a230024..0b142ee1c3f0024fedbce186aa41c65df83e484e 100644 (file)
@@ -6,12 +6,18 @@
  *
  * Copyright 2011 Novell, Inc (http://www.novell.com)
  * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
+ * Licensed under the MIT license. See LICENSE file in the project root for full license information.
  */
 
 #include <config.h>
 
+/* enable pthread extensions */
+#ifdef TARGET_MACH
+#define _DARWIN_C_SOURCE
+#endif
+
 #include <mono/utils/mono-compiler.h>
-#include <mono/utils/mono-semaphore.h>
+#include <mono/utils/mono-os-semaphore.h>
 #include <mono/utils/mono-threads.h>
 #include <mono/utils/mono-tls.h>
 #include <mono/utils/hazard-pointer.h>
@@ -19,7 +25,9 @@
 #include <mono/utils/mono-mmap.h>
 #include <mono/utils/atomic.h>
 #include <mono/utils/mono-time.h>
-
+#include <mono/utils/mono-lazy-init.h>
+#include <mono/utils/mono-coop-mutex.h>
+#include <mono/utils/mono-coop-semaphore.h>
 
 #include <errno.h>
 
@@ -39,7 +47,7 @@ The GC has to acquire this lock before starting a STW to make sure
 a runtime suspend won't make it wronly see a thread in a safepoint
 when it is in fact not.
 */
-static MonoSemType global_suspend_semaphore;
+static MonoCoopSem global_suspend_semaphore;
 
 static size_t thread_info_size;
 static MonoThreadInfoCallbacks threads_callbacks;
@@ -71,7 +79,7 @@ mono_threads_notify_initiator_of_abort (MonoThreadInfo* info)
 {
        THREADS_SUSPEND_DEBUG ("[INITIATOR-NOTIFY-ABORT] %p\n", mono_thread_info_get_tid (info));
        InterlockedIncrement (&abort_posts);
-       MONO_SEM_POST (&suspend_semaphore);
+       mono_os_sem_post (&suspend_semaphore);
 }
 
 void
@@ -79,7 +87,7 @@ mono_threads_notify_initiator_of_suspend (MonoThreadInfo* info)
 {
        THREADS_SUSPEND_DEBUG ("[INITIATOR-NOTIFY-SUSPEND] %p\n", mono_thread_info_get_tid (info));
        InterlockedIncrement (&suspend_posts);
-       MONO_SEM_POST (&suspend_semaphore);
+       mono_os_sem_post (&suspend_semaphore);
 }
 
 void
@@ -87,12 +95,38 @@ mono_threads_notify_initiator_of_resume (MonoThreadInfo* info)
 {
        THREADS_SUSPEND_DEBUG ("[INITIATOR-NOTIFY-RESUME] %p\n", mono_thread_info_get_tid (info));
        InterlockedIncrement (&resume_posts);
-       MONO_SEM_POST (&suspend_semaphore);
+       mono_os_sem_post (&suspend_semaphore);
+}
+
+static gboolean
+begin_async_suspend (MonoThreadInfo *info, gboolean interrupt_kernel)
+{
+       if (mono_threads_is_coop_enabled ()) {
+               /* There's nothing else to do after we async request the thread to suspend */
+               mono_threads_add_to_pending_operation_set (info);
+               return TRUE;
+       }
+
+       return mono_threads_core_begin_async_suspend (info, interrupt_kernel);
+}
+
+static gboolean
+check_async_suspend (MonoThreadInfo *info)
+{
+       if (mono_threads_is_coop_enabled ()) {
+               /* Async suspend can't async fail on coop */
+               return TRUE;
+       }
+
+       return mono_threads_core_check_suspend_result (info);
 }
 
 static void
 resume_async_suspended (MonoThreadInfo *info)
 {
+       if (mono_threads_is_coop_enabled ())
+               g_assert_not_reached ();
+
        g_assert (mono_threads_core_begin_async_resume (info));
 }
 
@@ -100,21 +134,23 @@ static void
 resume_self_suspended (MonoThreadInfo* info)
 {
        THREADS_SUSPEND_DEBUG ("**BEGIN self-resume %p\n", mono_thread_info_get_tid (info));
-       MONO_SEM_POST (&info->resume_semaphore);
+       mono_os_sem_post (&info->resume_semaphore);
 }
 
 void
 mono_thread_info_wait_for_resume (MonoThreadInfo* info)
 {
+       int res;
        THREADS_SUSPEND_DEBUG ("**WAIT self-resume %p\n", mono_thread_info_get_tid (info));
-       MONO_SEM_WAIT_UNITERRUPTIBLE (&info->resume_semaphore);
+       res = mono_os_sem_wait (&info->resume_semaphore, MONO_SEM_FLAGS_NONE);
+       g_assert (res != -1);
 }
 
 static void
 resume_blocking_suspended (MonoThreadInfo* info)
 {
        THREADS_SUSPEND_DEBUG ("**BEGIN blocking-resume %p\n", mono_thread_info_get_tid (info));
-       MONO_SEM_POST (&info->resume_semaphore);
+       mono_os_sem_post (&info->resume_semaphore);
 }
 
 void
@@ -128,27 +164,30 @@ mono_threads_add_to_pending_operation_set (MonoThreadInfo* info)
 void
 mono_threads_begin_global_suspend (void)
 {
-       g_assert (pending_suspends == 0);
+       size_t ps = pending_suspends;
+       if (G_UNLIKELY (ps != 0))
+               g_error ("pending_suspends = %d, but must be 0", ps);
        THREADS_SUSPEND_DEBUG ("------ BEGIN GLOBAL OP sp %d rp %d ap %d wd %d po %d (sp + rp + ap == wd) (wd == po)\n", suspend_posts, resume_posts,
                abort_posts, waits_done, pending_ops);
        g_assert ((suspend_posts + resume_posts + abort_posts) == waits_done);
-       mono_threads_core_begin_global_suspend ();
+       mono_threads_coop_begin_global_suspend ();
 }
 
 void
 mono_threads_end_global_suspend (void) 
 {
-       g_assert (pending_suspends == 0);
+       size_t ps = pending_suspends;
+       if (G_UNLIKELY (ps != 0))
+               g_error ("pending_suspends = %d, but must be 0", ps);
        THREADS_SUSPEND_DEBUG ("------ END GLOBAL OP sp %d rp %d ap %d wd %d po %d\n", suspend_posts, resume_posts,
                abort_posts, waits_done, pending_ops);
        g_assert ((suspend_posts + resume_posts + abort_posts) == waits_done);
-       mono_threads_core_end_global_suspend ();
+       mono_threads_coop_end_global_suspend ();
 }
 
 static void
 dump_threads (void)
 {
-       MonoThreadInfo *info;
        MonoThreadInfo *cur = mono_thread_info_current ();
 
        MOSTLY_ASYNC_SAFE_PRINTF ("STATE CUE CARD: (? means a positive number, usually 1 or 2, * means any number)\n");
@@ -163,8 +202,15 @@ dump_threads (void)
        MOSTLY_ASYNC_SAFE_PRINTF ("\t0x?08\t- blocking with pending suspend (GOOD)\n");
 
        FOREACH_THREAD_SAFE (info) {
+#ifdef TARGET_MACH
+               char thread_name [256] = { 0 };
+               pthread_getname_np (mono_thread_info_get_tid (info), thread_name, 255);
+
+               MOSTLY_ASYNC_SAFE_PRINTF ("--thread %p id %p [%p] (%s) state %x  %s\n", info, (void *) mono_thread_info_get_tid (info), (void*)(size_t)info->native_handle, thread_name, info->thread_state, info == cur ? "GC INITIATOR" : "" );
+#else
                MOSTLY_ASYNC_SAFE_PRINTF ("--thread %p id %p [%p] state %x  %s\n", info, (void *) mono_thread_info_get_tid (info), (void*)(size_t)info->native_handle, info->thread_state, info == cur ? "GC INITIATOR" : "" );
-       } END_FOREACH_THREAD_SAFE
+#endif
+       } FOREACH_THREAD_SAFE_END
 }
 
 gboolean
@@ -181,7 +227,7 @@ mono_threads_wait_pending_operations (void)
                for (i = 0; i < pending_suspends; ++i) {
                        THREADS_SUSPEND_DEBUG ("[INITIATOR-WAIT-WAITING]\n");
                        InterlockedIncrement (&waits_done);
-                       if (!MONO_SEM_TIMEDWAIT (&suspend_semaphore, SLEEP_DURATION_BEFORE_ABORT))
+                       if (!mono_os_sem_timedwait (&suspend_semaphore, SLEEP_DURATION_BEFORE_ABORT, MONO_SEM_FLAGS_NONE))
                                continue;
                        mono_stopwatch_stop (&suspension_time);
 
@@ -224,7 +270,7 @@ mono_thread_info_lookup (MonoNativeThreadId id)
 {
                MonoThreadHazardPointers *hp = mono_hazard_pointer_get ();
 
-       if (!mono_lls_find (&thread_list, hp, (uintptr_t)id)) {
+       if (!mono_lls_find (&thread_list, hp, (uintptr_t)id, HAZARD_FREE_ASYNC_CTX)) {
                mono_hazard_pointer_clear_all (hp, -1);
                return NULL;
        } 
@@ -238,7 +284,7 @@ mono_thread_info_insert (MonoThreadInfo *info)
 {
        MonoThreadHazardPointers *hp = mono_hazard_pointer_get ();
 
-       if (!mono_lls_insert (&thread_list, hp, (MonoLinkedListSetNode*)info)) {
+       if (!mono_lls_insert (&thread_list, hp, (MonoLinkedListSetNode*)info, HAZARD_FREE_SAFE_CTX)) {
                mono_hazard_pointer_clear_all (hp, -1);
                return FALSE;
        } 
@@ -254,7 +300,7 @@ mono_thread_info_remove (MonoThreadInfo *info)
        gboolean res;
 
        THREADS_DEBUG ("removing info %p\n", info);
-       res = mono_lls_remove (&thread_list, hp, (MonoLinkedListSetNode*)info);
+       res = mono_lls_remove (&thread_list, hp, (MonoLinkedListSetNode*)info, HAZARD_FREE_SAFE_CTX);
        mono_hazard_pointer_clear_all (hp, -1);
        return res;
 }
@@ -264,7 +310,7 @@ free_thread_info (gpointer mem)
 {
        MonoThreadInfo *info = (MonoThreadInfo *) mem;
 
-       MONO_SEM_DESTROY (&info->resume_semaphore);
+       mono_os_sem_destroy (&info->resume_semaphore);
        mono_threads_platform_free (info);
 
        g_free (info);
@@ -292,7 +338,7 @@ register_thread (MonoThreadInfo *info, gpointer baseptr)
        mono_thread_info_set_tid (info, mono_native_thread_id_get ());
        info->small_id = small_id;
 
-       MONO_SEM_INIT (&info->resume_semaphore, 0);
+       mono_os_sem_init (&info->resume_semaphore, 0);
 
        /*set TLS early so SMR works */
        mono_native_tls_set_value (thread_info_key, info);
@@ -313,6 +359,8 @@ register_thread (MonoThreadInfo *info, gpointer baseptr)
        info->stack_start_limit = staddr;
        info->stack_end = staddr + stsize;
 
+       info->stackdata = g_byte_array_new ();
+
        mono_threads_platform_register (info);
 
        /*
@@ -374,8 +422,13 @@ unregister_thread (void *arg)
 
        mono_thread_info_suspend_unlock ();
 
+       g_byte_array_free (info->stackdata, /*free_segment=*/TRUE);
+
        /*now it's safe to free the thread info.*/
-       mono_thread_hazardous_free_or_queue (info, free_thread_info, TRUE, FALSE);
+       mono_thread_hazardous_try_free (info, free_thread_info);
+       /* Pump the HP queue */
+       mono_thread_hazardous_try_free_some ();
+
        mono_thread_small_id_free (small_id);
 }
 
@@ -489,7 +542,7 @@ mono_threads_attach_tools_thread (void)
        g_assert (!mono_native_tls_get_value (thread_info_key));
        
        while (!mono_threads_inited) { 
-               g_usleep (10);
+               mono_thread_info_usleep (10);
        }
 
        info = mono_thread_info_attach (&dummy);
@@ -581,14 +634,16 @@ mono_threads_init (MonoThreadInfoCallbacks *callbacks, size_t info_size)
 #endif
        g_assert (res);
 
-       unified_suspend_enabled = g_getenv ("MONO_ENABLE_UNIFIED_SUSPEND") != NULL || MONO_THREADS_PLATFORM_REQUIRES_UNIFIED_SUSPEND;
+       unified_suspend_enabled = g_getenv ("MONO_ENABLE_UNIFIED_SUSPEND") != NULL || mono_threads_is_coop_enabled ();
 
-       MONO_SEM_INIT (&global_suspend_semaphore, 1);
-       MONO_SEM_INIT (&suspend_semaphore, 0);
+       mono_coop_sem_init (&global_suspend_semaphore, 1);
+       mono_os_sem_init (&suspend_semaphore, 0);
 
-       mono_lls_init (&thread_list, NULL);
+       mono_lls_init (&thread_list, NULL, HAZARD_FREE_NO_LOCK);
        mono_thread_smr_init ();
        mono_threads_init_platform ();
+       mono_threads_init_coop ();
+       mono_threads_init_abort_syscall ();
 
 #if defined(__MACH__)
        mono_mach_init (thread_info_key);
@@ -611,46 +666,6 @@ mono_threads_get_runtime_callbacks (void)
        return &runtime_callbacks;
 }
 
-/*
-The return value is only valid until a matching mono_thread_info_resume is called
-*/
-static MonoThreadInfo*
-mono_thread_info_suspend_sync (MonoNativeThreadId tid, gboolean interrupt_kernel, const char **error_condition)
-{
-       MonoThreadHazardPointers *hp = mono_hazard_pointer_get ();      
-       MonoThreadInfo *info = mono_thread_info_lookup (tid); /*info on HP1*/
-       if (!info) {
-               *error_condition = "Thread not found";
-               return NULL;
-       }
-
-       switch (mono_threads_transition_request_async_suspension (info)) {
-       case AsyncSuspendAlreadySuspended:
-               mono_hazard_pointer_clear (hp, 1); //XXX this is questionable we got to clean the suspend/resume nonsense of critical sections
-               return info;
-       case AsyncSuspendWait:
-               mono_threads_add_to_pending_operation_set (info);
-               break;
-       case AsyncSuspendInitSuspend:
-               if (!mono_threads_core_begin_async_suspend (info, interrupt_kernel)) {
-                       mono_hazard_pointer_clear (hp, 1);
-                       *error_condition = "Could not suspend thread";
-                       return NULL;
-               }
-       }
-
-       //Wait for the pending suspend to finish
-       mono_threads_wait_pending_operations ();
-
-       if (!mono_threads_core_check_suspend_result (info)) {
-
-               mono_hazard_pointer_clear (hp, 1);
-               *error_condition = "Post suspend failed";
-               return NULL;
-       }
-       return info;
-}
-
 /*
 Signal that the current thread wants to be suspended.
 This function can be called without holding the suspend lock held.
@@ -759,16 +774,17 @@ cleanup:
 }
 
 gboolean
-mono_thread_info_begin_suspend (MonoThreadInfo *info, gboolean interrupt_kernel)
+mono_thread_info_begin_suspend (MonoThreadInfo *info)
 {
        switch (mono_threads_transition_request_async_suspension (info)) {
        case AsyncSuspendAlreadySuspended:
+       case AsyncSuspendBlocking:
                return TRUE;
        case AsyncSuspendWait:
                mono_threads_add_to_pending_operation_set (info);
                return TRUE;
        case AsyncSuspendInitSuspend:
-               return mono_threads_core_begin_async_suspend (info, interrupt_kernel);
+               return begin_async_suspend (info, FALSE);
        default:
                g_assert_not_reached ();
        }
@@ -780,6 +796,12 @@ mono_thread_info_begin_resume (MonoThreadInfo *info)
        return mono_thread_info_core_resume (info);
 }
 
+gboolean
+mono_thread_info_check_suspend_result (MonoThreadInfo *info)
+{
+       return check_async_suspend (info);
+}
+
 /*
 FIXME fix cardtable WB to be out of line and check with the runtime if the target is not the
 WB trampoline. Another option is to encode wb ranges in MonoJitInfo, but that is somewhat hard.
@@ -829,14 +851,56 @@ mono_thread_info_in_critical_location (MonoThreadInfo *info)
        return is_thread_in_critical_region (info);
 }
 
+/*
+The return value is only valid until a matching mono_thread_info_resume is called
+*/
+static MonoThreadInfo*
+suspend_sync (MonoNativeThreadId tid, gboolean interrupt_kernel)
+{
+       MonoThreadHazardPointers *hp = mono_hazard_pointer_get ();
+       MonoThreadInfo *info = mono_thread_info_lookup (tid); /*info on HP1*/
+       if (!info)
+               return NULL;
+
+       switch (mono_threads_transition_request_async_suspension (info)) {
+       case AsyncSuspendAlreadySuspended:
+               mono_hazard_pointer_clear (hp, 1); //XXX this is questionable we got to clean the suspend/resume nonsense of critical sections
+               return info;
+       case AsyncSuspendWait:
+               mono_threads_add_to_pending_operation_set (info);
+               break;
+       case AsyncSuspendInitSuspend:
+               if (!begin_async_suspend (info, interrupt_kernel)) {
+                       mono_hazard_pointer_clear (hp, 1);
+                       return NULL;
+               }
+               break;
+       case AsyncSuspendBlocking:
+               if (interrupt_kernel)
+                       mono_threads_core_abort_syscall (info);
+
+               break;
+       default:
+               g_assert_not_reached ();
+       }
+
+       //Wait for the pending suspend to finish
+       mono_threads_wait_pending_operations ();
+
+       if (!check_async_suspend (info)) {
+               mono_hazard_pointer_clear (hp, 1);
+               return NULL;
+       }
+       return info;
+}
+
 static MonoThreadInfo*
 suspend_sync_nolock (MonoNativeThreadId id, gboolean interrupt_kernel)
 {
        MonoThreadInfo *info = NULL;
        int sleep_duration = 0;
        for (;;) {
-               const char *suspend_error = "Unknown error";
-               if (!(info = mono_thread_info_suspend_sync (id, interrupt_kernel, &suspend_error))) {
+               if (!(info = suspend_sync (id, interrupt_kernel))) {
                        mono_hazard_pointer_clear (mono_hazard_pointer_get (), 1);
                        return NULL;
                }
@@ -854,16 +918,11 @@ suspend_sync_nolock (MonoNativeThreadId id, gboolean interrupt_kernel)
                /* Wait for the pending resume to finish */
                mono_threads_wait_pending_operations ();
 
-               if (!sleep_duration) {
-#ifdef HOST_WIN32
-                       SwitchToThread ();
-#else
-                       sched_yield ();
-#endif
-               }
-               else {
+               if (sleep_duration == 0)
+                       mono_thread_info_yield ();
+               else
                        g_usleep (sleep_duration);
-               }
+
                sleep_duration += 10;
        }
        return info;
@@ -906,38 +965,6 @@ done:
        mono_thread_info_suspend_unlock ();
 }
 
-/*
-WARNING:
-If we are trying to suspend a target that is on a critical region
-and running a syscall we risk looping forever if @interrupt_kernel is FALSE.
-So, be VERY carefull in calling this with @interrupt_kernel == FALSE.
-
-Info is not put on a hazard pointer as a suspended thread cannot exit and be freed.
-
-This function MUST be matched with mono_thread_info_finish_suspend or mono_thread_info_finish_suspend_and_resume
-*/
-MonoThreadInfo*
-mono_thread_info_safe_suspend_sync (MonoNativeThreadId id, gboolean interrupt_kernel)
-{
-       MonoThreadInfo *info = NULL;
-
-       THREADS_SUSPEND_DEBUG ("SUSPENDING tid %p\n", (void*)id);
-       /*FIXME: unify this with self-suspend*/
-       g_assert (id != mono_native_thread_id_get ());
-
-       mono_thread_info_suspend_lock ();
-       mono_threads_begin_global_suspend ();
-
-       info = suspend_sync_nolock (id, interrupt_kernel);
-
-       /* XXX this clears HP 1, so we restated it again */
-       // mono_atomic_store_release (&mono_thread_info_current ()->inside_critical_region, TRUE);
-       mono_threads_end_global_suspend ();
-       mono_thread_info_suspend_unlock ();
-
-       return info;
-}
-
 /**
 Inject an assynchronous call into the target thread. The target thread must be suspended and
 only a single async call can be setup for a given suspend cycle.
@@ -965,15 +992,14 @@ STW to make sure no unsafe pending suspend is in progress.
 void
 mono_thread_info_suspend_lock (void)
 {
-       MONO_TRY_BLOCKING;
-       MONO_SEM_WAIT_UNITERRUPTIBLE (&global_suspend_semaphore);
-       MONO_FINISH_TRY_BLOCKING;
+       int res = mono_coop_sem_wait (&global_suspend_semaphore, MONO_SEM_FLAGS_NONE);
+       g_assert (res != -1);
 }
 
 void
 mono_thread_info_suspend_unlock (void)
 {
-       MONO_SEM_POST (&global_suspend_semaphore);
+       mono_coop_sem_post (&global_suspend_semaphore);
 }
 
 /*
@@ -991,16 +1017,16 @@ mono_thread_info_abort_socket_syscall_for_close (MonoNativeThreadId tid)
 {
        MonoThreadHazardPointers *hp;
        MonoThreadInfo *info;
-       
+
        if (tid == mono_native_thread_id_get () || !mono_threads_core_needs_abort_syscall ())
                return;
 
-       hp = mono_hazard_pointer_get ();        
-       info = mono_thread_info_lookup (tid); /*info on HP1*/
+       hp = mono_hazard_pointer_get ();
+       info = mono_thread_info_lookup (tid);
        if (!info)
                return;
 
-       if (mono_thread_info_run_state (info) > STATE_RUNNING) {
+       if (mono_thread_info_run_state (info) == STATE_DETACHED) {
                mono_hazard_pointer_clear (hp, 1);
                return;
        }
@@ -1087,6 +1113,148 @@ mono_thread_info_yield (void)
 {
        return mono_threads_core_yield ();
 }
+static mono_lazy_init_t sleep_init = MONO_LAZY_INIT_STATUS_NOT_INITIALIZED;
+static MonoCoopMutex sleep_mutex;
+static MonoCoopCond sleep_cond;
+
+static void
+sleep_initialize (void)
+{
+       mono_coop_mutex_init (&sleep_mutex);
+       mono_coop_cond_init (&sleep_cond);
+}
+
+static void
+sleep_interrupt (gpointer data)
+{
+       mono_coop_mutex_lock (&sleep_mutex);
+       mono_coop_cond_broadcast (&sleep_cond);
+       mono_coop_mutex_unlock (&sleep_mutex);
+}
+
+static inline guint32
+sleep_interruptable (guint32 ms, gboolean *alerted)
+{
+       gint64 now, end;
+
+       g_assert (INFINITE == G_MAXUINT32);
+
+       g_assert (alerted);
+       *alerted = FALSE;
+
+       if (ms != INFINITE)
+               end = mono_100ns_ticks () + (ms * 1000 * 10);
+
+       mono_lazy_initialize (&sleep_init, sleep_initialize);
+
+       mono_coop_mutex_lock (&sleep_mutex);
+
+       for (;;) {
+               if (ms != INFINITE) {
+                       now = mono_100ns_ticks ();
+                       if (now > end)
+                               break;
+               }
+
+               mono_thread_info_install_interrupt (sleep_interrupt, NULL, alerted);
+               if (*alerted) {
+                       mono_coop_mutex_unlock (&sleep_mutex);
+                       return WAIT_IO_COMPLETION;
+               }
+
+               if (ms != INFINITE)
+                       mono_coop_cond_timedwait (&sleep_cond, &sleep_mutex, (end - now) / 10 / 1000);
+               else
+                       mono_coop_cond_wait (&sleep_cond, &sleep_mutex);
+
+               mono_thread_info_uninstall_interrupt (alerted);
+               if (*alerted) {
+                       mono_coop_mutex_unlock (&sleep_mutex);
+                       return WAIT_IO_COMPLETION;
+               }
+       }
+
+       mono_coop_mutex_unlock (&sleep_mutex);
+
+       return 0;
+}
+
+gint
+mono_thread_info_sleep (guint32 ms, gboolean *alerted)
+{
+       if (ms == 0) {
+               MonoThreadInfo *info;
+
+               mono_thread_info_yield ();
+
+               info = mono_thread_info_current ();
+               if (info && mono_thread_info_is_interrupt_state (info))
+                       return WAIT_IO_COMPLETION;
+
+               return 0;
+       }
+
+       if (alerted)
+               return sleep_interruptable (ms, alerted);
+
+       MONO_PREPARE_BLOCKING;
+
+       if (ms == INFINITE) {
+               do {
+#ifdef HOST_WIN32
+                       Sleep (G_MAXUINT32);
+#else
+                       sleep (G_MAXUINT32);
+#endif
+               } while (1);
+       } else {
+               int ret;
+#if defined (__linux__) && !defined(PLATFORM_ANDROID)
+               struct timespec start, target;
+
+               /* Use clock_nanosleep () to prevent time drifting problems when nanosleep () is interrupted by signals */
+               ret = clock_gettime (CLOCK_MONOTONIC, &start);
+               g_assert (ret == 0);
+
+               target = start;
+               target.tv_sec += ms / 1000;
+               target.tv_nsec += (ms % 1000) * 1000000;
+               if (target.tv_nsec > 999999999) {
+                       target.tv_nsec -= 999999999;
+                       target.tv_sec ++;
+               }
+
+               do {
+                       ret = clock_nanosleep (CLOCK_MONOTONIC, TIMER_ABSTIME, &target, NULL);
+               } while (ret != 0);
+#elif HOST_WIN32
+               Sleep (ms);
+#else
+               struct timespec req, rem;
+
+               req.tv_sec = ms / 1000;
+               req.tv_nsec = (ms % 1000) * 1000000;
+
+               do {
+                       memset (&rem, 0, sizeof (rem));
+                       ret = nanosleep (&req, &rem);
+               } while (ret != 0);
+#endif /* __linux__ */
+       }
+
+       MONO_FINISH_BLOCKING;
+
+       return 0;
+}
+
+gint
+mono_thread_info_usleep (guint64 us)
+{
+       MONO_PREPARE_BLOCKING;
+       g_usleep (us);
+       MONO_FINISH_BLOCKING;
+       return 0;
+}
 
 gpointer
 mono_thread_info_tls_get (THREAD_INFO_TYPE *info, MonoTlsKey key)
@@ -1190,7 +1358,7 @@ mono_thread_info_install_interrupt (void (*callback) (gpointer data), gpointer d
        token->callback = callback;
        token->data = data;
 
-       previous_token = InterlockedCompareExchangePointer ((gpointer*) &info->interrupt_token, token, NULL);
+       previous_token = (MonoThreadInfoInterruptToken *)InterlockedCompareExchangePointer ((gpointer*) &info->interrupt_token, token, NULL);
 
        if (previous_token) {
                if (previous_token != INTERRUPT_STATE)
@@ -1217,7 +1385,7 @@ mono_thread_info_uninstall_interrupt (gboolean *interrupted)
        info = mono_thread_info_current ();
        g_assert (info);
 
-       previous_token = InterlockedExchangePointer ((gpointer*) &info->interrupt_token, NULL);
+       previous_token = (MonoThreadInfoInterruptToken *)InterlockedExchangePointer ((gpointer*) &info->interrupt_token, NULL);
 
        /* only the installer can uninstall the token */
        g_assert (previous_token);
@@ -1326,7 +1494,7 @@ mono_thread_info_clear_self_interrupt ()
        info = mono_thread_info_current ();
        g_assert (info);
 
-       previous_token = InterlockedCompareExchangePointer ((gpointer*) &info->interrupt_token, NULL, INTERRUPT_STATE);
+       previous_token = (MonoThreadInfoInterruptToken *)InterlockedCompareExchangePointer ((gpointer*) &info->interrupt_token, NULL, INTERRUPT_STATE);
        g_assert (previous_token == NULL || previous_token == INTERRUPT_STATE);
 
        THREADS_INTERRUPT_DEBUG ("interrupt clear self tid %p previous_token %p\n", mono_thread_info_get_tid (info), previous_token);