gboolean mono_threads_core_begin_async_suspend (MonoThreadInfo *info, gboolean interrupt_kernel) { kern_return_t ret; gboolean res; g_assert (info); ret = thread_suspend (info->native_handle); THREADS_SUSPEND_DEBUG ("SUSPEND %p -> %d\n", (void*)info->native_handle, ret); if (ret != KERN_SUCCESS) return FALSE; /* We're in the middle of a self-suspend, resume and register */ if (!mono_threads_transition_finish_async_suspend (info)) { mono_threads_add_to_pending_operation_set (info); g_assert (thread_resume (info->native_handle) == KERN_SUCCESS); THREADS_SUSPEND_DEBUG ("FAILSAFE RESUME/1 %p -> %d\n", (void*)info->native_handle, 0); //XXX interrupt_kernel doesn't make sense in this case as the target is not in a syscall return TRUE; } res = mono_threads_get_runtime_callbacks ()-> thread_state_init_from_handle (&info->thread_saved_state [ASYNC_SUSPEND_STATE_INDEX], info); THREADS_SUSPEND_DEBUG ("thread state %p -> %d\n", (void*)info->native_handle, res); if (res) { if (interrupt_kernel) thread_abort (info->native_handle); } else { mono_threads_transition_async_suspend_compensation (info); g_assert (thread_resume (info->native_handle) == KERN_SUCCESS); THREADS_SUSPEND_DEBUG ("FAILSAFE RESUME/2 %p -> %d\n", (void*)info->native_handle, 0); } return res; }
gboolean mono_threads_core_begin_async_suspend (MonoThreadInfo *info, gboolean interrupt_kernel) { DWORD id = mono_thread_info_get_tid (info); HANDLE handle; DWORD result; gboolean res; handle = OpenThread (THREAD_ALL_ACCESS, FALSE, id); g_assert (handle); result = SuspendThread (handle); THREADS_SUSPEND_DEBUG ("SUSPEND %p -> %d\n", (void*)id, ret); if (result == (DWORD)-1) { CloseHandle (handle); return FALSE; } /* We're in the middle of a self-suspend, resume and register */ if (!mono_threads_transition_finish_async_suspend (info)) { mono_threads_add_to_pending_operation_set (info); result = ResumeThread (handle); g_assert (result == 1); CloseHandle (handle); THREADS_SUSPEND_DEBUG ("FAILSAFE RESUME/1 %p -> %d\n", (void*)id, 0); //XXX interrupt_kernel doesn't make sense in this case as the target is not in a syscall return TRUE; } res = mono_threads_get_runtime_callbacks ()->thread_state_init_from_handle (&info->thread_saved_state [ASYNC_SUSPEND_STATE_INDEX], info); THREADS_SUSPEND_DEBUG ("thread state %p -> %d\n", (void*)id, res); if (res) { //FIXME do we need to QueueUserAPC on this case? if (interrupt_kernel) QueueUserAPC ((PAPCFUNC)interrupt_apc, handle, (ULONG_PTR)NULL); } else { mono_threads_transition_async_suspend_compensation (info); result = ResumeThread (handle); g_assert (result == 1); THREADS_SUSPEND_DEBUG ("FAILSAFE RESUME/2 %p -> %d\n", (void*)info->native_handle, 0); } CloseHandle (handle); return res; }
static void suspend_signal_handler (int _dummy, siginfo_t *info, void *context) { #if defined(__native_client__) g_assert_not_reached (); #else int old_errno = errno; int hp_save_index = mono_hazard_pointer_save_for_signal_handler (); MonoThreadInfo *current = mono_thread_info_current (); gboolean ret; THREADS_SUSPEND_DEBUG ("SIGNAL HANDLER FOR %p [%p]\n", current, (void*)current->native_handle); if (current->syscall_break_signal) { current->syscall_break_signal = FALSE; THREADS_SUSPEND_DEBUG ("\tsyscall break for %p\n", current); mono_threads_notify_initiator_of_abort (current); goto done; } /* Have we raced with self suspend? */ if (!mono_threads_transition_finish_async_suspend (current)) { current->suspend_can_continue = TRUE; THREADS_SUSPEND_DEBUG ("\tlost race with self suspend %p\n", current); goto done; } ret = mono_threads_get_runtime_callbacks ()->thread_state_init_from_sigctx (¤t->thread_saved_state [ASYNC_SUSPEND_STATE_INDEX], context); /* thread_state_init_from_sigctx return FALSE if the current thread is detaching and suspend can't continue. */ current->suspend_can_continue = ret; /* Block the restart signal. We need to block the restart signal while posting to the suspend_ack semaphore or we race to sigsuspend, which might miss the signal and get stuck. */ pthread_sigmask (SIG_BLOCK, &suspend_ack_signal_mask, NULL); /* This thread is doomed, all we can do is give up and let the suspender recover. */ if (!ret) { THREADS_SUSPEND_DEBUG ("\tThread is dying, failed to capture state %p\n", current); mono_threads_transition_async_suspend_compensation (current); /* We're done suspending */ mono_threads_notify_initiator_of_suspend (current); /* Unblock the restart signal. */ pthread_sigmask (SIG_UNBLOCK, &suspend_ack_signal_mask, NULL); goto done; } /* We're done suspending */ mono_threads_notify_initiator_of_suspend (current); do { current->signal = 0; sigsuspend (&suspend_signal_mask); } while (current->signal != restart_signal_num); /* Unblock the restart signal. */ pthread_sigmask (SIG_UNBLOCK, &suspend_ack_signal_mask, NULL); if (current->async_target) { #if MONO_ARCH_HAS_MONO_CONTEXT MonoContext tmp = current->thread_saved_state [ASYNC_SUSPEND_STATE_INDEX].ctx; mono_threads_get_runtime_callbacks ()->setup_async_callback (&tmp, current->async_target, current->user_data); current->async_target = current->user_data = NULL; mono_monoctx_to_sigctx (&tmp, context); #else g_error ("The new interruption machinery requires a working mono-context"); #endif } /* We're done resuming */ mono_threads_notify_initiator_of_resume (current); done: mono_hazard_pointer_restore_for_signal_handler (hp_save_index); errno = old_errno; #endif }