Update sdks/wasm/framework/src/WebAssembly.Bindings/Core/Array.cs
[mono-project.git] / mono / utils / mono-threads-mach.c
blob99c561b0ea2c3379dd8ad8f1c4830627123db0d5
1 /**
2 * \file
3 * Low-level threading, mach version
5 * Author:
6 * Rodrigo Kumpera (kumpera@gmail.com)
8 * (C) 2011 Novell, Inc
9 */
11 #include "config.h"
13 /* For pthread_main_np, pthread_get_stackaddr_np and pthread_get_stacksize_np */
14 #if defined (__MACH__)
15 #define _DARWIN_C_SOURCE 1
16 #endif
18 #include <mono/utils/mono-threads.h>
19 #include <mono/utils/mono-mmap.h>
21 #if defined (USE_MACH_BACKEND)
23 #include <mono/utils/mach-support.h>
24 #include <mono/utils/mono-compiler.h>
25 #include <mono/utils/mono-threads.h>
26 #include <mono/utils/hazard-pointer.h>
27 #include <mono/utils/mono-threads-coop.h>
28 #include <mono/utils/mono-threads-debug.h>
30 void
31 mono_threads_suspend_init (void)
33 mono_threads_init_dead_letter ();
36 #if defined(HOST_WATCHOS)
38 gboolean
39 mono_threads_suspend_begin_async_suspend (MonoThreadInfo *info, gboolean interrupt_kernel)
41 g_assert_not_reached ();
44 gboolean
45 mono_threads_suspend_check_suspend_result (MonoThreadInfo *info)
47 g_assert_not_reached ();
50 gboolean
51 mono_threads_suspend_begin_async_resume (MonoThreadInfo *info)
53 g_assert_not_reached ();
56 void
57 mono_threads_suspend_abort_syscall (MonoThreadInfo *info)
61 #else /* defined(HOST_WATCHOS) */
63 gboolean
64 mono_threads_suspend_begin_async_suspend (MonoThreadInfo *info, gboolean interrupt_kernel)
66 kern_return_t ret;
68 g_assert (info);
71 do {
72 ret = thread_suspend (info->native_handle);
73 } while (ret == KERN_ABORTED);
75 THREADS_SUSPEND_DEBUG ("SUSPEND %p -> %d\n", (gpointer)(gsize)info->native_handle, ret);
76 if (ret != KERN_SUCCESS)
77 return FALSE;
79 if (!mono_threads_transition_finish_async_suspend (info)) {
80 /* We raced with self-suspend and lost. Resume the native
81 * thread. It is still self-suspended, waiting to be resumed.
82 * So suspend can continue.
84 do {
85 ret = thread_resume (info->native_handle);
86 } while (ret == KERN_ABORTED);
87 g_assert (ret == KERN_SUCCESS);
88 info->suspend_can_continue = TRUE;
89 THREADS_SUSPEND_DEBUG ("\tlost race with self suspend %p\n", (gpointer)(gsize)info->native_handle);
90 g_assert (mono_threads_is_hybrid_suspension_enabled ());
91 //XXX interrupt_kernel doesn't make sense in this case as the target is not in a syscall
92 return TRUE;
94 info->suspend_can_continue = mono_threads_get_runtime_callbacks ()->
95 thread_state_init_from_handle (&info->thread_saved_state [ASYNC_SUSPEND_STATE_INDEX], info, NULL);
96 THREADS_SUSPEND_DEBUG ("thread state %p -> %d\n", (gpointer)(gsize)info->native_handle, ret);
97 if (info->suspend_can_continue) {
98 if (interrupt_kernel)
99 thread_abort (info->native_handle);
100 } else {
101 THREADS_SUSPEND_DEBUG ("FAILSAFE RESUME/2 %p -> %d\n", (gpointer)(gsize)info->native_handle, 0);
103 return TRUE;
106 gboolean
107 mono_threads_suspend_check_suspend_result (MonoThreadInfo *info)
109 return info->suspend_can_continue;
112 gboolean
113 mono_threads_suspend_begin_async_resume (MonoThreadInfo *info)
115 kern_return_t ret;
117 if (info->async_target) {
118 MonoContext tmp = info->thread_saved_state [ASYNC_SUSPEND_STATE_INDEX].ctx;
119 mach_msg_type_number_t num_state, num_fpstate;
120 thread_state_t state, fpstate;
121 ucontext_t uctx;
122 mcontext_t mctx;
124 mono_threads_get_runtime_callbacks ()->setup_async_callback (&tmp, info->async_target, info->user_data);
125 info->user_data = NULL;
126 info->async_target = (void (*)(void *)) info->user_data;
128 state = (thread_state_t) alloca (mono_mach_arch_get_thread_state_size ());
129 fpstate = (thread_state_t) alloca (mono_mach_arch_get_thread_fpstate_size ());
130 mctx = (mcontext_t) alloca (mono_mach_arch_get_mcontext_size ());
132 do {
133 ret = mono_mach_arch_get_thread_states (info->native_handle, state, &num_state, fpstate, &num_fpstate);
134 } while (ret == KERN_ABORTED);
136 if (ret != KERN_SUCCESS)
137 return FALSE;
139 mono_mach_arch_thread_states_to_mcontext (state, fpstate, mctx);
140 uctx.uc_mcontext = mctx;
141 mono_monoctx_to_sigctx (&tmp, &uctx);
143 mono_mach_arch_mcontext_to_thread_states (mctx, state, fpstate);
145 do {
146 ret = mono_mach_arch_set_thread_states (info->native_handle, state, num_state, fpstate, num_fpstate);
147 } while (ret == KERN_ABORTED);
149 if (ret != KERN_SUCCESS)
150 return FALSE;
153 do {
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;
161 void
162 mono_threads_suspend_abort_syscall (MonoThreadInfo *info)
164 kern_return_t ret;
166 do {
167 ret = thread_suspend (info->native_handle);
168 } while (ret == KERN_ABORTED);
170 if (ret != KERN_SUCCESS)
171 return;
173 do {
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);
188 do {
189 ret = thread_resume (info->native_handle);
190 } while (ret == KERN_ABORTED);
192 g_assert (ret == KERN_SUCCESS);
195 #endif /* defined(HOST_WATCHOS) */
197 void
198 mono_threads_suspend_register (MonoThreadInfo *info)
200 char thread_name [64];
202 info->native_handle = mach_thread_self ();
204 snprintf (thread_name, sizeof (thread_name), "tid_%x", (int) info->native_handle);
205 pthread_setname_np (thread_name);
207 mono_threads_install_dead_letter ();
210 void
211 mono_threads_suspend_free (MonoThreadInfo *info)
213 mach_port_deallocate (current_task (), info->native_handle);
216 void
217 mono_threads_suspend_init_signals (void)
221 gint
222 mono_threads_suspend_search_alternative_signal (void)
224 g_assert_not_reached ();
227 gint
228 mono_threads_suspend_get_suspend_signal (void)
230 return -1;
233 gint
234 mono_threads_suspend_get_restart_signal (void)
236 return -1;
239 gint
240 mono_threads_suspend_get_abort_signal (void)
242 return -1;
245 #endif /* USE_MACH_BACKEND */
247 #ifdef __MACH__
248 void
249 mono_threads_platform_get_stack_bounds (guint8 **staddr, size_t *stsize)
251 *staddr = (guint8*)pthread_get_stackaddr_np (pthread_self());
252 *stsize = pthread_get_stacksize_np (pthread_self());
254 #ifdef TARGET_OSX
256 * Mavericks reports stack sizes as 512kb:
257 * http://permalink.gmane.org/gmane.comp.java.openjdk.hotspot.devel/11590
258 * https://bugs.openjdk.java.net/browse/JDK-8020753
260 if (pthread_main_np () && *stsize == 512 * 1024)
261 *stsize = 2048 * mono_pagesize ();
262 #endif
264 /* staddr points to the start of the stack, not the end */
265 *staddr -= *stsize;
268 gboolean
269 mono_threads_platform_is_main_thread (void)
271 return pthread_main_np () == 1;
273 #endif