[sgen] Fix a thread startup deadlock on POSIX.
[mono.git] / mono / metadata / sgen-stw.c
1 /*
2  * sgen-stw.c: Stop the world functionality
3  *
4  * Author:
5  *      Paolo Molaro (lupus@ximian.com)
6  *  Rodrigo Kumpera (kumpera@gmail.com)
7  *
8  * Copyright 2005-2011 Novell, Inc (http://www.novell.com)
9  * Copyright 2011 Xamarin Inc (http://www.xamarin.com)
10  * Copyright 2011 Xamarin, Inc.
11  * Copyright (C) 2012 Xamarin Inc
12  *
13  * This library is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU Library General Public
15  * License 2.0 as published by the Free Software Foundation;
16  *
17  * This library is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Library General Public License for more details.
21  *
22  * You should have received a copy of the GNU Library General Public
23  * License 2.0 along with this library; if not, write to the Free
24  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25  */
26
27 #include "config.h"
28 #ifdef HAVE_SGEN_GC
29
30 #include "metadata/sgen-gc.h"
31 #include "metadata/sgen-protocol.h"
32 #include "metadata/sgen-memory-governor.h"
33 #include "metadata/profiler-private.h"
34 #include "utils/mono-time.h"
35 #include "utils/dtrace.h"
36
37 #define TV_DECLARE SGEN_TV_DECLARE
38 #define TV_GETTIME SGEN_TV_GETTIME
39 #define TV_ELAPSED SGEN_TV_ELAPSED
40 #define TV_ELAPSED_MS SGEN_TV_ELAPSED_MS
41
42 inline static void*
43 align_pointer (void *ptr)
44 {
45         mword p = (mword)ptr;
46         p += sizeof (gpointer) - 1;
47         p &= ~ (sizeof (gpointer) - 1);
48         return (void*)p;
49 }
50
51 #ifdef USE_MONO_CTX
52 static MonoContext cur_thread_ctx = {0};
53 #else
54 static mword cur_thread_regs [ARCH_NUM_REGS] = {0};
55 #endif
56
57 static void
58 update_current_thread_stack (void *start)
59 {
60         int stack_guard = 0;
61 #if !defined(USE_MONO_CTX)
62         void *reg_ptr = cur_thread_regs;
63 #endif
64         SgenThreadInfo *info = mono_thread_info_current ();
65         
66         info->stack_start = align_pointer (&stack_guard);
67         g_assert (info->stack_start >= info->stack_start_limit && info->stack_start < info->stack_end);
68 #ifdef USE_MONO_CTX
69         MONO_CONTEXT_GET_CURRENT (cur_thread_ctx);
70         memcpy (&info->ctx, &cur_thread_ctx, sizeof (MonoContext));
71         if (mono_gc_get_gc_callbacks ()->thread_suspend_func)
72                 mono_gc_get_gc_callbacks ()->thread_suspend_func (info->runtime_data, NULL, &info->ctx);
73 #else
74         ARCH_STORE_REGS (reg_ptr);
75         memcpy (&info->regs, reg_ptr, sizeof (info->regs));
76         if (mono_gc_get_gc_callbacks ()->thread_suspend_func)
77                 mono_gc_get_gc_callbacks ()->thread_suspend_func (info->runtime_data, NULL, NULL);
78 #endif
79 }
80
81 static gboolean
82 is_ip_in_managed_allocator (MonoDomain *domain, gpointer ip)
83 {
84         MonoJitInfo *ji;
85
86         if (!mono_thread_internal_current ())
87                 /* Happens during thread attach */
88                 return FALSE;
89
90         if (!ip || !domain)
91                 return FALSE;
92         if (!sgen_has_critical_method ())
93                 return FALSE;
94
95         /*
96          * mono_jit_info_table_find is not async safe since it calls into the AOT runtime to load information for
97          * missing methods (#13951). To work around this, we disable the AOT fallback. For this to work, the JIT needs
98          * to register the jit info for all GC critical methods after they are JITted/loaded.
99          */
100         ji = mono_jit_info_table_find_internal (domain, ip, FALSE);
101         if (!ji)
102                 return FALSE;
103
104         return sgen_is_critical_method (mono_jit_info_get_method (ji));
105 }
106
107 static int
108 restart_threads_until_none_in_managed_allocator (void)
109 {
110         SgenThreadInfo *info;
111         int num_threads_died = 0;
112         int sleep_duration = -1;
113
114         for (;;) {
115                 int restart_count = 0, restarted_count = 0;
116                 /* restart all threads that stopped in the
117                    allocator */
118                 FOREACH_THREAD_SAFE (info) {
119                         gboolean result;
120                         if (info->skip || info->gc_disabled)
121                                 continue;
122 #ifdef SGEN_POSIX_STW
123                         if (info->signal < 0)
124                                 continue;
125 #endif
126                         if (mono_thread_info_run_state (info) == STATE_RUNNING && (!info->stack_start || info->in_critical_region || info->info.inside_critical_region ||
127                                         is_ip_in_managed_allocator (info->stopped_domain, info->stopped_ip))) {
128                                 binary_protocol_thread_restart ((gpointer)mono_thread_info_get_tid (info));
129                                 SGEN_LOG (3, "thread %p resumed.", (void*) (size_t) info->info.native_handle);
130                                 result = sgen_resume_thread (info);
131                                 if (result) {
132                                         ++restart_count;
133                                 } else {
134                                         info->skip = 1;
135                                 }
136                         } else {
137                                 /* we set the stopped_ip to
138                                    NULL for threads which
139                                    we're not restarting so
140                                    that we can easily identify
141                                    the others */
142                                 info->stopped_ip = NULL;
143                                 info->stopped_domain = NULL;
144                         }
145                 } END_FOREACH_THREAD_SAFE
146                 /* if no threads were restarted, we're done */
147                 if (restart_count == 0)
148                         break;
149
150                 /* wait for the threads to signal their restart */
151                 sgen_wait_for_suspend_ack (restart_count);
152
153                 if (sleep_duration < 0) {
154 #ifdef HOST_WIN32
155                         SwitchToThread ();
156 #else
157                         sched_yield ();
158 #endif
159                         sleep_duration = 0;
160                 } else {
161                         g_usleep (sleep_duration);
162                         sleep_duration += 10;
163                 }
164
165                 /* stop them again */
166                 FOREACH_THREAD (info) {
167                         gboolean result;
168                         if (info->skip || info->stopped_ip == NULL)
169                                 continue;
170                         result = sgen_suspend_thread (info);
171
172                         if (result) {
173                                 ++restarted_count;
174                         } else {
175                                 info->skip = 1;
176                         }
177                 } END_FOREACH_THREAD
178                 /* some threads might have died */
179                 num_threads_died += restart_count - restarted_count;
180                 /* wait for the threads to signal their suspension
181                    again */
182                 sgen_wait_for_suspend_ack (restarted_count);
183         }
184
185         return num_threads_died;
186 }
187
188 static void
189 acquire_gc_locks (void)
190 {
191         LOCK_INTERRUPTION;
192         mono_thread_info_suspend_lock ();
193 }
194
195 static void
196 release_gc_locks (void)
197 {
198         mono_thread_info_suspend_unlock ();
199         UNLOCK_INTERRUPTION;
200 }
201
202 static TV_DECLARE (stop_world_time);
203 static unsigned long max_pause_usec = 0;
204
205 /* LOCKING: assumes the GC lock is held */
206 int
207 sgen_stop_world (int generation)
208 {
209         int count, dead;
210
211         /*XXX this is the right stop, thought might not be the nicest place to put it*/
212         sgen_process_togglerefs ();
213
214         mono_profiler_gc_event (MONO_GC_EVENT_PRE_STOP_WORLD, generation);
215         MONO_GC_WORLD_STOP_BEGIN ();
216         acquire_gc_locks ();
217
218         update_current_thread_stack (&count);
219
220         sgen_global_stop_count++;
221         SGEN_LOG (3, "stopping world n %d from %p %p", sgen_global_stop_count, mono_thread_info_current (), (gpointer)mono_native_thread_id_get ());
222         TV_GETTIME (stop_world_time);
223         count = sgen_thread_handshake (TRUE);
224         dead = restart_threads_until_none_in_managed_allocator ();
225         if (count < dead)
226                 g_error ("More threads have died (%d) that been initialy suspended %d", dead, count);
227         count -= dead;
228
229         SGEN_LOG (3, "world stopped %d thread(s)", count);
230         mono_profiler_gc_event (MONO_GC_EVENT_POST_STOP_WORLD, generation);
231         MONO_GC_WORLD_STOP_END ();
232
233         sgen_memgov_collection_start (generation);
234         sgen_bridge_reset_data ();
235
236         return count;
237 }
238
239 /* LOCKING: assumes the GC lock is held */
240 int
241 sgen_restart_world (int generation, GGTimingInfo *timing)
242 {
243         int count;
244         SgenThreadInfo *info;
245         TV_DECLARE (end_sw);
246         TV_DECLARE (end_bridge);
247         unsigned long usec, bridge_usec;
248
249         /* notify the profiler of the leftovers */
250         /* FIXME this is the wrong spot at we can STW for non collection reasons. */
251         if (G_UNLIKELY (mono_profiler_events & MONO_PROFILE_GC_MOVES))
252                 sgen_gc_event_moves ();
253         mono_profiler_gc_event (MONO_GC_EVENT_PRE_START_WORLD, generation);
254         MONO_GC_WORLD_RESTART_BEGIN (generation);
255         FOREACH_THREAD (info) {
256                 info->stack_start = NULL;
257 #ifdef USE_MONO_CTX
258                 memset (&info->ctx, 0, sizeof (MonoContext));
259 #else
260                 memset (&info->regs, 0, sizeof (info->regs));
261 #endif
262         } END_FOREACH_THREAD
263
264         release_gc_locks ();
265
266         count = sgen_thread_handshake (FALSE);
267         TV_GETTIME (end_sw);
268         usec = TV_ELAPSED (stop_world_time, end_sw);
269         max_pause_usec = MAX (usec, max_pause_usec);
270         SGEN_LOG (2, "restarted %d thread(s) (pause time: %d usec, max: %d)", count, (int)usec, (int)max_pause_usec);
271         mono_profiler_gc_event (MONO_GC_EVENT_POST_START_WORLD, generation);
272         MONO_GC_WORLD_RESTART_END (generation);
273
274         mono_thread_hazardous_try_free_some ();
275
276         sgen_bridge_processing_finish (generation);
277
278         TV_GETTIME (end_bridge);
279         bridge_usec = TV_ELAPSED (end_sw, end_bridge);
280
281         if (timing) {
282                 timing [0].stw_time = usec;
283                 timing [0].bridge_time = bridge_usec;
284         }
285         
286         sgen_memgov_collection_end (generation, timing, timing ? 2 : 0);
287
288         return count;
289 }
290
291 #endif