3 * Shared facility for Posix signals support
6 * Ludovic Henry (ludovic@gmail.com)
8 * (C) 2015 Xamarin, Inc
14 #include "mono-threads.h"
16 #if defined(USE_POSIX_BACKEND)
21 #include "mono-threads-debug.h"
24 mono_threads_suspend_search_alternative_signal (void)
26 #if !defined (SIGRTMIN)
27 g_error ("signal search only works with RTMIN");
30 /* we try to avoid SIGRTMIN and any one that might have been set already, see bug #75387 */
31 for (i
= SIGRTMIN
+ 1; i
< SIGRTMAX
; ++i
) {
32 struct sigaction sinfo
;
33 sigaction (i
, NULL
, &sinfo
);
34 if (sinfo
.sa_handler
== SIG_DFL
&& (void*)sinfo
.sa_sigaction
== (void*)SIG_DFL
) {
38 g_error ("Could not find an available signal");
42 #ifndef __native_client__
44 static int suspend_signal_num
= -1;
45 static int restart_signal_num
= -1;
46 static int abort_signal_num
= -1;
48 static sigset_t suspend_signal_mask
;
49 static sigset_t suspend_ack_signal_mask
;
52 signal_add_handler (int signo
, void (*handler
)(int, siginfo_t
*, void *), int flags
)
57 sa
.sa_sigaction
= handler
;
58 sigfillset (&sa
.sa_mask
);
59 sa
.sa_flags
= SA_SIGINFO
| flags
;
60 ret
= sigaction (signo
, &sa
, NULL
);
65 abort_signal_get (void)
67 #if defined(PLATFORM_ANDROID)
69 #elif defined (SIGRTMIN)
70 static int abort_signum
= -1;
71 if (abort_signum
== -1)
72 abort_signum
= mono_threads_suspend_search_alternative_signal ();
74 #elif defined (SIGTTIN)
77 g_error ("unable to get abort signal");
82 suspend_signal_get (void)
84 #if defined(PLATFORM_ANDROID)
86 #elif defined (SIGRTMIN)
87 static int suspend_signum
= -1;
88 if (suspend_signum
== -1)
89 suspend_signum
= mono_threads_suspend_search_alternative_signal ();
90 return suspend_signum
;
92 #if defined(__APPLE__) || defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
101 restart_signal_get (void)
103 #if defined(PLATFORM_ANDROID)
105 #elif defined (SIGRTMIN)
106 static int restart_signum
= -1;
107 if (restart_signum
== -1)
108 restart_signum
= mono_threads_suspend_search_alternative_signal ();
109 return restart_signum
;
116 restart_signal_handler (int _dummy
, siginfo_t
*_info
, void *context
)
118 MonoThreadInfo
*info
;
119 int old_errno
= errno
;
121 info
= mono_thread_info_current ();
122 info
->signal
= restart_signal_num
;
127 suspend_signal_handler (int _dummy
, siginfo_t
*info
, void *context
)
129 int old_errno
= errno
;
130 int hp_save_index
= mono_hazard_pointer_save_for_signal_handler ();
132 MonoThreadInfo
*current
= mono_thread_info_current ();
134 THREADS_SUSPEND_DEBUG ("SIGNAL HANDLER FOR %p [%p]\n", mono_thread_info_get_tid (current
), (void*)current
->native_handle
);
135 if (current
->syscall_break_signal
) {
136 current
->syscall_break_signal
= FALSE
;
137 THREADS_SUSPEND_DEBUG ("\tsyscall break for %p\n", mono_thread_info_get_tid (current
));
138 mono_threads_notify_initiator_of_abort (current
);
142 /* Have we raced with self suspend? */
143 if (!mono_threads_transition_finish_async_suspend (current
)) {
144 current
->suspend_can_continue
= TRUE
;
145 THREADS_SUSPEND_DEBUG ("\tlost race with self suspend %p\n", mono_thread_info_get_tid (current
));
150 * If the thread is starting, then thread_state_init_from_sigctx returns FALSE,
151 * as the thread might have been attached without the domain or lmf having been
154 * One way to fix that is to keep the thread suspended (wait for the restart
155 * signal), and make sgen aware that even if a thread might be suspended, there
156 * would be cases where you cannot scan its stack/registers. That would in fact
157 * consist in removing the async suspend compensation, and treat the case directly
158 * in sgen. That's also how it was done in the sgen specific suspend code.
161 /* thread_state_init_from_sigctx return FALSE if the current thread is starting or detaching and suspend can't continue. */
162 current
->suspend_can_continue
= mono_threads_get_runtime_callbacks ()->thread_state_init_from_sigctx (¤t
->thread_saved_state
[ASYNC_SUSPEND_STATE_INDEX
], context
);
164 if (!current
->suspend_can_continue
)
165 THREADS_SUSPEND_DEBUG ("\tThread is starting or detaching, failed to capture state %p\n", mono_thread_info_get_tid (current
));
168 Block the restart signal.
169 We need to block the restart signal while posting to the suspend_ack semaphore or we race to sigsuspend,
170 which might miss the signal and get stuck.
172 pthread_sigmask (SIG_BLOCK
, &suspend_ack_signal_mask
, NULL
);
174 /* We're done suspending */
175 mono_threads_notify_initiator_of_suspend (current
);
179 sigsuspend (&suspend_signal_mask
);
180 } while (current
->signal
!= restart_signal_num
);
182 /* Unblock the restart signal. */
183 pthread_sigmask (SIG_UNBLOCK
, &suspend_ack_signal_mask
, NULL
);
185 if (current
->async_target
) {
186 #if MONO_ARCH_HAS_MONO_CONTEXT
187 MonoContext tmp
= current
->thread_saved_state
[ASYNC_SUSPEND_STATE_INDEX
].ctx
;
188 mono_threads_get_runtime_callbacks ()->setup_async_callback (&tmp
, current
->async_target
, current
->user_data
);
189 current
->user_data
= NULL
;
190 current
->async_target
= NULL
;
191 mono_monoctx_to_sigctx (&tmp
, context
);
193 g_error ("The new interruption machinery requires a working mono-context");
197 /* We're done resuming */
198 mono_threads_notify_initiator_of_resume (current
);
201 mono_hazard_pointer_restore_for_signal_handler (hp_save_index
);
206 mono_threads_suspend_init_signals (void)
210 sigemptyset (&signal_set
);
212 /* add suspend signal */
213 suspend_signal_num
= suspend_signal_get ();
215 signal_add_handler (suspend_signal_num
, suspend_signal_handler
, SA_RESTART
);
217 sigaddset (&signal_set
, suspend_signal_num
);
219 /* add restart signal */
220 restart_signal_num
= restart_signal_get ();
222 sigfillset (&suspend_signal_mask
);
223 sigdelset (&suspend_signal_mask
, restart_signal_num
);
225 sigemptyset (&suspend_ack_signal_mask
);
226 sigaddset (&suspend_ack_signal_mask
, restart_signal_num
);
228 signal_add_handler (restart_signal_num
, restart_signal_handler
, SA_RESTART
);
230 sigaddset (&signal_set
, restart_signal_num
);
232 /* add abort signal */
233 abort_signal_num
= abort_signal_get ();
235 /* the difference between abort and suspend here is made by not
236 * passing SA_RESTART, meaning we won't restart the syscall when
237 * receiving a signal */
238 signal_add_handler (abort_signal_num
, suspend_signal_handler
, 0);
240 sigaddset (&signal_set
, abort_signal_num
);
242 /* ensure all the new signals are unblocked */
243 sigprocmask (SIG_UNBLOCK
, &signal_set
, NULL
);
246 On 32bits arm Android, signals with values >=32 are not usable as their headers ship a broken sigset_t.
247 See 5005c6f3fbc1da584c6a550281689cc23f59fe6d for more details.
249 #ifdef PLATFORM_ANDROID
250 g_assert (suspend_signal_num
< 32);
251 g_assert (restart_signal_num
< 32);
252 g_assert (abort_signal_num
< 32);
257 mono_threads_suspend_get_suspend_signal (void)
259 g_assert (suspend_signal_num
!= -1);
260 return suspend_signal_num
;
264 mono_threads_suspend_get_restart_signal (void)
266 g_assert (restart_signal_num
!= -1);
267 return restart_signal_num
;
271 mono_threads_suspend_get_abort_signal (void)
273 g_assert (abort_signal_num
!= -1);
274 return abort_signal_num
;
280 mono_threads_suspend_init_signals (void)
282 g_assert_not_reached ();
286 mono_threads_suspend_get_suspend_signal (void)
292 mono_threads_suspend_get_restart_signal (void)
298 mono_threads_suspend_get_abort_signal (void)
303 #endif /* __native_client__ */
306 #endif /* defined(USE_POSIX_BACKEND) */