[sgen] Clear the card table in the finishing pause
[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  */
9
10 #include <config.h>
11
12 /* enable pthread extensions */
13 #ifdef TARGET_MACH
14 #define _DARWIN_C_SOURCE
15 #endif
16
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>
27 #include <mono/utils/mono-threads-api.h>
28
29 #ifdef TARGET_OSX
30 #include <mono/utils/mach-support.h>
31 #endif
32
33 #ifdef _MSC_VER
34 // TODO: Find MSVC replacement for __builtin_unwind_init
35 #define SAVE_REGS_ON_STACK g_assert_not_reached ();
36 #else 
37 #define SAVE_REGS_ON_STACK __builtin_unwind_init ();
38 #endif
39
40 volatile size_t mono_polling_required;
41
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;
47
48 void
49 mono_threads_state_poll (void)
50 {
51         MonoThreadInfo *info;
52
53         g_assert (mono_threads_is_coop_enabled ());
54
55         ++coop_do_polling_count;
56
57         info = mono_thread_info_current_unchecked ();
58         if (!info)
59                 return;
60         THREADS_SUSPEND_DEBUG ("FINISH SELF SUSPEND OF %p\n", mono_thread_info_get_tid (info));
61
62         /* Fast check for pending suspend requests */
63         if (!(info->thread_state & (STATE_ASYNC_SUSPEND_REQUESTED | STATE_SELF_SUSPEND_REQUESTED)))
64                 return;
65
66         ++coop_save_count;
67         mono_threads_get_runtime_callbacks ()->thread_state_init (&info->thread_saved_state [SELF_SUSPEND_STATE_INDEX]);
68
69         /* commit the saved state and notify others if needed */
70         switch (mono_threads_transition_state_poll (info)) {
71         case SelfSuspendResumed:
72                 return;
73         case SelfSuspendWait:
74                 mono_thread_info_wait_for_resume (info);
75                 break;
76         case SelfSuspendNotifyAndWait:
77                 mono_threads_notify_initiator_of_suspend (info);
78                 mono_thread_info_wait_for_resume (info);
79                 break;
80         }
81 }
82
83 static void *
84 return_stack_ptr ()
85 {
86         gpointer i;
87         return &i;
88 }
89
90 static void
91 copy_stack_data (MonoThreadInfo *info, void* stackdata_begin)
92 {
93         MonoThreadUnwindState *state;
94         int stackdata_size;
95         void* stackdata_end = return_stack_ptr ();
96
97         SAVE_REGS_ON_STACK;
98
99         state = &info->thread_saved_state [SELF_SUSPEND_STATE_INDEX];
100
101         stackdata_size = (char*)stackdata_begin - (char*)stackdata_end;
102
103         if (((gsize) stackdata_begin & (SIZEOF_VOID_P - 1)) != 0)
104                 g_error ("stackdata_begin (%p) must be %d-byte aligned", stackdata_begin, SIZEOF_VOID_P);
105         if (((gsize) stackdata_end & (SIZEOF_VOID_P - 1)) != 0)
106                 g_error ("stackdata_end (%p) must be %d-byte aligned", stackdata_end, SIZEOF_VOID_P);
107
108         if (stackdata_size <= 0)
109                 g_error ("stackdata_size = %d, but must be > 0, stackdata_begin = %p, stackdata_end = %p", stackdata_size, stackdata_begin, stackdata_end);
110
111         g_byte_array_set_size (info->stackdata, stackdata_size);
112         state->gc_stackdata = info->stackdata->data;
113         memcpy (state->gc_stackdata, stackdata_end, stackdata_size);
114
115         state->gc_stackdata_size = stackdata_size;
116 }
117
118 void*
119 mono_threads_prepare_blocking (void* stackdata)
120 {
121         MonoThreadInfo *info;
122
123         if (!mono_threads_is_coop_enabled ())
124                 return NULL;
125
126         ++coop_do_blocking_count;
127
128         info = mono_thread_info_current_unchecked ();
129         /* If the thread is not attached, it doesn't make sense prepare for suspend. */
130         if (!info || !mono_thread_info_is_live (info)) {
131                 THREADS_SUSPEND_DEBUG ("PREPARE-BLOCKING failed %p\n", mono_thread_info_get_tid (info));
132                 return NULL;
133         }
134
135         copy_stack_data (info, stackdata);
136
137 retry:
138         ++coop_save_count;
139         mono_threads_get_runtime_callbacks ()->thread_state_init (&info->thread_saved_state [SELF_SUSPEND_STATE_INDEX]);
140
141         switch (mono_threads_transition_do_blocking (info)) {
142         case DoBlockingContinue:
143                 break;
144         case DoBlockingPollAndRetry:
145                 mono_threads_state_poll ();
146                 goto retry;
147         }
148
149         return info;
150 }
151
152 void
153 mono_threads_finish_blocking (void *cookie, void* stackdata)
154 {
155         static gboolean warned_about_bad_transition;
156         MonoThreadInfo *info;
157
158         if (!mono_threads_is_coop_enabled ())
159                 return;
160
161         info = (MonoThreadInfo *)cookie;
162         if (!info)
163                 return;
164
165         g_assert (info == mono_thread_info_current_unchecked ());
166
167         switch (mono_threads_transition_done_blocking (info)) {
168         case DoneBlockingAborted:
169                 if (!warned_about_bad_transition) {
170                         warned_about_bad_transition = TRUE;
171                         g_warning ("[%p] Blocking call ended in running state for, this might lead to unbound GC pauses.", mono_thread_info_get_tid (info));
172                 }
173                 mono_threads_state_poll ();
174                 break;
175         case DoneBlockingOk:
176                 info->thread_saved_state [SELF_SUSPEND_STATE_INDEX].valid = FALSE;
177                 break;
178         case DoneBlockingWait:
179                 THREADS_SUSPEND_DEBUG ("state polling done, notifying of resume\n");
180                 mono_thread_info_wait_for_resume (info);
181                 break;
182         default:
183                 g_error ("Unknown thread state");
184         }
185 }
186
187
188 void*
189 mono_threads_reset_blocking_start (void* stackdata)
190 {
191         MonoThreadInfo *info;
192
193         if (!mono_threads_is_coop_enabled ())
194                 return NULL;
195
196         ++coop_reset_blocking_count;
197
198         info = mono_thread_info_current_unchecked ();
199         /* If the thread is not attached, it doesn't make sense prepare for suspend. */
200         if (!info || !mono_thread_info_is_live (info))
201                 return NULL;
202
203         copy_stack_data (info, stackdata);
204
205         switch (mono_threads_transition_abort_blocking (info)) {
206         case AbortBlockingIgnore:
207                 info->thread_saved_state [SELF_SUSPEND_STATE_INDEX].valid = FALSE;
208                 return NULL;
209         case AbortBlockingIgnoreAndPoll:
210                 mono_threads_state_poll ();
211                 return NULL;
212         case AbortBlockingOk:
213                 info->thread_saved_state [SELF_SUSPEND_STATE_INDEX].valid = FALSE;
214                 return info;
215         case AbortBlockingOkAndPool:
216                 mono_threads_state_poll ();
217                 return info;
218         default:
219                 g_error ("Unknown thread state");
220         }
221 }
222
223 void
224 mono_threads_reset_blocking_end (void *cookie, void* stackdata)
225 {
226         MonoThreadInfo *info;
227
228         if (!mono_threads_is_coop_enabled ())
229                 return;
230
231         info = (MonoThreadInfo *)cookie;
232         if (!info)
233                 return;
234
235         g_assert (info == mono_thread_info_current_unchecked ());
236         mono_threads_prepare_blocking (stackdata);
237 }
238
239 void
240 mono_threads_init_coop (void)
241 {
242         if (!mono_threads_is_coop_enabled ())
243                 return;
244
245         mono_counters_register ("Coop Reset Blocking", MONO_COUNTER_GC | MONO_COUNTER_INT, &coop_reset_blocking_count);
246         mono_counters_register ("Coop Try Blocking", MONO_COUNTER_GC | MONO_COUNTER_INT, &coop_try_blocking_count);
247         mono_counters_register ("Coop Do Blocking", MONO_COUNTER_GC | MONO_COUNTER_INT, &coop_do_blocking_count);
248         mono_counters_register ("Coop Do Polling", MONO_COUNTER_GC | MONO_COUNTER_INT, &coop_do_polling_count);
249         mono_counters_register ("Coop Save Count", MONO_COUNTER_GC | MONO_COUNTER_INT, &coop_save_count);
250         //See the above for what's wrong here.
251 }
252
253 void
254 mono_threads_coop_begin_global_suspend (void)
255 {
256         if (mono_threads_is_coop_enabled ())
257                 mono_polling_required = 1;
258 }
259
260 void
261 mono_threads_coop_end_global_suspend (void)
262 {
263         if (mono_threads_is_coop_enabled ())
264                 mono_polling_required = 0;
265 }
266
267 gpointer
268 mono_threads_enter_gc_unsafe_region (gpointer* stackdata)
269 {
270         if (!mono_threads_is_coop_enabled ())
271                 return NULL;
272
273         return mono_threads_reset_blocking_start (stackdata);
274 }
275
276 void
277 mono_threads_exit_gc_unsafe_region (gpointer cookie, gpointer* stackdata)
278 {
279         if (!mono_threads_is_coop_enabled ())
280                 return;
281
282         mono_threads_reset_blocking_end (cookie, stackdata);
283 }
284
285 void
286 mono_threads_assert_gc_unsafe_region (void)
287 {
288         MONO_REQ_GC_UNSAFE_MODE;
289 }
290
291 gpointer
292 mono_threads_enter_gc_safe_region (gpointer *stackdata)
293 {
294         if (!mono_threads_is_coop_enabled ())
295                 return NULL;
296
297         return mono_threads_prepare_blocking (stackdata);
298 }
299
300 void
301 mono_threads_exit_gc_safe_region (gpointer cookie, gpointer *stackdata)
302 {
303         if (!mono_threads_is_coop_enabled ())
304                 return;
305
306         mono_threads_finish_blocking (cookie, stackdata);
307 }
308
309 void
310 mono_threads_assert_gc_safe_region (void)
311 {
312         MONO_REQ_GC_SAFE_MODE;
313 }