OSDN Git Service

2005-04-17 David S. Miller <davem@davemloft.net>
[pf3gnuchains/gcc-fork.git] / boehm-gc / pthread_stop_world.c
1 #include "private/pthread_support.h"
2
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)
6
7 #include <signal.h>
8 #include <semaphore.h>
9 #include <errno.h>
10 #include <unistd.h>
11
12 #if DEBUG_THREADS
13
14 #ifndef NSIG
15 # if defined(MAXSIG)
16 #  define NSIG (MAXSIG+1)
17 # elif defined(_NSIG)
18 #  define NSIG _NSIG
19 # elif defined(__SIGRTMAX)
20 #  define NSIG (__SIGRTMAX+1)
21 # else
22   --> please fix it
23 # endif
24 #endif
25
26 void GC_print_sig_mask()
27 {
28     sigset_t blocked;
29     int i;
30
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); }
36     }
37     GC_printf0("\n");
38 }
39
40 #endif
41
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)
45 {
46 #   ifdef NO_SIGNALS
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");
52       }
53 #   endif
54
55 #   ifdef MPROTECT_VDB
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
59 #         ifdef SIGBUS
60             || sigdelset(set, SIGBUS) != 0
61 #         endif
62           ) {
63         ABORT("sigdelset() failed");
64       }
65 #   endif
66 }
67
68 static sigset_t suspend_handler_mask;
69
70 word GC_stop_count;     /* Incremented at the beginning of GC_stop_world. */
71
72 #ifdef GC_OSF1_THREADS
73   GC_bool GC_retry_signals = TRUE;
74 #else
75   GC_bool GC_retry_signals = FALSE;
76 #endif
77
78 /*
79  * We use signals to stop threads during GC.
80  * 
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.)
84  *
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.
88  */
89
90 #ifndef SIG_THR_RESTART
91 #  if defined(GC_HPUX_THREADS) || defined(GC_OSF1_THREADS)
92 #    ifdef _SIGRTMIN
93 #      define SIG_THR_RESTART _SIGRTMIN + 5
94 #    else
95 #      define SIG_THR_RESTART SIGRTMIN + 5
96 #    endif
97 #  else
98 #   define SIG_THR_RESTART SIGXCPU
99 #  endif
100 #endif
101
102 sem_t GC_suspend_ack_sem;
103
104 void GC_suspend_handler_inner(ptr_t sig_arg);
105
106 #if defined(IA64) || defined(HP_PA)
107 extern void GC_with_callee_saves_pushed();
108
109 void GC_suspend_handler(int sig)
110 {
111   GC_with_callee_saves_pushed(GC_suspend_handler_inner, (ptr_t)(word)sig);
112 }
113
114 #else
115 /* We believe that in all other cases the full context is already       */
116 /* in the signal handler frame.                                         */
117 void GC_suspend_handler(int sig)
118 {
119   GC_suspend_handler_inner((ptr_t)(word)sig);
120 }
121 #endif
122
123 void GC_suspend_handler_inner(ptr_t sig_arg)
124 {
125     int sig = (int)(word)sig_arg;
126     int dummy;
127     pthread_t my_thread = pthread_self();
128     GC_thread me;
129 #   ifdef PARALLEL_MARK
130         word my_mark_no = GC_mark_no;
131         /* Marker can't proceed until we acknowledge.  Thus this is     */
132         /* guaranteed to be the mark_no correspending to our            */
133         /* suspension, i.e. the marker can't have incremented it yet.   */
134 #   endif
135     word my_stop_count = GC_stop_count;
136
137     if (sig != SIG_SUSPEND) ABORT("Bad signal in suspend_handler");
138
139 #if DEBUG_THREADS
140     GC_printf1("Suspending 0x%lx\n", my_thread);
141 #endif
142
143     me = GC_lookup_thread(my_thread);
144     /* The lookup here is safe, since I'm doing this on behalf  */
145     /* of a thread which holds the allocation lock in order     */
146     /* to stop the world.  Thus concurrent modification of the  */
147     /* data structure is impossible.                            */
148     if (me -> stop_info.last_stop_count == my_stop_count) {
149         /* Duplicate signal.  OK if we are retrying.    */
150         if (!GC_retry_signals) {
151             WARN("Duplicate suspend signal in thread %lx\n",
152                  pthread_self());
153         }
154         return;
155     }
156 #   ifdef SPARC
157         me -> stop_info.stack_ptr = (ptr_t)GC_save_regs_in_stack();
158 #   else
159         me -> stop_info.stack_ptr = (ptr_t)(&dummy);
160 #   endif
161 #   ifdef IA64
162         me -> backing_store_ptr = (ptr_t)GC_save_regs_in_stack();
163 #   endif
164
165     /* Tell the thread that wants to stop the world that this   */
166     /* thread has been stopped.  Note that sem_post() is        */
167     /* the only async-signal-safe primitive in LinuxThreads.    */
168     sem_post(&GC_suspend_ack_sem);
169     me -> stop_info.last_stop_count = my_stop_count;
170
171     /* Wait until that thread tells us to restart by sending    */
172     /* this thread a SIG_THR_RESTART signal.                    */
173     /* SIG_THR_RESTART should be masked at this point.  Thus there      */
174     /* is no race.                                              */
175     do {
176             me->stop_info.signal = 0;
177             sigsuspend(&suspend_handler_mask);        /* Wait for signal */
178     } while (me->stop_info.signal != SIG_THR_RESTART);
179     /* If the RESTART signal gets lost, we can still lose.  That should be  */
180     /* less likely than losing the SUSPEND signal, since we don't do much   */
181     /* between the sem_post and sigsuspend.                                 */
182     /* We'd need more handshaking to work around that, since we don't want  */
183     /* to accidentally leave a RESTART signal pending, thus causing us to   */
184     /* continue prematurely in a future round.                              */ 
185
186 #if DEBUG_THREADS
187     GC_printf1("Continuing 0x%lx\n", my_thread);
188 #endif
189 }
190
191 void GC_restart_handler(int sig)
192 {
193     pthread_t my_thread = pthread_self();
194     GC_thread me;
195
196     if (sig != SIG_THR_RESTART) ABORT("Bad signal in suspend_handler");
197
198     /* Let the GC_suspend_handler() know that we got a SIG_THR_RESTART. */
199     /* The lookup here is safe, since I'm doing this on behalf  */
200     /* of a thread which holds the allocation lock in order     */
201     /* to stop the world.  Thus concurrent modification of the  */
202     /* data structure is impossible.                            */
203     me = GC_lookup_thread(my_thread);
204     me->stop_info.signal = SIG_THR_RESTART;
205
206     /*
207     ** Note: even if we didn't do anything useful here,
208     ** it would still be necessary to have a signal handler,
209     ** rather than ignoring the signals, otherwise
210     ** the signals will not be delivered at all, and
211     ** will thus not interrupt the sigsuspend() above.
212     */
213
214 #if DEBUG_THREADS
215     GC_printf1("In GC_restart_handler for 0x%lx\n", pthread_self());
216 #endif
217 }
218
219 # ifdef IA64
220 #   define IF_IA64(x) x
221 # else
222 #   define IF_IA64(x)
223 # endif
224 /* We hold allocation lock.  Should do exactly the right thing if the   */
225 /* world is stopped.  Should not fail if it isn't.                      */
226 void GC_push_all_stacks()
227 {
228     GC_bool found_me = FALSE;
229     int i;
230     GC_thread p;
231     ptr_t lo, hi;
232     /* On IA64, we also need to scan the register backing store. */
233     IF_IA64(ptr_t bs_lo; ptr_t bs_hi;)
234     pthread_t me = pthread_self();
235     
236     if (!GC_thr_initialized) GC_thr_init();
237     #if DEBUG_THREADS
238         GC_printf1("Pushing stacks from thread 0x%lx\n", (unsigned long) me);
239     #endif
240     for (i = 0; i < THREAD_TABLE_SZ; i++) {
241       for (p = GC_threads[i]; p != 0; p = p -> next) {
242         if (p -> flags & FINISHED) continue;
243         if (pthread_equal(p -> id, me)) {
244 #           ifdef SPARC
245                 lo = (ptr_t)GC_save_regs_in_stack();
246 #           else
247                 lo = GC_approx_sp();
248 #           endif
249             found_me = TRUE;
250             IF_IA64(bs_hi = (ptr_t)GC_save_regs_in_stack();)
251         } else {
252             lo = p -> stop_info.stack_ptr;
253             IF_IA64(bs_hi = p -> backing_store_ptr;)
254         }
255         if ((p -> flags & MAIN_THREAD) == 0) {
256             hi = p -> stack_end;
257             IF_IA64(bs_lo = p -> backing_store_end);
258         } else {
259             /* The original stack. */
260             hi = GC_stackbottom;
261             IF_IA64(bs_lo = BACKING_STORE_BASE;)
262         }
263         #if DEBUG_THREADS
264             GC_printf3("Stack for thread 0x%lx = [%lx,%lx)\n",
265                 (unsigned long) p -> id,
266                 (unsigned long) lo, (unsigned long) hi);
267         #endif
268         if (0 == lo) ABORT("GC_push_all_stacks: sp not set!\n");
269 #       ifdef STACK_GROWS_UP
270           /* We got them backwards! */
271           GC_push_all_stack(hi, lo);
272 #       else
273           GC_push_all_stack(lo, hi);
274 #       endif
275 #       ifdef IA64
276 #         if DEBUG_THREADS
277             GC_printf3("Reg stack for thread 0x%lx = [%lx,%lx)\n",
278                 (unsigned long) p -> id,
279                 (unsigned long) bs_lo, (unsigned long) bs_hi);
280 #         endif
281           if (pthread_equal(p -> id, me)) {
282             GC_push_all_eager(bs_lo, bs_hi);
283           } else {
284             GC_push_all_stack(bs_lo, bs_hi);
285           }
286 #       endif
287       }
288     }
289     if (!found_me && !GC_in_thread_creation)
290       ABORT("Collecting from unknown thread.");
291 }
292
293 /* There seems to be a very rare thread stopping problem.  To help us  */
294 /* debug that, we save the ids of the stopping thread. */
295 pthread_t GC_stopping_thread;
296 int GC_stopping_pid;
297
298 /* We hold the allocation lock.  Suspend all threads that might */
299 /* still be running.  Return the number of suspend signals that */
300 /* were sent. */
301 int GC_suspend_all()
302 {
303     int n_live_threads = 0;
304     int i;
305     GC_thread p;
306     int result;
307     pthread_t my_thread = pthread_self();
308     
309     GC_stopping_thread = my_thread;    /* debugging only.      */
310     GC_stopping_pid = getpid();                /* debugging only.      */
311     for (i = 0; i < THREAD_TABLE_SZ; i++) {
312       for (p = GC_threads[i]; p != 0; p = p -> next) {
313         if (p -> id != my_thread) {
314             if (p -> flags & FINISHED) continue;
315             if (p -> stop_info.last_stop_count == GC_stop_count) continue;
316             if (p -> thread_blocked) /* Will wait */ continue;
317             n_live_threads++;
318             #if DEBUG_THREADS
319               GC_printf1("Sending suspend signal to 0x%lx\n", p -> id);
320             #endif
321         
322         result = pthread_kill(p -> id, SIG_SUSPEND);
323             switch(result) {
324                 case ESRCH:
325                     /* Not really there anymore.  Possible? */
326                     n_live_threads--;
327                     break;
328                 case 0:
329                     break;
330                 default:
331                     ABORT("pthread_kill failed");
332             }
333         }
334       }
335     }
336     return n_live_threads;
337 }
338
339 /* Caller holds allocation lock.        */
340 void GC_stop_world()
341 {
342     int i;
343     int n_live_threads;
344     int code;
345
346     #if DEBUG_THREADS
347     GC_printf1("Stopping the world from 0x%lx\n", pthread_self());
348     #endif
349        
350     /* Make sure all free list construction has stopped before we start. */
351     /* No new construction can start, since free list construction is   */
352     /* required to acquire and release the GC lock before it starts,    */
353     /* and we have the lock.                                            */
354 #   ifdef PARALLEL_MARK
355       GC_acquire_mark_lock();
356       GC_ASSERT(GC_fl_builder_count == 0);
357       /* We should have previously waited for it to become zero. */
358 #   endif /* PARALLEL_MARK */
359     ++GC_stop_count;
360     n_live_threads = GC_suspend_all();
361
362       if (GC_retry_signals) {
363           unsigned long wait_usecs = 0;  /* Total wait since retry.     */
364 #         define WAIT_UNIT 3000
365 #         define RETRY_INTERVAL 100000
366           for (;;) {
367               int ack_count;
368
369               sem_getvalue(&GC_suspend_ack_sem, &ack_count);
370               if (ack_count == n_live_threads) break;
371               if (wait_usecs > RETRY_INTERVAL) {
372                   int newly_sent = GC_suspend_all();
373
374 #                 ifdef CONDPRINT
375                     if (GC_print_stats) {
376                       GC_printf1("Resent %ld signals after timeout\n",
377                                  newly_sent);
378                     }
379 #                 endif
380                   sem_getvalue(&GC_suspend_ack_sem, &ack_count);
381                   if (newly_sent < n_live_threads - ack_count) {
382                       WARN("Lost some threads during GC_stop_world?!\n",0);
383                       n_live_threads = ack_count + newly_sent;
384                   }
385                   wait_usecs = 0;
386               }
387               usleep(WAIT_UNIT);
388               wait_usecs += WAIT_UNIT;
389           }
390       }
391     for (i = 0; i < n_live_threads; i++) {
392           while (0 != (code = sem_wait(&GC_suspend_ack_sem))) {
393             if (errno != EINTR) {
394               GC_err_printf1("Sem_wait returned %ld\n", (unsigned long)code);
395               ABORT("sem_wait for handler failed");
396             }
397           }
398     }
399 #   ifdef PARALLEL_MARK
400       GC_release_mark_lock();
401 #   endif
402     #if DEBUG_THREADS
403       GC_printf1("World stopped from 0x%lx\n", pthread_self());
404     #endif
405     GC_stopping_thread = 0;  /* debugging only */
406 }
407
408 /* Caller holds allocation lock, and has held it continuously since     */
409 /* the world stopped.                                                   */
410 void GC_start_world()
411 {
412     pthread_t my_thread = pthread_self();
413     register int i;
414     register GC_thread p;
415     register int n_live_threads = 0;
416     register int result;
417
418 #   if DEBUG_THREADS
419       GC_printf0("World starting\n");
420 #   endif
421
422     for (i = 0; i < THREAD_TABLE_SZ; i++) {
423       for (p = GC_threads[i]; p != 0; p = p -> next) {
424         if (p -> id != my_thread) {
425             if (p -> flags & FINISHED) continue;
426             if (p -> thread_blocked) continue;
427             n_live_threads++;
428             #if DEBUG_THREADS
429               GC_printf1("Sending restart signal to 0x%lx\n", p -> id);
430             #endif
431         
432         result = pthread_kill(p -> id, SIG_THR_RESTART);
433             switch(result) {
434                 case ESRCH:
435                     /* Not really there anymore.  Possible? */
436                     n_live_threads--;
437                     break;
438                 case 0:
439                     break;
440                 default:
441                     ABORT("pthread_kill failed");
442             }
443         }
444       }
445     }
446     #if DEBUG_THREADS
447       GC_printf0("World started\n");
448     #endif
449 }
450
451 void GC_stop_init() {
452     struct sigaction act;
453     
454     if (sem_init(&GC_suspend_ack_sem, 0, 0) != 0)
455         ABORT("sem_init failed");
456
457     act.sa_flags = SA_RESTART;
458     if (sigfillset(&act.sa_mask) != 0) {
459         ABORT("sigfillset() failed");
460     }
461     GC_remove_allowed_signals(&act.sa_mask);
462     /* SIG_THR_RESTART is set in the resulting mask.            */
463     /* It is unmasked by the handler when necessary.            */
464     act.sa_handler = GC_suspend_handler;
465     if (sigaction(SIG_SUSPEND, &act, NULL) != 0) {
466         ABORT("Cannot set SIG_SUSPEND handler");
467     }
468
469     act.sa_handler = GC_restart_handler;
470     if (sigaction(SIG_THR_RESTART, &act, NULL) != 0) {
471         ABORT("Cannot set SIG_THR_RESTART handler");
472     }
473
474     /* Inititialize suspend_handler_mask. It excludes SIG_THR_RESTART. */
475       if (sigfillset(&suspend_handler_mask) != 0) ABORT("sigfillset() failed");
476       GC_remove_allowed_signals(&suspend_handler_mask);
477       if (sigdelset(&suspend_handler_mask, SIG_THR_RESTART) != 0)
478           ABORT("sigdelset() failed");
479
480     /* Check for GC_RETRY_SIGNALS.      */
481       if (0 != GETENV("GC_RETRY_SIGNALS")) {
482           GC_retry_signals = TRUE;
483       }
484       if (0 != GETENV("GC_NO_RETRY_SIGNALS")) {
485           GC_retry_signals = FALSE;
486       }
487 #     ifdef CONDPRINT
488           if (GC_print_stats && GC_retry_signals) {
489               GC_printf0("Will retry suspend signal if necessary.\n");
490           }
491 #     endif
492 }
493
494 #endif