2 * mono-threads.c: Coop threading
5 * Rodrigo Kumpera (kumpera@gmail.com)
7 * Copyright 2015 Xamarin, Inc (http://www.xamarin.com)
8 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
13 /* enable pthread extensions */
15 #define _DARWIN_C_SOURCE
18 #include <mono/utils/mono-compiler.h>
19 #include <mono/utils/mono-threads.h>
20 #include <mono/utils/mono-tls.h>
21 #include <mono/utils/hazard-pointer.h>
22 #include <mono/utils/mono-memory-model.h>
23 #include <mono/utils/mono-mmap.h>
24 #include <mono/utils/atomic.h>
25 #include <mono/utils/mono-time.h>
26 #include <mono/utils/mono-counters.h>
27 #include <mono/utils/mono-threads-coop.h>
28 #include <mono/utils/mono-threads-api.h>
29 #include <mono/utils/checked-build.h>
32 #include <mono/utils/mach-support.h>
36 // TODO: Find MSVC replacement for __builtin_unwind_init
37 #define SAVE_REGS_ON_STACK g_assert_not_reached ();
39 #define SAVE_REGS_ON_STACK __builtin_unwind_init ();
42 volatile size_t mono_polling_required;
44 // FIXME: This would be more efficient if instead of instantiating the stack it just pushed a simple depth counter up and down,
45 // perhaps with a per-thread cookie in the high bits.
46 #ifdef ENABLE_CHECKED_BUILD_GC
48 // Maintains a single per-thread stack of ints, used to ensure nesting is not violated
49 static MonoNativeTlsKey coop_reset_count_stack_key;
52 coop_tls_push (gpointer cookie)
56 stack = mono_native_tls_get_value (coop_reset_count_stack_key);
58 stack = g_array_new (FALSE, FALSE, sizeof(gpointer));
59 mono_native_tls_set_value (coop_reset_count_stack_key, stack);
62 g_array_append_val (stack, cookie);
66 coop_tls_pop (gpointer received_cookie)
69 gpointer expected_cookie;
71 stack = mono_native_tls_get_value (coop_reset_count_stack_key);
72 if (!stack || 0 == stack->len)
73 mono_fatal_with_history ("Received cookie %p but found no stack at all\n", received_cookie);
75 expected_cookie = g_array_index (stack, gpointer, stack->len - 1);
78 if (0 == stack->len) {
79 g_array_free (stack,TRUE);
80 mono_native_tls_set_value (coop_reset_count_stack_key, NULL);
83 if (expected_cookie != received_cookie)
84 mono_fatal_with_history ("Received cookie %p but expected %p\n", received_cookie, expected_cookie);
90 check_info (MonoThreadInfo *info, const gchar *action, const gchar *state)
93 g_error ("Cannot %s GC %s region if the thread is not attached", action, state);
94 if (!mono_thread_info_is_current (info))
95 g_error ("[%p] Cannot %s GC %s region on a different thread", mono_thread_info_get_tid (info), action, state);
96 if (!mono_thread_info_is_live (info))
97 g_error ("[%p] Cannot %s GC %s region if the thread is not live", mono_thread_info_get_tid (info), action, state);
100 static int coop_reset_blocking_count;
101 static int coop_try_blocking_count;
102 static int coop_do_blocking_count;
103 static int coop_do_polling_count;
104 static int coop_save_count;
107 mono_threads_state_poll_with_info (MonoThreadInfo *info);
110 mono_threads_state_poll (void)
112 mono_threads_state_poll_with_info (mono_thread_info_current_unchecked ());
116 mono_threads_state_poll_with_info (MonoThreadInfo *info)
118 g_assert (mono_threads_is_coop_enabled ());
120 ++coop_do_polling_count;
125 THREADS_SUSPEND_DEBUG ("FINISH SELF SUSPEND OF %p\n", mono_thread_info_get_tid (info));
127 /* Fast check for pending suspend requests */
128 if (!(info->thread_state & (STATE_ASYNC_SUSPEND_REQUESTED | STATE_SELF_SUSPEND_REQUESTED)))
132 mono_threads_get_runtime_callbacks ()->thread_state_init (&info->thread_saved_state [SELF_SUSPEND_STATE_INDEX]);
134 /* commit the saved state and notify others if needed */
135 switch (mono_threads_transition_state_poll (info)) {
136 case SelfSuspendResumed:
138 case SelfSuspendWait:
139 mono_thread_info_wait_for_resume (info);
141 case SelfSuspendNotifyAndWait:
142 mono_threads_notify_initiator_of_suspend (info);
143 mono_thread_info_wait_for_resume (info);
148 static volatile gpointer* dummy_global;
150 static MONO_NEVER_INLINE
152 return_stack_ptr (gpointer *i)
159 copy_stack_data (MonoThreadInfo *info, gpointer *stackdata_begin)
161 MonoThreadUnwindState *state;
164 void* stackdata_end = return_stack_ptr (&dummy);
168 state = &info->thread_saved_state [SELF_SUSPEND_STATE_INDEX];
170 stackdata_size = (char*)stackdata_begin - (char*)stackdata_end;
172 if (((gsize) stackdata_begin & (SIZEOF_VOID_P - 1)) != 0)
173 g_error ("stackdata_begin (%p) must be %d-byte aligned", stackdata_begin, SIZEOF_VOID_P);
174 if (((gsize) stackdata_end & (SIZEOF_VOID_P - 1)) != 0)
175 g_error ("stackdata_end (%p) must be %d-byte aligned", stackdata_end, SIZEOF_VOID_P);
177 if (stackdata_size <= 0)
178 g_error ("stackdata_size = %d, but must be > 0, stackdata_begin = %p, stackdata_end = %p", stackdata_size, stackdata_begin, stackdata_end);
180 g_byte_array_set_size (info->stackdata, stackdata_size);
181 state->gc_stackdata = info->stackdata->data;
182 memcpy (state->gc_stackdata, stackdata_end, stackdata_size);
184 state->gc_stackdata_size = stackdata_size;
188 mono_threads_enter_gc_safe_region_unbalanced_with_info (MonoThreadInfo *info, gpointer *stackdata);
191 mono_threads_enter_gc_safe_region (gpointer *stackdata)
193 return mono_threads_enter_gc_safe_region_with_info (mono_thread_info_current_unchecked (), stackdata);
197 mono_threads_enter_gc_safe_region_with_info (MonoThreadInfo *info, gpointer *stackdata)
201 if (!mono_threads_is_coop_enabled ())
204 cookie = mono_threads_enter_gc_safe_region_unbalanced_with_info (info, stackdata);
206 #ifdef ENABLE_CHECKED_BUILD_GC
207 if (mono_check_mode_enabled (MONO_CHECK_MODE_GC))
208 coop_tls_push (cookie);
215 mono_threads_enter_gc_safe_region_unbalanced (gpointer *stackdata)
217 return mono_threads_enter_gc_safe_region_unbalanced_with_info (mono_thread_info_current_unchecked (), stackdata);
221 mono_threads_enter_gc_safe_region_unbalanced_with_info (MonoThreadInfo *info, gpointer *stackdata)
223 if (!mono_threads_is_coop_enabled ())
226 ++coop_do_blocking_count;
228 check_info (info, "enter", "safe");
230 copy_stack_data (info, stackdata);
234 mono_threads_get_runtime_callbacks ()->thread_state_init (&info->thread_saved_state [SELF_SUSPEND_STATE_INDEX]);
236 switch (mono_threads_transition_do_blocking (info)) {
237 case DoBlockingContinue:
239 case DoBlockingPollAndRetry:
240 mono_threads_state_poll_with_info (info);
248 mono_threads_exit_gc_safe_region (gpointer cookie, gpointer *stackdata)
250 if (!mono_threads_is_coop_enabled ())
253 #ifdef ENABLE_CHECKED_BUILD_GC
254 if (mono_check_mode_enabled (MONO_CHECK_MODE_GC))
255 coop_tls_pop (cookie);
258 mono_threads_exit_gc_safe_region_unbalanced (cookie, stackdata);
262 mono_threads_exit_gc_safe_region_unbalanced (gpointer cookie, gpointer *stackdata)
264 MonoThreadInfo *info;
266 if (!mono_threads_is_coop_enabled ())
269 info = (MonoThreadInfo *)cookie;
271 check_info (info, "exit", "safe");
273 switch (mono_threads_transition_done_blocking (info)) {
275 info->thread_saved_state [SELF_SUSPEND_STATE_INDEX].valid = FALSE;
277 case DoneBlockingWait:
278 THREADS_SUSPEND_DEBUG ("state polling done, notifying of resume\n");
279 mono_thread_info_wait_for_resume (info);
282 g_error ("Unknown thread state");
287 mono_threads_assert_gc_safe_region (void)
289 MONO_REQ_GC_SAFE_MODE;
293 mono_threads_enter_gc_unsafe_region (gpointer *stackdata)
295 return mono_threads_enter_gc_unsafe_region_with_info (mono_thread_info_current_unchecked (), stackdata);
299 mono_threads_enter_gc_unsafe_region_with_info (THREAD_INFO_TYPE *info, gpointer *stackdata)
303 if (!mono_threads_is_coop_enabled ())
306 cookie = mono_threads_enter_gc_unsafe_region_unbalanced_with_info (info, stackdata);
308 #ifdef ENABLE_CHECKED_BUILD_GC
309 if (mono_check_mode_enabled (MONO_CHECK_MODE_GC))
310 coop_tls_push (cookie);
317 mono_threads_enter_gc_unsafe_region_unbalanced (gpointer *stackdata)
319 return mono_threads_enter_gc_unsafe_region_unbalanced_with_info (mono_thread_info_current_unchecked (), stackdata);
323 mono_threads_enter_gc_unsafe_region_unbalanced_with_info (MonoThreadInfo *info, gpointer *stackdata)
325 if (!mono_threads_is_coop_enabled ())
328 ++coop_reset_blocking_count;
330 check_info (info, "enter", "unsafe");
332 copy_stack_data (info, stackdata);
334 switch (mono_threads_transition_abort_blocking (info)) {
335 case AbortBlockingIgnore:
336 info->thread_saved_state [SELF_SUSPEND_STATE_INDEX].valid = FALSE;
338 case AbortBlockingIgnoreAndPoll:
339 mono_threads_state_poll_with_info (info);
341 case AbortBlockingOk:
342 info->thread_saved_state [SELF_SUSPEND_STATE_INDEX].valid = FALSE;
344 case AbortBlockingWait:
345 mono_thread_info_wait_for_resume (info);
348 g_error ("Unknown thread state");
355 mono_threads_enter_gc_unsafe_region_cookie (void)
357 MonoThreadInfo *info;
359 g_assert (mono_threads_is_coop_enabled ());
361 info = mono_thread_info_current_unchecked ();
363 check_info (info, "enter (cookie)", "unsafe");
365 #ifdef ENABLE_CHECKED_BUILD_GC
366 if (mono_check_mode_enabled (MONO_CHECK_MODE_GC))
367 coop_tls_push (info);
374 mono_threads_exit_gc_unsafe_region (gpointer cookie, gpointer *stackdata)
376 if (!mono_threads_is_coop_enabled ())
379 #ifdef ENABLE_CHECKED_BUILD_GC
380 if (mono_check_mode_enabled (MONO_CHECK_MODE_GC))
381 coop_tls_pop (cookie);
384 mono_threads_exit_gc_unsafe_region_unbalanced (cookie, stackdata);
388 mono_threads_exit_gc_unsafe_region_unbalanced (gpointer cookie, gpointer *stackdata)
390 if (!mono_threads_is_coop_enabled ())
396 mono_threads_enter_gc_safe_region_unbalanced (stackdata);
400 mono_threads_assert_gc_unsafe_region (void)
402 MONO_REQ_GC_UNSAFE_MODE;
406 mono_threads_is_coop_enabled (void)
408 #if defined(USE_COOP_GC)
411 static int is_coop_enabled = -1;
412 if (G_UNLIKELY (is_coop_enabled == -1))
413 is_coop_enabled = g_getenv ("MONO_ENABLE_COOP") != NULL ? 1 : 0;
414 return is_coop_enabled == 1;
420 mono_threads_coop_init (void)
422 if (!mono_threads_is_coop_enabled ())
425 mono_counters_register ("Coop Reset Blocking", MONO_COUNTER_GC | MONO_COUNTER_INT, &coop_reset_blocking_count);
426 mono_counters_register ("Coop Try Blocking", MONO_COUNTER_GC | MONO_COUNTER_INT, &coop_try_blocking_count);
427 mono_counters_register ("Coop Do Blocking", MONO_COUNTER_GC | MONO_COUNTER_INT, &coop_do_blocking_count);
428 mono_counters_register ("Coop Do Polling", MONO_COUNTER_GC | MONO_COUNTER_INT, &coop_do_polling_count);
429 mono_counters_register ("Coop Save Count", MONO_COUNTER_GC | MONO_COUNTER_INT, &coop_save_count);
430 //See the above for what's wrong here.
432 #ifdef ENABLE_CHECKED_BUILD_GC
433 mono_native_tls_alloc (&coop_reset_count_stack_key, NULL);
438 mono_threads_coop_begin_global_suspend (void)
440 if (mono_threads_is_coop_enabled ())
441 mono_polling_required = 1;
445 mono_threads_coop_end_global_suspend (void)
447 if (mono_threads_is_coop_enabled ())
448 mono_polling_required = 0;