1 #include "private/pthread_support.h"
3 #if defined(GC_PTHREADS) && !defined(GC_SOLARIS_THREADS) \
4 && !defined(GC_IRIX_THREADS) && !defined(GC_WIN32_THREADS) \
5 && !defined(GC_DARWIN_THREADS) && !defined(GC_AIX_THREADS)
13 /* work around a dlopen issue (bug #75390), undefs to avoid warnings with redefinitions */
14 #undef PACKAGE_BUGREPORT
17 #undef PACKAGE_TARNAME
18 #undef PACKAGE_VERSION
19 #include "mono/utils/mono-compiler.h"
22 volatile int __nacl_thread_suspension_needed
= 0;
23 pthread_t nacl_thread_parker
= -1;
25 volatile int nacl_thread_parked
[MAX_NACL_GC_THREADS
];
26 volatile int nacl_thread_used
[MAX_NACL_GC_THREADS
];
27 volatile int nacl_thread_parking_inited
= 0;
28 volatile int nacl_num_gc_threads
= 0;
29 pthread_mutex_t nacl_thread_alloc_lock
= PTHREAD_MUTEX_INITIALIZER
;
30 __thread
int nacl_thread_idx
= -1;
31 __thread GC_thread nacl_gc_thread_self
= NULL
;
38 # define NSIG (MAXSIG+1)
41 # elif defined(__SIGRTMAX)
42 # define NSIG (__SIGRTMAX+1)
49 void GC_print_sig_mask()
54 if (pthread_sigmask(SIG_BLOCK
, NULL
, &blocked
) != 0)
55 ABORT("pthread_sigmask");
56 GC_printf0("Blocked: ");
57 for (i
= 1; i
< NSIG
; i
++) {
58 if (sigismember(&blocked
, i
)) { GC_printf1("%ld ",(long) i
); }
65 /* Remove the signals that we want to allow in thread stopping */
66 /* handler from a set. */
67 void GC_remove_allowed_signals(sigset_t
*set
)
70 if (sigdelset(set
, SIGINT
) != 0
71 || sigdelset(set
, SIGQUIT
) != 0
72 || sigdelset(set
, SIGABRT
) != 0
73 || sigdelset(set
, SIGTERM
) != 0) {
74 ABORT("sigdelset() failed");
79 /* Handlers write to the thread structure, which is in the heap, */
80 /* and hence can trigger a protection fault. */
81 if (sigdelset(set
, SIGSEGV
) != 0
83 || sigdelset(set
, SIGBUS
) != 0
86 ABORT("sigdelset() failed");
91 static sigset_t suspend_handler_mask
;
93 word GC_stop_count
; /* Incremented at the beginning of GC_stop_world. */
95 #ifdef GC_OSF1_THREADS
96 GC_bool GC_retry_signals
= TRUE
;
98 GC_bool GC_retry_signals
= FALSE
;
102 * We use signals to stop threads during GC.
104 * Suspended threads wait in signal handler for SIG_THR_RESTART.
105 * That's more portable than semaphores or condition variables.
106 * (We do use sem_post from a signal handler, but that should be portable.)
108 * The thread suspension signal SIG_SUSPEND is now defined in gc_priv.h.
109 * Note that we can't just stop a thread; we need it to save its stack
110 * pointer(s) and acknowledge.
113 #ifndef SIG_THR_RESTART
114 # if defined(GC_HPUX_THREADS) || defined(GC_OSF1_THREADS)
116 # define SIG_THR_RESTART _SIGRTMIN + 5
118 # define SIG_THR_RESTART SIGRTMIN + 5
121 # define SIG_THR_RESTART SIGXCPU
125 sem_t GC_suspend_ack_sem
;
127 static void _GC_suspend_handler(int sig
)
131 pthread_t my_thread
= pthread_self();
133 # ifdef PARALLEL_MARK
134 word my_mark_no
= GC_mark_no
;
135 /* Marker can't proceed until we acknowledge. Thus this is */
136 /* guaranteed to be the mark_no correspending to our */
137 /* suspension, i.e. the marker can't have incremented it yet. */
139 word my_stop_count
= GC_stop_count
;
141 if (sig
!= SIG_SUSPEND
) ABORT("Bad signal in suspend_handler");
144 GC_printf1("Suspending 0x%lx\n", my_thread
);
147 me
= GC_lookup_thread(my_thread
);
148 /* The lookup here is safe, since I'm doing this on behalf */
149 /* of a thread which holds the allocation lock in order */
150 /* to stop the world. Thus concurrent modification of the */
151 /* data structure is impossible. */
152 if (me
-> stop_info
.last_stop_count
== my_stop_count
) {
153 /* Duplicate signal. OK if we are retrying. */
154 if (!GC_retry_signals
) {
155 WARN("Duplicate suspend signal in thread %lx\n",
161 me
-> stop_info
.stack_ptr
= (ptr_t
)GC_save_regs_in_stack();
163 me
-> stop_info
.stack_ptr
= (ptr_t
)(&dummy
);
166 me
-> backing_store_ptr
= (ptr_t
)GC_save_regs_in_stack();
169 /* Tell the thread that wants to stop the world that this */
170 /* thread has been stopped. Note that sem_post() is */
171 /* the only async-signal-safe primitive in LinuxThreads. */
172 sem_post(&GC_suspend_ack_sem
);
173 me
-> stop_info
.last_stop_count
= my_stop_count
;
175 /* Wait until that thread tells us to restart by sending */
176 /* this thread a SIG_THR_RESTART signal. */
177 /* SIG_THR_RESTART should be masked at this point. Thus there */
180 me
->stop_info
.signal
= 0;
181 sigsuspend(&suspend_handler_mask
); /* Wait for signal */
182 } while (me
->stop_info
.signal
!= SIG_THR_RESTART
);
183 /* If the RESTART signal gets lost, we can still lose. That should be */
184 /* less likely than losing the SUSPEND signal, since we don't do much */
185 /* between the sem_post and sigsuspend. */
186 /* We'd need more handshaking to work around that, since we don't want */
187 /* to accidentally leave a RESTART signal pending, thus causing us to */
188 /* continue prematurely in a future round. */
190 /* Tell the thread that wants to start the world that this */
191 /* thread has been started. Note that sem_post() is */
192 /* the only async-signal-safe primitive in LinuxThreads. */
193 sem_post(&GC_suspend_ack_sem
);
197 GC_printf1("Continuing 0x%lx\n", my_thread
);
203 void GC_suspend_handler(int sig
)
205 int old_errno
= errno
;
206 _GC_suspend_handler(sig
);
210 static void _GC_restart_handler(int sig
)
212 pthread_t my_thread
= pthread_self();
215 if (sig
!= SIG_THR_RESTART
) ABORT("Bad signal in suspend_handler");
217 /* Let the GC_suspend_handler() know that we got a SIG_THR_RESTART. */
218 /* The lookup here is safe, since I'm doing this on behalf */
219 /* of a thread which holds the allocation lock in order */
220 /* to stop the world. Thus concurrent modification of the */
221 /* data structure is impossible. */
222 me
= GC_lookup_thread(my_thread
);
223 me
->stop_info
.signal
= SIG_THR_RESTART
;
226 ** Note: even if we didn't do anything useful here,
227 ** it would still be necessary to have a signal handler,
228 ** rather than ignoring the signals, otherwise
229 ** the signals will not be delivered at all, and
230 ** will thus not interrupt the sigsuspend() above.
234 GC_printf1("In GC_restart_handler for 0x%lx\n", pthread_self());
239 # define IF_IA64(x) x
243 /* We hold allocation lock. Should do exactly the right thing if the */
244 /* world is stopped. Should not fail if it isn't. */
245 static void pthread_push_all_stacks()
247 GC_bool found_me
= FALSE
;
251 /* On IA64, we also need to scan the register backing store. */
252 IF_IA64(ptr_t bs_lo
; ptr_t bs_hi
;)
253 pthread_t me
= pthread_self();
255 if (!GC_thr_initialized
) GC_thr_init();
257 GC_printf1("Pushing stacks from thread 0x%lx\n", (unsigned long) me
);
259 for (i
= 0; i
< THREAD_TABLE_SZ
; i
++) {
260 for (p
= GC_threads
[i
]; p
!= 0; p
= p
-> next
) {
261 if (p
-> flags
& FINISHED
) continue;
262 if (pthread_equal(p
-> id
, me
)) {
264 lo
= (ptr_t
)GC_save_regs_in_stack();
269 IF_IA64(bs_hi
= (ptr_t
)GC_save_regs_in_stack();)
271 lo
= p
-> stop_info
.stack_ptr
;
272 IF_IA64(bs_hi
= p
-> backing_store_ptr
;)
274 if ((p
-> flags
& MAIN_THREAD
) == 0) {
276 IF_IA64(bs_lo
= p
-> backing_store_end
);
278 /* The original stack. */
280 IF_IA64(bs_lo
= BACKING_STORE_BASE
;)
283 GC_printf3("Stack for thread 0x%lx = [%lx,%lx)\n",
284 (unsigned long) p
-> id
,
285 (unsigned long) lo
, (unsigned long) hi
);
287 if (0 == lo
) ABORT("GC_push_all_stacks: sp not set!\n");
288 if (p
->altstack
&& lo
>= p
->altstack
&& lo
<= p
->altstack
+ p
->altstack_size
)
289 hi
= p
->altstack
+ p
->altstack_size
;
290 /* FIXME: Need to scan the normal stack too, but how ? */
292 # ifdef STACK_GROWS_UP
293 /* We got them backwards! */
294 GC_push_all_stack(hi
, lo
);
296 GC_push_all_stack(lo
, hi
);
299 /* Push reg_storage as roots, this will cover the reg context */
300 GC_push_all_stack(p
-> stop_info
.reg_storage
, p
-> stop_info
.reg_storage
+ NACL_GC_REG_STORAGE_SIZE
);
304 GC_printf3("Reg stack for thread 0x%lx = [%lx,%lx)\n",
305 (unsigned long) p
-> id
,
306 (unsigned long) bs_lo
, (unsigned long) bs_hi
);
308 if (pthread_equal(p
-> id
, me
)) {
309 GC_push_all_eager(bs_lo
, bs_hi
);
311 GC_push_all_stack(bs_lo
, bs_hi
);
316 if (!found_me
&& !GC_in_thread_creation
)
317 ABORT("Collecting from unknown thread.");
320 void GC_restart_handler(int sig
)
322 int old_errno
= errno
;
323 _GC_restart_handler (sig
);
327 /* We hold allocation lock. Should do exactly the right thing if the */
328 /* world is stopped. Should not fail if it isn't. */
329 void GC_push_all_stacks()
331 pthread_push_all_stacks();
334 /* There seems to be a very rare thread stopping problem. To help us */
335 /* debug that, we save the ids of the stopping thread. */
336 pthread_t GC_stopping_thread
;
341 int android_thread_kill(pid_t tid
, int sig
)
344 int old_errno
= errno
;
346 ret
= tkill(tid
, sig
);
356 /* We hold the allocation lock. Suspend all threads that might */
357 /* still be running. Return the number of suspend signals that */
362 int n_live_threads
= 0;
366 pthread_t my_thread
= pthread_self();
368 GC_stopping_thread
= my_thread
; /* debugging only. */
369 GC_stopping_pid
= getpid(); /* debugging only. */
370 for (i
= 0; i
< THREAD_TABLE_SZ
; i
++) {
371 for (p
= GC_threads
[i
]; p
!= 0; p
= p
-> next
) {
372 if (p
-> id
!= my_thread
) {
373 if (p
-> flags
& FINISHED
) continue;
374 if (p
-> stop_info
.last_stop_count
== GC_stop_count
) continue;
375 if (p
-> thread_blocked
) /* Will wait */ continue;
378 GC_printf1("Sending suspend signal to 0x%lx\n", p
-> id
);
382 result
= pthread_kill(p
-> id
, SIG_SUSPEND
);
384 result
= android_thread_kill(p
-> kernel_id
, SIG_SUSPEND
);
388 /* Not really there anymore. Possible? */
394 ABORT("pthread_kill failed");
399 return n_live_threads
;
405 /* Caller holds allocation lock. */
406 static void pthread_stop_world()
414 GC_printf1("Stopping the world from 0x%lx\n", pthread_self());
417 n_live_threads
= GC_suspend_all();
419 if (GC_retry_signals
) {
420 unsigned long wait_usecs
= 0; /* Total wait since retry. */
421 # define WAIT_UNIT 3000
422 # define RETRY_INTERVAL 100000
426 sem_getvalue(&GC_suspend_ack_sem
, &ack_count
);
427 if (ack_count
== n_live_threads
) break;
428 if (wait_usecs
> RETRY_INTERVAL
) {
429 int newly_sent
= GC_suspend_all();
432 if (GC_print_stats
) {
433 GC_printf1("Resent %ld signals after timeout\n",
437 sem_getvalue(&GC_suspend_ack_sem
, &ack_count
);
438 if (newly_sent
< n_live_threads
- ack_count
) {
439 WARN("Lost some threads during GC_stop_world?!\n",0);
440 n_live_threads
= ack_count
+ newly_sent
;
445 wait_usecs
+= WAIT_UNIT
;
448 for (i
= 0; i
< n_live_threads
; i
++) {
449 while (0 != (code
= sem_wait(&GC_suspend_ack_sem
))) {
450 if (errno
!= EINTR
) {
451 GC_err_printf1("Sem_wait returned %ld\n", (unsigned long)code
);
452 ABORT("sem_wait for handler failed");
457 GC_printf1("World stopped from 0x%lx\n", pthread_self());
459 GC_stopping_thread
= 0; /* debugging only */
466 GC_printf1("pthread_stop_world: num_threads %d\n", nacl_num_gc_threads
- 1);
468 nacl_thread_parker
= pthread_self();
469 __nacl_thread_suspension_needed
= 1;
472 #define NACL_PARK_WAIT_NANOSECONDS 100000
473 #define NANOS_PER_SECOND 1000000000
474 int num_threads_parked
= 0;
477 /* Check the 'parked' flag for each thread the GC knows about */
478 for (i
= 0; i
< MAX_NACL_GC_THREADS
&& num_used
< nacl_num_gc_threads
; i
++) {
479 if (nacl_thread_used
[i
] == 1) {
481 if (nacl_thread_parked
[i
] == 1) {
482 num_threads_parked
++;
486 /* -1 for the current thread */
487 if (num_threads_parked
>= nacl_num_gc_threads
- 1)
490 ts
.tv_nsec
= NACL_PARK_WAIT_NANOSECONDS
;
492 GC_printf1("sleeping waiting for %d threads to park...\n", nacl_num_gc_threads
- num_threads_parked
- 1);
495 if (++num_sleeps
> NANOS_PER_SECOND
/ NACL_PARK_WAIT_NANOSECONDS
) {
496 GC_printf1("GC appears stalled waiting for %d threads to park...\n", nacl_num_gc_threads
- num_threads_parked
- 1);
509 #define NACL_STORE_REGS() \
511 __asm__ __volatile__ ("push %rbx");\
512 __asm__ __volatile__ ("push %rbp");\
513 __asm__ __volatile__ ("push %r12");\
514 __asm__ __volatile__ ("push %r13");\
515 __asm__ __volatile__ ("push %r14");\
516 __asm__ __volatile__ ("push %r15");\
517 __asm__ __volatile__ ("mov %%esp, %0" : "=m" (nacl_gc_thread_self->stop_info.stack_ptr));\
518 memcpy(nacl_gc_thread_self->stop_info.reg_storage, nacl_gc_thread_self->stop_info.stack_ptr, NACL_GC_REG_STORAGE_SIZE * sizeof(ptr_t));\
519 __asm__ __volatile__ ("naclasp $48, %r15");\
524 #define NACL_STORE_REGS() \
526 __asm__ __volatile__ ("push %ebx");\
527 __asm__ __volatile__ ("push %ebp");\
528 __asm__ __volatile__ ("push %esi");\
529 __asm__ __volatile__ ("push %edi");\
530 __asm__ __volatile__ ("mov %%esp, %0" : "=m" (nacl_gc_thread_self->stop_info.stack_ptr));\
531 memcpy(nacl_gc_thread_self->stop_info.reg_storage, nacl_gc_thread_self->stop_info.stack_ptr, NACL_GC_REG_STORAGE_SIZE * sizeof(ptr_t));\
532 __asm__ __volatile__ ("add $16, %esp");\
537 #define NACL_STORE_REGS() \
539 __asm__ __volatile__ ( \
541 "bic %0, %0, #0xc0000000\n\t" \
543 "bic %1, %1, #0xc0000000\n\t" \
544 "stm %1, {r4-r8,r10-r12,lr}\n\t" \
546 : "r" (&nacl_gc_thread_self->stop_info.stack_ptr), \
547 "r"(nacl_gc_thread_self->stop_info.reg_storage) \
552 #error "Please port NACL_STORE_REGS"
556 void nacl_pre_syscall_hook()
559 if (nacl_thread_idx
!= -1) {
561 nacl_gc_thread_self
->stop_info
.stack_ptr
= (ptr_t
)(&local_dummy
);
562 nacl_thread_parked
[nacl_thread_idx
] = 1;
566 void __nacl_suspend_thread_if_needed();
568 void nacl_post_syscall_hook()
570 /* Calling __nacl_suspend_thread_if_needed() right away should guarantee we don't mutate the GC set. */
571 __nacl_suspend_thread_if_needed();
572 if (nacl_thread_idx
!= -1) {
573 nacl_thread_parked
[nacl_thread_idx
] = 0;
577 void __nacl_suspend_thread_if_needed() {
578 if (__nacl_thread_suspension_needed
) {
579 pthread_t self
= pthread_self();
581 /* Don't try to park the thread parker. */
582 if (nacl_thread_parker
== self
)
585 /* This can happen when a thread is created */
586 /* outside of the GC system (wthread mostly). */
587 if (nacl_thread_idx
< 0)
590 /* If it was already 'parked', we're returning from a syscall, */
591 /* so don't bother storing registers again, the GC has a set. */
592 if (!nacl_thread_parked
[nacl_thread_idx
]) {
594 nacl_gc_thread_self
->stop_info
.stack_ptr
= (ptr_t
)(&local_dummy
);
596 nacl_thread_parked
[nacl_thread_idx
] = 1;
597 while (__nacl_thread_suspension_needed
)
599 nacl_thread_parked
[nacl_thread_idx
] = 0;
601 /* Clear out the reg storage for next suspend. */
602 memset(nacl_gc_thread_self
->stop_info
.reg_storage
, 0, NACL_GC_REG_STORAGE_SIZE
* sizeof(ptr_t
));
608 /* Caller holds allocation lock. */
612 GC_notify_event (GC_EVENT_PRE_STOP_WORLD
);
613 GC_process_togglerefs ();
614 /* Make sure all free list construction has stopped before we start. */
615 /* No new construction can start, since free list construction is */
616 /* required to acquire and release the GC lock before it starts, */
617 /* and we have the lock. */
618 # ifdef PARALLEL_MARK
619 GC_acquire_mark_lock();
620 GC_ASSERT(GC_fl_builder_count
== 0);
621 /* We should have previously waited for it to become zero. */
622 # endif /* PARALLEL_MARK */
624 pthread_stop_world ();
625 # ifdef PARALLEL_MARK
626 GC_release_mark_lock();
629 GC_notify_event (GC_EVENT_POST_STOP_WORLD
);
632 /* Caller holds allocation lock, and has held it continuously since */
633 /* the world stopped. */
634 static void pthread_start_world()
637 pthread_t my_thread
= pthread_self();
639 register GC_thread p
;
640 register int n_live_threads
= 0;
645 GC_printf0("World starting\n");
648 GC_notify_event (GC_EVENT_PRE_START_WORLD
);
650 for (i
= 0; i
< THREAD_TABLE_SZ
; i
++) {
651 for (p
= GC_threads
[i
]; p
!= 0; p
= p
-> next
) {
652 if (p
-> id
!= my_thread
) {
653 if (p
-> flags
& FINISHED
) continue;
654 if (p
-> thread_blocked
) continue;
657 GC_printf1("Sending restart signal to 0x%lx\n", p
-> id
);
661 result
= pthread_kill(p
-> id
, SIG_THR_RESTART
);
663 result
= android_thread_kill(p
-> kernel_id
, SIG_THR_RESTART
);
667 /* Not really there anymore. Possible? */
673 ABORT("pthread_kill failed");
680 GC_printf0 ("All threads signaled");
683 for (i
= 0; i
< n_live_threads
; i
++) {
684 while (0 != (code
= sem_wait(&GC_suspend_ack_sem
))) {
685 if (errno
!= EINTR
) {
686 GC_err_printf1("Sem_wait returned %ld\n", (unsigned long)code
);
687 ABORT("sem_wait for handler failed");
693 GC_notify_event (GC_EVENT_POST_START_WORLD
);
695 GC_printf0("World started\n");
699 GC_notify_event (GC_EVENT_PRE_START_WORLD
);
701 GC_printf0("World starting\n");
703 __nacl_thread_suspension_needed
= 0;
705 GC_notify_event (GC_EVENT_POST_START_WORLD
);
709 void GC_start_world()
711 pthread_start_world ();
714 static void pthread_stop_init() {
716 struct sigaction act
;
718 if (sem_init(&GC_suspend_ack_sem
, 0, 0) != 0)
719 ABORT("sem_init failed");
721 act
.sa_flags
= SA_RESTART
;
722 if (sigfillset(&act
.sa_mask
) != 0) {
723 ABORT("sigfillset() failed");
725 GC_remove_allowed_signals(&act
.sa_mask
);
726 /* SIG_THR_RESTART is set in the resulting mask. */
727 /* It is unmasked by the handler when necessary. */
728 act
.sa_handler
= GC_suspend_handler
;
729 if (sigaction(SIG_SUSPEND
, &act
, NULL
) != 0) {
730 ABORT("Cannot set SIG_SUSPEND handler");
733 act
.sa_handler
= GC_restart_handler
;
734 if (sigaction(SIG_THR_RESTART
, &act
, NULL
) != 0) {
735 ABORT("Cannot set SIG_THR_RESTART handler");
738 /* Inititialize suspend_handler_mask. It excludes SIG_THR_RESTART. */
739 if (sigfillset(&suspend_handler_mask
) != 0) ABORT("sigfillset() failed");
740 GC_remove_allowed_signals(&suspend_handler_mask
);
741 if (sigdelset(&suspend_handler_mask
, SIG_THR_RESTART
) != 0)
742 ABORT("sigdelset() failed");
744 /* Check for GC_RETRY_SIGNALS. */
745 if (0 != GETENV("GC_RETRY_SIGNALS")) {
746 GC_retry_signals
= TRUE
;
748 if (0 != GETENV("GC_NO_RETRY_SIGNALS")) {
749 GC_retry_signals
= FALSE
;
752 if (GC_print_stats
&& GC_retry_signals
) {
753 GC_printf0("Will retry suspend signal if necessary.\n");
759 /* We hold the allocation lock. */
762 pthread_stop_init ();