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>
26 #include <mono/utils/mono-threads-debug.h>
29 mono_threads_suspend_init (void)
31 mono_threads_init_dead_letter ();
34 #if defined(HOST_WATCHOS) || defined(HOST_TVOS)
37 mono_threads_suspend_begin_async_suspend (MonoThreadInfo *info, gboolean interrupt_kernel)
39 g_assert_not_reached ();
43 mono_threads_suspend_check_suspend_result (MonoThreadInfo *info)
45 g_assert_not_reached ();
49 mono_threads_suspend_begin_async_resume (MonoThreadInfo *info)
51 g_assert_not_reached ();
55 mono_threads_suspend_abort_syscall (MonoThreadInfo *info)
57 g_assert_not_reached ();
61 mono_threads_suspend_needs_abort_syscall (void)
66 #else /* defined(HOST_WATCHOS) || defined(HOST_TVOS) */
69 mono_threads_suspend_begin_async_suspend (MonoThreadInfo *info, gboolean interrupt_kernel)
77 ret = thread_suspend (info->native_handle);
78 } while (ret == KERN_ABORTED);
80 THREADS_SUSPEND_DEBUG ("SUSPEND %p -> %d\n", (gpointer)(gsize)info->native_handle, ret);
81 if (ret != KERN_SUCCESS)
84 /* We're in the middle of a self-suspend, resume and register */
85 if (!mono_threads_transition_finish_async_suspend (info)) {
86 mono_threads_add_to_pending_operation_set (info);
88 ret = thread_resume (info->native_handle);
89 } while (ret == KERN_ABORTED);
90 g_assert (ret == KERN_SUCCESS);
91 THREADS_SUSPEND_DEBUG ("FAILSAFE RESUME/1 %p -> %d\n", (gpointer)(gsize)info->native_handle, 0);
92 //XXX interrupt_kernel doesn't make sense in this case as the target is not in a syscall
95 info->suspend_can_continue = mono_threads_get_runtime_callbacks ()->
96 thread_state_init_from_handle (&info->thread_saved_state [ASYNC_SUSPEND_STATE_INDEX], info);
97 THREADS_SUSPEND_DEBUG ("thread state %p -> %d\n", (gpointer)(gsize)info->native_handle, ret);
98 if (info->suspend_can_continue) {
100 thread_abort (info->native_handle);
102 THREADS_SUSPEND_DEBUG ("FAILSAFE RESUME/2 %p -> %d\n", (gpointer)(gsize)info->native_handle, 0);
108 mono_threads_suspend_check_suspend_result (MonoThreadInfo *info)
110 return info->suspend_can_continue;
114 mono_threads_suspend_begin_async_resume (MonoThreadInfo *info)
118 if (info->async_target) {
119 MonoContext tmp = info->thread_saved_state [ASYNC_SUSPEND_STATE_INDEX].ctx;
120 mach_msg_type_number_t num_state;
121 thread_state_t state;
125 mono_threads_get_runtime_callbacks ()->setup_async_callback (&tmp, info->async_target, info->user_data);
126 info->user_data = NULL;
127 info->async_target = (void (*)(void *)) info->user_data;
129 state = (thread_state_t) alloca (mono_mach_arch_get_thread_state_size ());
130 mctx = (mcontext_t) alloca (mono_mach_arch_get_mcontext_size ());
133 ret = mono_mach_arch_get_thread_state (info->native_handle, state, &num_state);
134 } while (ret == KERN_ABORTED);
136 if (ret != KERN_SUCCESS)
139 mono_mach_arch_thread_state_to_mcontext (state, mctx);
140 uctx.uc_mcontext = mctx;
141 mono_monoctx_to_sigctx (&tmp, &uctx);
143 mono_mach_arch_mcontext_to_thread_state (mctx, state);
146 ret = mono_mach_arch_set_thread_state (info->native_handle, state, num_state);
147 } while (ret == KERN_ABORTED);
149 if (ret != KERN_SUCCESS)
154 ret = thread_resume (info->native_handle);
155 } while (ret == KERN_ABORTED);
156 THREADS_SUSPEND_DEBUG ("RESUME %p -> %d\n", (gpointer)(gsize)info->native_handle, ret);
158 return ret == KERN_SUCCESS;
162 mono_threads_suspend_abort_syscall (MonoThreadInfo *info)
167 ret = thread_suspend (info->native_handle);
168 } while (ret == KERN_ABORTED);
170 if (ret != KERN_SUCCESS)
174 ret = thread_abort_safely (info->native_handle);
175 } while (ret == KERN_ABORTED);
178 * We are doing thread_abort when thread_abort_safely returns KERN_SUCCESS because
179 * for some reason accept is not interrupted by thread_abort_safely.
180 * The risk of aborting non-atomic operations while calling thread_abort should not
181 * exist because by the time thread_abort_safely returns KERN_SUCCESS the target
182 * thread should have return from the kernel and should be waiting for thread_resume
183 * to resume the user code.
185 if (ret == KERN_SUCCESS)
186 ret = thread_abort (info->native_handle);
189 ret = thread_resume (info->native_handle);
190 } while (ret == KERN_ABORTED);
192 g_assert (ret == KERN_SUCCESS);
196 mono_threads_suspend_needs_abort_syscall (void)
201 #endif /* defined(HOST_WATCHOS) || defined(HOST_TVOS) */
204 mono_threads_suspend_register (MonoThreadInfo *info)
206 char thread_name [64];
208 info->native_handle = mach_thread_self ();
210 snprintf (thread_name, sizeof (thread_name), "tid_%x", (int) info->native_handle);
211 pthread_setname_np (thread_name);
213 mono_threads_install_dead_letter ();
217 mono_threads_suspend_free (MonoThreadInfo *info)
219 mach_port_deallocate (current_task (), info->native_handle);
223 mono_threads_suspend_init_signals (void)
228 mono_threads_suspend_search_alternative_signal (void)
230 g_assert_not_reached ();
234 mono_threads_suspend_get_suspend_signal (void)
240 mono_threads_suspend_get_restart_signal (void)
246 mono_threads_suspend_get_abort_signal (void)
251 #endif /* USE_MACH_BACKEND */
255 mono_threads_platform_get_stack_bounds (guint8 **staddr, size_t *stsize)
257 *staddr = (guint8*)pthread_get_stackaddr_np (pthread_self());
258 *stsize = pthread_get_stacksize_np (pthread_self());
262 * Mavericks reports stack sizes as 512kb:
263 * http://permalink.gmane.org/gmane.comp.java.openjdk.hotspot.devel/11590
264 * https://bugs.openjdk.java.net/browse/JDK-8020753
266 if (pthread_main_np () && *stsize == 512 * 1024)
267 *stsize = 2048 * mono_pagesize ();
270 /* staddr points to the start of the stack, not the end */