Merge pull request #1691 from esdrubal/exitevent
[mono.git] / mono / utils / mono-threads-posix.c
1 /*
2  * mono-threads-posix.c: Low-level threading, posix version
3  *
4  * Author:
5  *      Rodrigo Kumpera (kumpera@gmail.com)
6  *
7  * (C) 2011 Novell, Inc
8  */
9
10 #include <config.h>
11
12 /* For pthread_main_np, pthread_get_stackaddr_np and pthread_get_stacksize_np */
13 #if defined (__MACH__)
14 #define _DARWIN_C_SOURCE 1
15 #endif
16
17 #include <mono/utils/mono-compiler.h>
18 #include <mono/utils/mono-semaphore.h>
19 #include <mono/utils/mono-threads.h>
20 #include <mono/utils/mono-tls.h>
21 #include <mono/utils/mono-mmap.h>
22 #include <mono/metadata/threads-types.h>
23 #include <limits.h>
24
25 #include <errno.h>
26
27 #if defined(PLATFORM_ANDROID) && !defined(TARGET_ARM64) && !defined(TARGET_AMD64)
28 #define USE_TKILL_ON_ANDROID 1
29 #endif
30
31 #ifdef USE_TKILL_ON_ANDROID
32 extern int tkill (pid_t tid, int signal);
33 #endif
34
35 #if defined(_POSIX_VERSION) || defined(__native_client__)
36 #include <sys/resource.h>
37 #include <signal.h>
38
39 #if defined(__native_client__)
40 void nacl_shutdown_gc_thread(void);
41 #endif
42
43 typedef struct {
44         void *(*start_routine)(void*);
45         void *arg;
46         int flags;
47         MonoSemType registered;
48         HANDLE handle;
49 } StartInfo;
50
51 static void*
52 inner_start_thread (void *arg)
53 {
54         StartInfo *start_info = (StartInfo *) arg;
55         void *t_arg = start_info->arg;
56         int res;
57         void *(*start_func)(void*) = start_info->start_routine;
58         guint32 flags = start_info->flags;
59         void *result;
60         HANDLE handle;
61         MonoThreadInfo *info;
62
63         /* Register the thread with the io-layer */
64         handle = wapi_create_thread_handle ();
65         if (!handle) {
66                 res = MONO_SEM_POST (&(start_info->registered));
67                 g_assert (!res);
68                 return NULL;
69         }
70         start_info->handle = handle;
71
72         info = mono_thread_info_attach (&result);
73         MONO_PREPARE_BLOCKING
74
75         info->runtime_thread = TRUE;
76         info->handle = handle;
77
78         if (flags & CREATE_SUSPENDED) {
79                 info->create_suspended = TRUE;
80                 MONO_SEM_INIT (&info->create_suspended_sem, 0);
81         }
82
83         /* start_info is not valid after this */
84         res = MONO_SEM_POST (&(start_info->registered));
85         g_assert (!res);
86         start_info = NULL;
87
88         if (flags & CREATE_SUSPENDED) {
89                 while (MONO_SEM_WAIT (&info->create_suspended_sem) != 0 &&
90                            errno == EINTR);
91                 MONO_SEM_DESTROY (&info->create_suspended_sem);
92         }
93
94         MONO_FINISH_BLOCKING
95         /* Run the actual main function of the thread */
96         result = start_func (t_arg);
97
98         /*
99         mono_thread_info_detach ();
100         */
101
102 #if defined(__native_client__)
103         nacl_shutdown_gc_thread();
104 #endif
105
106         wapi_thread_handle_set_exited (handle, GPOINTER_TO_UINT (result));
107         /* This is needed by mono_threads_core_unregister () which is called later */
108         info->handle = NULL;
109
110         g_assert (mono_threads_get_callbacks ()->thread_exit);
111         mono_threads_get_callbacks ()->thread_exit (NULL);
112         g_assert_not_reached ();
113         return result;
114 }
115
116 HANDLE
117 mono_threads_core_create_thread (LPTHREAD_START_ROUTINE start_routine, gpointer arg, guint32 stack_size, guint32 creation_flags, MonoNativeThreadId *out_tid)
118 {
119         pthread_attr_t attr;
120         int res;
121         pthread_t thread;
122         StartInfo start_info;
123
124         res = pthread_attr_init (&attr);
125         g_assert (!res);
126
127         if (stack_size == 0) {
128 #if HAVE_VALGRIND_MEMCHECK_H
129                 if (RUNNING_ON_VALGRIND)
130                         stack_size = 1 << 20;
131                 else
132                         stack_size = (SIZEOF_VOID_P / 4) * 1024 * 1024;
133 #else
134                 stack_size = (SIZEOF_VOID_P / 4) * 1024 * 1024;
135 #endif
136         }
137
138 #ifdef PTHREAD_STACK_MIN
139         if (stack_size < PTHREAD_STACK_MIN)
140                 stack_size = PTHREAD_STACK_MIN;
141 #endif
142
143 #ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE
144         res = pthread_attr_setstacksize (&attr, stack_size);
145         g_assert (!res);
146 #endif
147
148         memset (&start_info, 0, sizeof (StartInfo));
149         start_info.start_routine = (void *(*)(void *)) start_routine;
150         start_info.arg = arg;
151         start_info.flags = creation_flags;
152         MONO_SEM_INIT (&(start_info.registered), 0);
153
154         /* Actually start the thread */
155         res = mono_threads_get_callbacks ()->mono_gc_pthread_create (&thread, &attr, inner_start_thread, &start_info);
156         if (res) {
157                 MONO_SEM_DESTROY (&(start_info.registered));
158                 return NULL;
159         }
160
161         /* Wait until the thread register itself in various places */
162         while (MONO_SEM_WAIT (&(start_info.registered)) != 0) {
163                 /*if (EINTR != errno) ABORT("sem_wait failed"); */
164         }
165         MONO_SEM_DESTROY (&(start_info.registered));
166
167         if (out_tid)
168                 *out_tid = thread;
169
170         return start_info.handle;
171 }
172
173 /*
174  * mono_threads_core_resume_created:
175  *
176  *   Resume a newly created thread created using CREATE_SUSPENDED.
177  */
178 void
179 mono_threads_core_resume_created (MonoThreadInfo *info, MonoNativeThreadId tid)
180 {
181         MONO_SEM_POST (&info->create_suspended_sem);
182 }
183
184 gboolean
185 mono_threads_core_yield (void)
186 {
187         return sched_yield () == 0;
188 }
189
190 void
191 mono_threads_core_exit (int exit_code)
192 {
193         MonoThreadInfo *current = mono_thread_info_current ();
194
195 #if defined(__native_client__)
196         nacl_shutdown_gc_thread();
197 #endif
198
199         wapi_thread_handle_set_exited (current->handle, exit_code);
200
201         g_assert (mono_threads_get_callbacks ()->thread_exit);
202         mono_threads_get_callbacks ()->thread_exit (NULL);
203 }
204
205 void
206 mono_threads_core_unregister (MonoThreadInfo *info)
207 {
208         if (info->handle) {
209                 wapi_thread_handle_set_exited (info->handle, 0);
210                 info->handle = NULL;
211         }
212 }
213
214 HANDLE
215 mono_threads_core_open_handle (void)
216 {
217         MonoThreadInfo *info;
218
219         info = mono_thread_info_current ();
220         g_assert (info);
221
222         if (!info->handle)
223                 info->handle = wapi_create_thread_handle ();
224         else
225                 wapi_ref_thread_handle (info->handle);
226         return info->handle;
227 }
228
229 int
230 mono_threads_get_max_stack_size (void)
231 {
232         struct rlimit lim;
233
234         /* If getrlimit fails, we don't enforce any limits. */
235         if (getrlimit (RLIMIT_STACK, &lim))
236                 return INT_MAX;
237         /* rlim_t is an unsigned long long on 64bits OSX but we want an int response. */
238         if (lim.rlim_max > (rlim_t)INT_MAX)
239                 return INT_MAX;
240         return (int)lim.rlim_max;
241 }
242
243 HANDLE
244 mono_threads_core_open_thread_handle (HANDLE handle, MonoNativeThreadId tid)
245 {
246         wapi_ref_thread_handle (handle);
247
248         return handle;
249 }
250
251 gpointer
252 mono_threads_core_prepare_interrupt (HANDLE thread_handle)
253 {
254         return wapi_prepare_interrupt_thread (thread_handle);
255 }
256
257 void
258 mono_threads_core_finish_interrupt (gpointer wait_handle)
259 {
260         wapi_finish_interrupt_thread (wait_handle);
261 }
262
263 void
264 mono_threads_core_self_interrupt (void)
265 {
266         wapi_self_interrupt ();
267 }
268
269 void
270 mono_threads_core_clear_interruption (void)
271 {
272         wapi_clear_interruption ();
273 }
274
275 int
276 mono_threads_pthread_kill (MonoThreadInfo *info, int signum)
277 {
278         THREADS_SUSPEND_DEBUG ("sending signal %d to %p[%p]\n", signum, info, mono_thread_info_get_tid (info));
279 #ifdef USE_TKILL_ON_ANDROID
280         int result, old_errno = errno;
281         result = tkill (info->native_handle, signum);
282         if (result < 0) {
283                 result = errno;
284                 errno = old_errno;
285         }
286         return result;
287 #elif defined(__native_client__)
288         /* Workaround pthread_kill abort() in NaCl glibc. */
289         return 0;
290 #else
291         return pthread_kill (mono_thread_info_get_tid (info), signum);
292 #endif
293 }
294
295 MonoNativeThreadId
296 mono_native_thread_id_get (void)
297 {
298         return pthread_self ();
299 }
300
301 gboolean
302 mono_native_thread_id_equals (MonoNativeThreadId id1, MonoNativeThreadId id2)
303 {
304         return pthread_equal (id1, id2);
305 }
306
307 /*
308  * mono_native_thread_create:
309  *
310  *   Low level thread creation function without any GC wrappers.
311  */
312 gboolean
313 mono_native_thread_create (MonoNativeThreadId *tid, gpointer func, gpointer arg)
314 {
315         return pthread_create (tid, NULL, (void *(*)(void *)) func, arg) == 0;
316 }
317
318 void
319 mono_threads_core_set_name (MonoNativeThreadId tid, const char *name)
320 {
321 #if defined (HAVE_PTHREAD_SETNAME_NP) && !defined (__MACH__)
322         if (!name) {
323                 pthread_setname_np (tid, "");
324         } else {
325                 char n [16];
326
327                 strncpy (n, name, 16);
328                 n [15] = '\0';
329                 pthread_setname_np (tid, n);
330         }
331 #endif
332 }
333
334
335 #if defined (USE_POSIX_BACKEND) && !defined (USE_COOP_GC)
336
337 static int suspend_signal_num;
338 static int restart_signal_num;
339 static int abort_signal_num;
340 static sigset_t suspend_signal_mask;
341 static sigset_t suspend_ack_signal_mask;
342
343
344 #if defined(__APPLE__) || defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
345 #define DEFAULT_SUSPEND_SIGNAL SIGXFSZ
346 #else
347 #define DEFAULT_SUSPEND_SIGNAL SIGPWR
348 #endif
349 #define DEFAULT_RESTART_SIGNAL SIGXCPU
350
351 static int
352 mono_thread_search_alt_signal (int min_signal)
353 {
354 #if !defined (SIGRTMIN)
355         g_error ("signal search only works with RTMIN");
356 #else
357         int i;
358         /* we try to avoid SIGRTMIN and any one that might have been set already, see bug #75387 */
359         for (i = MAX (min_signal, SIGRTMIN) + 1; i < SIGRTMAX; ++i) {
360                 struct sigaction sinfo;
361                 sigaction (i, NULL, &sinfo);
362                 if (sinfo.sa_handler == SIG_DFL && (void*)sinfo.sa_sigaction == (void*)SIG_DFL) {
363                         return i;
364                 }
365         }
366         g_error ("Could not find an available signal");
367 #endif
368 }
369
370 static int
371 mono_thread_get_alt_suspend_signal (void)
372 {
373 #if defined(PLATFORM_ANDROID)
374         return SIGUNUSED;
375 #elif !defined (SIGRTMIN)
376 #ifdef SIGUSR1
377         return SIGUSR1;
378 #else
379         return -1;
380 #endif /* SIGUSR1 */
381 #else
382         static int suspend_signum = -1;
383         if (suspend_signum == -1)
384                 suspend_signum = mono_thread_search_alt_signal (-1);
385         return suspend_signum;
386 #endif /* SIGRTMIN */
387 }
388
389 static int
390 mono_thread_get_alt_resume_signal (void)
391 {
392 #if defined(PLATFORM_ANDROID)
393         return SIGTTOU;
394 #elif !defined (SIGRTMIN)
395 #ifdef SIGUSR2
396         return SIGUSR2;
397 #else
398         return -1;
399 #endif /* SIGUSR1 */
400 #else
401         static int resume_signum = -1;
402         if (resume_signum == -1)
403                 resume_signum = mono_thread_search_alt_signal (mono_thread_get_alt_suspend_signal () + 1);
404         return resume_signum;
405 #endif /* SIGRTMIN */
406 }
407
408
409 static int
410 mono_threads_get_abort_signal (void)
411 {
412 #if defined(PLATFORM_ANDROID)
413         return SIGTTIN;
414 #elif !defined (SIGRTMIN)
415 #ifdef SIGTTIN
416         return SIGTTIN;
417 #else
418         return -1;
419 #endif /* SIGRTMIN */
420 #else
421         static int abort_signum = -1;
422         if (abort_signum == -1)
423                 abort_signum = mono_thread_search_alt_signal (mono_thread_get_alt_resume_signal () + 1);
424         return abort_signum;
425 #endif /* SIGRTMIN */
426 }
427
428
429 #if !defined(__native_client__)
430 static void
431 restart_signal_handler (int _dummy, siginfo_t *_info, void *context)
432 {
433         MonoThreadInfo *info;
434         int old_errno = errno;
435
436         info = mono_thread_info_current ();
437         info->signal = restart_signal_num;
438         errno = old_errno;
439 }
440
441 static void
442 suspend_signal_handler (int _dummy, siginfo_t *info, void *context)
443 {
444         int old_errno = errno;
445         int hp_save_index = mono_hazard_pointer_save_for_signal_handler ();
446
447
448         MonoThreadInfo *current = mono_thread_info_current ();
449         gboolean ret;
450
451         THREADS_SUSPEND_DEBUG ("SIGNAL HANDLER FOR %p [%p]\n", current, (void*)current->native_handle);
452         if (current->syscall_break_signal) {
453                 current->syscall_break_signal = FALSE;
454                 THREADS_SUSPEND_DEBUG ("\tsyscall break for %p\n", current);
455                 goto done;
456         }
457
458         /* Have we raced with self suspend? */
459         if (!mono_threads_transition_finish_async_suspend (current)) {
460                 current->suspend_can_continue = TRUE;
461                 THREADS_SUSPEND_DEBUG ("\tlost race with self suspend %p\n", current);
462                 goto done;
463         }
464
465         ret = mono_threads_get_runtime_callbacks ()->thread_state_init_from_sigctx (&current->thread_saved_state [ASYNC_SUSPEND_STATE_INDEX], context);
466
467         /* thread_state_init_from_sigctx return FALSE if the current thread is detaching and suspend can't continue. */
468         current->suspend_can_continue = ret;
469
470
471         /*
472         Block the restart signal.
473         We need to block the restart signal while posting to the suspend_ack semaphore or we race to sigsuspend,
474         which might miss the signal and get stuck.
475         */
476         pthread_sigmask (SIG_BLOCK, &suspend_ack_signal_mask, NULL);
477
478         /* We're done suspending */
479         mono_threads_notify_initiator_of_suspend (current);
480
481         /* This thread is doomed, all we can do is give up and let the suspender recover. */
482         if (!ret) {
483                 THREADS_SUSPEND_DEBUG ("\tThread is dying, failed to capture state %p\n", current);
484                 mono_threads_transition_async_suspend_compensation (current);
485                 /* Unblock the restart signal. */
486                 pthread_sigmask (SIG_UNBLOCK, &suspend_ack_signal_mask, NULL);
487
488                 goto done;
489         }
490
491         do {
492                 current->signal = 0;
493                 sigsuspend (&suspend_signal_mask);
494         } while (current->signal != restart_signal_num);
495
496         /* Unblock the restart signal. */
497         pthread_sigmask (SIG_UNBLOCK, &suspend_ack_signal_mask, NULL);
498
499         if (current->async_target) {
500 #if MONO_ARCH_HAS_MONO_CONTEXT
501                 MonoContext tmp = current->thread_saved_state [ASYNC_SUSPEND_STATE_INDEX].ctx;
502                 mono_threads_get_runtime_callbacks ()->setup_async_callback (&tmp, current->async_target, current->user_data);
503                 current->async_target = current->user_data = NULL;
504                 mono_monoctx_to_sigctx (&tmp, context);
505 #else
506                 g_error ("The new interruption machinery requires a working mono-context");
507 #endif
508         }
509
510         /* We're done resuming */
511         mono_threads_notify_initiator_of_resume (current);
512
513 done:
514         mono_hazard_pointer_restore_for_signal_handler (hp_save_index);
515         errno = old_errno;
516 }
517
518 static void
519 abort_signal_handler (int _dummy, siginfo_t *info, void *context)
520 {
521         suspend_signal_handler (_dummy, info, context);
522 }
523
524 #endif
525
526 static void
527 mono_posix_add_signal_handler (int signo, gpointer handler, int flags)
528 {
529 #if !defined(__native_client__)
530         /*FIXME, move the code from mini to utils and do the right thing!*/
531         struct sigaction sa;
532         struct sigaction previous_sa;
533         int ret;
534
535         sa.sa_sigaction = handler;
536         sigfillset (&sa.sa_mask);
537
538         sa.sa_flags = SA_SIGINFO | flags;
539         ret = sigaction (signo, &sa, &previous_sa);
540
541         g_assert (ret != -1);
542 #endif
543 }
544
545 void
546 mono_threads_init_platform (void)
547 {
548         sigset_t signal_set;
549
550         abort_signal_num = mono_threads_get_abort_signal ();
551         if (mono_thread_info_unified_management_enabled ()) {
552                 suspend_signal_num = DEFAULT_SUSPEND_SIGNAL;
553                 restart_signal_num = DEFAULT_RESTART_SIGNAL;
554         } else {
555                 suspend_signal_num = mono_thread_get_alt_suspend_signal ();
556                 restart_signal_num = mono_thread_get_alt_resume_signal ();
557         }
558
559         sigfillset (&suspend_signal_mask);
560         sigdelset (&suspend_signal_mask, restart_signal_num);
561
562         sigemptyset (&suspend_ack_signal_mask);
563         sigaddset (&suspend_ack_signal_mask, restart_signal_num);
564
565         mono_posix_add_signal_handler (suspend_signal_num, suspend_signal_handler, SA_RESTART);
566         mono_posix_add_signal_handler (restart_signal_num, restart_signal_handler, SA_RESTART);
567         mono_posix_add_signal_handler (abort_signal_num, abort_signal_handler, 0);
568
569         /* ensure all the new signals are unblocked */
570         sigemptyset (&signal_set);
571         sigaddset (&signal_set, suspend_signal_num);
572         sigaddset (&signal_set, restart_signal_num);
573         sigaddset (&signal_set, abort_signal_num);
574         sigprocmask (SIG_UNBLOCK, &signal_set, NULL);
575 }
576
577 void
578 mono_threads_core_abort_syscall (MonoThreadInfo *info)
579 {
580         /*
581         We signal a thread to break it from the urrent syscall.
582         This signal should not be interpreted as a suspend request.
583         */
584         info->syscall_break_signal = TRUE;
585         mono_threads_pthread_kill (info, abort_signal_num);
586 }
587
588 gboolean
589 mono_threads_core_needs_abort_syscall (void)
590 {
591         return TRUE;
592 }
593
594 gboolean
595 mono_threads_core_begin_async_suspend (MonoThreadInfo *info, gboolean interrupt_kernel)
596 {
597         int sig = interrupt_kernel ? abort_signal_num :  suspend_signal_num;
598
599         if (!mono_threads_pthread_kill (info, sig)) {
600                 mono_threads_add_to_pending_operation_set (info);
601                 return TRUE;
602         }
603         return FALSE;
604 }
605
606 gboolean
607 mono_threads_core_check_suspend_result (MonoThreadInfo *info)
608 {
609         return info->suspend_can_continue;
610 }
611
612 /*
613 This begins async resume. This function must do the following:
614
615 - Install an async target if one was requested.
616 - Notify the target to resume.
617 */
618 gboolean
619 mono_threads_core_begin_async_resume (MonoThreadInfo *info)
620 {
621         mono_threads_add_to_pending_operation_set (info);
622         return mono_threads_pthread_kill (info, restart_signal_num) == 0;
623 }
624
625 void
626 mono_threads_platform_register (MonoThreadInfo *info)
627 {
628 #if defined (PLATFORM_ANDROID)
629         info->native_handle = gettid ();
630 #endif
631 }
632
633 void
634 mono_threads_platform_free (MonoThreadInfo *info)
635 {
636 }
637
638 void
639 mono_threads_core_begin_global_suspend (void)
640 {
641 }
642
643 void
644 mono_threads_core_end_global_suspend (void)
645 {
646 }
647
648 #endif /*defined (USE_POSIX_BACKEND)*/
649
650 #endif