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