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)
16 # define NSIG (MAXSIG+1)
19 # elif defined(__SIGRTMAX)
20 # define NSIG (__SIGRTMAX+1)
26 void GC_print_sig_mask()
31 if (pthread_sigmask(SIG_BLOCK
, NULL
, &blocked
) != 0)
32 ABORT("pthread_sigmask");
33 GC_printf0("Blocked: ");
34 for (i
= 1; i
< NSIG
; i
++) {
35 if (sigismember(&blocked
, i
)) { GC_printf1("%ld ",(long) i
); }
42 /* Remove the signals that we want to allow in thread stopping */
43 /* handler from a set. */
44 void GC_remove_allowed_signals(sigset_t
*set
)
47 if (sigdelset(set
, SIGINT
) != 0
48 || sigdelset(set
, SIGQUIT
) != 0
49 || sigdelset(set
, SIGABRT
) != 0
50 || sigdelset(set
, SIGTERM
) != 0) {
51 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 word GC_stop_count
; /* Incremented at the beginning of GC_stop_world. */
72 #ifdef GC_OSF1_THREADS
73 GC_bool GC_retry_signals
= TRUE
;
75 GC_bool GC_retry_signals
= FALSE
;
79 * We use signals to stop threads during GC.
81 * Suspended threads wait in signal handler for SIG_THR_RESTART.
82 * That's more portable than semaphores or condition variables.
83 * (We do use sem_post from a signal handler, but that should be portable.)
85 * The thread suspension signal SIG_SUSPEND is now defined in gc_priv.h.
86 * Note that we can't just stop a thread; we need it to save its stack
87 * pointer(s) and acknowledge.
90 #ifndef SIG_THR_RESTART
91 # if defined(GC_HPUX_THREADS) || defined(GC_OSF1_THREADS)
93 # define SIG_THR_RESTART _SIGRTMIN + 5
95 # define SIG_THR_RESTART SIGRTMIN + 5
98 # define SIG_THR_RESTART SIGXCPU
102 sem_t GC_suspend_ack_sem
;
104 void GC_suspend_handler(int sig
)
107 pthread_t my_thread
= pthread_self();
109 # ifdef PARALLEL_MARK
110 word my_mark_no
= GC_mark_no
;
111 /* Marker can't proceed until we acknowledge. Thus this is */
112 /* guaranteed to be the mark_no correspending to our */
113 /* suspension, i.e. the marker can't have incremented it yet. */
115 word my_stop_count
= GC_stop_count
;
117 if (sig
!= SIG_SUSPEND
) ABORT("Bad signal in suspend_handler");
120 GC_printf1("Suspending 0x%lx\n", my_thread
);
123 me
= GC_lookup_thread(my_thread
);
124 /* The lookup here is safe, since I'm doing this on behalf */
125 /* of a thread which holds the allocation lock in order */
126 /* to stop the world. Thus concurrent modification of the */
127 /* data structure is impossible. */
128 if (me
-> stop_info
.last_stop_count
== my_stop_count
) {
129 /* Duplicate signal. OK if we are retrying. */
130 if (!GC_retry_signals
) {
131 WARN("Duplicate suspend signal in thread %lx\n",
137 me
-> stop_info
.stack_ptr
= (ptr_t
)GC_save_regs_in_stack();
139 me
-> stop_info
.stack_ptr
= (ptr_t
)(&dummy
);
142 me
-> backing_store_ptr
= (ptr_t
)GC_save_regs_in_stack();
145 /* Tell the thread that wants to stop the world that this */
146 /* thread has been stopped. Note that sem_post() is */
147 /* the only async-signal-safe primitive in LinuxThreads. */
148 sem_post(&GC_suspend_ack_sem
);
149 me
-> stop_info
.last_stop_count
= my_stop_count
;
151 /* Wait until that thread tells us to restart by sending */
152 /* this thread a SIG_THR_RESTART signal. */
153 /* SIG_THR_RESTART should be masked at this point. Thus there */
156 me
->stop_info
.signal
= 0;
157 sigsuspend(&suspend_handler_mask
); /* Wait for signal */
158 } while (me
->stop_info
.signal
!= SIG_THR_RESTART
);
159 /* If the RESTART signal gets lost, we can still lose. That should be */
160 /* less likely than losing the SUSPEND signal, since we don't do much */
161 /* between the sem_post and sigsuspend. */
162 /* We'd need more handshaking to work around that, since we don't want */
163 /* to accidentally leave a RESTART signal pending, thus causing us to */
164 /* continue prematurely in a future round. */
167 GC_printf1("Continuing 0x%lx\n", my_thread
);
171 void GC_restart_handler(int sig
)
173 pthread_t my_thread
= pthread_self();
176 if (sig
!= SIG_THR_RESTART
) ABORT("Bad signal in suspend_handler");
178 /* Let the GC_suspend_handler() know that we got a SIG_THR_RESTART. */
179 /* The lookup here is safe, since I'm doing this on behalf */
180 /* of a thread which holds the allocation lock in order */
181 /* to stop the world. Thus concurrent modification of the */
182 /* data structure is impossible. */
183 me
= GC_lookup_thread(my_thread
);
184 me
->stop_info
.signal
= SIG_THR_RESTART
;
187 ** Note: even if we didn't do anything useful here,
188 ** it would still be necessary to have a signal handler,
189 ** rather than ignoring the signals, otherwise
190 ** the signals will not be delivered at all, and
191 ** will thus not interrupt the sigsuspend() above.
195 GC_printf1("In GC_restart_handler for 0x%lx\n", pthread_self());
200 # define IF_IA64(x) x
204 /* We hold allocation lock. Should do exactly the right thing if the */
205 /* world is stopped. Should not fail if it isn't. */
206 void GC_push_all_stacks()
208 GC_bool found_me
= FALSE
;
212 /* On IA64, we also need to scan the register backing store. */
213 IF_IA64(ptr_t bs_lo
; ptr_t bs_hi
;)
214 pthread_t me
= pthread_self();
216 if (!GC_thr_initialized
) GC_thr_init();
218 GC_printf1("Pushing stacks from thread 0x%lx\n", (unsigned long) me
);
220 for (i
= 0; i
< THREAD_TABLE_SZ
; i
++) {
221 for (p
= GC_threads
[i
]; p
!= 0; p
= p
-> next
) {
222 if (p
-> flags
& FINISHED
) continue;
223 if (pthread_equal(p
-> id
, me
)) {
225 lo
= (ptr_t
)GC_save_regs_in_stack();
230 IF_IA64(bs_hi
= (ptr_t
)GC_save_regs_in_stack();)
232 lo
= p
-> stop_info
.stack_ptr
;
233 IF_IA64(bs_hi
= p
-> backing_store_ptr
;)
235 if ((p
-> flags
& MAIN_THREAD
) == 0) {
237 IF_IA64(bs_lo
= p
-> backing_store_end
);
239 /* The original stack. */
241 IF_IA64(bs_lo
= BACKING_STORE_BASE
;)
244 GC_printf3("Stack for thread 0x%lx = [%lx,%lx)\n",
245 (unsigned long) p
-> id
,
246 (unsigned long) lo
, (unsigned long) hi
);
248 if (0 == lo
) ABORT("GC_push_all_stacks: sp not set!\n");
249 # ifdef STACK_GROWS_UP
250 /* We got them backwards! */
251 GC_push_all_stack(hi
, lo
);
253 GC_push_all_stack(lo
, hi
);
257 GC_printf3("Reg stack for thread 0x%lx = [%lx,%lx)\n",
258 (unsigned long) p
-> id
,
259 (unsigned long) bs_lo
, (unsigned long) bs_hi
);
261 if (pthread_equal(p
-> id
, me
)) {
262 GC_push_all_eager(bs_lo
, bs_hi
);
264 GC_push_all_stack(bs_lo
, bs_hi
);
269 if (!found_me
&& !GC_in_thread_creation
)
270 ABORT("Collecting from unknown thread.");
273 /* There seems to be a very rare thread stopping problem. To help us */
274 /* debug that, we save the ids of the stopping thread. */
275 pthread_t GC_stopping_thread
;
278 /* We hold the allocation lock. Suspend all threads that might */
279 /* still be running. Return the number of suspend signals that */
283 int n_live_threads
= 0;
287 pthread_t my_thread
= pthread_self();
289 GC_stopping_thread
= my_thread
; /* debugging only. */
290 GC_stopping_pid
= getpid(); /* debugging only. */
291 for (i
= 0; i
< THREAD_TABLE_SZ
; i
++) {
292 for (p
= GC_threads
[i
]; p
!= 0; p
= p
-> next
) {
293 if (p
-> id
!= my_thread
) {
294 if (p
-> flags
& FINISHED
) continue;
295 if (p
-> stop_info
.last_stop_count
== GC_stop_count
) continue;
296 if (p
-> thread_blocked
) /* Will wait */ continue;
299 GC_printf1("Sending suspend signal to 0x%lx\n", p
-> id
);
302 result
= pthread_kill(p
-> id
, SIG_SUSPEND
);
305 /* Not really there anymore. Possible? */
311 ABORT("pthread_kill failed");
316 return n_live_threads
;
319 /* Caller holds allocation lock. */
327 GC_printf1("Stopping the world from 0x%lx\n", pthread_self());
330 /* Make sure all free list construction has stopped before we start. */
331 /* No new construction can start, since free list construction is */
332 /* required to acquire and release the GC lock before it starts, */
333 /* and we have the lock. */
334 # ifdef PARALLEL_MARK
335 GC_acquire_mark_lock();
336 GC_ASSERT(GC_fl_builder_count
== 0);
337 /* We should have previously waited for it to become zero. */
338 # endif /* PARALLEL_MARK */
340 n_live_threads
= GC_suspend_all();
342 if (GC_retry_signals
) {
343 unsigned long wait_usecs
= 0; /* Total wait since retry. */
344 # define WAIT_UNIT 3000
345 # define RETRY_INTERVAL 100000
349 sem_getvalue(&GC_suspend_ack_sem
, &ack_count
);
350 if (ack_count
== n_live_threads
) break;
351 if (wait_usecs
> RETRY_INTERVAL
) {
352 int newly_sent
= GC_suspend_all();
355 if (GC_print_stats
) {
356 GC_printf1("Resent %ld signals after timeout\n",
360 sem_getvalue(&GC_suspend_ack_sem
, &ack_count
);
361 if (newly_sent
< n_live_threads
- ack_count
) {
362 WARN("Lost some threads during GC_stop_world?!\n",0);
363 n_live_threads
= ack_count
+ newly_sent
;
368 wait_usecs
+= WAIT_UNIT
;
371 for (i
= 0; i
< n_live_threads
; i
++) {
372 if (0 != (code
= sem_wait(&GC_suspend_ack_sem
))) {
373 GC_err_printf1("Sem_wait returned %ld\n", (unsigned long)code
);
374 ABORT("sem_wait for handler failed");
377 # ifdef PARALLEL_MARK
378 GC_release_mark_lock();
381 GC_printf1("World stopped from 0x%lx\n", pthread_self());
383 GC_stopping_thread
= 0; /* debugging only */
386 /* Caller holds allocation lock, and has held it continuously since */
387 /* the world stopped. */
388 void GC_start_world()
390 pthread_t my_thread
= pthread_self();
392 register GC_thread p
;
393 register int n_live_threads
= 0;
397 GC_printf0("World starting\n");
400 for (i
= 0; i
< THREAD_TABLE_SZ
; i
++) {
401 for (p
= GC_threads
[i
]; p
!= 0; p
= p
-> next
) {
402 if (p
-> id
!= my_thread
) {
403 if (p
-> flags
& FINISHED
) continue;
404 if (p
-> thread_blocked
) continue;
407 GC_printf1("Sending restart signal to 0x%lx\n", p
-> id
);
410 result
= pthread_kill(p
-> id
, SIG_THR_RESTART
);
413 /* Not really there anymore. Possible? */
419 ABORT("pthread_kill failed");
425 GC_printf0("World started\n");
429 void GC_stop_init() {
430 struct sigaction act
;
432 if (sem_init(&GC_suspend_ack_sem
, 0, 0) != 0)
433 ABORT("sem_init failed");
435 act
.sa_flags
= SA_RESTART
;
436 if (sigfillset(&act
.sa_mask
) != 0) {
437 ABORT("sigfillset() failed");
439 GC_remove_allowed_signals(&act
.sa_mask
);
440 /* SIG_THR_RESTART is set in the resulting mask. */
441 /* It is unmasked by the handler when necessary. */
442 act
.sa_handler
= GC_suspend_handler
;
443 if (sigaction(SIG_SUSPEND
, &act
, NULL
) != 0) {
444 ABORT("Cannot set SIG_SUSPEND handler");
447 act
.sa_handler
= GC_restart_handler
;
448 if (sigaction(SIG_THR_RESTART
, &act
, NULL
) != 0) {
449 ABORT("Cannot set SIG_THR_RESTART handler");
452 /* Inititialize suspend_handler_mask. It excludes SIG_THR_RESTART. */
453 if (sigfillset(&suspend_handler_mask
) != 0) ABORT("sigfillset() failed");
454 GC_remove_allowed_signals(&suspend_handler_mask
);
455 if (sigdelset(&suspend_handler_mask
, SIG_THR_RESTART
) != 0)
456 ABORT("sigdelset() failed");
458 /* Check for GC_RETRY_SIGNALS. */
459 if (0 != GETENV("GC_RETRY_SIGNALS")) {
460 GC_retry_signals
= TRUE
;
462 if (0 != GETENV("GC_NO_RETRY_SIGNALS")) {
463 GC_retry_signals
= FALSE
;
466 if (GC_print_stats
&& GC_retry_signals
) {
467 GC_printf0("Will retry suspend signal if necessary.\n");