2 * sgen-os-mach.c: Mach-OS support.
5 * Paolo Molaro (lupus@ximian.com)
6 * Mark Probst (mprobst@novell.com)
7 * Geoff Norton (gnorton@novell.com)
9 * Copyright 2010 Novell, Inc (http://www.novell.com)
10 * Copyright (C) 2012 Xamarin Inc
12 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
20 #include "sgen/sgen-gc.h"
21 #include "sgen/sgen-archdep.h"
22 #include "sgen/sgen-protocol.h"
23 #include "sgen/sgen-thread-pool.h"
24 #include "metadata/object-internals.h"
25 #include "metadata/gc-internals.h"
28 #include "utils/mach-support.h"
31 #if defined(__MACH__) && MONO_MACH_ARCH_SUPPORTED
33 #if !defined(USE_COOP_GC)
35 sgen_resume_thread (SgenThreadInfo *info)
39 ret = thread_resume (info->client_info.info.native_handle);
40 } while (ret == KERN_ABORTED);
41 return ret == KERN_SUCCESS;
45 sgen_suspend_thread (SgenThreadInfo *info)
47 mach_msg_type_number_t num_state;
55 state = (thread_state_t) alloca (mono_mach_arch_get_thread_state_size ());
56 mctx = (mcontext_t) alloca (mono_mach_arch_get_mcontext_size ());
59 ret = thread_suspend (info->client_info.info.native_handle);
60 } while (ret == KERN_ABORTED);
61 if (ret != KERN_SUCCESS)
65 ret = mono_mach_arch_get_thread_state (info->client_info.info.native_handle, state, &num_state);
66 } while (ret == KERN_ABORTED);
67 if (ret != KERN_SUCCESS)
70 mono_mach_arch_thread_state_to_mcontext (state, mctx);
71 ctx.uc_mcontext = mctx;
73 info->client_info.stopped_domain = mono_thread_info_tls_get (info, TLS_KEY_DOMAIN);
74 info->client_info.stopped_ip = (gpointer) mono_mach_arch_get_ip (state);
75 info->client_info.stack_start = NULL;
76 stack_start = (char*) mono_mach_arch_get_sp (state) - REDZONE_SIZE;
77 /* If stack_start is not within the limits, then don't set it in info and we will be restarted. */
78 if (stack_start >= info->client_info.stack_start_limit && stack_start <= info->client_info.stack_end) {
79 info->client_info.stack_start = stack_start;
81 mono_sigctx_to_monoctx (&ctx, &info->client_info.ctx);
83 g_assert (!info->client_info.stack_start);
87 if (mono_gc_get_gc_callbacks ()->thread_suspend_func)
88 mono_gc_get_gc_callbacks ()->thread_suspend_func (info->client_info.runtime_data, &ctx, NULL);
90 SGEN_LOG (2, "thread %p stopped at %p stack_start=%p", (void*)(gsize)info->client_info.info.native_handle, info->client_info.stopped_ip, info->client_info.stack_start);
91 binary_protocol_thread_suspend ((gpointer)mono_thread_info_get_tid (info), info->client_info.stopped_ip);
97 sgen_wait_for_suspend_ack (int count)
99 /* mach thread_resume is synchronous so we dont need to wait for them */
102 /* LOCKING: assumes the GC lock is held */
104 sgen_thread_handshake (BOOL suspend)
106 SgenThreadInfo *cur_thread = mono_thread_info_current ();
111 cur_thread->client_info.suspend_done = TRUE;
112 FOREACH_THREAD (info) {
113 if (info == cur_thread || sgen_thread_pool_is_thread_pool_thread (mono_thread_info_get_tid (info)))
116 info->client_info.suspend_done = FALSE;
117 if (info->client_info.gc_disabled)
121 if (!sgen_suspend_thread (info))
125 ret = thread_resume (info->client_info.info.native_handle);
126 } while (ret == KERN_ABORTED);
127 if (ret != KERN_SUCCESS)
141 mono_gc_get_suspend_signal (void)
147 mono_gc_get_restart_signal (void)