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>
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 #ifdef USE_COOP_BACKEND
41 volatile size_t mono_polling_required;
43 static int coop_reset_blocking_count, coop_try_blocking_count, coop_do_blocking_count, coop_do_polling_count, coop_save_count;
46 mono_threads_state_poll (void)
49 ++coop_do_polling_count;
51 info = mono_thread_info_current_unchecked ();
54 THREADS_SUSPEND_DEBUG ("FINISH SELF SUSPEND OF %p\n", mono_thread_info_get_tid (info));
56 /* Fast check for pending suspend requests */
57 if (!(info->thread_state & (STATE_ASYNC_SUSPEND_REQUESTED | STATE_SELF_SUSPEND_REQUESTED)))
61 mono_threads_get_runtime_callbacks ()->thread_state_init (&info->thread_saved_state [SELF_SUSPEND_STATE_INDEX]);
63 /* commit the saved state and notify others if needed */
64 switch (mono_threads_transition_state_poll (info)) {
65 case SelfSuspendResumed:
68 mono_thread_info_wait_for_resume (info);
70 case SelfSuspendNotifyAndWait:
71 mono_threads_notify_initiator_of_suspend (info);
72 mono_thread_info_wait_for_resume (info);
85 copy_stack_data (MonoThreadInfo *info, void* stackdata_begin)
87 MonoThreadUnwindState *state;
89 void* stackdata_end = return_stack_ptr ();
93 state = &info->thread_saved_state [SELF_SUSPEND_STATE_INDEX];
95 stackdata_size = (char*)stackdata_begin - (char*)stackdata_end;
96 g_assert (stackdata_size > 0);
98 g_byte_array_set_size (info->stackdata, stackdata_size);
99 state->gc_stackdata = info->stackdata->data;
100 memcpy (state->gc_stackdata, stackdata_end, stackdata_size);
102 state->gc_stackdata_size = stackdata_size;
106 mono_threads_prepare_blocking (void* stackdata)
108 MonoThreadInfo *info;
109 ++coop_do_blocking_count;
111 info = mono_thread_info_current_unchecked ();
112 /* If the thread is not attached, it doesn't make sense prepare for suspend. */
113 if (!info || !mono_thread_info_is_live (info)) {
114 THREADS_SUSPEND_DEBUG ("PREPARE-BLOCKING failed %p\n", mono_thread_info_get_tid (info));
118 copy_stack_data (info, stackdata);
122 mono_threads_get_runtime_callbacks ()->thread_state_init (&info->thread_saved_state [SELF_SUSPEND_STATE_INDEX]);
124 switch (mono_threads_transition_do_blocking (info)) {
125 case DoBlockingContinue:
127 case DoBlockingPollAndRetry:
128 mono_threads_state_poll ();
136 mono_threads_finish_blocking (void *cookie, void* stackdata)
138 static gboolean warned_about_bad_transition;
139 MonoThreadInfo *info = cookie;
144 g_assert (info == mono_thread_info_current_unchecked ());
146 switch (mono_threads_transition_done_blocking (info)) {
147 case DoneBlockingAborted:
148 if (!warned_about_bad_transition) {
149 warned_about_bad_transition = TRUE;
150 g_warning ("[%p] Blocking call ended in running state for, this might lead to unbound GC pauses.", mono_thread_info_get_tid (info));
152 mono_threads_state_poll ();
155 info->thread_saved_state [SELF_SUSPEND_STATE_INDEX].valid = FALSE;
157 case DoneBlockingWait:
158 THREADS_SUSPEND_DEBUG ("state polling done, notifying of resume\n");
159 mono_thread_info_wait_for_resume (info);
162 g_error ("Unknown thread state");
168 mono_threads_reset_blocking_start (void* stackdata)
170 MonoThreadInfo *info = mono_thread_info_current_unchecked ();
171 ++coop_reset_blocking_count;
173 /* If the thread is not attached, it doesn't make sense prepare for suspend. */
174 if (!info || !mono_thread_info_is_live (info))
177 copy_stack_data (info, stackdata);
179 switch (mono_threads_transition_abort_blocking (info)) {
180 case AbortBlockingIgnore:
181 info->thread_saved_state [SELF_SUSPEND_STATE_INDEX].valid = FALSE;
183 case AbortBlockingIgnoreAndPoll:
184 mono_threads_state_poll ();
186 case AbortBlockingOk:
187 info->thread_saved_state [SELF_SUSPEND_STATE_INDEX].valid = FALSE;
189 case AbortBlockingOkAndPool:
190 mono_threads_state_poll ();
193 g_error ("Unknown thread state");
198 mono_threads_reset_blocking_end (void *cookie, void* stackdata)
200 MonoThreadInfo *info = cookie;
205 g_assert (info == mono_thread_info_current_unchecked ());
206 mono_threads_prepare_blocking (stackdata);
210 mono_threads_try_prepare_blocking (void* stackdata)
212 MonoThreadInfo *info;
213 ++coop_try_blocking_count;
215 info = mono_thread_info_current_unchecked ();
216 /* If the thread is not attached, it doesn't make sense prepare for suspend. */
217 if (!info || !mono_thread_info_is_live (info) || mono_thread_info_current_state (info) == STATE_BLOCKING) {
218 THREADS_SUSPEND_DEBUG ("PREPARE-TRY-BLOCKING failed %p\n", mono_thread_info_get_tid (info));
222 copy_stack_data (info, stackdata);
226 mono_threads_get_runtime_callbacks ()->thread_state_init (&info->thread_saved_state [SELF_SUSPEND_STATE_INDEX]);
228 switch (mono_threads_transition_do_blocking (info)) {
229 case DoBlockingContinue:
231 case DoBlockingPollAndRetry:
232 mono_threads_state_poll ();
240 mono_threads_finish_try_blocking (void* cookie, void* stackdata)
242 mono_threads_finish_blocking (cookie, stackdata);
246 mono_threads_core_begin_async_resume (MonoThreadInfo *info)
253 mono_threads_core_begin_async_suspend (MonoThreadInfo *info, gboolean interrupt_kernel)
255 mono_threads_add_to_pending_operation_set (info);
256 /* There's nothing else to do after we async request the thread to suspend */
261 mono_threads_core_check_suspend_result (MonoThreadInfo *info)
263 /* Async suspend can't async fail on coop */
268 mono_threads_init_platform (void)
270 mono_counters_register ("Coop Reset Blocking", MONO_COUNTER_GC | MONO_COUNTER_INT, &coop_reset_blocking_count);
271 mono_counters_register ("Coop Try Blocking", MONO_COUNTER_GC | MONO_COUNTER_INT, &coop_try_blocking_count);
272 mono_counters_register ("Coop Do Blocking", MONO_COUNTER_GC | MONO_COUNTER_INT, &coop_do_blocking_count);
273 mono_counters_register ("Coop Do Polling", MONO_COUNTER_GC | MONO_COUNTER_INT, &coop_do_polling_count);
274 mono_counters_register ("Coop Save Count", MONO_COUNTER_GC | MONO_COUNTER_INT, &coop_save_count);
275 //See the above for what's wrong here.
279 mono_threads_platform_free (MonoThreadInfo *info)
282 mach_port_deallocate (current_task (), info->native_handle);
285 //See the above for what's wrong here.
289 mono_threads_platform_register (MonoThreadInfo *info)
292 char thread_name [64];
294 info->native_handle = mach_thread_self ();
295 snprintf (thread_name, 64, "tid_%x", (int)info->native_handle);
296 pthread_setname_np (thread_name);
299 //See the above for what's wrong here.
303 mono_threads_core_begin_global_suspend (void)
305 mono_polling_required = 1;
309 mono_threads_core_end_global_suspend (void)
311 mono_polling_required = 0;
315 mono_threads_enter_gc_unsafe_region (void* stackdata)
317 return mono_threads_reset_blocking_start (stackdata);
321 mono_threads_exit_gc_unsafe_region (void *regions_cookie, void* stackdata)
323 mono_threads_reset_blocking_end (regions_cookie, stackdata);
329 mono_threads_enter_gc_unsafe_region (void* stackdata)
335 mono_threads_exit_gc_unsafe_region (void *regions_cookie, void* stackdata)