2 * mono-threads-mach.c: Low-level threading, mach version
5 * Rodrigo Kumpera (kumpera@gmail.com)
12 /* For pthread_main_np, pthread_get_stackaddr_np and pthread_get_stacksize_np */
13 #if defined (__MACH__)
14 #define _DARWIN_C_SOURCE 1
17 #include <mono/utils/mono-threads.h>
18 #include <mono/utils/mono-mmap.h>
20 #if defined (USE_MACH_BACKEND)
22 #include <mono/utils/mach-support.h>
23 #include <mono/utils/mono-compiler.h>
24 #include <mono/utils/mono-threads.h>
25 #include <mono/utils/hazard-pointer.h>
28 mono_threads_init_platform (void)
30 mono_threads_init_dead_letter ();
33 #if defined(HOST_WATCHOS) || defined(HOST_TVOS)
36 mono_threads_core_begin_async_suspend (MonoThreadInfo *info, gboolean interrupt_kernel)
38 g_assert_not_reached ();
42 mono_threads_core_check_suspend_result (MonoThreadInfo *info)
44 g_assert_not_reached ();
48 mono_threads_core_begin_async_resume (MonoThreadInfo *info)
50 g_assert_not_reached ();
53 #else /* defined(HOST_WATCHOS) || defined(HOST_TVOS) */
56 mono_threads_core_begin_async_suspend (MonoThreadInfo *info, gboolean interrupt_kernel)
63 ret = thread_suspend (info->native_handle);
64 THREADS_SUSPEND_DEBUG ("SUSPEND %p -> %d\n", (void*)info->native_handle, ret);
65 if (ret != KERN_SUCCESS)
68 /* We're in the middle of a self-suspend, resume and register */
69 if (!mono_threads_transition_finish_async_suspend (info)) {
70 mono_threads_add_to_pending_operation_set (info);
71 g_assert (thread_resume (info->native_handle) == KERN_SUCCESS);
72 THREADS_SUSPEND_DEBUG ("FAILSAFE RESUME/1 %p -> %d\n", (void*)info->native_handle, 0);
73 //XXX interrupt_kernel doesn't make sense in this case as the target is not in a syscall
76 res = mono_threads_get_runtime_callbacks ()->
77 thread_state_init_from_handle (&info->thread_saved_state [ASYNC_SUSPEND_STATE_INDEX], info);
78 THREADS_SUSPEND_DEBUG ("thread state %p -> %d\n", (void*)info->native_handle, res);
81 thread_abort (info->native_handle);
83 mono_threads_transition_async_suspend_compensation (info);
84 g_assert (thread_resume (info->native_handle) == KERN_SUCCESS);
85 THREADS_SUSPEND_DEBUG ("FAILSAFE RESUME/2 %p -> %d\n", (void*)info->native_handle, 0);
91 mono_threads_core_check_suspend_result (MonoThreadInfo *info)
97 mono_threads_core_begin_async_resume (MonoThreadInfo *info)
101 if (info->async_target) {
102 MonoContext tmp = info->thread_saved_state [ASYNC_SUSPEND_STATE_INDEX].ctx;
103 mach_msg_type_number_t num_state;
104 thread_state_t state;
108 mono_threads_get_runtime_callbacks ()->setup_async_callback (&tmp, info->async_target, info->user_data);
109 info->user_data = NULL;
110 info->async_target = (void (*)(void *)) info->user_data;
112 state = (thread_state_t) alloca (mono_mach_arch_get_thread_state_size ());
113 mctx = (mcontext_t) alloca (mono_mach_arch_get_mcontext_size ());
115 ret = mono_mach_arch_get_thread_state (info->native_handle, state, &num_state);
116 if (ret != KERN_SUCCESS)
119 mono_mach_arch_thread_state_to_mcontext (state, mctx);
120 uctx.uc_mcontext = mctx;
121 mono_monoctx_to_sigctx (&tmp, &uctx);
123 mono_mach_arch_mcontext_to_thread_state (mctx, state);
125 ret = mono_mach_arch_set_thread_state (info->native_handle, state, num_state);
126 if (ret != KERN_SUCCESS)
130 ret = thread_resume (info->native_handle);
131 THREADS_SUSPEND_DEBUG ("RESUME %p -> %d\n", (void*)info->native_handle, ret);
133 return ret == KERN_SUCCESS;
136 #endif /* defined(HOST_WATCHOS) || defined(HOST_TVOS) */
139 mono_threads_platform_register (MonoThreadInfo *info)
141 char thread_name [64];
143 info->native_handle = mach_thread_self ();
145 snprintf (thread_name, sizeof (thread_name), "tid_%x", (int) info->native_handle);
146 pthread_setname_np (thread_name);
148 mono_threads_install_dead_letter ();
152 mono_threads_platform_free (MonoThreadInfo *info)
154 mach_port_deallocate (current_task (), info->native_handle);
157 #endif /* USE_MACH_BACKEND */
161 mono_threads_core_get_stack_bounds (guint8 **staddr, size_t *stsize)
163 *staddr = (guint8*)pthread_get_stackaddr_np (pthread_self());
164 *stsize = pthread_get_stacksize_np (pthread_self());
168 * Mavericks reports stack sizes as 512kb:
169 * http://permalink.gmane.org/gmane.comp.java.openjdk.hotspot.devel/11590
170 * https://bugs.openjdk.java.net/browse/JDK-8020753
172 if (pthread_main_np () && *stsize == 512 * 1024)
173 *stsize = 2048 * mono_pagesize ();
176 /* staddr points to the start of the stack, not the end */