3 #include "private/pthread_support.h"
5 #if defined(GC_PTHREADS) && !defined(GC_WIN32_THREADS) && \
6 !defined(GC_DARWIN_THREADS)
12 #include "atomic_ops.h"
18 # define NSIG (MAXSIG+1)
21 # elif defined(__SIGRTMAX)
22 # define NSIG (__SIGRTMAX+1)
28 void GC_print_sig_mask()
33 if (pthread_sigmask(SIG_BLOCK, NULL, &blocked) != 0)
34 ABORT("pthread_sigmask");
35 GC_printf("Blocked: ");
36 for (i = 1; i < NSIG; i++) {
37 if (sigismember(&blocked, i)) { GC_printf("%d ", i); }
44 /* Remove the signals that we want to allow in thread stopping */
45 /* handler from a set. */
46 void GC_remove_allowed_signals(sigset_t *set)
48 if (sigdelset(set, SIGINT) != 0
49 || sigdelset(set, SIGQUIT) != 0
50 || sigdelset(set, SIGABRT) != 0
51 || sigdelset(set, SIGTERM) != 0) {
52 ABORT("sigdelset() failed");
56 /* Handlers write to the thread structure, which is in the heap, */
57 /* and hence can trigger a protection fault. */
58 if (sigdelset(set, SIGSEGV) != 0
60 || sigdelset(set, SIGBUS) != 0
63 ABORT("sigdelset() failed");
68 static sigset_t suspend_handler_mask;
70 volatile AO_t GC_stop_count;
71 /* Incremented at the beginning of GC_stop_world. */
73 volatile AO_t GC_world_is_stopped = FALSE;
74 /* FALSE ==> it is safe for threads to restart, i.e. */
75 /* they will see another suspend signal before they */
76 /* are expected to stop (unless they have voluntarily */
79 #ifdef GC_OSF1_THREADS
80 GC_bool GC_retry_signals = TRUE;
82 GC_bool GC_retry_signals = FALSE;
86 * We use signals to stop threads during GC.
88 * Suspended threads wait in signal handler for SIG_THR_RESTART.
89 * That's more portable than semaphores or condition variables.
90 * (We do use sem_post from a signal handler, but that should be portable.)
92 * The thread suspension signal SIG_SUSPEND is now defined in gc_priv.h.
93 * Note that we can't just stop a thread; we need it to save its stack
94 * pointer(s) and acknowledge.
97 #ifndef SIG_THR_RESTART
98 # if defined(GC_HPUX_THREADS) || defined(GC_OSF1_THREADS) || defined(GC_NETBSD_THREADS)
100 # define SIG_THR_RESTART _SIGRTMIN + 5
102 # define SIG_THR_RESTART SIGRTMIN + 5
105 # define SIG_THR_RESTART SIGXCPU
109 sem_t GC_suspend_ack_sem;
111 #ifdef GC_NETBSD_THREADS
112 # define GC_NETBSD_THREADS_WORKAROUND
113 /* It seems to be necessary to wait until threads have restarted. */
114 /* But it is unclear why that is the case. */
115 sem_t GC_restart_ack_sem;
118 void GC_suspend_handler_inner(ptr_t sig_arg, void *context);
119 /* int cacao_suspendhandler(void *); */
121 #if defined(IA64) || defined(HP_PA) || defined(M68K)
122 void GC_suspend_handler(int sig, siginfo_t *info, void *context)
124 int old_errno = errno;
125 GC_with_callee_saves_pushed(GC_suspend_handler_inner, (ptr_t)(word)sig);
129 /* We believe that in all other cases the full context is already */
130 /* in the signal handler frame. */
131 void GC_suspend_handler(int sig, siginfo_t *info, void *context)
133 int old_errno = errno;
135 /* if (cacao_suspendhandler(uctx)) */
138 GC_suspend_handler_inner((ptr_t)(word)sig, context);
143 void GC_suspend_handler_inner(ptr_t sig_arg, void *context)
145 int sig = (int)(word)sig_arg;
147 pthread_t my_thread = pthread_self();
149 # ifdef PARALLEL_MARK
150 word my_mark_no = GC_mark_no;
151 /* Marker can't proceed until we acknowledge. Thus this is */
152 /* guaranteed to be the mark_no correspending to our */
153 /* suspension, i.e. the marker can't have incremented it yet. */
155 AO_t my_stop_count = AO_load(&GC_stop_count);
157 if (sig != SIG_SUSPEND) ABORT("Bad signal in suspend_handler");
160 GC_printf("Suspending 0x%x\n", (unsigned)my_thread);
163 me = GC_lookup_thread(my_thread);
164 /* The lookup here is safe, since I'm doing this on behalf */
165 /* of a thread which holds the allocation lock in order */
166 /* to stop the world. Thus concurrent modification of the */
167 /* data structure is impossible. */
168 if (me -> stop_info.last_stop_count == my_stop_count) {
169 /* Duplicate signal. OK if we are retrying. */
170 if (!GC_retry_signals) {
171 WARN("Duplicate suspend signal in thread %lx\n",
177 me -> stop_info.stack_ptr = GC_save_regs_in_stack();
179 me -> stop_info.stack_ptr = (ptr_t)(&dummy);
182 me -> backing_store_ptr = GC_save_regs_in_stack();
185 /* Tell the thread that wants to stop the world that this */
186 /* thread has been stopped. Note that sem_post() is */
187 /* the only async-signal-safe primitive in LinuxThreads. */
188 sem_post(&GC_suspend_ack_sem);
189 me -> stop_info.last_stop_count = my_stop_count;
191 /* Wait until that thread tells us to restart by sending */
192 /* this thread a SIG_THR_RESTART signal. */
193 /* SIG_THR_RESTART should be masked at this point. Thus there */
195 /* We do not continue until we receive a SIG_THR_RESTART, */
196 /* but we do not take that as authoritative. (We may be */
197 /* accidentally restarted by one of the user signals we */
198 /* don't block.) After we receive the signal, we use a */
199 /* primitive and expensive mechanism to wait until it's */
200 /* really safe to proceed. Under normal circumstances, */
201 /* this code should not be executed. */
203 sigsuspend (&suspend_handler_mask);
204 } while (AO_load_acquire(&GC_world_is_stopped)
205 && AO_load(&GC_stop_count) == my_stop_count);
206 /* If the RESTART signal gets lost, we can still lose. That should be */
207 /* less likely than losing the SUSPEND signal, since we don't do much */
208 /* between the sem_post and sigsuspend. */
209 /* We'd need more handshaking to work around that. */
210 /* Simply dropping the sigsuspend call should be safe, but is unlikely */
211 /* to be efficient. */
214 GC_printf("Continuing 0x%x\n", (unsigned)my_thread);
218 void GC_restart_handler(int sig)
220 pthread_t my_thread = pthread_self();
223 if (sig != SIG_THR_RESTART) ABORT("Bad signal in suspend_handler");
225 # ifdef GC_NETBSD_THREADS_WORKAROUND
226 sem_post(&GC_restart_ack_sem);
230 ** Note: even if we don't do anything useful here,
231 ** it would still be necessary to have a signal handler,
232 ** rather than ignoring the signals, otherwise
233 ** the signals will not be delivered at all, and
234 ** will thus not interrupt the sigsuspend() above.
238 GC_printf("In GC_restart_handler for 0x%x\n", (unsigned)pthread_self());
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()
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)) {
270 lo = (ptr_t)GC_save_regs_in_stack();
275 IF_IA64(bs_hi = (ptr_t)GC_save_regs_in_stack();)
277 lo = p -> stop_info.stack_ptr;
278 IF_IA64(bs_hi = p -> backing_store_ptr;)
280 if ((p -> flags & MAIN_THREAD) == 0) {
282 IF_IA64(bs_lo = p -> backing_store_end);
284 /* The original stack. */
286 IF_IA64(bs_lo = BACKING_STORE_BASE;)
289 GC_printf("Stack for thread 0x%x = [%p,%p)\n",
290 (unsigned)(p -> id), lo, hi);
292 if (0 == lo) ABORT("GC_push_all_stacks: sp not set!\n");
293 # ifdef STACK_GROWS_UP
294 /* We got them backwards! */
295 GC_push_all_stack(hi, lo);
297 GC_push_all_stack(lo, hi);
301 GC_printf("Reg stack for thread 0x%x = [%lx,%lx)\n",
302 (unsigned)p -> id, bs_lo, bs_hi);
304 if (THREAD_EQUAL(p -> id, me)) {
305 /* FIXME: This may add an unbounded number of entries, */
306 /* and hence overflow the mark stack, which is bad. */
307 GC_push_all_eager(bs_lo, bs_hi);
309 GC_push_all_stack(bs_lo, bs_hi);
314 if (GC_print_stats == VERBOSE) {
315 GC_log_printf("Pushed %d thread stacks\n", nthreads);
317 if (!found_me && !GC_in_thread_creation)
318 ABORT("Collecting from unknown thread.");
321 /* There seems to be a very rare thread stopping problem. To help us */
322 /* debug that, we save the ids of the stopping thread. */
323 pthread_t GC_stopping_thread;
326 /* We hold the allocation lock. Suspend all threads that might */
327 /* still be running. Return the number of suspend signals that */
331 int n_live_threads = 0;
335 pthread_t my_thread = pthread_self();
337 GC_stopping_thread = my_thread; /* debugging only. */
338 GC_stopping_pid = getpid(); /* debugging only. */
339 for (i = 0; i < THREAD_TABLE_SZ; i++) {
340 for (p = GC_threads[i]; p != 0; p = p -> next) {
341 if (!THREAD_EQUAL(p -> id, my_thread)) {
342 if (p -> flags & FINISHED) continue;
343 if (p -> stop_info.last_stop_count == GC_stop_count) continue;
344 if (p -> thread_blocked) /* Will wait */ continue;
347 GC_printf("Sending suspend signal to 0x%x\n",
348 (unsigned)(p -> id));
351 result = pthread_kill(p -> id, SIG_SUSPEND);
354 /* Not really there anymore. Possible? */
360 ABORT("pthread_kill failed");
365 return n_live_threads;
368 void lock_stopworld(int);
369 void unlock_stopworld();
377 GC_ASSERT(I_HOLD_LOCK());
379 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
389 GC_acquire_mark_lock();
390 GC_ASSERT(GC_fl_builder_count == 0);
391 /* We should have previously waited for it to become zero. */
392 # endif /* PARALLEL_MARK */
393 AO_store(&GC_stop_count, GC_stop_count+1);
394 /* Only concurrent reads are possible. */
395 AO_store_release(&GC_world_is_stopped, TRUE);
396 n_live_threads = GC_suspend_all();
398 if (GC_retry_signals) {
399 unsigned long wait_usecs = 0; /* Total wait since retry. */
400 # define WAIT_UNIT 3000
401 # define RETRY_INTERVAL 100000
405 sem_getvalue(&GC_suspend_ack_sem, &ack_count);
406 if (ack_count == n_live_threads) break;
407 if (wait_usecs > RETRY_INTERVAL) {
408 int newly_sent = GC_suspend_all();
410 if (GC_print_stats) {
411 GC_log_printf("Resent %d signals after timeout\n",
414 sem_getvalue(&GC_suspend_ack_sem, &ack_count);
415 if (newly_sent < n_live_threads - ack_count) {
416 WARN("Lost some threads during GC_stop_world?!\n",0);
417 n_live_threads = ack_count + newly_sent;
422 wait_usecs += WAIT_UNIT;
425 for (i = 0; i < n_live_threads; i++) {
427 if (0 != (code = sem_wait(&GC_suspend_ack_sem))) {
428 /* On Linux, sem_wait is documented to always return zero.*/
429 /* But the documentation appears to be incorrect. */
430 if (errno == EINTR) {
431 /* Seems to happen with some versions of gdb. */
434 ABORT("sem_wait for handler failed");
437 # ifdef PARALLEL_MARK
438 GC_release_mark_lock();
441 GC_printf("World stopped from 0x%x\n", (unsigned)pthread_self());
443 GC_stopping_thread = 0; /* debugging only */
446 /* Caller holds allocation lock, and has held it continuously since */
447 /* the world stopped. */
448 void GC_start_world()
450 pthread_t my_thread = pthread_self();
452 register GC_thread p;
453 register int n_live_threads = 0;
455 # ifdef GC_NETBSD_THREADS_WORKAROUND
460 GC_printf("World starting\n");
463 AO_store(&GC_world_is_stopped, FALSE);
464 for (i = 0; i < THREAD_TABLE_SZ; i++) {
465 for (p = GC_threads[i]; p != 0; p = p -> next) {
466 if (!THREAD_EQUAL(p -> id, my_thread)) {
467 if (p -> flags & FINISHED) continue;
468 if (p -> thread_blocked) continue;
471 GC_printf("Sending restart signal to 0x%x\n",
472 (unsigned)(p -> id));
475 result = pthread_kill(p -> id, SIG_THR_RESTART);
478 /* Not really there anymore. Possible? */
484 ABORT("pthread_kill failed");
489 # ifdef GC_NETBSD_THREADS_WORKAROUND
490 for (i = 0; i < n_live_threads; i++)
491 while (0 != (code = sem_wait(&GC_restart_ack_sem)))
492 if (errno != EINTR) {
493 GC_err_printf1("sem_wait() returned %ld\n",
494 (unsigned long)code);
495 ABORT("sem_wait() for restart handler failed");
502 GC_printf("World started\n");
506 void GC_stop_init() {
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 | SA_SIGINFO;
517 if (sigfillset(&act.sa_mask) != 0) {
518 ABORT("sigfillset() failed");
520 GC_remove_allowed_signals(&act.sa_mask);
521 /* SIG_THR_RESTART is set in the resulting mask. */
522 /* It is unmasked by the handler when necessary. */
523 act.sa_sigaction = GC_suspend_handler;
524 if (sigaction(SIG_SUSPEND, &act, NULL) != 0) {
525 ABORT("Cannot set SIG_SUSPEND handler");
528 act.sa_flags &= ~ SA_SIGINFO;
529 act.sa_handler = GC_restart_handler;
530 if (sigaction(SIG_THR_RESTART, &act, NULL) != 0) {
531 ABORT("Cannot set SIG_THR_RESTART handler");
534 /* Inititialize suspend_handler_mask. It excludes SIG_THR_RESTART. */
535 if (sigfillset(&suspend_handler_mask) != 0) ABORT("sigfillset() failed");
536 GC_remove_allowed_signals(&suspend_handler_mask);
537 if (sigdelset(&suspend_handler_mask, SIG_THR_RESTART) != 0)
538 ABORT("sigdelset() failed");
540 /* Check for GC_RETRY_SIGNALS. */
541 if (0 != GETENV("GC_RETRY_SIGNALS")) {
542 GC_retry_signals = TRUE;
544 if (0 != GETENV("GC_NO_RETRY_SIGNALS")) {
545 GC_retry_signals = FALSE;
547 if (GC_print_stats && GC_retry_signals) {
548 GC_log_printf("Will retry suspend signal if necessary.\n");
552 /* Added for cacao */
560 return SIG_THR_RESTART;