2 * mono-threads-mach.c: Low-level threading, mach version
5 * Rodrigo Kumpera (kumpera@gmail.com)
14 /* For pthread_main_np, pthread_get_stackaddr_np and pthread_get_stacksize_np */
15 #define _DARWIN_C_SOURCE 1
17 #include <mono/utils/mach-support.h>
18 #include <mono/utils/mono-compiler.h>
19 #include <mono/utils/mono-semaphore.h>
20 #include <mono/utils/mono-threads.h>
21 #include <mono/utils/hazard-pointer.h>
22 #include <mono/utils/mono-mmap.h>
25 mono_threads_init_platform (void)
27 mono_threads_init_dead_letter ();
31 mono_threads_core_interrupt (MonoThreadInfo *info)
33 thread_abort (info->native_handle);
37 mono_threads_core_abort_syscall (MonoThreadInfo *info)
42 mono_threads_core_needs_abort_syscall (void)
48 mono_threads_core_suspend (MonoThreadInfo *info, gboolean interrupt_kernel)
55 ret = thread_suspend (info->native_handle);
56 if (ret != KERN_SUCCESS)
58 res = mono_threads_get_runtime_callbacks ()->
59 thread_state_init_from_handle (&info->suspend_state, info);
61 thread_resume (info->native_handle);
66 mono_threads_core_resume (MonoThreadInfo *info)
70 if (info->async_target) {
71 MonoContext tmp = info->suspend_state.ctx;
72 mach_msg_type_number_t num_state;
77 mono_threads_get_runtime_callbacks ()->setup_async_callback (&tmp, info->async_target, info->user_data);
78 info->async_target = info->user_data = NULL;
80 state = (thread_state_t) alloca (mono_mach_arch_get_thread_state_size ());
81 mctx = (mcontext_t) alloca (mono_mach_arch_get_mcontext_size ());
83 ret = mono_mach_arch_get_thread_state (info->native_handle, state, &num_state);
84 if (ret != KERN_SUCCESS)
87 mono_mach_arch_thread_state_to_mcontext (state, mctx);
89 g_assert_not_reached ();
91 uctx.uc_mcontext = mctx;
93 mono_monoctx_to_sigctx (&tmp, &uctx);
95 mono_mach_arch_mcontext_to_thread_state (mctx, state);
97 ret = mono_mach_arch_set_thread_state (info->native_handle, state, num_state);
98 if (ret != KERN_SUCCESS)
103 ret = thread_resume (info->native_handle);
104 return ret == KERN_SUCCESS;
108 mono_threads_platform_register (MonoThreadInfo *info)
110 info->native_handle = mach_thread_self ();
111 mono_threads_install_dead_letter ();
115 mono_threads_platform_free (MonoThreadInfo *info)
117 mach_port_deallocate (current_task (), info->native_handle);
121 mono_native_thread_id_get (void)
123 return pthread_self ();
127 mono_native_thread_id_equals (MonoNativeThreadId id1, MonoNativeThreadId id2)
129 return pthread_equal (id1, id2);
133 * mono_native_thread_create:
135 * Low level thread creation function without any GC wrappers.
138 mono_native_thread_create (MonoNativeThreadId *tid, gpointer func, gpointer arg)
140 return pthread_create (tid, NULL, func, arg) == 0;
144 mono_threads_core_set_name (MonoNativeThreadId tid, const char *name)
146 /* pthread_setnmae_np() on Mac is not documented and doesn't receive thread id. */
150 mono_threads_core_get_stack_bounds (guint8 **staddr, size_t *stsize)
152 *staddr = (guint8*)pthread_get_stackaddr_np (pthread_self());
153 *stsize = pthread_get_stacksize_np (pthread_self());
157 * Mavericks reports stack sizes as 512kb:
158 * http://permalink.gmane.org/gmane.comp.java.openjdk.hotspot.devel/11590
159 * https://bugs.openjdk.java.net/browse/JDK-8020753
161 if (pthread_main_np () && *stsize == 512 * 1024)
162 *stsize = 2048 * mono_pagesize ();
165 /* staddr points to the start of the stack, not the end */