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, %x\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);
156 copy_stack_data (MonoThreadInfo *info, gpointer *stackdata_begin)
158 MonoThreadUnwindState *state;
160 void* stackdata_end = return_stack_ptr ();
164 state = &info->thread_saved_state [SELF_SUSPEND_STATE_INDEX];
166 stackdata_size = (char*)stackdata_begin - (char*)stackdata_end;
168 if (((gsize) stackdata_begin & (SIZEOF_VOID_P - 1)) != 0)
169 g_error ("stackdata_begin (%p) must be %d-byte aligned", stackdata_begin, SIZEOF_VOID_P);
170 if (((gsize) stackdata_end & (SIZEOF_VOID_P - 1)) != 0)
171 g_error ("stackdata_end (%p) must be %d-byte aligned", stackdata_end, SIZEOF_VOID_P);
173 if (stackdata_size <= 0)
174 g_error ("stackdata_size = %d, but must be > 0, stackdata_begin = %p, stackdata_end = %p", stackdata_size, stackdata_begin, stackdata_end);
176 g_byte_array_set_size (info->stackdata, stackdata_size);
177 state->gc_stackdata = info->stackdata->data;
178 memcpy (state->gc_stackdata, stackdata_end, stackdata_size);
180 state->gc_stackdata_size = stackdata_size;
184 mono_threads_enter_gc_safe_region_unbalanced_with_info (MonoThreadInfo *info, gpointer *stackdata);
187 mono_threads_enter_gc_safe_region (gpointer *stackdata)
189 return mono_threads_enter_gc_safe_region_with_info (mono_thread_info_current_unchecked (), stackdata);
193 mono_threads_enter_gc_safe_region_with_info (MonoThreadInfo *info, gpointer *stackdata)
197 if (!mono_threads_is_coop_enabled ())
200 cookie = mono_threads_enter_gc_safe_region_unbalanced_with_info (info, stackdata);
202 #ifdef ENABLE_CHECKED_BUILD_GC
203 if (mono_check_mode_enabled (MONO_CHECK_MODE_GC))
204 coop_tls_push (cookie);
211 mono_threads_enter_gc_safe_region_unbalanced (gpointer *stackdata)
213 return mono_threads_enter_gc_safe_region_unbalanced_with_info (mono_thread_info_current_unchecked (), stackdata);
217 mono_threads_enter_gc_safe_region_unbalanced_with_info (MonoThreadInfo *info, gpointer *stackdata)
219 if (!mono_threads_is_coop_enabled ())
222 ++coop_do_blocking_count;
224 check_info (info, "enter", "safe");
226 copy_stack_data (info, stackdata);
230 mono_threads_get_runtime_callbacks ()->thread_state_init (&info->thread_saved_state [SELF_SUSPEND_STATE_INDEX]);
232 switch (mono_threads_transition_do_blocking (info)) {
233 case DoBlockingContinue:
235 case DoBlockingPollAndRetry:
236 mono_threads_state_poll_with_info (info);
244 mono_threads_exit_gc_safe_region (gpointer cookie, gpointer *stackdata)
246 if (!mono_threads_is_coop_enabled ())
249 #ifdef ENABLE_CHECKED_BUILD_GC
250 if (mono_check_mode_enabled (MONO_CHECK_MODE_GC))
251 coop_tls_pop (cookie);
254 mono_threads_exit_gc_safe_region_unbalanced (cookie, stackdata);
258 mono_threads_exit_gc_safe_region_unbalanced (gpointer cookie, gpointer *stackdata)
260 MonoThreadInfo *info;
262 if (!mono_threads_is_coop_enabled ())
265 info = (MonoThreadInfo *)cookie;
267 check_info (info, "exit", "safe");
269 switch (mono_threads_transition_done_blocking (info)) {
271 info->thread_saved_state [SELF_SUSPEND_STATE_INDEX].valid = FALSE;
273 case DoneBlockingWait:
274 THREADS_SUSPEND_DEBUG ("state polling done, notifying of resume\n");
275 mono_thread_info_wait_for_resume (info);
278 g_error ("Unknown thread state");
283 mono_threads_assert_gc_safe_region (void)
285 MONO_REQ_GC_SAFE_MODE;
289 mono_threads_enter_gc_unsafe_region (gpointer *stackdata)
291 return mono_threads_enter_gc_unsafe_region_with_info (mono_thread_info_current_unchecked (), stackdata);
295 mono_threads_enter_gc_unsafe_region_with_info (THREAD_INFO_TYPE *info, gpointer *stackdata)
299 if (!mono_threads_is_coop_enabled ())
302 cookie = mono_threads_enter_gc_unsafe_region_unbalanced_with_info (info, stackdata);
304 #ifdef ENABLE_CHECKED_BUILD_GC
305 if (mono_check_mode_enabled (MONO_CHECK_MODE_GC))
306 coop_tls_push (cookie);
313 mono_threads_enter_gc_unsafe_region_unbalanced (gpointer *stackdata)
315 return mono_threads_enter_gc_unsafe_region_unbalanced_with_info (mono_thread_info_current_unchecked (), stackdata);
319 mono_threads_enter_gc_unsafe_region_unbalanced_with_info (MonoThreadInfo *info, gpointer *stackdata)
321 if (!mono_threads_is_coop_enabled ())
324 ++coop_reset_blocking_count;
326 check_info (info, "enter", "unsafe");
328 copy_stack_data (info, stackdata);
330 switch (mono_threads_transition_abort_blocking (info)) {
331 case AbortBlockingIgnore:
332 info->thread_saved_state [SELF_SUSPEND_STATE_INDEX].valid = FALSE;
334 case AbortBlockingIgnoreAndPoll:
335 mono_threads_state_poll_with_info (info);
337 case AbortBlockingOk:
338 info->thread_saved_state [SELF_SUSPEND_STATE_INDEX].valid = FALSE;
340 case AbortBlockingWait:
341 mono_thread_info_wait_for_resume (info);
344 g_error ("Unknown thread state");
351 mono_threads_enter_gc_unsafe_region_cookie (void)
353 MonoThreadInfo *info;
355 g_assert (mono_threads_is_coop_enabled ());
357 info = mono_thread_info_current_unchecked ();
359 check_info (info, "enter (cookie)", "unsafe");
361 #ifdef ENABLE_CHECKED_BUILD_GC
362 if (mono_check_mode_enabled (MONO_CHECK_MODE_GC))
363 coop_tls_push (info);
370 mono_threads_exit_gc_unsafe_region (gpointer cookie, gpointer *stackdata)
372 if (!mono_threads_is_coop_enabled ())
375 #ifdef ENABLE_CHECKED_BUILD_GC
376 if (mono_check_mode_enabled (MONO_CHECK_MODE_GC))
377 coop_tls_pop (cookie);
380 mono_threads_exit_gc_unsafe_region_unbalanced (cookie, stackdata);
384 mono_threads_exit_gc_unsafe_region_unbalanced (gpointer cookie, gpointer *stackdata)
386 if (!mono_threads_is_coop_enabled ())
392 mono_threads_enter_gc_safe_region_unbalanced (stackdata);
396 mono_threads_assert_gc_unsafe_region (void)
398 MONO_REQ_GC_UNSAFE_MODE;
402 mono_threads_is_coop_enabled (void)
404 #if defined(USE_COOP_GC)
407 static int is_coop_enabled = -1;
408 if (G_UNLIKELY (is_coop_enabled == -1))
409 is_coop_enabled = g_getenv ("MONO_ENABLE_COOP") != NULL ? 1 : 0;
410 return is_coop_enabled == 1;
416 mono_threads_init_coop (void)
418 if (!mono_threads_is_coop_enabled ())
421 mono_counters_register ("Coop Reset Blocking", MONO_COUNTER_GC | MONO_COUNTER_INT, &coop_reset_blocking_count);
422 mono_counters_register ("Coop Try Blocking", MONO_COUNTER_GC | MONO_COUNTER_INT, &coop_try_blocking_count);
423 mono_counters_register ("Coop Do Blocking", MONO_COUNTER_GC | MONO_COUNTER_INT, &coop_do_blocking_count);
424 mono_counters_register ("Coop Do Polling", MONO_COUNTER_GC | MONO_COUNTER_INT, &coop_do_polling_count);
425 mono_counters_register ("Coop Save Count", MONO_COUNTER_GC | MONO_COUNTER_INT, &coop_save_count);
426 //See the above for what's wrong here.
428 #ifdef ENABLE_CHECKED_BUILD_GC
429 mono_native_tls_alloc (&coop_reset_count_stack_key, NULL);
434 mono_threads_coop_begin_global_suspend (void)
436 if (mono_threads_is_coop_enabled ())
437 mono_polling_required = 1;
441 mono_threads_coop_end_global_suspend (void)
443 if (mono_threads_is_coop_enabled ())
444 mono_polling_required = 0;