undo change in mini.c
[mono-project.git] / libgc / pthread_stop_world.c
blobf93ce26b56280191117b967b6d896124bbc3d761
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)
7 #include <signal.h>
8 #include <semaphore.h>
9 #include <errno.h>
10 #include <unistd.h>
11 #include <sys/time.h>
13 /* work around a dlopen issue (bug #75390), undefs to avoid warnings with redefinitions */
14 #undef PACKAGE_BUGREPORT
15 #undef PACKAGE_NAME
16 #undef PACKAGE_STRING
17 #undef PACKAGE_TARNAME
18 #undef PACKAGE_VERSION
19 #include "mono/utils/mono-compiler.h"
21 #ifdef NACL
22 volatile int __nacl_thread_suspension_needed = 0;
23 pthread_t nacl_thread_parker = -1;
25 volatile int nacl_thread_parked[MAX_NACL_GC_THREADS];
26 volatile int nacl_thread_used[MAX_NACL_GC_THREADS];
27 volatile int nacl_thread_parking_inited = 0;
28 volatile int nacl_num_gc_threads = 0;
29 pthread_mutex_t nacl_thread_alloc_lock = PTHREAD_MUTEX_INITIALIZER;
30 __thread int nacl_thread_idx = -1;
31 __thread GC_thread nacl_gc_thread_self = NULL;
32 #endif
34 #if DEBUG_THREADS
36 #ifndef NSIG
37 # if defined(MAXSIG)
38 # define NSIG (MAXSIG+1)
39 # elif defined(_NSIG)
40 # define NSIG _NSIG
41 # elif defined(__SIGRTMAX)
42 # define NSIG (__SIGRTMAX+1)
43 # else
44 --> please fix it
45 # endif
46 #endif
48 #ifndef NACL
49 void GC_print_sig_mask()
51 sigset_t blocked;
52 int i;
54 if (pthread_sigmask(SIG_BLOCK, NULL, &blocked) != 0)
55 ABORT("pthread_sigmask");
56 GC_printf0("Blocked: ");
57 for (i = 1; i < NSIG; i++) {
58 if (sigismember(&blocked, i)) { GC_printf1("%ld ",(long) i); }
60 GC_printf0("\n");
62 #endif /* NACL */
63 #endif
65 /* Remove the signals that we want to allow in thread stopping */
66 /* handler from a set. */
67 void GC_remove_allowed_signals(sigset_t *set)
69 # ifdef NO_SIGNALS
70 if (sigdelset(set, SIGINT) != 0
71 || sigdelset(set, SIGQUIT) != 0
72 || sigdelset(set, SIGABRT) != 0
73 || sigdelset(set, SIGTERM) != 0) {
74 ABORT("sigdelset() failed");
76 # endif
78 # ifdef MPROTECT_VDB
79 /* Handlers write to the thread structure, which is in the heap, */
80 /* and hence can trigger a protection fault. */
81 if (sigdelset(set, SIGSEGV) != 0
82 # ifdef SIGBUS
83 || sigdelset(set, SIGBUS) != 0
84 # endif
85 ) {
86 ABORT("sigdelset() failed");
88 # endif
91 static sigset_t suspend_handler_mask;
93 word GC_stop_count; /* Incremented at the beginning of GC_stop_world. */
95 #ifdef GC_OSF1_THREADS
96 GC_bool GC_retry_signals = TRUE;
97 #else
98 GC_bool GC_retry_signals = FALSE;
99 #endif
102 * We use signals to stop threads during GC.
104 * Suspended threads wait in signal handler for SIG_THR_RESTART.
105 * That's more portable than semaphores or condition variables.
106 * (We do use sem_post from a signal handler, but that should be portable.)
108 * The thread suspension signal SIG_SUSPEND is now defined in gc_priv.h.
109 * Note that we can't just stop a thread; we need it to save its stack
110 * pointer(s) and acknowledge.
113 #ifndef SIG_THR_RESTART
114 # if defined(GC_HPUX_THREADS) || defined(GC_OSF1_THREADS)
115 # ifdef _SIGRTMIN
116 # define SIG_THR_RESTART _SIGRTMIN + 5
117 # else
118 # define SIG_THR_RESTART SIGRTMIN + 5
119 # endif
120 # else
121 # define SIG_THR_RESTART SIGXCPU
122 # endif
123 #endif
125 sem_t GC_suspend_ack_sem;
127 static void _GC_suspend_handler(int sig)
129 #ifndef NACL
130 int dummy;
131 pthread_t my_thread = pthread_self();
132 GC_thread me;
133 # ifdef PARALLEL_MARK
134 word my_mark_no = GC_mark_no;
135 /* Marker can't proceed until we acknowledge. Thus this is */
136 /* guaranteed to be the mark_no correspending to our */
137 /* suspension, i.e. the marker can't have incremented it yet. */
138 # endif
139 word my_stop_count = GC_stop_count;
141 if (sig != SIG_SUSPEND) ABORT("Bad signal in suspend_handler");
143 #if DEBUG_THREADS
144 GC_printf1("Suspending 0x%lx\n", my_thread);
145 #endif
147 me = GC_lookup_thread(my_thread);
148 /* The lookup here is safe, since I'm doing this on behalf */
149 /* of a thread which holds the allocation lock in order */
150 /* to stop the world. Thus concurrent modification of the */
151 /* data structure is impossible. */
152 if (me -> stop_info.last_stop_count == my_stop_count) {
153 /* Duplicate signal. OK if we are retrying. */
154 if (!GC_retry_signals) {
155 WARN("Duplicate suspend signal in thread %lx\n",
156 pthread_self());
158 return;
160 # ifdef SPARC
161 me -> stop_info.stack_ptr = (ptr_t)GC_save_regs_in_stack();
162 # else
163 me -> stop_info.stack_ptr = (ptr_t)(&dummy);
164 # endif
165 # ifdef IA64
166 me -> backing_store_ptr = (ptr_t)GC_save_regs_in_stack();
167 # endif
169 /* Tell the thread that wants to stop the world that this */
170 /* thread has been stopped. Note that sem_post() is */
171 /* the only async-signal-safe primitive in LinuxThreads. */
172 sem_post(&GC_suspend_ack_sem);
173 me -> stop_info.last_stop_count = my_stop_count;
175 /* Wait until that thread tells us to restart by sending */
176 /* this thread a SIG_THR_RESTART signal. */
177 /* SIG_THR_RESTART should be masked at this point. Thus there */
178 /* is no race. */
179 do {
180 me->stop_info.signal = 0;
181 sigsuspend(&suspend_handler_mask); /* Wait for signal */
182 } while (me->stop_info.signal != SIG_THR_RESTART);
183 /* If the RESTART signal gets lost, we can still lose. That should be */
184 /* less likely than losing the SUSPEND signal, since we don't do much */
185 /* between the sem_post and sigsuspend. */
186 /* We'd need more handshaking to work around that, since we don't want */
187 /* to accidentally leave a RESTART signal pending, thus causing us to */
188 /* continue prematurely in a future round. */
190 /* Tell the thread that wants to start the world that this */
191 /* thread has been started. Note that sem_post() is */
192 /* the only async-signal-safe primitive in LinuxThreads. */
193 sem_post(&GC_suspend_ack_sem);
196 #if DEBUG_THREADS
197 GC_printf1("Continuing 0x%lx\n", my_thread);
198 #endif
200 #endif /* NACL */
203 void GC_suspend_handler(int sig)
205 int old_errno = errno;
206 _GC_suspend_handler(sig);
207 errno = old_errno;
210 static void _GC_restart_handler(int sig)
212 pthread_t my_thread = pthread_self();
213 GC_thread me;
215 if (sig != SIG_THR_RESTART) ABORT("Bad signal in suspend_handler");
217 /* Let the GC_suspend_handler() know that we got a SIG_THR_RESTART. */
218 /* The lookup here is safe, since I'm doing this on behalf */
219 /* of a thread which holds the allocation lock in order */
220 /* to stop the world. Thus concurrent modification of the */
221 /* data structure is impossible. */
222 me = GC_lookup_thread(my_thread);
223 me->stop_info.signal = SIG_THR_RESTART;
226 ** Note: even if we didn't do anything useful here,
227 ** it would still be necessary to have a signal handler,
228 ** rather than ignoring the signals, otherwise
229 ** the signals will not be delivered at all, and
230 ** will thus not interrupt the sigsuspend() above.
233 #if DEBUG_THREADS
234 GC_printf1("In GC_restart_handler for 0x%lx\n", pthread_self());
235 #endif
238 # ifdef IA64
239 # define IF_IA64(x) x
240 # else
241 # define IF_IA64(x)
242 # endif
243 /* We hold allocation lock. Should do exactly the right thing if the */
244 /* world is stopped. Should not fail if it isn't. */
245 static void pthread_push_all_stacks()
247 GC_bool found_me = FALSE;
248 int i;
249 GC_thread p;
250 ptr_t lo, hi;
251 /* On IA64, we also need to scan the register backing store. */
252 IF_IA64(ptr_t bs_lo; ptr_t bs_hi;)
253 pthread_t me = pthread_self();
255 if (!GC_thr_initialized) GC_thr_init();
256 #if DEBUG_THREADS
257 GC_printf1("Pushing stacks from thread 0x%lx\n", (unsigned long) me);
258 #endif
259 for (i = 0; i < THREAD_TABLE_SZ; i++) {
260 for (p = GC_threads[i]; p != 0; p = p -> next) {
261 if (p -> flags & FINISHED) continue;
262 if (pthread_equal(p -> id, me)) {
263 # ifdef SPARC
264 lo = (ptr_t)GC_save_regs_in_stack();
265 # else
266 lo = GC_approx_sp();
267 # endif
268 found_me = TRUE;
269 IF_IA64(bs_hi = (ptr_t)GC_save_regs_in_stack();)
270 } else {
271 lo = p -> stop_info.stack_ptr;
272 IF_IA64(bs_hi = p -> backing_store_ptr;)
274 if ((p -> flags & MAIN_THREAD) == 0) {
275 hi = p -> stack_end;
276 IF_IA64(bs_lo = p -> backing_store_end);
277 } else {
278 /* The original stack. */
279 hi = GC_stackbottom;
280 IF_IA64(bs_lo = BACKING_STORE_BASE;)
282 #if DEBUG_THREADS
283 GC_printf3("Stack for thread 0x%lx = [%lx,%lx)\n",
284 (unsigned long) p -> id,
285 (unsigned long) lo, (unsigned long) hi);
286 #endif
287 if (0 == lo) ABORT("GC_push_all_stacks: sp not set!\n");
288 if (p->altstack && lo >= p->altstack && lo <= p->altstack + p->altstack_size)
289 hi = p->altstack + p->altstack_size;
290 /* FIXME: Need to scan the normal stack too, but how ? */
292 # ifdef STACK_GROWS_UP
293 /* We got them backwards! */
294 GC_push_all_stack(hi, lo);
295 # else
296 GC_push_all_stack(lo, hi);
297 # endif
298 # ifdef NACL
299 /* Push reg_storage as roots, this will cover the reg context */
300 GC_push_all_stack(p -> stop_info.reg_storage, p -> stop_info.reg_storage + NACL_GC_REG_STORAGE_SIZE);
301 # endif
302 # ifdef IA64
303 # if DEBUG_THREADS
304 GC_printf3("Reg stack for thread 0x%lx = [%lx,%lx)\n",
305 (unsigned long) p -> id,
306 (unsigned long) bs_lo, (unsigned long) bs_hi);
307 # endif
308 if (pthread_equal(p -> id, me)) {
309 GC_push_all_eager(bs_lo, bs_hi);
310 } else {
311 GC_push_all_stack(bs_lo, bs_hi);
313 # endif
316 if (!found_me && !GC_in_thread_creation)
317 ABORT("Collecting from unknown thread.");
320 void GC_restart_handler(int sig)
322 int old_errno = errno;
323 _GC_restart_handler (sig);
324 errno = old_errno;
327 /* We hold allocation lock. Should do exactly the right thing if the */
328 /* world is stopped. Should not fail if it isn't. */
329 void GC_push_all_stacks()
331 pthread_push_all_stacks();
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;
337 int GC_stopping_pid;
339 #ifdef HOST_ANDROID
340 static
341 int android_thread_kill(pid_t tid, int sig)
343 int ret;
344 int old_errno = errno;
346 ret = tkill(tid, sig);
347 if (ret < 0) {
348 ret = errno;
349 errno = old_errno;
352 return ret;
354 #endif
356 /* We hold the allocation lock. Suspend all threads that might */
357 /* still be running. Return the number of suspend signals that */
358 /* were sent. */
359 int GC_suspend_all()
361 #ifndef NACL
362 int n_live_threads = 0;
363 int i;
364 GC_thread p;
365 int result;
366 pthread_t my_thread = pthread_self();
368 GC_stopping_thread = my_thread; /* debugging only. */
369 GC_stopping_pid = getpid(); /* debugging only. */
370 for (i = 0; i < THREAD_TABLE_SZ; i++) {
371 for (p = GC_threads[i]; p != 0; p = p -> next) {
372 if (p -> id != my_thread) {
373 if (p -> flags & FINISHED) continue;
374 if (p -> stop_info.last_stop_count == GC_stop_count) continue;
375 if (p -> thread_blocked) /* Will wait */ continue;
376 n_live_threads++;
377 #if DEBUG_THREADS
378 GC_printf1("Sending suspend signal to 0x%lx\n", p -> id);
379 #endif
381 #ifndef HOST_ANDROID
382 result = pthread_kill(p -> id, SIG_SUSPEND);
383 #else
384 result = android_thread_kill(p -> kernel_id, SIG_SUSPEND);
385 #endif
386 switch(result) {
387 case ESRCH:
388 /* Not really there anymore. Possible? */
389 n_live_threads--;
390 break;
391 case 0:
392 break;
393 default:
394 ABORT("pthread_kill failed");
399 return n_live_threads;
400 #else /* NACL */
401 return 0;
402 #endif
405 /* Caller holds allocation lock. */
406 static void pthread_stop_world()
408 #ifndef NACL
409 int i;
410 int n_live_threads;
411 int code;
413 #if DEBUG_THREADS
414 GC_printf1("Stopping the world from 0x%lx\n", pthread_self());
415 #endif
417 n_live_threads = GC_suspend_all();
419 if (GC_retry_signals) {
420 unsigned long wait_usecs = 0; /* Total wait since retry. */
421 # define WAIT_UNIT 3000
422 # define RETRY_INTERVAL 100000
423 for (;;) {
424 int ack_count;
426 sem_getvalue(&GC_suspend_ack_sem, &ack_count);
427 if (ack_count == n_live_threads) break;
428 if (wait_usecs > RETRY_INTERVAL) {
429 int newly_sent = GC_suspend_all();
431 # ifdef CONDPRINT
432 if (GC_print_stats) {
433 GC_printf1("Resent %ld signals after timeout\n",
434 newly_sent);
436 # endif
437 sem_getvalue(&GC_suspend_ack_sem, &ack_count);
438 if (newly_sent < n_live_threads - ack_count) {
439 WARN("Lost some threads during GC_stop_world?!\n",0);
440 n_live_threads = ack_count + newly_sent;
442 wait_usecs = 0;
444 usleep(WAIT_UNIT);
445 wait_usecs += WAIT_UNIT;
448 for (i = 0; i < n_live_threads; i++) {
449 while (0 != (code = sem_wait(&GC_suspend_ack_sem))) {
450 if (errno != EINTR) {
451 GC_err_printf1("Sem_wait returned %ld\n", (unsigned long)code);
452 ABORT("sem_wait for handler failed");
456 #if DEBUG_THREADS
457 GC_printf1("World stopped from 0x%lx\n", pthread_self());
458 #endif
459 GC_stopping_thread = 0; /* debugging only */
460 #else /* NACL */
461 GC_thread p;
462 int i;
463 int num_sleeps = 0;
465 #if DEBUG_THREADS
466 GC_printf1("pthread_stop_world: num_threads %d\n", nacl_num_gc_threads - 1);
467 #endif
468 nacl_thread_parker = pthread_self();
469 __nacl_thread_suspension_needed = 1;
471 while (1) {
472 #define NACL_PARK_WAIT_NANOSECONDS 100000
473 #define NANOS_PER_SECOND 1000000000
474 int num_threads_parked = 0;
475 struct timespec ts;
476 int num_used = 0;
477 /* Check the 'parked' flag for each thread the GC knows about */
478 for (i = 0; i < MAX_NACL_GC_THREADS && num_used < nacl_num_gc_threads; i++) {
479 if (nacl_thread_used[i] == 1) {
480 num_used++;
481 if (nacl_thread_parked[i] == 1) {
482 num_threads_parked++;
486 /* -1 for the current thread */
487 if (num_threads_parked >= nacl_num_gc_threads - 1)
488 break;
489 ts.tv_sec = 0;
490 ts.tv_nsec = NACL_PARK_WAIT_NANOSECONDS;
491 #if DEBUG_THREADS
492 GC_printf1("sleeping waiting for %d threads to park...\n", nacl_num_gc_threads - num_threads_parked - 1);
493 #endif
494 nanosleep(&ts, 0);
495 if (++num_sleeps > NANOS_PER_SECOND / NACL_PARK_WAIT_NANOSECONDS) {
496 GC_printf1("GC appears stalled waiting for %d threads to park...\n", nacl_num_gc_threads - num_threads_parked - 1);
497 num_sleeps = 0;
501 #endif /* NACL */
505 #ifdef NACL
507 #if __x86_64__
509 #define NACL_STORE_REGS() \
510 do { \
511 __asm__ __volatile__ ("push %rbx");\
512 __asm__ __volatile__ ("push %rbp");\
513 __asm__ __volatile__ ("push %r12");\
514 __asm__ __volatile__ ("push %r13");\
515 __asm__ __volatile__ ("push %r14");\
516 __asm__ __volatile__ ("push %r15");\
517 __asm__ __volatile__ ("mov %%esp, %0" : "=m" (nacl_gc_thread_self->stop_info.stack_ptr));\
518 memcpy(nacl_gc_thread_self->stop_info.reg_storage, nacl_gc_thread_self->stop_info.stack_ptr, NACL_GC_REG_STORAGE_SIZE * sizeof(ptr_t));\
519 __asm__ __volatile__ ("naclasp $48, %r15");\
520 } while (0)
522 #elif __i386__
524 #define NACL_STORE_REGS() \
525 do { \
526 __asm__ __volatile__ ("push %ebx");\
527 __asm__ __volatile__ ("push %ebp");\
528 __asm__ __volatile__ ("push %esi");\
529 __asm__ __volatile__ ("push %edi");\
530 __asm__ __volatile__ ("mov %%esp, %0" : "=m" (nacl_gc_thread_self->stop_info.stack_ptr));\
531 memcpy(nacl_gc_thread_self->stop_info.reg_storage, nacl_gc_thread_self->stop_info.stack_ptr, NACL_GC_REG_STORAGE_SIZE * sizeof(ptr_t));\
532 __asm__ __volatile__ ("add $16, %esp");\
533 } while (0)
535 #elif __arm__
537 #define NACL_STORE_REGS() \
538 do { \
539 __asm__ __volatile__ ( \
540 ".align 4\n\t" \
541 "bic %0, %0, #0xc0000000\n\t" \
542 "str sp, [%0]\n\t" \
543 "bic %1, %1, #0xc0000000\n\t" \
544 "stm %1, {r4-r8,r10-r12,lr}\n\t" \
546 : "r" (&nacl_gc_thread_self->stop_info.stack_ptr), \
547 "r"(nacl_gc_thread_self->stop_info.reg_storage) \
548 : "memory"); \
549 } while (0)
550 #else
552 #error "Please port NACL_STORE_REGS"
554 #endif
556 void nacl_pre_syscall_hook()
558 int local_dummy = 0;
559 if (nacl_thread_idx != -1) {
560 NACL_STORE_REGS();
561 nacl_gc_thread_self->stop_info.stack_ptr = (ptr_t)(&local_dummy);
562 nacl_thread_parked[nacl_thread_idx] = 1;
566 void __nacl_suspend_thread_if_needed();
568 void nacl_post_syscall_hook()
570 /* Calling __nacl_suspend_thread_if_needed() right away should guarantee we don't mutate the GC set. */
571 __nacl_suspend_thread_if_needed();
572 if (nacl_thread_idx != -1) {
573 nacl_thread_parked[nacl_thread_idx] = 0;
577 void __nacl_suspend_thread_if_needed() {
578 if (__nacl_thread_suspension_needed) {
579 pthread_t self = pthread_self();
580 int local_dummy = 0;
581 /* Don't try to park the thread parker. */
582 if (nacl_thread_parker == self)
583 return;
585 /* This can happen when a thread is created */
586 /* outside of the GC system (wthread mostly). */
587 if (nacl_thread_idx < 0)
588 return;
590 /* If it was already 'parked', we're returning from a syscall, */
591 /* so don't bother storing registers again, the GC has a set. */
592 if (!nacl_thread_parked[nacl_thread_idx]) {
593 NACL_STORE_REGS();
594 nacl_gc_thread_self->stop_info.stack_ptr = (ptr_t)(&local_dummy);
596 nacl_thread_parked[nacl_thread_idx] = 1;
597 while (__nacl_thread_suspension_needed)
598 ; /* spin */
599 nacl_thread_parked[nacl_thread_idx] = 0;
601 /* Clear out the reg storage for next suspend. */
602 memset(nacl_gc_thread_self->stop_info.reg_storage, 0, NACL_GC_REG_STORAGE_SIZE * sizeof(ptr_t));
606 #endif /* NACL */
608 /* Caller holds allocation lock. */
609 void GC_stop_world()
611 if (GC_notify_event)
612 GC_notify_event (GC_EVENT_PRE_STOP_WORLD);
613 GC_process_togglerefs ();
614 /* Make sure all free list construction has stopped before we start. */
615 /* No new construction can start, since free list construction is */
616 /* required to acquire and release the GC lock before it starts, */
617 /* and we have the lock. */
618 # ifdef PARALLEL_MARK
619 GC_acquire_mark_lock();
620 GC_ASSERT(GC_fl_builder_count == 0);
621 /* We should have previously waited for it to become zero. */
622 # endif /* PARALLEL_MARK */
623 ++GC_stop_count;
624 pthread_stop_world ();
625 # ifdef PARALLEL_MARK
626 GC_release_mark_lock();
627 # endif
628 if (GC_notify_event)
629 GC_notify_event (GC_EVENT_POST_STOP_WORLD);
632 /* Caller holds allocation lock, and has held it continuously since */
633 /* the world stopped. */
634 static void pthread_start_world()
636 #ifndef NACL
637 pthread_t my_thread = pthread_self();
638 register int i;
639 register GC_thread p;
640 register int n_live_threads = 0;
641 register int result;
642 int code;
644 # if DEBUG_THREADS
645 GC_printf0("World starting\n");
646 # endif
647 if (GC_notify_event)
648 GC_notify_event (GC_EVENT_PRE_START_WORLD);
650 for (i = 0; i < THREAD_TABLE_SZ; i++) {
651 for (p = GC_threads[i]; p != 0; p = p -> next) {
652 if (p -> id != my_thread) {
653 if (p -> flags & FINISHED) continue;
654 if (p -> thread_blocked) continue;
655 n_live_threads++;
656 #if DEBUG_THREADS
657 GC_printf1("Sending restart signal to 0x%lx\n", p -> id);
658 #endif
660 #ifndef HOST_ANDROID
661 result = pthread_kill(p -> id, SIG_THR_RESTART);
662 #else
663 result = android_thread_kill(p -> kernel_id, SIG_THR_RESTART);
664 #endif
665 switch(result) {
666 case ESRCH:
667 /* Not really there anymore. Possible? */
668 n_live_threads--;
669 break;
670 case 0:
671 break;
672 default:
673 ABORT("pthread_kill failed");
679 #if DEBUG_THREADS
680 GC_printf0 ("All threads signaled");
681 #endif
683 for (i = 0; i < n_live_threads; i++) {
684 while (0 != (code = sem_wait(&GC_suspend_ack_sem))) {
685 if (errno != EINTR) {
686 GC_err_printf1("Sem_wait returned %ld\n", (unsigned long)code);
687 ABORT("sem_wait for handler failed");
692 if (GC_notify_event)
693 GC_notify_event (GC_EVENT_POST_START_WORLD);
694 #if DEBUG_THREADS
695 GC_printf0("World started\n");
696 #endif
697 #else /* NACL */
698 if (GC_notify_event)
699 GC_notify_event (GC_EVENT_PRE_START_WORLD);
700 # if DEBUG_THREADS
701 GC_printf0("World starting\n");
702 # endif
703 __nacl_thread_suspension_needed = 0;
704 if (GC_notify_event)
705 GC_notify_event (GC_EVENT_POST_START_WORLD);
706 #endif /* NACL */
709 void GC_start_world()
711 pthread_start_world ();
714 static void pthread_stop_init() {
715 #ifndef NACL
716 struct sigaction act;
718 if (sem_init(&GC_suspend_ack_sem, 0, 0) != 0)
719 ABORT("sem_init failed");
721 act.sa_flags = SA_RESTART;
722 if (sigfillset(&act.sa_mask) != 0) {
723 ABORT("sigfillset() failed");
725 GC_remove_allowed_signals(&act.sa_mask);
726 /* SIG_THR_RESTART is set in the resulting mask. */
727 /* It is unmasked by the handler when necessary. */
728 act.sa_handler = GC_suspend_handler;
729 if (sigaction(SIG_SUSPEND, &act, NULL) != 0) {
730 ABORT("Cannot set SIG_SUSPEND handler");
733 act.sa_handler = GC_restart_handler;
734 if (sigaction(SIG_THR_RESTART, &act, NULL) != 0) {
735 ABORT("Cannot set SIG_THR_RESTART handler");
738 /* Inititialize suspend_handler_mask. It excludes SIG_THR_RESTART. */
739 if (sigfillset(&suspend_handler_mask) != 0) ABORT("sigfillset() failed");
740 GC_remove_allowed_signals(&suspend_handler_mask);
741 if (sigdelset(&suspend_handler_mask, SIG_THR_RESTART) != 0)
742 ABORT("sigdelset() failed");
744 /* Check for GC_RETRY_SIGNALS. */
745 if (0 != GETENV("GC_RETRY_SIGNALS")) {
746 GC_retry_signals = TRUE;
748 if (0 != GETENV("GC_NO_RETRY_SIGNALS")) {
749 GC_retry_signals = FALSE;
751 # ifdef CONDPRINT
752 if (GC_print_stats && GC_retry_signals) {
753 GC_printf0("Will retry suspend signal if necessary.\n");
755 # endif
756 #endif /* NACL */
759 /* We hold the allocation lock. */
760 void GC_stop_init()
762 pthread_stop_init ();
765 #endif