[coop] Remove DoneBlockingAborted state transition
[mono.git] / mono / utils / mono-threads-coop.c
1  /*
2  * mono-threads.c: Coop threading
3  *
4  * Author:
5  *      Rodrigo Kumpera (kumpera@gmail.com)
6  *
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.
9  */
10
11 #include <config.h>
12
13 /* enable pthread extensions */
14 #ifdef TARGET_MACH
15 #define _DARWIN_C_SOURCE
16 #endif
17
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>
30
31 #ifdef TARGET_OSX
32 #include <mono/utils/mach-support.h>
33 #endif
34
35 #ifdef _MSC_VER
36 // TODO: Find MSVC replacement for __builtin_unwind_init
37 #define SAVE_REGS_ON_STACK g_assert_not_reached ();
38 #else 
39 #define SAVE_REGS_ON_STACK __builtin_unwind_init ();
40 #endif
41
42 volatile size_t mono_polling_required;
43
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
47
48 // Maintains a single per-thread stack of ints, used to ensure nesting is not violated
49 static MonoNativeTlsKey coop_reset_count_stack_key;
50
51 static void
52 coop_tls_push (gpointer cookie)
53 {
54         GArray *stack;
55
56         stack = mono_native_tls_get_value (coop_reset_count_stack_key);
57         if (!stack) {
58                 stack = g_array_new (FALSE, FALSE, sizeof(gpointer));
59                 mono_native_tls_set_value (coop_reset_count_stack_key, stack);
60         }
61
62         g_array_append_val (stack, cookie);
63 }
64
65 static void
66 coop_tls_pop (gpointer received_cookie)
67 {
68         GArray *stack;
69         gpointer expected_cookie;
70
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);
74
75         expected_cookie = g_array_index (stack, gpointer, stack->len - 1);
76         stack->len --;
77
78         if (0 == stack->len) {
79                 g_array_free (stack,TRUE);
80                 mono_native_tls_set_value (coop_reset_count_stack_key, NULL);
81         }
82
83         if (expected_cookie != received_cookie)
84                 mono_fatal_with_history ("Received cookie %p but expected %p\n", received_cookie, expected_cookie);
85 }
86
87 #endif
88
89 static int coop_reset_blocking_count;
90 static int coop_try_blocking_count;
91 static int coop_do_blocking_count;
92 static int coop_do_polling_count;
93 static int coop_save_count;
94
95 void
96 mono_threads_state_poll (void)
97 {
98         MonoThreadInfo *info;
99
100         g_assert (mono_threads_is_coop_enabled ());
101
102         ++coop_do_polling_count;
103
104         info = mono_thread_info_current_unchecked ();
105         if (!info)
106                 return;
107         THREADS_SUSPEND_DEBUG ("FINISH SELF SUSPEND OF %p\n", mono_thread_info_get_tid (info));
108
109         /* Fast check for pending suspend requests */
110         if (!(info->thread_state & (STATE_ASYNC_SUSPEND_REQUESTED | STATE_SELF_SUSPEND_REQUESTED)))
111                 return;
112
113         ++coop_save_count;
114         mono_threads_get_runtime_callbacks ()->thread_state_init (&info->thread_saved_state [SELF_SUSPEND_STATE_INDEX]);
115
116         /* commit the saved state and notify others if needed */
117         switch (mono_threads_transition_state_poll (info)) {
118         case SelfSuspendResumed:
119                 return;
120         case SelfSuspendWait:
121                 mono_thread_info_wait_for_resume (info);
122                 break;
123         case SelfSuspendNotifyAndWait:
124                 mono_threads_notify_initiator_of_suspend (info);
125                 mono_thread_info_wait_for_resume (info);
126                 break;
127         }
128 }
129
130 static void *
131 return_stack_ptr ()
132 {
133         gpointer i;
134         return &i;
135 }
136
137 static void
138 copy_stack_data (MonoThreadInfo *info, gpointer *stackdata_begin)
139 {
140         MonoThreadUnwindState *state;
141         int stackdata_size;
142         void* stackdata_end = return_stack_ptr ();
143
144         SAVE_REGS_ON_STACK;
145
146         state = &info->thread_saved_state [SELF_SUSPEND_STATE_INDEX];
147
148         stackdata_size = (char*)stackdata_begin - (char*)stackdata_end;
149
150         if (((gsize) stackdata_begin & (SIZEOF_VOID_P - 1)) != 0)
151                 g_error ("stackdata_begin (%p) must be %d-byte aligned", stackdata_begin, SIZEOF_VOID_P);
152         if (((gsize) stackdata_end & (SIZEOF_VOID_P - 1)) != 0)
153                 g_error ("stackdata_end (%p) must be %d-byte aligned", stackdata_end, SIZEOF_VOID_P);
154
155         if (stackdata_size <= 0)
156                 g_error ("stackdata_size = %d, but must be > 0, stackdata_begin = %p, stackdata_end = %p", stackdata_size, stackdata_begin, stackdata_end);
157
158         g_byte_array_set_size (info->stackdata, stackdata_size);
159         state->gc_stackdata = info->stackdata->data;
160         memcpy (state->gc_stackdata, stackdata_end, stackdata_size);
161
162         state->gc_stackdata_size = stackdata_size;
163 }
164
165 gpointer
166 mono_threads_enter_gc_safe_region (gpointer *stackdata)
167 {
168         gpointer cookie;
169
170         if (!mono_threads_is_coop_enabled ())
171                 return NULL;
172
173         cookie = mono_threads_enter_gc_safe_region_unbalanced (stackdata);
174
175 #ifdef ENABLE_CHECKED_BUILD_GC
176         if (mono_check_mode_enabled (MONO_CHECK_MODE_GC))
177                 coop_tls_push (cookie);
178 #endif
179
180         return cookie;
181 }
182
183 gpointer
184 mono_threads_enter_gc_safe_region_unbalanced (gpointer *stackdata)
185 {
186         MonoThreadInfo *info;
187
188         if (!mono_threads_is_coop_enabled ())
189                 return NULL;
190
191         ++coop_do_blocking_count;
192
193         info = mono_thread_info_current_unchecked ();
194         /* If the thread is not attached, it doesn't make sense prepare for suspend. */
195         if (!info || !mono_thread_info_is_live (info)) {
196                 THREADS_SUSPEND_DEBUG ("PREPARE-BLOCKING failed %p\n", info ? mono_thread_info_get_tid (info) : NULL);
197                 return NULL;
198         }
199
200         copy_stack_data (info, stackdata);
201
202 retry:
203         ++coop_save_count;
204         mono_threads_get_runtime_callbacks ()->thread_state_init (&info->thread_saved_state [SELF_SUSPEND_STATE_INDEX]);
205
206         switch (mono_threads_transition_do_blocking (info)) {
207         case DoBlockingContinue:
208                 break;
209         case DoBlockingPollAndRetry:
210                 mono_threads_state_poll ();
211                 goto retry;
212         }
213
214         return info;
215 }
216
217 void
218 mono_threads_exit_gc_safe_region (gpointer cookie, gpointer *stackdata)
219 {
220         if (!mono_threads_is_coop_enabled ())
221                 return;
222
223 #ifdef ENABLE_CHECKED_BUILD_GC
224         if (mono_check_mode_enabled (MONO_CHECK_MODE_GC))
225                 coop_tls_pop (cookie);
226 #endif
227
228         mono_threads_exit_gc_safe_region_unbalanced (cookie, stackdata);
229 }
230
231 void
232 mono_threads_exit_gc_safe_region_unbalanced (gpointer cookie, gpointer *stackdata)
233 {
234         MonoThreadInfo *info;
235
236         if (!mono_threads_is_coop_enabled ())
237                 return;
238
239         info = (MonoThreadInfo *)cookie;
240         if (!info)
241                 return;
242
243         g_assert (info == mono_thread_info_current_unchecked ());
244
245         switch (mono_threads_transition_done_blocking (info)) {
246         case DoneBlockingOk:
247                 info->thread_saved_state [SELF_SUSPEND_STATE_INDEX].valid = FALSE;
248                 break;
249         case DoneBlockingWait:
250                 THREADS_SUSPEND_DEBUG ("state polling done, notifying of resume\n");
251                 mono_thread_info_wait_for_resume (info);
252                 break;
253         default:
254                 g_error ("Unknown thread state");
255         }
256 }
257
258 void
259 mono_threads_assert_gc_safe_region (void)
260 {
261         MONO_REQ_GC_SAFE_MODE;
262 }
263
264 gpointer
265 mono_threads_enter_gc_unsafe_region (gpointer *stackdata)
266 {
267         gpointer cookie;
268
269         if (!mono_threads_is_coop_enabled ())
270                 return NULL;
271
272         cookie = mono_threads_enter_gc_unsafe_region_unbalanced (stackdata);
273
274 #ifdef ENABLE_CHECKED_BUILD_GC
275         if (mono_check_mode_enabled (MONO_CHECK_MODE_GC))
276                 coop_tls_push (cookie);
277 #endif
278
279         return cookie;
280 }
281
282 gpointer
283 mono_threads_enter_gc_unsafe_region_unbalanced (gpointer *stackdata)
284 {
285         MonoThreadInfo *info;
286
287         if (!mono_threads_is_coop_enabled ())
288                 return NULL;
289
290         ++coop_reset_blocking_count;
291
292         info = mono_thread_info_current_unchecked ();
293
294         /* If the thread is not attached, it doesn't make sense prepare for suspend. */
295         if (!info || !mono_thread_info_is_live (info))
296                 return NULL;
297
298         copy_stack_data (info, stackdata);
299
300         switch (mono_threads_transition_abort_blocking (info)) {
301         case AbortBlockingIgnore:
302                 info->thread_saved_state [SELF_SUSPEND_STATE_INDEX].valid = FALSE;
303                 return NULL;
304         case AbortBlockingIgnoreAndPoll:
305                 mono_threads_state_poll ();
306                 return NULL;
307         case AbortBlockingOk:
308                 info->thread_saved_state [SELF_SUSPEND_STATE_INDEX].valid = FALSE;
309                 break;
310         case AbortBlockingWait:
311                 mono_thread_info_wait_for_resume (info);
312                 break;
313         default:
314                 g_error ("Unknown thread state");
315         }
316
317         return info;
318 }
319
320 gpointer
321 mono_threads_enter_gc_unsafe_region_cookie (MonoThreadInfo *info)
322 {
323         g_assert (mono_threads_is_coop_enabled ());
324
325 #ifdef ENABLE_CHECKED_BUILD_GC
326         if (mono_check_mode_enabled (MONO_CHECK_MODE_GC))
327                 coop_tls_push (info);
328 #endif
329
330         return info;
331 }
332
333 void
334 mono_threads_exit_gc_unsafe_region (gpointer cookie, gpointer *stackdata)
335 {
336         if (!mono_threads_is_coop_enabled ())
337                 return;
338
339 #ifdef ENABLE_CHECKED_BUILD_GC
340         if (mono_check_mode_enabled (MONO_CHECK_MODE_GC))
341                 coop_tls_pop (cookie);
342 #endif
343
344         mono_threads_exit_gc_unsafe_region_unbalanced (cookie, stackdata);
345 }
346
347 void
348 mono_threads_exit_gc_unsafe_region_unbalanced (gpointer cookie, gpointer *stackdata)
349 {
350         if (!mono_threads_is_coop_enabled ())
351                 return;
352
353         if (!cookie)
354                 return;
355
356 #ifdef ENABLE_CHECKED_BUILD_GC
357         if (!mono_check_mode_enabled (MONO_CHECK_MODE_GC))
358 #endif
359         {
360                 g_assert (((MonoThreadInfo *)cookie) == mono_thread_info_current_unchecked ());
361         }
362
363         mono_threads_enter_gc_safe_region_unbalanced (stackdata);
364 }
365
366 void
367 mono_threads_assert_gc_unsafe_region (void)
368 {
369         MONO_REQ_GC_UNSAFE_MODE;
370 }
371
372 gboolean
373 mono_threads_is_coop_enabled (void)
374 {
375 #if defined(USE_COOP_GC)
376         return TRUE;
377 #else
378         static int is_coop_enabled = -1;
379         if (G_UNLIKELY (is_coop_enabled == -1))
380                 is_coop_enabled = g_getenv ("MONO_ENABLE_COOP") != NULL ? 1 : 0;
381         return is_coop_enabled == 1;
382 #endif
383 }
384
385
386 void
387 mono_threads_init_coop (void)
388 {
389         if (!mono_threads_is_coop_enabled ())
390                 return;
391
392         mono_counters_register ("Coop Reset Blocking", MONO_COUNTER_GC | MONO_COUNTER_INT, &coop_reset_blocking_count);
393         mono_counters_register ("Coop Try Blocking", MONO_COUNTER_GC | MONO_COUNTER_INT, &coop_try_blocking_count);
394         mono_counters_register ("Coop Do Blocking", MONO_COUNTER_GC | MONO_COUNTER_INT, &coop_do_blocking_count);
395         mono_counters_register ("Coop Do Polling", MONO_COUNTER_GC | MONO_COUNTER_INT, &coop_do_polling_count);
396         mono_counters_register ("Coop Save Count", MONO_COUNTER_GC | MONO_COUNTER_INT, &coop_save_count);
397         //See the above for what's wrong here.
398
399 #ifdef ENABLE_CHECKED_BUILD_GC
400         mono_native_tls_alloc (&coop_reset_count_stack_key, NULL);
401 #endif
402 }
403
404 void
405 mono_threads_coop_begin_global_suspend (void)
406 {
407         if (mono_threads_is_coop_enabled ())
408                 mono_polling_required = 1;
409 }
410
411 void
412 mono_threads_coop_end_global_suspend (void)
413 {
414         if (mono_threads_is_coop_enabled ())
415                 mono_polling_required = 0;
416 }