2 * mono-threads.c: Coop threading
5 * Rodrigo Kumpera (kumpera@gmail.com)
7 * Copyright 2015 Xamarin, Inc (http://www.xamarin.com)
12 /* enable pthread extensions */
14 #define _DARWIN_C_SOURCE
17 #include <mono/utils/mono-compiler.h>
18 #include <mono/utils/mono-threads.h>
19 #include <mono/utils/mono-tls.h>
20 #include <mono/utils/hazard-pointer.h>
21 #include <mono/utils/mono-memory-model.h>
22 #include <mono/utils/mono-mmap.h>
23 #include <mono/utils/atomic.h>
24 #include <mono/utils/mono-time.h>
25 #include <mono/utils/mono-counters.h>
26 #include <mono/utils/mono-threads-coop.h>
29 #include <mono/utils/mach-support.h>
33 // TODO: Find MSVC replacement for __builtin_unwind_init
34 #define SAVE_REGS_ON_STACK g_assert_not_reached ();
36 #define SAVE_REGS_ON_STACK __builtin_unwind_init ();
39 volatile size_t mono_polling_required;
41 static int coop_reset_blocking_count;
42 static int coop_try_blocking_count;
43 static int coop_do_blocking_count;
44 static int coop_do_polling_count;
45 static int coop_save_count;
48 mono_threads_state_poll (void)
52 g_assert (mono_threads_is_coop_enabled ());
54 ++coop_do_polling_count;
56 info = mono_thread_info_current_unchecked ();
59 THREADS_SUSPEND_DEBUG ("FINISH SELF SUSPEND OF %p\n", mono_thread_info_get_tid (info));
61 /* Fast check for pending suspend requests */
62 if (!(info->thread_state & (STATE_ASYNC_SUSPEND_REQUESTED | STATE_SELF_SUSPEND_REQUESTED)))
66 mono_threads_get_runtime_callbacks ()->thread_state_init (&info->thread_saved_state [SELF_SUSPEND_STATE_INDEX]);
68 /* commit the saved state and notify others if needed */
69 switch (mono_threads_transition_state_poll (info)) {
70 case SelfSuspendResumed:
73 mono_thread_info_wait_for_resume (info);
75 case SelfSuspendNotifyAndWait:
76 mono_threads_notify_initiator_of_suspend (info);
77 mono_thread_info_wait_for_resume (info);
90 copy_stack_data (MonoThreadInfo *info, void* stackdata_begin)
92 MonoThreadUnwindState *state;
94 void* stackdata_end = return_stack_ptr ();
98 state = &info->thread_saved_state [SELF_SUSPEND_STATE_INDEX];
100 stackdata_size = (char*)stackdata_begin - (char*)stackdata_end;
102 if (((gsize) stackdata_begin & (SIZEOF_VOID_P - 1)) != 0)
103 g_error ("stackdata_begin (%p) must be %d-byte aligned", stackdata_begin, SIZEOF_VOID_P);
104 if (((gsize) stackdata_end & (SIZEOF_VOID_P - 1)) != 0)
105 g_error ("stackdata_end (%p) must be %d-byte aligned", stackdata_end, SIZEOF_VOID_P);
107 if (stackdata_size <= 0)
108 g_error ("stackdata_size = %d, but must be > 0, stackdata_begin = %p, stackdata_end = %p", stackdata_size, stackdata_begin, stackdata_end);
110 g_byte_array_set_size (info->stackdata, stackdata_size);
111 state->gc_stackdata = info->stackdata->data;
112 memcpy (state->gc_stackdata, stackdata_end, stackdata_size);
114 state->gc_stackdata_size = stackdata_size;
118 mono_threads_prepare_blocking (void* stackdata)
120 MonoThreadInfo *info;
122 if (!mono_threads_is_coop_enabled ())
125 ++coop_do_blocking_count;
127 info = mono_thread_info_current_unchecked ();
128 /* If the thread is not attached, it doesn't make sense prepare for suspend. */
129 if (!info || !mono_thread_info_is_live (info)) {
130 THREADS_SUSPEND_DEBUG ("PREPARE-BLOCKING failed %p\n", mono_thread_info_get_tid (info));
134 copy_stack_data (info, stackdata);
138 mono_threads_get_runtime_callbacks ()->thread_state_init (&info->thread_saved_state [SELF_SUSPEND_STATE_INDEX]);
140 switch (mono_threads_transition_do_blocking (info)) {
141 case DoBlockingContinue:
143 case DoBlockingPollAndRetry:
144 mono_threads_state_poll ();
152 mono_threads_finish_blocking (void *cookie, void* stackdata)
154 static gboolean warned_about_bad_transition;
155 MonoThreadInfo *info;
157 if (!mono_threads_is_coop_enabled ())
164 g_assert (info == mono_thread_info_current_unchecked ());
166 switch (mono_threads_transition_done_blocking (info)) {
167 case DoneBlockingAborted:
168 if (!warned_about_bad_transition) {
169 warned_about_bad_transition = TRUE;
170 g_warning ("[%p] Blocking call ended in running state for, this might lead to unbound GC pauses.", mono_thread_info_get_tid (info));
172 mono_threads_state_poll ();
175 info->thread_saved_state [SELF_SUSPEND_STATE_INDEX].valid = FALSE;
177 case DoneBlockingWait:
178 THREADS_SUSPEND_DEBUG ("state polling done, notifying of resume\n");
179 mono_thread_info_wait_for_resume (info);
182 g_error ("Unknown thread state");
188 mono_threads_reset_blocking_start (void* stackdata)
190 MonoThreadInfo *info;
192 if (!mono_threads_is_coop_enabled ())
195 ++coop_reset_blocking_count;
197 info = mono_thread_info_current_unchecked ();
198 /* If the thread is not attached, it doesn't make sense prepare for suspend. */
199 if (!info || !mono_thread_info_is_live (info))
202 copy_stack_data (info, stackdata);
204 switch (mono_threads_transition_abort_blocking (info)) {
205 case AbortBlockingIgnore:
206 info->thread_saved_state [SELF_SUSPEND_STATE_INDEX].valid = FALSE;
208 case AbortBlockingIgnoreAndPoll:
209 mono_threads_state_poll ();
211 case AbortBlockingOk:
212 info->thread_saved_state [SELF_SUSPEND_STATE_INDEX].valid = FALSE;
214 case AbortBlockingOkAndPool:
215 mono_threads_state_poll ();
218 g_error ("Unknown thread state");
223 mono_threads_reset_blocking_end (void *cookie, void* stackdata)
225 MonoThreadInfo *info;
227 if (!mono_threads_is_coop_enabled ())
234 g_assert (info == mono_thread_info_current_unchecked ());
235 mono_threads_prepare_blocking (stackdata);
239 mono_threads_init_coop (void)
241 if (!mono_threads_is_coop_enabled ())
244 mono_counters_register ("Coop Reset Blocking", MONO_COUNTER_GC | MONO_COUNTER_INT, &coop_reset_blocking_count);
245 mono_counters_register ("Coop Try Blocking", MONO_COUNTER_GC | MONO_COUNTER_INT, &coop_try_blocking_count);
246 mono_counters_register ("Coop Do Blocking", MONO_COUNTER_GC | MONO_COUNTER_INT, &coop_do_blocking_count);
247 mono_counters_register ("Coop Do Polling", MONO_COUNTER_GC | MONO_COUNTER_INT, &coop_do_polling_count);
248 mono_counters_register ("Coop Save Count", MONO_COUNTER_GC | MONO_COUNTER_INT, &coop_save_count);
249 //See the above for what's wrong here.
253 mono_threads_coop_begin_global_suspend (void)
255 if (mono_threads_is_coop_enabled ())
256 mono_polling_required = 1;
260 mono_threads_coop_end_global_suspend (void)
262 if (mono_threads_is_coop_enabled ())
263 mono_polling_required = 0;
267 mono_threads_enter_gc_unsafe_region (void* stackdata)
269 if (!mono_threads_is_coop_enabled ())
272 return mono_threads_reset_blocking_start (stackdata);
276 mono_threads_exit_gc_unsafe_region (void *regions_cookie, void* stackdata)
278 if (!mono_threads_is_coop_enabled ())
281 mono_threads_reset_blocking_end (regions_cookie, stackdata);