+/* Unified suspend code */
+
+static gboolean
+sgen_is_thread_in_current_stw (SgenThreadInfo *info)
+{
+ /*
+ A thread explicitly asked to be skiped because it holds no managed state.
+ This is used by TP and finalizer threads.
+ FIXME Use an atomic variable for this to avoid everyone taking the GC LOCK.
+ */
+ if (info->client_info.gc_disabled) {
+ return FALSE;
+ }
+
+ /*
+ We have detected that this thread is failing/dying, ignore it.
+ FIXME: can't we merge this with thread_is_dying?
+ */
+ if (info->client_info.skip) {
+ return FALSE;
+ }
+
+ /*
+ Suspending the current thread will deadlock us, bad idea.
+ */
+ if (info == mono_thread_info_current ()) {
+ return FALSE;
+ }
+
+ /*
+ We can't suspend the workers that will do all the heavy lifting.
+ FIXME Use some state bit in SgenThreadInfo for this.
+ */
+ if (sgen_thread_pool_is_thread_pool_thread (mono_thread_info_get_tid (info))) {
+ return FALSE;
+ }
+
+ /*
+ The thread has signaled that it started to detach, ignore it.
+ FIXME: can't we merge this with skip
+ */
+ if (!mono_thread_info_is_live (info)) {
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+static void
+update_sgen_info (SgenThreadInfo *info)
+{
+ char *stack_start;
+
+ /* Once we remove the old suspend code, we should move sgen to directly access the state in MonoThread */
+ info->client_info.stopped_domain = mono_thread_info_tls_get (info, TLS_KEY_DOMAIN);
+ info->client_info.stopped_ip = (gpointer) MONO_CONTEXT_GET_IP (&mono_thread_info_get_suspend_state (info)->ctx);
+ stack_start = (char*)MONO_CONTEXT_GET_SP (&mono_thread_info_get_suspend_state (info)->ctx) - REDZONE_SIZE;
+
+ /* altstack signal handler, sgen can't handle them, mono-threads should have handled this. */
+ if (stack_start < (char*)info->client_info.stack_start_limit || stack_start >= (char*)info->client_info.stack_end)
+ g_error ("BAD STACK");
+
+ info->client_info.stack_start = stack_start;
+#ifdef USE_MONO_CTX
+ info->client_info.ctx = mono_thread_info_get_suspend_state (info)->ctx;
+#else
+ g_assert_not_reached ();
+#endif
+}
+
+static void
+sgen_unified_suspend_stop_world (void)
+{
+ int restart_counter;
+ SgenThreadInfo *info;
+ int sleep_duration = -1;
+
+ mono_threads_begin_global_suspend ();
+ THREADS_STW_DEBUG ("[GC-STW-BEGIN] *** BEGIN SUSPEND *** \n");
+
+ FOREACH_THREAD_SAFE (info) {
+ info->client_info.skip = FALSE;
+ info->client_info.suspend_done = FALSE;
+ if (sgen_is_thread_in_current_stw (info)) {
+ info->client_info.skip = !mono_thread_info_begin_suspend (info, FALSE);
+ THREADS_STW_DEBUG ("[GC-STW-BEGIN-SUSPEND] SUSPEND thread %p skip %d\n", mono_thread_info_get_tid (info), info->client_info.skip);
+ } else {
+ THREADS_STW_DEBUG ("[GC-STW-BEGIN-SUSPEND] IGNORE thread %p skip %d\n", mono_thread_info_get_tid (info), info->skip);
+ }
+ } END_FOREACH_THREAD_SAFE
+
+ mono_thread_info_current ()->client_info.suspend_done = TRUE;
+ mono_threads_wait_pending_operations ();
+
+ for (;;) {
+ restart_counter = 0;
+ FOREACH_THREAD_SAFE (info) {
+ if (info->client_info.suspend_done || !sgen_is_thread_in_current_stw (info)) {
+ THREADS_STW_DEBUG ("[GC-STW-RESTART] IGNORE thread %p not been processed done %d current %d\n", mono_thread_info_get_tid (info), info->client_info.suspend_done, !sgen_is_thread_in_current_stw (info));
+ continue;
+ }
+
+ /*
+ All threads that reach here are pristine suspended. This means the following:
+
+ - We haven't accepted the previous suspend as good.
+ - We haven't gave up on it for this STW (it's either bad or asked not to)
+ */
+ if (!mono_threads_core_check_suspend_result (info)) {
+ THREADS_STW_DEBUG ("[GC-STW-RESTART] SKIP thread %p failed to finish to suspend\n", mono_thread_info_get_tid (info));
+ info->client_info.skip = TRUE;
+ } else if (mono_thread_info_in_critical_location (info)) {
+ gboolean res;
+ g_assert (mono_thread_info_suspend_count (info) == 1);
+ res = mono_thread_info_begin_resume (info);
+ THREADS_STW_DEBUG ("[GC-STW-RESTART] RESTART thread %p skip %d\n", mono_thread_info_get_tid (info), res);
+ if (res)
+ ++restart_counter;
+ else
+ info->client_info.skip = TRUE;
+ } else {
+ THREADS_STW_DEBUG ("[GC-STW-RESTART] DONE thread %p deemed fully suspended\n", mono_thread_info_get_tid (info));
+ g_assert (!info->client_info.in_critical_region);
+ info->client_info.suspend_done = TRUE;
+ }
+ } END_FOREACH_THREAD_SAFE
+
+ if (restart_counter == 0)
+ break;
+ mono_threads_wait_pending_operations ();
+
+ if (sleep_duration < 0) {
+#ifdef HOST_WIN32
+ SwitchToThread ();
+#else
+ sched_yield ();
+#endif
+ sleep_duration = 0;
+ } else {
+ g_usleep (sleep_duration);
+ sleep_duration += 10;
+ }
+
+ FOREACH_THREAD_SAFE (info) {
+ if (sgen_is_thread_in_current_stw (info) && mono_thread_info_is_running (info)) {
+ gboolean res = mono_thread_info_begin_suspend (info, FALSE);
+ THREADS_STW_DEBUG ("[GC-STW-RESTART] SUSPEND thread %p skip %d\n", mono_thread_info_get_tid (info), res);
+ if (!res)
+ info->client_info.skip = TRUE;
+ }
+ } END_FOREACH_THREAD_SAFE
+
+ mono_threads_wait_pending_operations ();
+ }
+
+ FOREACH_THREAD_SAFE (info) {
+ if (sgen_is_thread_in_current_stw (info)) {
+ THREADS_STW_DEBUG ("[GC-STW-SUSPEND-END] thread %p is suspended\n", mono_thread_info_get_tid (info));
+ g_assert (info->client_info.suspend_done);
+ update_sgen_info (info);
+ } else {
+ g_assert (!info->client_info.suspend_done || info == mono_thread_info_current ());
+ }
+ } END_FOREACH_THREAD_SAFE
+}
+
+static void
+sgen_unified_suspend_restart_world (void)
+{
+ SgenThreadInfo *info;
+
+ THREADS_STW_DEBUG ("[GC-STW-END] *** BEGIN RESUME ***\n");
+ FOREACH_THREAD_SAFE (info) {
+ if (sgen_is_thread_in_current_stw (info)) {
+ g_assert (mono_thread_info_begin_resume (info));
+ THREADS_STW_DEBUG ("[GC-STW-RESUME-WORLD] RESUME thread %p\n", mono_thread_info_get_tid (info));
+ } else {
+ THREADS_STW_DEBUG ("[GC-STW-RESUME-WORLD] IGNORE thread %p\n", mono_thread_info_get_tid (info));
+ }
+ } END_FOREACH_THREAD_SAFE
+
+ mono_threads_wait_pending_operations ();
+ mono_threads_end_global_suspend ();
+}