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-semaphore.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>
30 #include <mono/utils/mach-support.h>
34 // TODO: Find MSVC replacement for __builtin_unwind_init
35 #define SAVE_REGS_ON_STACK g_assert_not_reached ();
37 #define SAVE_REGS_ON_STACK __builtin_unwind_init ();
40 volatile size_t mono_polling_required;
42 static int coop_reset_blocking_count;
43 static int coop_try_blocking_count;
44 static int coop_do_blocking_count;
45 static int coop_do_polling_count;
46 static int coop_save_count;
49 mono_threads_state_poll (void)
53 g_assert (mono_threads_is_coop_enabled ());
55 ++coop_do_polling_count;
57 info = mono_thread_info_current_unchecked ();
60 THREADS_SUSPEND_DEBUG ("FINISH SELF SUSPEND OF %p\n", mono_thread_info_get_tid (info));
62 /* Fast check for pending suspend requests */
63 if (!(info->thread_state & (STATE_ASYNC_SUSPEND_REQUESTED | STATE_SELF_SUSPEND_REQUESTED)))
67 mono_threads_get_runtime_callbacks ()->thread_state_init (&info->thread_saved_state [SELF_SUSPEND_STATE_INDEX]);
69 /* commit the saved state and notify others if needed */
70 switch (mono_threads_transition_state_poll (info)) {
71 case SelfSuspendResumed:
74 mono_thread_info_wait_for_resume (info);
76 case SelfSuspendNotifyAndWait:
77 mono_threads_notify_initiator_of_suspend (info);
78 mono_thread_info_wait_for_resume (info);
91 copy_stack_data (MonoThreadInfo *info, void* stackdata_begin)
93 MonoThreadUnwindState *state;
95 void* stackdata_end = return_stack_ptr ();
99 state = &info->thread_saved_state [SELF_SUSPEND_STATE_INDEX];
101 stackdata_size = (char*)stackdata_begin - (char*)stackdata_end;
102 if (stackdata_size <= 0)
103 g_error ("stackdata_size = %d, but must be > 0, stackdata_begin = %p, stackdata_end = %p", stackdata_size, stackdata_begin, stackdata_end);
105 g_byte_array_set_size (info->stackdata, stackdata_size);
106 state->gc_stackdata = info->stackdata->data;
107 memcpy (state->gc_stackdata, stackdata_end, stackdata_size);
109 state->gc_stackdata_size = stackdata_size;
113 mono_threads_prepare_blocking (void* stackdata)
115 MonoThreadInfo *info;
117 if (!mono_threads_is_coop_enabled ())
120 ++coop_do_blocking_count;
122 info = mono_thread_info_current_unchecked ();
123 /* If the thread is not attached, it doesn't make sense prepare for suspend. */
124 if (!info || !mono_thread_info_is_live (info)) {
125 THREADS_SUSPEND_DEBUG ("PREPARE-BLOCKING failed %p\n", mono_thread_info_get_tid (info));
129 copy_stack_data (info, stackdata);
133 mono_threads_get_runtime_callbacks ()->thread_state_init (&info->thread_saved_state [SELF_SUSPEND_STATE_INDEX]);
135 switch (mono_threads_transition_do_blocking (info)) {
136 case DoBlockingContinue:
138 case DoBlockingPollAndRetry:
139 mono_threads_state_poll ();
147 mono_threads_finish_blocking (void *cookie, void* stackdata)
149 static gboolean warned_about_bad_transition;
150 MonoThreadInfo *info;
152 if (!mono_threads_is_coop_enabled ())
159 g_assert (info == mono_thread_info_current_unchecked ());
161 switch (mono_threads_transition_done_blocking (info)) {
162 case DoneBlockingAborted:
163 if (!warned_about_bad_transition) {
164 warned_about_bad_transition = TRUE;
165 g_warning ("[%p] Blocking call ended in running state for, this might lead to unbound GC pauses.", mono_thread_info_get_tid (info));
167 mono_threads_state_poll ();
170 info->thread_saved_state [SELF_SUSPEND_STATE_INDEX].valid = FALSE;
172 case DoneBlockingWait:
173 THREADS_SUSPEND_DEBUG ("state polling done, notifying of resume\n");
174 mono_thread_info_wait_for_resume (info);
177 g_error ("Unknown thread state");
183 mono_threads_reset_blocking_start (void* stackdata)
185 MonoThreadInfo *info;
187 if (!mono_threads_is_coop_enabled ())
190 ++coop_reset_blocking_count;
192 info = mono_thread_info_current_unchecked ();
193 /* If the thread is not attached, it doesn't make sense prepare for suspend. */
194 if (!info || !mono_thread_info_is_live (info))
197 copy_stack_data (info, stackdata);
199 switch (mono_threads_transition_abort_blocking (info)) {
200 case AbortBlockingIgnore:
201 info->thread_saved_state [SELF_SUSPEND_STATE_INDEX].valid = FALSE;
203 case AbortBlockingIgnoreAndPoll:
204 mono_threads_state_poll ();
206 case AbortBlockingOk:
207 info->thread_saved_state [SELF_SUSPEND_STATE_INDEX].valid = FALSE;
209 case AbortBlockingOkAndPool:
210 mono_threads_state_poll ();
213 g_error ("Unknown thread state");
218 mono_threads_reset_blocking_end (void *cookie, void* stackdata)
220 MonoThreadInfo *info;
222 if (!mono_threads_is_coop_enabled ())
229 g_assert (info == mono_thread_info_current_unchecked ());
230 mono_threads_prepare_blocking (stackdata);
234 mono_threads_try_prepare_blocking (void* stackdata)
236 MonoThreadInfo *info;
238 if (!mono_threads_is_coop_enabled ())
241 ++coop_try_blocking_count;
243 info = mono_thread_info_current_unchecked ();
244 /* If the thread is not attached, it doesn't make sense prepare for suspend. */
245 if (!info || !mono_thread_info_is_live (info) || mono_thread_info_current_state (info) == STATE_BLOCKING) {
246 THREADS_SUSPEND_DEBUG ("PREPARE-TRY-BLOCKING failed %p\n", mono_thread_info_get_tid (info));
250 copy_stack_data (info, stackdata);
254 mono_threads_get_runtime_callbacks ()->thread_state_init (&info->thread_saved_state [SELF_SUSPEND_STATE_INDEX]);
256 switch (mono_threads_transition_do_blocking (info)) {
257 case DoBlockingContinue:
259 case DoBlockingPollAndRetry:
260 mono_threads_state_poll ();
268 mono_threads_finish_try_blocking (void* cookie, void* stackdata)
270 if (!mono_threads_is_coop_enabled ())
273 mono_threads_finish_blocking (cookie, stackdata);
277 mono_threads_init_coop (void)
279 if (!mono_threads_is_coop_enabled ())
282 mono_counters_register ("Coop Reset Blocking", MONO_COUNTER_GC | MONO_COUNTER_INT, &coop_reset_blocking_count);
283 mono_counters_register ("Coop Try Blocking", MONO_COUNTER_GC | MONO_COUNTER_INT, &coop_try_blocking_count);
284 mono_counters_register ("Coop Do Blocking", MONO_COUNTER_GC | MONO_COUNTER_INT, &coop_do_blocking_count);
285 mono_counters_register ("Coop Do Polling", MONO_COUNTER_GC | MONO_COUNTER_INT, &coop_do_polling_count);
286 mono_counters_register ("Coop Save Count", MONO_COUNTER_GC | MONO_COUNTER_INT, &coop_save_count);
287 //See the above for what's wrong here.
291 mono_threads_coop_begin_global_suspend (void)
293 if (mono_threads_is_coop_enabled ())
294 mono_polling_required = 1;
298 mono_threads_coop_end_global_suspend (void)
300 if (mono_threads_is_coop_enabled ())
301 mono_polling_required = 0;
305 mono_threads_enter_gc_unsafe_region (void* stackdata)
307 if (!mono_threads_is_coop_enabled ())
310 return mono_threads_reset_blocking_start (stackdata);
314 mono_threads_exit_gc_unsafe_region (void *regions_cookie, void* stackdata)
316 if (!mono_threads_is_coop_enabled ())
319 mono_threads_reset_blocking_end (regions_cookie, stackdata);