1 #include "private/pthread_support.h"
3 #if defined(GC_PTHREADS) && !defined(GC_WIN32_THREADS) && \
4 !defined(GC_DARWIN_THREADS)
10 #include "atomic_ops.h"
16 # define NSIG (MAXSIG+1)
19 # elif defined(__SIGRTMAX)
20 # define NSIG (__SIGRTMAX+1)
26 void GC_print_sig_mask(void)
31 if (pthread_sigmask(SIG_BLOCK, NULL, &blocked) != 0)
32 ABORT("pthread_sigmask");
33 GC_printf("Blocked: ");
34 for (i = 1; i < NSIG; i++) {
35 if (sigismember(&blocked, i)) { GC_printf("%d ", i); }
42 /* Remove the signals that we want to allow in thread stopping */
43 /* handler from a set. */
44 STATIC void GC_remove_allowed_signals(sigset_t *set)
46 if (sigdelset(set, SIGINT) != 0
47 || sigdelset(set, SIGQUIT) != 0
48 || sigdelset(set, SIGABRT) != 0
49 || sigdelset(set, SIGTERM) != 0) {
50 ABORT("sigdelset() failed");
54 /* Handlers write to the thread structure, which is in the heap, */
55 /* and hence can trigger a protection fault. */
56 if (sigdelset(set, SIGSEGV) != 0
58 || sigdelset(set, SIGBUS) != 0
61 ABORT("sigdelset() failed");
66 static sigset_t suspend_handler_mask;
68 volatile AO_t GC_stop_count;
69 /* Incremented at the beginning of GC_stop_world. */
71 volatile AO_t GC_world_is_stopped = FALSE;
72 /* FALSE ==> it is safe for threads to restart, i.e. */
73 /* they will see another suspend signal before they */
74 /* are expected to stop (unless they have voluntarily */
77 #ifdef GC_OSF1_THREADS
78 STATIC GC_bool GC_retry_signals = TRUE;
80 STATIC GC_bool GC_retry_signals = FALSE;
84 * We use signals to stop threads during GC.
86 * Suspended threads wait in signal handler for SIG_THR_RESTART.
87 * That's more portable than semaphores or condition variables.
88 * (We do use sem_post from a signal handler, but that should be portable.)
90 * The thread suspension signal SIG_SUSPEND is now defined in gc_priv.h.
91 * Note that we can't just stop a thread; we need it to save its stack
92 * pointer(s) and acknowledge.
95 #ifndef SIG_THR_RESTART
96 # if defined(GC_HPUX_THREADS) || defined(GC_OSF1_THREADS) || defined(GC_NETBSD_THREADS)
98 # define SIG_THR_RESTART _SIGRTMIN + 5
100 # define SIG_THR_RESTART SIGRTMIN + 5
103 # define SIG_THR_RESTART SIGXCPU
107 STATIC sem_t GC_suspend_ack_sem;
109 #ifdef GC_NETBSD_THREADS
110 # define GC_NETBSD_THREADS_WORKAROUND
111 /* It seems to be necessary to wait until threads have restarted. */
112 /* But it is unclear why that is the case. */
113 STATIC sem_t GC_restart_ack_sem;
116 STATIC void GC_suspend_handler_inner(ptr_t sig_arg, void *context);
118 #if defined(IA64) || defined(HP_PA) || defined(M68K)
121 STATIC void GC_suspend_handler(int sig, siginfo_t *info, void *context)
123 STATIC void GC_suspend_handler(int sig)
126 int old_errno = errno;
127 GC_with_callee_saves_pushed(GC_suspend_handler_inner, (ptr_t)(word)sig);
131 /* We believe that in all other cases the full context is already */
132 /* in the signal handler frame. */
134 STATIC void GC_suspend_handler(int sig, siginfo_t *info, void *context)
136 STATIC void GC_suspend_handler(int sig)
139 int old_errno = errno;
143 GC_suspend_handler_inner((ptr_t)(word)sig, context);
149 STATIC void GC_suspend_handler_inner(ptr_t sig_arg, void *context)
151 int sig = (int)(word)sig_arg;
153 pthread_t my_thread = pthread_self();
156 AO_t my_stop_count = AO_load(&GC_stop_count);
158 if (sig != SIG_SUSPEND) ABORT("Bad signal in suspend_handler");
161 GC_printf("Suspending 0x%x\n", (unsigned)my_thread);
164 me = GC_lookup_thread(my_thread);
165 /* The lookup here is safe, since I'm doing this on behalf */
166 /* of a thread which holds the allocation lock in order */
167 /* to stop the world. Thus concurrent modification of the */
168 /* data structure is impossible. */
169 if (me -> stop_info.last_stop_count == my_stop_count) {
170 /* Duplicate signal. OK if we are retrying. */
171 if (!GC_retry_signals) {
172 WARN("Duplicate suspend signal in thread %p\n",
173 (word)pthread_self());
178 me -> stop_info.stack_ptr = GC_save_regs_in_stack();
180 me -> stop_info.stack_ptr = (ptr_t)(&dummy);
183 me -> backing_store_ptr = GC_save_regs_in_stack();
186 /* Tell the thread that wants to stop the world that this */
187 /* thread has been stopped. Note that sem_post() is */
188 /* the only async-signal-safe primitive in LinuxThreads. */
189 sem_post(&GC_suspend_ack_sem);
190 me -> stop_info.last_stop_count = my_stop_count;
192 /* Wait until that thread tells us to restart by sending */
193 /* this thread a SIG_THR_RESTART signal. */
194 /* SIG_THR_RESTART should be masked at this point. Thus there */
196 /* We do not continue until we receive a SIG_THR_RESTART, */
197 /* but we do not take that as authoritative. (We may be */
198 /* accidentally restarted by one of the user signals we */
199 /* don't block.) After we receive the signal, we use a */
200 /* primitive and expensive mechanism to wait until it's */
201 /* really safe to proceed. Under normal circumstances, */
202 /* this code should not be executed. */
204 sigsuspend (&suspend_handler_mask);
205 } while (AO_load_acquire(&GC_world_is_stopped)
206 && AO_load(&GC_stop_count) == my_stop_count);
207 /* If the RESTART signal gets lost, we can still lose. That should be */
208 /* less likely than losing the SUSPEND signal, since we don't do much */
209 /* between the sem_post and sigsuspend. */
210 /* We'd need more handshaking to work around that. */
211 /* Simply dropping the sigsuspend call should be safe, but is unlikely */
212 /* to be efficient. */
215 GC_printf("Continuing 0x%x\n", (unsigned)my_thread);
219 STATIC void GC_restart_handler(int sig)
221 if (sig != SIG_THR_RESTART) ABORT("Bad signal in suspend_handler");
223 # ifdef GC_NETBSD_THREADS_WORKAROUND
224 sem_post(&GC_restart_ack_sem);
228 ** Note: even if we don't do anything useful here,
229 ** it would still be necessary to have a signal handler,
230 ** rather than ignoring the signals, otherwise
231 ** the signals will not be delivered at all, and
232 ** will thus not interrupt the sigsuspend() above.
236 GC_printf("In GC_restart_handler for 0x%x\n", (unsigned)pthread_self());
240 void GC_thr_init(void);
243 # define IF_IA64(x) x
247 /* We hold allocation lock. Should do exactly the right thing if the */
248 /* world is stopped. Should not fail if it isn't. */
249 void GC_push_all_stacks(void)
251 GC_bool found_me = FALSE;
256 /* On IA64, we also need to scan the register backing store. */
257 IF_IA64(ptr_t bs_lo; ptr_t bs_hi;)
258 pthread_t me = pthread_self();
260 if (!GC_thr_initialized) GC_thr_init();
262 GC_printf("Pushing stacks from thread 0x%x\n", (unsigned) me);
264 for (i = 0; i < THREAD_TABLE_SZ; i++) {
265 for (p = GC_threads[i]; p != 0; p = p -> next) {
266 if (p -> flags & FINISHED) continue;
268 if (THREAD_EQUAL(p -> id, me)) {
269 GC_ASSERT(!p->thread_blocked);
271 lo = (ptr_t)GC_save_regs_in_stack();
276 IF_IA64(bs_hi = (ptr_t)GC_save_regs_in_stack();)
278 lo = p -> stop_info.stack_ptr;
279 IF_IA64(bs_hi = p -> backing_store_ptr;)
281 if ((p -> flags & MAIN_THREAD) == 0) {
283 IF_IA64(bs_lo = p -> backing_store_end);
285 /* The original stack. */
287 IF_IA64(bs_lo = BACKING_STORE_BASE;)
290 GC_printf("Stack for thread 0x%x = [%p,%p)\n",
291 (unsigned)(p -> id), lo, hi);
293 if (0 == lo) ABORT("GC_push_all_stacks: sp not set!\n");
294 # ifdef STACK_GROWS_UP
295 /* We got them backwards! */
296 GC_push_all_stack(hi, lo);
298 GC_push_all_stack(lo, hi);
302 GC_printf("Reg stack for thread 0x%x = [%p,%p)\n",
303 (unsigned)p -> id, bs_lo, bs_hi);
305 if (THREAD_EQUAL(p -> id, me)) {
306 /* FIXME: This may add an unbounded number of entries, */
307 /* and hence overflow the mark stack, which is bad. */
308 GC_push_all_eager(bs_lo, bs_hi);
310 GC_push_all_stack(bs_lo, bs_hi);
315 if (GC_print_stats == VERBOSE) {
316 GC_log_printf("Pushed %d thread stacks\n", (int)nthreads);
318 if (!found_me && !GC_in_thread_creation)
319 ABORT("Collecting from unknown thread.");
322 /* There seems to be a very rare thread stopping problem. To help us */
323 /* debug that, we save the ids of the stopping thread. */
325 pthread_t GC_stopping_thread;
329 /* We hold the allocation lock. Suspend all threads that might */
330 /* still be running. Return the number of suspend signals that */
332 STATIC int GC_suspend_all(void)
334 int n_live_threads = 0;
338 pthread_t my_thread = pthread_self();
341 GC_stopping_thread = my_thread;
342 GC_stopping_pid = getpid();
344 for (i = 0; i < THREAD_TABLE_SZ; i++) {
345 for (p = GC_threads[i]; p != 0; p = p -> next) {
346 if (!THREAD_EQUAL(p -> id, my_thread)) {
347 if (p -> flags & FINISHED) continue;
348 if (p -> stop_info.last_stop_count == GC_stop_count) continue;
349 if (p -> thread_blocked) /* Will wait */ continue;
352 GC_printf("Sending suspend signal to 0x%x\n",
353 (unsigned)(p -> id));
356 result = pthread_kill(p -> id, SIG_SUSPEND);
359 /* Not really there anymore. Possible? */
365 ABORT("pthread_kill failed");
370 return n_live_threads;
373 void GC_stop_world(void)
379 GC_ASSERT(I_HOLD_LOCK());
381 GC_printf("Stopping the world from 0x%x\n", (unsigned)pthread_self());
384 /* Make sure all free list construction has stopped before we start. */
385 /* No new construction can start, since free list construction is */
386 /* required to acquire and release the GC lock before it starts, */
387 /* and we have the lock. */
388 # ifdef PARALLEL_MARK
390 GC_acquire_mark_lock();
391 GC_ASSERT(GC_fl_builder_count == 0);
392 /* We should have previously waited for it to become zero. */
394 # endif /* PARALLEL_MARK */
395 AO_store(&GC_stop_count, GC_stop_count+1);
396 /* Only concurrent reads are possible. */
397 AO_store_release(&GC_world_is_stopped, TRUE);
398 n_live_threads = GC_suspend_all();
400 if (GC_retry_signals) {
401 unsigned long wait_usecs = 0; /* Total wait since retry. */
402 # define WAIT_UNIT 3000
403 # define RETRY_INTERVAL 100000
407 sem_getvalue(&GC_suspend_ack_sem, &ack_count);
408 if (ack_count == n_live_threads) break;
409 if (wait_usecs > RETRY_INTERVAL) {
410 int newly_sent = GC_suspend_all();
412 if (GC_print_stats) {
413 GC_log_printf("Resent %d signals after timeout\n",
416 sem_getvalue(&GC_suspend_ack_sem, &ack_count);
417 if (newly_sent < n_live_threads - ack_count) {
418 WARN("Lost some threads during GC_stop_world?!\n",0);
419 n_live_threads = ack_count + newly_sent;
424 wait_usecs += WAIT_UNIT;
427 for (i = 0; i < n_live_threads; i++) {
429 if (0 != (code = sem_wait(&GC_suspend_ack_sem))) {
430 /* On Linux, sem_wait is documented to always return zero.*/
431 /* But the documentation appears to be incorrect. */
432 if (errno == EINTR) {
433 /* Seems to happen with some versions of gdb. */
436 ABORT("sem_wait for handler failed");
439 # ifdef PARALLEL_MARK
441 GC_release_mark_lock();
444 GC_printf("World stopped from 0x%x\n", (unsigned)pthread_self());
445 GC_stopping_thread = 0;
449 /* Caller holds allocation lock, and has held it continuously since */
450 /* the world stopped. */
451 void GC_start_world(void)
453 pthread_t my_thread = pthread_self();
455 register GC_thread p;
456 register int n_live_threads = 0;
458 # ifdef GC_NETBSD_THREADS_WORKAROUND
463 GC_printf("World starting\n");
466 AO_store(&GC_world_is_stopped, FALSE);
467 for (i = 0; i < THREAD_TABLE_SZ; i++) {
468 for (p = GC_threads[i]; p != 0; p = p -> next) {
469 if (!THREAD_EQUAL(p -> id, my_thread)) {
470 if (p -> flags & FINISHED) continue;
471 if (p -> thread_blocked) continue;
474 GC_printf("Sending restart signal to 0x%x\n",
475 (unsigned)(p -> id));
478 result = pthread_kill(p -> id, SIG_THR_RESTART);
481 /* Not really there anymore. Possible? */
487 ABORT("pthread_kill failed");
492 # ifdef GC_NETBSD_THREADS_WORKAROUND
493 for (i = 0; i < n_live_threads; i++)
494 while (0 != (code = sem_wait(&GC_restart_ack_sem)))
495 if (errno != EINTR) {
496 GC_err_printf("sem_wait() returned %d\n",
498 ABORT("sem_wait() for restart handler failed");
502 GC_printf("World started\n");
506 void GC_stop_init(void) {
507 struct sigaction act;
509 if (sem_init(&GC_suspend_ack_sem, 0, 0) != 0)
510 ABORT("sem_init failed");
511 # ifdef GC_NETBSD_THREADS_WORKAROUND
512 if (sem_init(&GC_restart_ack_sem, 0, 0) != 0)
513 ABORT("sem_init failed");
516 act.sa_flags = SA_RESTART
521 if (sigfillset(&act.sa_mask) != 0) {
522 ABORT("sigfillset() failed");
524 GC_remove_allowed_signals(&act.sa_mask);
525 /* SIG_THR_RESTART is set in the resulting mask. */
526 /* It is unmasked by the handler when necessary. */
528 act.sa_sigaction = GC_suspend_handler;
530 act.sa_handler = GC_suspend_handler;
532 if (sigaction(SIG_SUSPEND, &act, NULL) != 0) {
533 ABORT("Cannot set SIG_SUSPEND handler");
537 act.sa_flags &= ~ SA_SIGINFO;
539 act.sa_handler = GC_restart_handler;
540 if (sigaction(SIG_THR_RESTART, &act, NULL) != 0) {
541 ABORT("Cannot set SIG_THR_RESTART handler");
544 /* Initialize suspend_handler_mask. It excludes SIG_THR_RESTART. */
545 if (sigfillset(&suspend_handler_mask) != 0) ABORT("sigfillset() failed");
546 GC_remove_allowed_signals(&suspend_handler_mask);
547 if (sigdelset(&suspend_handler_mask, SIG_THR_RESTART) != 0)
548 ABORT("sigdelset() failed");
550 /* Check for GC_RETRY_SIGNALS. */
551 if (0 != GETENV("GC_RETRY_SIGNALS")) {
552 GC_retry_signals = TRUE;
554 if (0 != GETENV("GC_NO_RETRY_SIGNALS")) {
555 GC_retry_signals = FALSE;
557 if (GC_print_stats && GC_retry_signals) {
558 GC_log_printf("Will retry suspend signal if necessary.\n");