3 #include "private/pthread_support.h"
5 #if defined(GC_PTHREADS) && !defined(GC_SOLARIS_THREADS) \
6 && !defined(GC_WIN32_THREADS) && !defined(GC_DARWIN_THREADS)
14 # include <sys/select.h>
15 /* Doesn't exist on HP/UX 11.11. */
22 # define NSIG (MAXSIG+1)
25 # elif defined(__SIGRTMAX)
26 # define NSIG (__SIGRTMAX+1)
32 void GC_print_sig_mask()
37 if (pthread_sigmask(SIG_BLOCK, NULL, &blocked) != 0)
38 ABORT("pthread_sigmask");
39 GC_printf0("Blocked: ");
40 for (i = 1; i < NSIG; i++) {
41 if (sigismember(&blocked, i)) { GC_printf1("%ld ",(long) i); }
48 /* Remove the signals that we want to allow in thread stopping */
49 /* handler from a set. */
50 void GC_remove_allowed_signals(sigset_t *set)
53 if (sigdelset(set, SIGINT) != 0
54 || sigdelset(set, SIGQUIT) != 0
55 || sigdelset(set, SIGABRT) != 0
56 || sigdelset(set, SIGTERM) != 0) {
57 ABORT("sigdelset() failed");
62 /* Handlers write to the thread structure, which is in the heap, */
63 /* and hence can trigger a protection fault. */
64 if (sigdelset(set, SIGSEGV) != 0
66 || sigdelset(set, SIGBUS) != 0
69 ABORT("sigdelset() failed");
74 static sigset_t suspend_handler_mask;
76 volatile sig_atomic_t GC_stop_count;
77 /* Incremented at the beginning of GC_stop_world. */
79 volatile sig_atomic_t GC_world_is_stopped = FALSE;
80 /* FALSE ==> it is safe for threads to restart, i.e. */
81 /* they will see another suspend signal before they */
82 /* are expected to stop (unless they have voluntarily */
85 void GC_brief_async_signal_safe_sleep()
89 tv.tv_usec = 1000 * TIME_LIMIT / 2;
90 select(0, 0, 0, 0, &tv);
93 #ifdef GC_OSF1_THREADS
94 GC_bool GC_retry_signals = TRUE;
96 GC_bool GC_retry_signals = FALSE;
100 * We use signals to stop threads during GC.
102 * Suspended threads wait in signal handler for SIG_THR_RESTART.
103 * That's more portable than semaphores or condition variables.
104 * (We do use sem_post from a signal handler, but that should be portable.)
106 * The thread suspension signal SIG_SUSPEND is now defined in gc_priv.h.
107 * Note that we can't just stop a thread; we need it to save its stack
108 * pointer(s) and acknowledge.
111 #ifndef SIG_THR_RESTART
112 # if defined(GC_HPUX_THREADS) || defined(GC_OSF1_THREADS) || defined(GC_NETBSD_THREADS)
114 # define SIG_THR_RESTART _SIGRTMIN + 5
116 # define SIG_THR_RESTART SIGRTMIN + 5
119 # define SIG_THR_RESTART SIGXCPU
123 sem_t GC_suspend_ack_sem;
125 #ifdef GC_NETBSD_THREADS
126 # define GC_NETBSD_THREADS_WORKAROUND
127 /* It seems to be necessary to wait until threads have restarted. */
128 /* But it is unclear why that is the case. */
129 sem_t GC_restart_ack_sem;
132 void GC_suspend_handler_inner(ptr_t sig_arg);
133 /* int cacao_suspendhandler(void *); */
135 #if defined(IA64) || defined(HP_PA)
136 extern void GC_with_callee_saves_pushed();
138 void GC_suspend_handler(int sig)
140 int old_errno = errno;
141 GC_with_callee_saves_pushed(GC_suspend_handler_inner, (ptr_t)(word)sig);
146 /* We believe that in all other cases the full context is already */
147 /* in the signal handler frame. */
148 void GC_suspend_handler(int sig, siginfo_t *info, void *uctx)
150 int old_errno = errno;
152 GC_suspend_handler_inner((ptr_t)(word)sig);
157 void GC_suspend_handler_inner(ptr_t sig_arg)
159 int sig = (int)(word)sig_arg;
161 pthread_t my_thread = pthread_self();
163 # ifdef PARALLEL_MARK
164 word my_mark_no = GC_mark_no;
165 /* Marker can't proceed until we acknowledge. Thus this is */
166 /* guaranteed to be the mark_no correspending to our */
167 /* suspension, i.e. the marker can't have incremented it yet. */
169 word my_stop_count = GC_stop_count;
171 if (sig != SIG_SUSPEND) ABORT("Bad signal in suspend_handler");
174 GC_printf1("Suspending 0x%lx\n", my_thread);
177 me = GC_lookup_thread(my_thread);
178 /* The lookup here is safe, since I'm doing this on behalf */
179 /* of a thread which holds the allocation lock in order */
180 /* to stop the world. Thus concurrent modification of the */
181 /* data structure is impossible. */
182 if (me -> stop_info.last_stop_count == my_stop_count) {
183 /* Duplicate signal. OK if we are retrying. */
184 if (!GC_retry_signals) {
185 WARN("Duplicate suspend signal in thread %lx\n",
191 me -> stop_info.stack_ptr = (ptr_t)GC_save_regs_in_stack();
193 me -> stop_info.stack_ptr = (ptr_t)(&dummy);
196 me -> backing_store_ptr = (ptr_t)GC_save_regs_in_stack();
199 /* Tell the thread that wants to stop the world that this */
200 /* thread has been stopped. Note that sem_post() is */
201 /* the only async-signal-safe primitive in LinuxThreads. */
202 sem_post(&GC_suspend_ack_sem);
203 me -> stop_info.last_stop_count = my_stop_count;
205 /* Wait until that thread tells us to restart by sending */
206 /* this thread a SIG_THR_RESTART signal. */
207 /* SIG_THR_RESTART should be masked at this point. Thus there */
209 /* We do not continue until we receive a SIG_THR_RESTART, */
210 /* but we do not take that as authoritative. (We may be */
211 /* accidentally restarted by one of the user signals we */
212 /* don't block.) After we receive the signal, we use a */
213 /* primitive and expensive mechanism to wait until it's */
214 /* really safe to proceed. Under normal circumstances, */
215 /* this code should not be executed. */
216 sigsuspend(&suspend_handler_mask); /* Wait for signal */
217 while (GC_world_is_stopped && GC_stop_count == my_stop_count) {
218 GC_brief_async_signal_safe_sleep();
220 GC_err_printf0("Sleeping in signal handler");
223 /* If the RESTART signal gets lost, we can still lose. That should be */
224 /* less likely than losing the SUSPEND signal, since we don't do much */
225 /* between the sem_post and sigsuspend. */
226 /* We'd need more handshaking to work around that. */
227 /* Simply dropping the sigsuspend call should be safe, but is unlikely */
228 /* to be efficient. */
231 GC_printf1("Continuing 0x%lx\n", my_thread);
235 void GC_restart_handler(int sig)
237 pthread_t my_thread = pthread_self();
239 if (sig != SIG_THR_RESTART) ABORT("Bad signal in suspend_handler");
241 #ifdef GC_NETBSD_THREADS_WORKAROUND
242 sem_post(&GC_restart_ack_sem);
246 ** Note: even if we don't do anything useful here,
247 ** it would still be necessary to have a signal handler,
248 ** rather than ignoring the signals, otherwise
249 ** the signals will not be delivered at all, and
250 ** will thus not interrupt the sigsuspend() above.
254 GC_printf1("In GC_restart_handler for 0x%lx\n", pthread_self());
259 # define IF_IA64(x) x
263 /* We hold allocation lock. Should do exactly the right thing if the */
264 /* world is stopped. Should not fail if it isn't. */
265 void GC_push_all_stacks()
267 GC_bool found_me = FALSE;
271 /* On IA64, we also need to scan the register backing store. */
272 IF_IA64(ptr_t bs_lo; ptr_t bs_hi;)
273 pthread_t me = pthread_self();
275 if (!GC_thr_initialized) GC_thr_init();
277 GC_printf1("Pushing stacks from thread 0x%lx\n", (unsigned long) me);
279 for (i = 0; i < THREAD_TABLE_SZ; i++) {
280 for (p = GC_threads[i]; p != 0; p = p -> next) {
281 if (p -> flags & FINISHED) continue;
282 if (pthread_equal(p -> id, me)) {
284 lo = (ptr_t)GC_save_regs_in_stack();
289 IF_IA64(bs_hi = (ptr_t)GC_save_regs_in_stack();)
291 lo = p -> stop_info.stack_ptr;
292 IF_IA64(bs_hi = p -> backing_store_ptr;)
294 if ((p -> flags & MAIN_THREAD) == 0) {
296 IF_IA64(bs_lo = p -> backing_store_end);
298 /* The original stack. */
300 IF_IA64(bs_lo = BACKING_STORE_BASE;)
303 GC_printf3("Stack for thread 0x%lx = [%lx,%lx)\n",
304 (unsigned long) p -> id,
305 (unsigned long) lo, (unsigned long) hi);
307 if (0 == lo) ABORT("GC_push_all_stacks: sp not set!\n");
308 # ifdef STACK_GROWS_UP
309 /* We got them backwards! */
310 GC_push_all_stack(hi, lo);
312 GC_push_all_stack(lo, hi);
316 GC_printf3("Reg stack for thread 0x%lx = [%lx,%lx)\n",
317 (unsigned long) p -> id,
318 (unsigned long) bs_lo, (unsigned long) bs_hi);
320 if (pthread_equal(p -> id, me)) {
321 /* FIXME: This may add an unbounded number of entries, */
322 /* and hence overflow the mark stack, which is bad. */
323 GC_push_all_eager(bs_lo, bs_hi);
325 GC_push_all_stack(bs_lo, bs_hi);
330 if (!found_me && !GC_in_thread_creation)
331 ABORT("Collecting from unknown thread.");
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;
339 /* We hold the allocation lock. Suspend all threads that might */
340 /* still be running. Return the number of suspend signals that */
344 int n_live_threads = 0;
348 pthread_t my_thread = pthread_self();
350 GC_stopping_thread = my_thread; /* debugging only. */
351 GC_stopping_pid = getpid(); /* debugging only. */
352 for (i = 0; i < THREAD_TABLE_SZ; i++) {
353 for (p = GC_threads[i]; p != 0; p = p -> next) {
354 if (p -> id != my_thread) {
355 if (p -> flags & FINISHED) continue;
356 if (p -> stop_info.last_stop_count == GC_stop_count) continue;
357 if (p -> thread_blocked) /* Will wait */ continue;
360 GC_printf1("Sending suspend signal to 0x%lx\n", p -> id);
363 result = pthread_kill(p -> id, SIG_SUSPEND);
366 /* Not really there anymore. Possible? */
372 ABORT("pthread_kill failed");
377 return n_live_threads;
380 void lock_stopworld(int);
381 void unlock_stopworld();
383 /* Caller holds allocation lock. */
391 GC_printf1("Stopping the world from 0x%lx\n", pthread_self());
394 /* Make sure all free list construction has stopped before we start. */
395 /* No new construction can start, since free list construction is */
396 /* required to acquire and release the GC lock before it starts, */
397 /* and we have the lock. */
398 # ifdef PARALLEL_MARK
399 GC_acquire_mark_lock();
400 GC_ASSERT(GC_fl_builder_count == 0);
401 /* We should have previously waited for it to become zero. */
402 # endif /* PARALLEL_MARK */
404 GC_world_is_stopped = TRUE;
405 n_live_threads = GC_suspend_all();
407 if (GC_retry_signals) {
408 unsigned long wait_usecs = 0; /* Total wait since retry. */
409 # define WAIT_UNIT 3000
410 # define RETRY_INTERVAL 100000
414 sem_getvalue(&GC_suspend_ack_sem, &ack_count);
415 if (ack_count == n_live_threads) break;
416 if (wait_usecs > RETRY_INTERVAL) {
417 int newly_sent = GC_suspend_all();
420 if (GC_print_stats) {
421 GC_printf1("Resent %ld signals after timeout\n",
425 sem_getvalue(&GC_suspend_ack_sem, &ack_count);
426 if (newly_sent < n_live_threads - ack_count) {
427 WARN("Lost some threads during GC_stop_world?!\n",0);
428 n_live_threads = ack_count + newly_sent;
433 wait_usecs += WAIT_UNIT;
436 for (i = 0; i < n_live_threads; i++) {
437 while (0 != (code = sem_wait(&GC_suspend_ack_sem))) {
438 if (errno != EINTR) {
439 GC_err_printf1("Sem_wait returned %ld\n", (unsigned long)code);
440 ABORT("sem_wait for handler failed");
444 # ifdef PARALLEL_MARK
445 GC_release_mark_lock();
448 GC_printf1("World stopped from 0x%lx\n", pthread_self());
450 GC_stopping_thread = 0; /* debugging only */
453 /* Caller holds allocation lock, and has held it continuously since */
454 /* the world stopped. */
455 void GC_start_world()
457 pthread_t my_thread = pthread_self();
459 register GC_thread p;
460 register int n_live_threads = 0;
462 #ifdef GC_NETBSD_THREADS_WORKAROUND
467 GC_printf0("World starting\n");
470 GC_world_is_stopped = FALSE;
471 for (i = 0; i < THREAD_TABLE_SZ; i++) {
472 for (p = GC_threads[i]; p != 0; p = p -> next) {
473 if (p -> id != my_thread) {
474 if (p -> flags & FINISHED) continue;
475 if (p -> thread_blocked) continue;
478 GC_printf1("Sending restart signal to 0x%lx\n", p -> id);
480 result = pthread_kill(p -> id, SIG_THR_RESTART);
483 /* Not really there anymore. Possible? */
489 ABORT("pthread_kill failed");
494 #ifdef GC_NETBSD_THREADS_WORKAROUND
495 for (i = 0; i < n_live_threads; i++)
496 while (0 != (code = sem_wait(&GC_restart_ack_sem)))
497 if (errno != EINTR) {
498 GC_err_printf1("sem_wait() returned %ld\n", (unsigned long)code);
499 ABORT("sem_wait() for restart handler failed");
504 GC_printf0("World started\n");
508 void GC_stop_init() {
509 struct sigaction act;
511 if (sem_init(&GC_suspend_ack_sem, 0, 0) != 0)
512 ABORT("sem_init failed");
513 #ifdef GC_NETBSD_THREADS_WORKAROUND
514 if (sem_init(&GC_restart_ack_sem, 0, 0) != 0)
515 ABORT("sem_init failed");
518 act.sa_flags = SA_RESTART | SA_SIGINFO;
519 if (sigfillset(&act.sa_mask) != 0) {
520 ABORT("sigfillset() failed");
522 GC_remove_allowed_signals(&act.sa_mask);
523 /* SIG_THR_RESTART is set in the resulting mask. */
524 /* It is unmasked by the handler when necessary. */
525 act.sa_handler = GC_suspend_handler;
526 if (sigaction(SIG_SUSPEND, &act, NULL) != 0) {
527 ABORT("Cannot set SIG_SUSPEND handler");
530 act.sa_handler = GC_restart_handler;
531 if (sigaction(SIG_THR_RESTART, &act, NULL) != 0) {
532 ABORT("Cannot set SIG_THR_RESTART handler");
535 /* Inititialize suspend_handler_mask. It excludes SIG_THR_RESTART. */
536 if (sigfillset(&suspend_handler_mask) != 0) ABORT("sigfillset() failed");
537 GC_remove_allowed_signals(&suspend_handler_mask);
538 if (sigdelset(&suspend_handler_mask, SIG_THR_RESTART) != 0)
539 ABORT("sigdelset() failed");
541 /* Check for GC_RETRY_SIGNALS. */
542 if (0 != GETENV("GC_RETRY_SIGNALS")) {
543 GC_retry_signals = TRUE;
545 if (0 != GETENV("GC_NO_RETRY_SIGNALS")) {
546 GC_retry_signals = FALSE;
549 if (GC_print_stats && GC_retry_signals) {
550 GC_printf0("Will retry suspend signal if necessary.\n");
555 /* Added for cacao */
563 return SIG_THR_RESTART;