Merge pull request #3262 from lindenlab/add_continuations_test
[mono.git] / mono / metadata / sgen-os-posix.c
1 /*
2  * sgen-os-posix.c: Posix support.
3  *
4  * Author:
5  *      Paolo Molaro (lupus@ximian.com)
6  *      Mark Probst (mprobst@novell.com)
7  *      Geoff Norton (gnorton@novell.com)
8  *
9  * Copyright 2010 Novell, Inc (http://www.novell.com)
10  * Copyright (C) 2012 Xamarin Inc
11  *
12  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
13  */
14
15 #include "config.h"
16
17 #if defined(HAVE_SGEN_GC) && !defined(USE_COOP_GC)
18 #if !defined(__MACH__) && !MONO_MACH_ARCH_SUPPORTED && defined(HAVE_PTHREAD_KILL)
19
20 #include <errno.h>
21 #include <glib.h>
22 #include "sgen/sgen-gc.h"
23 #include "metadata/gc-internals.h"
24 #include "sgen/sgen-archdep.h"
25 #include "metadata/object-internals.h"
26 #include "utils/mono-signal-handler.h"
27
28 #if defined(__APPLE__) || defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
29 const static int suspend_signal_num = SIGXFSZ;
30 #else
31 const static int suspend_signal_num = SIGPWR;
32 #endif
33 const static int restart_signal_num = SIGXCPU;
34
35 static SgenSemaphore suspend_ack_semaphore;
36 static SgenSemaphore *suspend_ack_semaphore_ptr;
37
38 static sigset_t suspend_signal_mask;
39 static sigset_t suspend_ack_signal_mask;
40
41 static void
42 suspend_thread (SgenThreadInfo *info, void *context)
43 {
44         int stop_count;
45         MonoContext ctx;
46         gpointer stack_start;
47
48         info->client_info.stopped_domain = mono_domain_get ();
49         info->client_info.stack_start = NULL;
50         info->client_info.signal = 0;
51         stop_count = sgen_global_stop_count;
52         /* duplicate signal */
53         if (0 && info->client_info.stop_count == stop_count)
54                 return;
55
56         if (context) {
57                 mono_sigctx_to_monoctx (context, &ctx);
58                 info->client_info.stopped_ip = MONO_CONTEXT_GET_IP (&ctx);
59                 stack_start = (((guint8 *) MONO_CONTEXT_GET_SP (&ctx)) - REDZONE_SIZE);
60         } else {
61                 info->client_info.stopped_ip = NULL;
62                 stack_start = NULL;
63         }
64
65         /* If stack_start is not within the limits, then don't set it
66            in info and we will be restarted. */
67         if (stack_start >= info->client_info.stack_start_limit && stack_start <= info->client_info.stack_end) {
68                 info->client_info.stack_start = stack_start;
69
70                 if (context) {
71                         memcpy (&info->client_info.ctx, &ctx, sizeof (MonoContext));
72                 } else {
73                         memset (&info->client_info.ctx, 0, sizeof (MonoContext));
74                 }
75         } else {
76                 g_assert (!info->client_info.stack_start);
77         }
78
79         /* Notify the JIT */
80         if (mono_gc_get_gc_callbacks ()->thread_suspend_func)
81                 mono_gc_get_gc_callbacks ()->thread_suspend_func (info->client_info.runtime_data, context, NULL);
82
83         SGEN_LOG (4, "Posting suspend_ack_semaphore for suspend from %p %p", info, (gpointer) (gsize) mono_native_thread_id_get ());
84
85         /*
86         Block the restart signal. 
87         We need to block the restart signal while posting to the suspend_ack semaphore or we race to sigsuspend,
88         which might miss the signal and get stuck.
89         */
90         pthread_sigmask (SIG_BLOCK, &suspend_ack_signal_mask, NULL);
91
92         /* notify the waiting thread */
93         SGEN_SEMAPHORE_POST (suspend_ack_semaphore_ptr);
94         info->client_info.stop_count = stop_count;
95
96         /* wait until we receive the restart signal */
97         do {
98                 info->client_info.signal = 0;
99                 sigsuspend (&suspend_signal_mask);
100         } while (info->client_info.signal != restart_signal_num);
101
102         /* Unblock the restart signal. */
103         pthread_sigmask (SIG_UNBLOCK, &suspend_ack_signal_mask, NULL);
104
105         SGEN_LOG (4, "Posting suspend_ack_semaphore for resume from %p %p\n", info, (gpointer) (gsize) mono_native_thread_id_get ());
106         /* notify the waiting thread */
107         SGEN_SEMAPHORE_POST (suspend_ack_semaphore_ptr);
108 }
109
110 /* LOCKING: assumes the GC lock is held (by the stopping thread) */
111 MONO_SIG_HANDLER_FUNC (static, suspend_handler)
112 {
113         /*
114          * The suspend signal handler potentially uses syscalls that
115          * can set errno, and it calls functions that use the hazard
116          * pointer machinery.  Since we're interrupting other code we
117          * must restore those to the values they had when we
118          * interrupted.
119          */
120         SgenThreadInfo *info;
121         int old_errno = errno;
122         int hp_save_index = mono_hazard_pointer_save_for_signal_handler ();
123         MONO_SIG_HANDLER_GET_CONTEXT;
124
125         info = mono_thread_info_current ();
126         suspend_thread (info, ctx);
127
128         mono_hazard_pointer_restore_for_signal_handler (hp_save_index);
129         errno = old_errno;
130 }
131
132 MONO_SIG_HANDLER_FUNC (static, restart_handler)
133 {
134         SgenThreadInfo *info;
135         int old_errno = errno;
136
137         info = mono_thread_info_current ();
138         info->client_info.signal = restart_signal_num;
139         SGEN_LOG (4, "Restart handler in %p %p", info, (gpointer) (gsize) mono_native_thread_id_get ());
140         errno = old_errno;
141 }
142
143 gboolean
144 sgen_resume_thread (SgenThreadInfo *info)
145 {
146         return mono_threads_pthread_kill (info, restart_signal_num) == 0;
147 }
148
149 gboolean
150 sgen_suspend_thread (SgenThreadInfo *info)
151 {
152         return mono_threads_pthread_kill (info, suspend_signal_num) == 0;
153 }
154
155 void
156 sgen_wait_for_suspend_ack (int count)
157 {
158         int i, result;
159
160         for (i = 0; i < count; ++i) {
161                 while ((result = SGEN_SEMAPHORE_WAIT (suspend_ack_semaphore_ptr)) != 0) {
162                         if (errno != EINTR) {
163                                 g_error ("SGEN_SEMAPHORE_WAIT FAILED with %d errno %d (%s)", result, errno, strerror (errno));
164                         }
165                 }
166         }
167 }
168
169 int
170 sgen_thread_handshake (BOOL suspend)
171 {
172         int count, result;
173         int signum = suspend ? suspend_signal_num : restart_signal_num;
174
175         MonoNativeThreadId me = mono_native_thread_id_get ();
176
177         count = 0;
178         mono_thread_info_current ()->client_info.suspend_done = TRUE;
179         FOREACH_THREAD (info) {
180                 if (mono_native_thread_id_equals (mono_thread_info_get_tid (info), me)) {
181                         continue;
182                 }
183                 info->client_info.suspend_done = FALSE;
184                 if (info->client_info.gc_disabled)
185                         continue;
186                 /*if (signum == suspend_signal_num && info->stop_count == global_stop_count)
187                         continue;*/
188                 result = mono_threads_pthread_kill (info, signum);
189                 if (result == 0) {
190                         count++;
191                 } else {
192                         info->client_info.skip = 1;
193                 }
194         } FOREACH_THREAD_END
195
196         sgen_wait_for_suspend_ack (count);
197
198         SGEN_LOG (4, "%s handshake for %d threads\n", suspend ? "suspend" : "resume", count);
199
200         return count;
201 }
202
203 void
204 sgen_os_init (void)
205 {
206         struct sigaction sinfo;
207
208         if (mono_thread_info_unified_management_enabled ())
209                 return;
210
211         suspend_ack_semaphore_ptr = &suspend_ack_semaphore;
212         SGEN_SEMAPHORE_INIT (&suspend_ack_semaphore, 0);
213
214         sigfillset (&sinfo.sa_mask);
215         sinfo.sa_flags = SA_RESTART | SA_SIGINFO;
216         sinfo.sa_sigaction = suspend_handler;
217         if (sigaction (suspend_signal_num, &sinfo, NULL) != 0) {
218                 g_error ("failed sigaction");
219         }
220
221         sinfo.sa_handler = (void (*)(int))restart_handler;
222         if (sigaction (restart_signal_num, &sinfo, NULL) != 0) {
223                 g_error ("failed sigaction");
224         }
225
226         sigfillset (&suspend_signal_mask);
227         sigdelset (&suspend_signal_mask, restart_signal_num);
228
229         sigemptyset (&suspend_ack_signal_mask);
230         sigaddset (&suspend_ack_signal_mask, restart_signal_num);
231         
232 }
233
234 int
235 mono_gc_get_suspend_signal (void)
236 {
237         return suspend_signal_num;
238 }
239
240 int
241 mono_gc_get_restart_signal (void)
242 {
243         return restart_signal_num;
244 }
245 #endif
246 #endif