[sgen] Add missing memory barrier
[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.signal = 0;
50         stop_count = sgen_global_stop_count;
51         /* duplicate signal */
52         if (0 && info->client_info.stop_count == stop_count)
53                 return;
54
55         if (context) {
56                 mono_sigctx_to_monoctx (context, &ctx);
57                 info->client_info.stopped_ip = MONO_CONTEXT_GET_IP (&ctx);
58                 stack_start = (((guint8 *) MONO_CONTEXT_GET_SP (&ctx)) - REDZONE_SIZE);
59         } else {
60                 info->client_info.stopped_ip = NULL;
61                 stack_start = NULL;
62         }
63
64         /* If stack_start is not within the limits, then don't set it
65            in info and we will be restarted. */
66         if (stack_start >= info->client_info.stack_start_limit && stack_start <= info->client_info.stack_end) {
67                 info->client_info.stack_start = stack_start;
68
69                 if (context) {
70                         memcpy (&info->client_info.ctx, &ctx, sizeof (MonoContext));
71                 } else {
72                         memset (&info->client_info.ctx, 0, sizeof (MonoContext));
73                 }
74         } else {
75                 g_assert (!info->client_info.stack_start);
76         }
77
78         /* Notify the JIT */
79         if (mono_gc_get_gc_callbacks ()->thread_suspend_func)
80                 mono_gc_get_gc_callbacks ()->thread_suspend_func (info->client_info.runtime_data, context, NULL);
81
82         SGEN_LOG (4, "Posting suspend_ack_semaphore for suspend from %p %p", info, (gpointer) (gsize) mono_native_thread_id_get ());
83
84         /*
85         Block the restart signal. 
86         We need to block the restart signal while posting to the suspend_ack semaphore or we race to sigsuspend,
87         which might miss the signal and get stuck.
88         */
89         pthread_sigmask (SIG_BLOCK, &suspend_ack_signal_mask, NULL);
90
91         /* notify the waiting thread */
92         SGEN_SEMAPHORE_POST (suspend_ack_semaphore_ptr);
93         info->client_info.stop_count = stop_count;
94
95         /* wait until we receive the restart signal */
96         do {
97                 info->client_info.signal = 0;
98                 sigsuspend (&suspend_signal_mask);
99         } while (info->client_info.signal != restart_signal_num);
100
101         /* Unblock the restart signal. */
102         pthread_sigmask (SIG_UNBLOCK, &suspend_ack_signal_mask, NULL);
103
104         SGEN_LOG (4, "Posting suspend_ack_semaphore for resume from %p %p\n", info, (gpointer) (gsize) mono_native_thread_id_get ());
105         /* notify the waiting thread */
106         SGEN_SEMAPHORE_POST (suspend_ack_semaphore_ptr);
107 }
108
109 /* LOCKING: assumes the GC lock is held (by the stopping thread) */
110 MONO_SIG_HANDLER_FUNC (static, suspend_handler)
111 {
112         /*
113          * The suspend signal handler potentially uses syscalls that
114          * can set errno, and it calls functions that use the hazard
115          * pointer machinery.  Since we're interrupting other code we
116          * must restore those to the values they had when we
117          * interrupted.
118          */
119         SgenThreadInfo *info;
120         int old_errno = errno;
121         int hp_save_index = mono_hazard_pointer_save_for_signal_handler ();
122         MONO_SIG_HANDLER_GET_CONTEXT;
123
124         info = mono_thread_info_current ();
125         suspend_thread (info, ctx);
126
127         mono_hazard_pointer_restore_for_signal_handler (hp_save_index);
128         errno = old_errno;
129 }
130
131 MONO_SIG_HANDLER_FUNC (static, restart_handler)
132 {
133         SgenThreadInfo *info;
134         int old_errno = errno;
135
136         info = mono_thread_info_current ();
137         info->client_info.signal = restart_signal_num;
138         SGEN_LOG (4, "Restart handler in %p %p", info, (gpointer) (gsize) mono_native_thread_id_get ());
139         errno = old_errno;
140 }
141
142 gboolean
143 sgen_resume_thread (SgenThreadInfo *info)
144 {
145         return mono_threads_pthread_kill (info, restart_signal_num) == 0;
146 }
147
148 gboolean
149 sgen_suspend_thread (SgenThreadInfo *info)
150 {
151         return mono_threads_pthread_kill (info, suspend_signal_num) == 0;
152 }
153
154 void
155 sgen_wait_for_suspend_ack (int count)
156 {
157         int i, result;
158
159         for (i = 0; i < count; ++i) {
160                 while ((result = SGEN_SEMAPHORE_WAIT (suspend_ack_semaphore_ptr)) != 0) {
161                         if (errno != EINTR) {
162                                 g_error ("SGEN_SEMAPHORE_WAIT FAILED with %d errno %d (%s)", result, errno, strerror (errno));
163                         }
164                 }
165         }
166 }
167
168 int
169 sgen_thread_handshake (BOOL suspend)
170 {
171         int count, result;
172         int signum = suspend ? suspend_signal_num : restart_signal_num;
173
174         MonoNativeThreadId me = mono_native_thread_id_get ();
175
176         count = 0;
177         mono_thread_info_current ()->client_info.suspend_done = TRUE;
178         FOREACH_THREAD (info) {
179                 if (mono_native_thread_id_equals (mono_thread_info_get_tid (info), me)) {
180                         continue;
181                 }
182                 info->client_info.suspend_done = FALSE;
183                 if (info->client_info.gc_disabled)
184                         continue;
185                 /*if (signum == suspend_signal_num && info->stop_count == global_stop_count)
186                         continue;*/
187                 result = mono_threads_pthread_kill (info, signum);
188                 if (result == 0) {
189                         count++;
190                 } else {
191                         info->client_info.skip = 1;
192                 }
193         } FOREACH_THREAD_END
194
195         sgen_wait_for_suspend_ack (count);
196
197         SGEN_LOG (4, "%s handshake for %d threads\n", suspend ? "suspend" : "resume", count);
198
199         return count;
200 }
201
202 void
203 sgen_os_init (void)
204 {
205         struct sigaction sinfo;
206
207         if (mono_thread_info_unified_management_enabled ())
208                 return;
209
210         suspend_ack_semaphore_ptr = &suspend_ack_semaphore;
211         SGEN_SEMAPHORE_INIT (&suspend_ack_semaphore, 0);
212
213         sigfillset (&sinfo.sa_mask);
214         sinfo.sa_flags = SA_RESTART | SA_SIGINFO;
215         sinfo.sa_sigaction = suspend_handler;
216         if (sigaction (suspend_signal_num, &sinfo, NULL) != 0) {
217                 g_error ("failed sigaction");
218         }
219
220         sinfo.sa_handler = (void (*)(int))restart_handler;
221         if (sigaction (restart_signal_num, &sinfo, NULL) != 0) {
222                 g_error ("failed sigaction");
223         }
224
225         sigfillset (&suspend_signal_mask);
226         sigdelset (&suspend_signal_mask, restart_signal_num);
227
228         sigemptyset (&suspend_ack_signal_mask);
229         sigaddset (&suspend_ack_signal_mask, restart_signal_num);
230         
231 }
232
233 int
234 mono_gc_get_suspend_signal (void)
235 {
236         return suspend_signal_num;
237 }
238
239 int
240 mono_gc_get_restart_signal (void)
241 {
242         return restart_signal_num;
243 }
244 #endif
245 #endif