6 * Rodrigo Kumpera (kumpera@gmail.com)
8 * Copyright 2015 Xamarin, Inc (http://www.xamarin.com)
9 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
14 /* enable pthread extensions */
16 #define _DARWIN_C_SOURCE
19 #include <mono/utils/mono-compiler.h>
20 #include <mono/utils/mono-threads.h>
21 #include <mono/utils/mono-tls.h>
22 #include <mono/utils/hazard-pointer.h>
23 #include <mono/utils/mono-memory-model.h>
24 #include <mono/utils/mono-mmap.h>
25 #include <mono/utils/atomic.h>
26 #include <mono/utils/mono-time.h>
27 #include <mono/utils/mono-counters.h>
28 #include <mono/utils/mono-threads-coop.h>
29 #include <mono/utils/mono-threads-api.h>
30 #include <mono/utils/checked-build.h>
31 #include <mono/utils/mono-threads-debug.h>
34 #include <mono/utils/mach-support.h>
38 // TODO: Find MSVC replacement for __builtin_unwind_init
39 #define SAVE_REGS_ON_STACK g_assert_not_reached ();
41 #define SAVE_REGS_ON_STACK __builtin_unwind_init ();
44 volatile size_t mono_polling_required;
46 // FIXME: This would be more efficient if instead of instantiating the stack it just pushed a simple depth counter up and down,
47 // perhaps with a per-thread cookie in the high bits.
48 #ifdef ENABLE_CHECKED_BUILD_GC
50 // Maintains a single per-thread stack of ints, used to ensure nesting is not violated
51 static MonoNativeTlsKey coop_reset_count_stack_key;
54 coop_tls_push (gpointer cookie)
58 stack = mono_native_tls_get_value (coop_reset_count_stack_key);
60 stack = g_array_new (FALSE, FALSE, sizeof(gpointer));
61 mono_native_tls_set_value (coop_reset_count_stack_key, stack);
64 g_array_append_val (stack, cookie);
68 coop_tls_pop (gpointer received_cookie)
71 gpointer expected_cookie;
73 stack = mono_native_tls_get_value (coop_reset_count_stack_key);
74 if (!stack || 0 == stack->len)
75 mono_fatal_with_history ("Received cookie %p but found no stack at all\n", received_cookie);
77 expected_cookie = g_array_index (stack, gpointer, stack->len - 1);
80 if (0 == stack->len) {
81 g_array_free (stack,TRUE);
82 mono_native_tls_set_value (coop_reset_count_stack_key, NULL);
85 if (expected_cookie != received_cookie)
86 mono_fatal_with_history ("Received cookie %p but expected %p\n", received_cookie, expected_cookie);
92 check_info (MonoThreadInfo *info, const gchar *action, const gchar *state)
95 g_error ("Cannot %s GC %s region if the thread is not attached", action, state);
96 if (!mono_thread_info_is_current (info))
97 g_error ("[%p] Cannot %s GC %s region on a different thread", mono_thread_info_get_tid (info), action, state);
98 if (!mono_thread_info_is_live (info))
99 g_error ("[%p] Cannot %s GC %s region if the thread is not live", mono_thread_info_get_tid (info), action, state);
102 static int coop_reset_blocking_count;
103 static int coop_try_blocking_count;
104 static int coop_do_blocking_count;
105 static int coop_do_polling_count;
106 static int coop_save_count;
109 mono_threads_state_poll_with_info (MonoThreadInfo *info);
112 mono_threads_state_poll (void)
114 mono_threads_state_poll_with_info (mono_thread_info_current_unchecked ());
118 mono_threads_state_poll_with_info (MonoThreadInfo *info)
120 g_assert (mono_threads_is_coop_enabled ());
122 ++coop_do_polling_count;
127 THREADS_SUSPEND_DEBUG ("FINISH SELF SUSPEND OF %p\n", mono_thread_info_get_tid (info));
129 /* Fast check for pending suspend requests */
130 if (!(info->thread_state & (STATE_ASYNC_SUSPEND_REQUESTED | STATE_SELF_SUSPEND_REQUESTED)))
134 mono_threads_get_runtime_callbacks ()->thread_state_init (&info->thread_saved_state [SELF_SUSPEND_STATE_INDEX]);
136 /* commit the saved state and notify others if needed */
137 switch (mono_threads_transition_state_poll (info)) {
138 case SelfSuspendResumed:
140 case SelfSuspendWait:
141 mono_thread_info_wait_for_resume (info);
143 case SelfSuspendNotifyAndWait:
144 mono_threads_notify_initiator_of_suspend (info);
145 mono_thread_info_wait_for_resume (info);
149 if (info->async_target) {
150 info->async_target (info->user_data);
151 info->async_target = NULL;
152 info->user_data = NULL;
156 static volatile gpointer* dummy_global;
158 static MONO_NEVER_INLINE
160 return_stack_ptr (gpointer *i)
167 copy_stack_data (MonoThreadInfo *info, gpointer *stackdata_begin)
169 MonoThreadUnwindState *state;
172 void* stackdata_end = return_stack_ptr (&dummy);
176 state = &info->thread_saved_state [SELF_SUSPEND_STATE_INDEX];
178 stackdata_size = (char*)stackdata_begin - (char*)stackdata_end;
180 if (((gsize) stackdata_begin & (SIZEOF_VOID_P - 1)) != 0)
181 g_error ("stackdata_begin (%p) must be %d-byte aligned", stackdata_begin, SIZEOF_VOID_P);
182 if (((gsize) stackdata_end & (SIZEOF_VOID_P - 1)) != 0)
183 g_error ("stackdata_end (%p) must be %d-byte aligned", stackdata_end, SIZEOF_VOID_P);
185 if (stackdata_size <= 0)
186 g_error ("stackdata_size = %d, but must be > 0, stackdata_begin = %p, stackdata_end = %p", stackdata_size, stackdata_begin, stackdata_end);
188 g_byte_array_set_size (info->stackdata, stackdata_size);
189 state->gc_stackdata = info->stackdata->data;
190 memcpy (state->gc_stackdata, stackdata_end, stackdata_size);
192 state->gc_stackdata_size = stackdata_size;
196 mono_threads_enter_gc_safe_region_unbalanced_with_info (MonoThreadInfo *info, gpointer *stackdata);
199 mono_threads_enter_gc_safe_region (gpointer *stackdata)
201 return mono_threads_enter_gc_safe_region_with_info (mono_thread_info_current_unchecked (), stackdata);
205 mono_threads_enter_gc_safe_region_with_info (MonoThreadInfo *info, gpointer *stackdata)
209 if (!mono_threads_is_coop_enabled ())
212 cookie = mono_threads_enter_gc_safe_region_unbalanced_with_info (info, stackdata);
214 #ifdef ENABLE_CHECKED_BUILD_GC
215 if (mono_check_mode_enabled (MONO_CHECK_MODE_GC))
216 coop_tls_push (cookie);
223 mono_threads_enter_gc_safe_region_unbalanced (gpointer *stackdata)
225 return mono_threads_enter_gc_safe_region_unbalanced_with_info (mono_thread_info_current_unchecked (), stackdata);
229 mono_threads_enter_gc_safe_region_unbalanced_with_info (MonoThreadInfo *info, gpointer *stackdata)
231 if (!mono_threads_is_coop_enabled ())
234 ++coop_do_blocking_count;
236 check_info (info, "enter", "safe");
238 copy_stack_data (info, stackdata);
242 mono_threads_get_runtime_callbacks ()->thread_state_init (&info->thread_saved_state [SELF_SUSPEND_STATE_INDEX]);
244 switch (mono_threads_transition_do_blocking (info)) {
245 case DoBlockingContinue:
247 case DoBlockingPollAndRetry:
248 mono_threads_state_poll_with_info (info);
256 mono_threads_exit_gc_safe_region (gpointer cookie, gpointer *stackdata)
258 if (!mono_threads_is_coop_enabled ())
261 #ifdef ENABLE_CHECKED_BUILD_GC
262 if (mono_check_mode_enabled (MONO_CHECK_MODE_GC))
263 coop_tls_pop (cookie);
266 mono_threads_exit_gc_safe_region_unbalanced (cookie, stackdata);
270 mono_threads_exit_gc_safe_region_unbalanced (gpointer cookie, gpointer *stackdata)
272 MonoThreadInfo *info;
274 if (!mono_threads_is_coop_enabled ())
277 info = (MonoThreadInfo *)cookie;
279 check_info (info, "exit", "safe");
281 switch (mono_threads_transition_done_blocking (info)) {
283 info->thread_saved_state [SELF_SUSPEND_STATE_INDEX].valid = FALSE;
285 case DoneBlockingWait:
286 THREADS_SUSPEND_DEBUG ("state polling done, notifying of resume\n");
287 mono_thread_info_wait_for_resume (info);
290 g_error ("Unknown thread state");
293 if (info->async_target) {
294 info->async_target (info->user_data);
295 info->async_target = NULL;
296 info->user_data = NULL;
301 mono_threads_assert_gc_safe_region (void)
303 MONO_REQ_GC_SAFE_MODE;
307 mono_threads_enter_gc_unsafe_region (gpointer *stackdata)
309 return mono_threads_enter_gc_unsafe_region_with_info (mono_thread_info_current_unchecked (), stackdata);
313 mono_threads_enter_gc_unsafe_region_with_info (THREAD_INFO_TYPE *info, gpointer *stackdata)
317 if (!mono_threads_is_coop_enabled ())
320 cookie = mono_threads_enter_gc_unsafe_region_unbalanced_with_info (info, stackdata);
322 #ifdef ENABLE_CHECKED_BUILD_GC
323 if (mono_check_mode_enabled (MONO_CHECK_MODE_GC))
324 coop_tls_push (cookie);
331 mono_threads_enter_gc_unsafe_region_unbalanced (gpointer *stackdata)
333 return mono_threads_enter_gc_unsafe_region_unbalanced_with_info (mono_thread_info_current_unchecked (), stackdata);
337 mono_threads_enter_gc_unsafe_region_unbalanced_with_info (MonoThreadInfo *info, gpointer *stackdata)
339 if (!mono_threads_is_coop_enabled ())
342 ++coop_reset_blocking_count;
344 check_info (info, "enter", "unsafe");
346 copy_stack_data (info, stackdata);
348 switch (mono_threads_transition_abort_blocking (info)) {
349 case AbortBlockingIgnore:
350 info->thread_saved_state [SELF_SUSPEND_STATE_INDEX].valid = FALSE;
352 case AbortBlockingIgnoreAndPoll:
353 mono_threads_state_poll_with_info (info);
355 case AbortBlockingOk:
356 info->thread_saved_state [SELF_SUSPEND_STATE_INDEX].valid = FALSE;
358 case AbortBlockingWait:
359 mono_thread_info_wait_for_resume (info);
362 g_error ("Unknown thread state");
365 if (info->async_target) {
366 info->async_target (info->user_data);
367 info->async_target = NULL;
368 info->user_data = NULL;
375 mono_threads_enter_gc_unsafe_region_cookie (void)
377 MonoThreadInfo *info;
379 g_assert (mono_threads_is_coop_enabled ());
381 info = mono_thread_info_current_unchecked ();
383 check_info (info, "enter (cookie)", "unsafe");
385 #ifdef ENABLE_CHECKED_BUILD_GC
386 if (mono_check_mode_enabled (MONO_CHECK_MODE_GC))
387 coop_tls_push (info);
394 mono_threads_exit_gc_unsafe_region (gpointer cookie, gpointer *stackdata)
396 if (!mono_threads_is_coop_enabled ())
399 #ifdef ENABLE_CHECKED_BUILD_GC
400 if (mono_check_mode_enabled (MONO_CHECK_MODE_GC))
401 coop_tls_pop (cookie);
404 mono_threads_exit_gc_unsafe_region_unbalanced (cookie, stackdata);
408 mono_threads_exit_gc_unsafe_region_unbalanced (gpointer cookie, gpointer *stackdata)
410 if (!mono_threads_is_coop_enabled ())
416 mono_threads_enter_gc_safe_region_unbalanced (stackdata);
420 mono_threads_assert_gc_unsafe_region (void)
422 MONO_REQ_GC_UNSAFE_MODE;
426 mono_threads_is_coop_enabled (void)
428 #if defined(USE_COOP_GC)
431 static int is_coop_enabled = -1;
432 if (G_UNLIKELY (is_coop_enabled == -1))
433 is_coop_enabled = g_hasenv ("MONO_ENABLE_COOP") ? 1 : 0;
434 return is_coop_enabled == 1;
440 mono_threads_coop_init (void)
442 if (!mono_threads_is_coop_enabled ())
445 mono_counters_register ("Coop Reset Blocking", MONO_COUNTER_GC | MONO_COUNTER_INT, &coop_reset_blocking_count);
446 mono_counters_register ("Coop Try Blocking", MONO_COUNTER_GC | MONO_COUNTER_INT, &coop_try_blocking_count);
447 mono_counters_register ("Coop Do Blocking", MONO_COUNTER_GC | MONO_COUNTER_INT, &coop_do_blocking_count);
448 mono_counters_register ("Coop Do Polling", MONO_COUNTER_GC | MONO_COUNTER_INT, &coop_do_polling_count);
449 mono_counters_register ("Coop Save Count", MONO_COUNTER_GC | MONO_COUNTER_INT, &coop_save_count);
450 //See the above for what's wrong here.
452 #ifdef ENABLE_CHECKED_BUILD_GC
453 mono_native_tls_alloc (&coop_reset_count_stack_key, NULL);
458 mono_threads_coop_begin_global_suspend (void)
460 if (mono_threads_is_coop_enabled ())
461 mono_polling_required = 1;
465 mono_threads_coop_end_global_suspend (void)
467 if (mono_threads_is_coop_enabled ())
468 mono_polling_required = 0;