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