f908428bc457b457c97ef8904fb656f41033d6d2
[sbcl.git] / src / runtime / thread.c
1 /*
2  * This software is part of the SBCL system. See the README file for
3  * more information.
4  *
5  * This software is derived from the CMU CL system, which was
6  * written at Carnegie Mellon University and released into the
7  * public domain. The software is in the public domain and is
8  * provided with absolutely no warranty. See the COPYING and CREDITS
9  * files for more information.
10  */
11
12 #include "sbcl.h"
13
14 #include <stdlib.h>
15 #include <stdio.h>
16 #include <string.h>
17 #ifndef LISP_FEATURE_WIN32
18 #include <sched.h>
19 #endif
20 #include <signal.h>
21 #include <stddef.h>
22 #include <errno.h>
23 #include <sys/types.h>
24 #ifndef LISP_FEATURE_WIN32
25 #include <sys/wait.h>
26 #endif
27
28 #include "runtime.h"
29 #include "validate.h"           /* for CONTROL_STACK_SIZE etc */
30 #include "alloc.h"
31 #include "thread.h"
32 #include "arch.h"
33 #include "target-arch-os.h"
34 #include "os.h"
35 #include "globals.h"
36 #include "dynbind.h"
37 #include "genesis/cons.h"
38 #include "genesis/fdefn.h"
39 #include "interr.h"             /* for lose() */
40 #include "gc-internal.h"
41
42 #ifdef LISP_FEATURE_WIN32
43 /*
44  * Win32 doesn't have SIGSTKSZ, and we're not switching stacks anyway,
45  * so define it arbitrarily
46  */
47 #define SIGSTKSZ 1024
48 #endif
49
50 #if defined(LISP_FEATURE_DARWIN) && defined(LISP_FEATURE_SB_THREAD)
51 #define QUEUE_FREEABLE_THREAD_STACKS
52 #endif
53
54 #define ALIEN_STACK_SIZE (1*1024*1024) /* 1Mb size chosen at random */
55
56 struct freeable_stack {
57 #ifdef QUEUE_FREEABLE_THREAD_STACKS
58     struct freeable_stack *next;
59 #endif
60     os_thread_t os_thread;
61     os_vm_address_t stack;
62 };
63
64
65 #ifdef QUEUE_FREEABLE_THREAD_STACKS
66 static struct freeable_stack * volatile freeable_stack_queue = 0;
67 static int freeable_stack_count = 0;
68 pthread_mutex_t freeable_stack_lock = PTHREAD_MUTEX_INITIALIZER;
69 #else
70 static struct freeable_stack * volatile freeable_stack = 0;
71 #endif
72
73 int dynamic_values_bytes=4096*sizeof(lispobj);  /* same for all threads */
74 struct thread * volatile all_threads;
75 extern struct interrupt_data * global_interrupt_data;
76
77 #ifdef LISP_FEATURE_SB_THREAD
78 pthread_mutex_t all_threads_lock = PTHREAD_MUTEX_INITIALIZER;
79 #endif
80
81 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
82 extern lispobj call_into_lisp_first_time(lispobj fun, lispobj *args, int nargs);
83 #endif
84
85 static void
86 link_thread(struct thread *th)
87 {
88     if (all_threads) all_threads->prev=th;
89     th->next=all_threads;
90     th->prev=0;
91     all_threads=th;
92 }
93
94 #ifdef LISP_FEATURE_SB_THREAD
95 static void
96 unlink_thread(struct thread *th)
97 {
98     if (th->prev)
99         th->prev->next = th->next;
100     else
101         all_threads = th->next;
102     if (th->next)
103         th->next->prev = th->prev;
104 }
105 #endif
106
107 static int
108 initial_thread_trampoline(struct thread *th)
109 {
110     lispobj function;
111 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
112     lispobj *args = NULL;
113 #endif
114     function = th->no_tls_value_marker;
115     th->no_tls_value_marker = NO_TLS_VALUE_MARKER_WIDETAG;
116     if(arch_os_thread_init(th)==0) return 1;
117     link_thread(th);
118     th->os_thread=thread_self();
119 #ifndef LISP_FEATURE_WIN32
120     protect_control_stack_guard_page(1);
121 #endif
122
123 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
124     return call_into_lisp_first_time(function,args,0);
125 #else
126     return funcall0(function);
127 #endif
128 }
129
130 #define THREAD_STRUCT_SIZE (THREAD_CONTROL_STACK_SIZE + BINDING_STACK_SIZE + \
131                             ALIEN_STACK_SIZE + dynamic_values_bytes + \
132                             32 * SIGSTKSZ)
133
134 #ifdef LISP_FEATURE_SB_THREAD
135
136 #ifdef QUEUE_FREEABLE_THREAD_STACKS
137
138 queue_freeable_thread_stack(struct thread *thread_to_be_cleaned_up)
139 {
140      if (thread_to_be_cleaned_up) {
141         pthread_mutex_lock(&freeable_stack_lock);
142         if (freeable_stack_queue) {
143             struct freeable_stack *new_freeable_stack = 0, *next;
144             next = freeable_stack_queue;
145             while (next->next) {
146                 next = next->next;
147             }
148             new_freeable_stack = (struct freeable_stack *)
149                 os_validate(0, sizeof(struct freeable_stack));
150             new_freeable_stack->next = NULL;
151             new_freeable_stack->os_thread = thread_to_be_cleaned_up->os_thread;
152             new_freeable_stack->stack = (os_vm_address_t)
153                 thread_to_be_cleaned_up->control_stack_start;
154             next->next = new_freeable_stack;
155             freeable_stack_count++;
156         } else {
157             struct freeable_stack *new_freeable_stack = 0;
158             new_freeable_stack = (struct freeable_stack *)
159                 os_validate(0, sizeof(struct freeable_stack));
160             new_freeable_stack->next = NULL;
161             new_freeable_stack->os_thread = thread_to_be_cleaned_up->os_thread;
162             new_freeable_stack->stack = (os_vm_address_t)
163                 thread_to_be_cleaned_up->control_stack_start;
164             freeable_stack_queue = new_freeable_stack;
165             freeable_stack_count++;
166         }
167         pthread_mutex_unlock(&freeable_stack_lock);
168     }
169 }
170
171 #define FREEABLE_STACK_QUEUE_SIZE 4
172
173 static void
174 free_freeable_stacks() {
175     if (freeable_stack_queue && (freeable_stack_count > FREEABLE_STACK_QUEUE_SIZE)) {
176         struct freeable_stack* old;
177         pthread_mutex_lock(&freeable_stack_lock);
178         old = freeable_stack_queue;
179         freeable_stack_queue = old->next;
180         freeable_stack_count--;
181         gc_assert(pthread_join(old->os_thread, NULL) == 0);
182         FSHOW((stderr, "freeing thread %x stack\n", old->os_thread));
183         os_invalidate(old->stack, THREAD_STRUCT_SIZE);
184         os_invalidate((os_vm_address_t)old, sizeof(struct freeable_stack));
185         pthread_mutex_unlock(&freeable_stack_lock);
186     }
187 }
188
189 #else
190 static void
191 free_thread_stack_later(struct thread *thread_to_be_cleaned_up)
192 {
193     struct freeable_stack *new_freeable_stack = 0;
194     if (thread_to_be_cleaned_up) {
195         new_freeable_stack = (struct freeable_stack *)
196             os_validate(0, sizeof(struct freeable_stack));
197         new_freeable_stack->os_thread = thread_to_be_cleaned_up->os_thread;
198         new_freeable_stack->stack = (os_vm_address_t)
199             thread_to_be_cleaned_up->control_stack_start;
200     }
201     new_freeable_stack = (struct freeable_stack *)
202         swap_lispobjs((lispobj *)(void *)&freeable_stack,
203                       (lispobj)new_freeable_stack);
204     if (new_freeable_stack) {
205         FSHOW((stderr,"/reaping %p\n", (void*) new_freeable_stack->os_thread));
206         /* Under NPTL pthread_join really waits until the thread
207          * exists and the stack can be safely freed. This is sadly not
208          * mandated by the pthread spec. */
209         gc_assert(pthread_join(new_freeable_stack->os_thread, NULL) == 0);
210         os_invalidate(new_freeable_stack->stack, THREAD_STRUCT_SIZE);
211         os_invalidate((os_vm_address_t) new_freeable_stack,
212                       sizeof(struct freeable_stack));
213     }
214 }
215 #endif
216
217 /* this is the first thing that runs in the child (which is why the
218  * silly calling convention).  Basically it calls the user's requested
219  * lisp function after doing arch_os_thread_init and whatever other
220  * bookkeeping needs to be done
221  */
222 int
223 new_thread_trampoline(struct thread *th)
224 {
225     lispobj function;
226     int result, lock_ret;
227
228     FSHOW((stderr,"/creating thread %lu\n", thread_self()));
229     function = th->no_tls_value_marker;
230     th->no_tls_value_marker = NO_TLS_VALUE_MARKER_WIDETAG;
231     if(arch_os_thread_init(th)==0) {
232         /* FIXME: handle error */
233         lose("arch_os_thread_init failed\n");
234     }
235
236     th->os_thread=thread_self();
237     protect_control_stack_guard_page(1);
238     /* Since GC can only know about this thread from the all_threads
239      * list and we're just adding this thread to it there is no danger
240      * of deadlocking even with SIG_STOP_FOR_GC blocked (which it is
241      * not). */
242     lock_ret = pthread_mutex_lock(&all_threads_lock);
243     gc_assert(lock_ret == 0);
244     link_thread(th);
245     lock_ret = pthread_mutex_unlock(&all_threads_lock);
246     gc_assert(lock_ret == 0);
247
248     result = funcall0(function);
249
250     /* Block GC */
251     block_blockable_signals();
252     th->state=STATE_DEAD;
253
254     /* SIG_STOP_FOR_GC is blocked and GC might be waiting for this
255      * thread, but since we are already dead it won't wait long. */
256     lock_ret = pthread_mutex_lock(&all_threads_lock);
257     gc_assert(lock_ret == 0);
258
259     gc_alloc_update_page_tables(0, &th->alloc_region);
260     unlink_thread(th);
261     pthread_mutex_unlock(&all_threads_lock);
262     gc_assert(lock_ret == 0);
263
264     if(th->tls_cookie>=0) arch_os_thread_cleanup(th);
265     os_invalidate((os_vm_address_t)th->interrupt_data,
266                   (sizeof (struct interrupt_data)));
267
268 #ifdef QUEUE_FREEABLE_THREAD_STACKS
269     queue_freeable_thread_stack(th);
270 #else
271     free_thread_stack_later(th);
272 #endif
273
274     FSHOW((stderr,"/exiting thread %p\n", thread_self()));
275     return result;
276 }
277
278 #endif /* LISP_FEATURE_SB_THREAD */
279
280 static void
281 free_thread_struct(struct thread *th)
282 {
283     if (th->interrupt_data)
284         os_invalidate((os_vm_address_t) th->interrupt_data,
285                       (sizeof (struct interrupt_data)));
286     os_invalidate((os_vm_address_t) th->control_stack_start,
287                   THREAD_STRUCT_SIZE);
288 }
289
290 /* this is called from any other thread to create the new one, and
291  * initialize all parts of it that can be initialized from another
292  * thread
293  */
294
295 static struct thread *
296 create_thread_struct(lispobj initial_function) {
297     union per_thread_data *per_thread;
298     struct thread *th=0;        /*  subdue gcc */
299     void *spaces=0;
300 #ifdef LISP_FEATURE_SB_THREAD
301     int i;
302 #endif
303
304     /* may as well allocate all the spaces at once: it saves us from
305      * having to decide what to do if only some of the allocations
306      * succeed */
307     spaces=os_validate(0, THREAD_STRUCT_SIZE);
308     if(!spaces)
309          return NULL;
310     per_thread=(union per_thread_data *)
311         (spaces+
312          THREAD_CONTROL_STACK_SIZE+
313          BINDING_STACK_SIZE+
314          ALIEN_STACK_SIZE);
315
316 #ifdef LISP_FEATURE_SB_THREAD
317     for(i = 0; i < (dynamic_values_bytes / sizeof(lispobj)); i++)
318         per_thread->dynamic_values[i] = NO_TLS_VALUE_MARKER_WIDETAG;
319     if (all_threads == 0) {
320         if(SymbolValue(FREE_TLS_INDEX,0)==UNBOUND_MARKER_WIDETAG) {
321             SetSymbolValue
322                 (FREE_TLS_INDEX,
323                  /* FIXME: should be MAX_INTERRUPTS -1 ? */
324                  make_fixnum(MAX_INTERRUPTS+
325                              sizeof(struct thread)/sizeof(lispobj)),
326                  0);
327             SetSymbolValue(TLS_INDEX_LOCK,make_fixnum(0),0);
328         }
329 #define STATIC_TLS_INIT(sym,field) \
330   ((struct symbol *)(sym-OTHER_POINTER_LOWTAG))->tls_index= \
331   make_fixnum(THREAD_SLOT_OFFSET_WORDS(field))
332
333         STATIC_TLS_INIT(BINDING_STACK_START,binding_stack_start);
334         STATIC_TLS_INIT(BINDING_STACK_POINTER,binding_stack_pointer);
335         STATIC_TLS_INIT(CONTROL_STACK_START,control_stack_start);
336         STATIC_TLS_INIT(CONTROL_STACK_END,control_stack_end);
337         STATIC_TLS_INIT(ALIEN_STACK,alien_stack_pointer);
338 #if defined(LISP_FEATURE_X86) || defined (LISP_FEATURE_X86_64)
339         STATIC_TLS_INIT(PSEUDO_ATOMIC_BITS,pseudo_atomic_bits);
340 #endif
341 #undef STATIC_TLS_INIT
342     }
343 #endif
344
345     th=&per_thread->thread;
346     th->control_stack_start = spaces;
347     th->binding_stack_start=
348         (lispobj*)((void*)th->control_stack_start+THREAD_CONTROL_STACK_SIZE);
349     th->control_stack_end = th->binding_stack_start;
350     th->alien_stack_start=
351         (lispobj*)((void*)th->binding_stack_start+BINDING_STACK_SIZE);
352     th->binding_stack_pointer=th->binding_stack_start;
353     th->this=th;
354     th->os_thread=0;
355     th->state=STATE_RUNNING;
356 #ifdef LISP_FEATURE_STACK_GROWS_DOWNWARD_NOT_UPWARD
357     th->alien_stack_pointer=((void *)th->alien_stack_start
358                              + ALIEN_STACK_SIZE-N_WORD_BYTES);
359 #else
360     th->alien_stack_pointer=((void *)th->alien_stack_start);
361 #endif
362 #if defined(LISP_FEATURE_X86) || defined (LISP_FEATURE_X86_64)
363     th->pseudo_atomic_bits=0;
364 #endif
365 #ifdef LISP_FEATURE_GENCGC
366     gc_set_region_empty(&th->alloc_region);
367 #endif
368
369 #ifndef LISP_FEATURE_SB_THREAD
370     /* the tls-points-into-struct-thread trick is only good for threaded
371      * sbcl, because unithread sbcl doesn't have tls.  So, we copy the
372      * appropriate values from struct thread here, and make sure that
373      * we use the appropriate SymbolValue macros to access any of the
374      * variable quantities from the C runtime.  It's not quite OAOOM,
375      * it just feels like it */
376     SetSymbolValue(BINDING_STACK_START,(lispobj)th->binding_stack_start,th);
377     SetSymbolValue(CONTROL_STACK_START,(lispobj)th->control_stack_start,th);
378     SetSymbolValue(CONTROL_STACK_END,(lispobj)th->control_stack_end,th);
379 #if defined(LISP_FEATURE_X86) || defined (LISP_FEATURE_X86_64)
380     SetSymbolValue(BINDING_STACK_POINTER,(lispobj)th->binding_stack_pointer,th);
381     SetSymbolValue(ALIEN_STACK,(lispobj)th->alien_stack_pointer,th);
382     SetSymbolValue(PSEUDO_ATOMIC_BITS,(lispobj)th->pseudo_atomic_bits,th);
383 #else
384     current_binding_stack_pointer=th->binding_stack_pointer;
385     current_control_stack_pointer=th->control_stack_start;
386 #endif
387 #endif
388     bind_variable(CURRENT_CATCH_BLOCK,make_fixnum(0),th);
389     bind_variable(CURRENT_UNWIND_PROTECT_BLOCK,make_fixnum(0),th);
390     bind_variable(FREE_INTERRUPT_CONTEXT_INDEX,make_fixnum(0),th);
391     bind_variable(INTERRUPT_PENDING, NIL,th);
392     bind_variable(INTERRUPTS_ENABLED,T,th);
393     bind_variable(GC_PENDING,NIL,th);
394 #ifdef LISP_FEATURE_SB_THREAD
395     bind_variable(STOP_FOR_GC_PENDING,NIL,th);
396 #endif
397
398     th->interrupt_data = (struct interrupt_data *)
399         os_validate(0,(sizeof (struct interrupt_data)));
400     if (!th->interrupt_data) {
401         free_thread_struct(th);
402         return 0;
403     }
404     th->interrupt_data->pending_handler = 0;
405     th->no_tls_value_marker=initial_function;
406
407     th->stepping = NIL;
408     return th;
409 }
410
411 void create_initial_thread(lispobj initial_function) {
412     struct thread *th=create_thread_struct(initial_function);
413     if(th) {
414         initial_thread_trampoline(th); /* no return */
415     } else lose("can't create initial thread\n");
416 }
417
418 #ifdef LISP_FEATURE_SB_THREAD
419
420 #ifndef __USE_XOPEN2K
421 extern int pthread_attr_setstack (pthread_attr_t *__attr, void *__stackaddr,
422                                   size_t __stacksize);
423 #endif
424
425 boolean create_os_thread(struct thread *th,os_thread_t *kid_tid)
426 {
427     /* The new thread inherits the restrictive signal mask set here,
428      * and enables signals again when it is set up properly. */
429     pthread_attr_t attr;
430     sigset_t newset,oldset;
431     boolean r=1;
432     int retcode, initcode, sizecode, addrcode;
433
434     FSHOW_SIGNAL((stderr,"/create_os_thread: creating new thread\n"));
435
436     sigemptyset(&newset);
437     /* Blocking deferrable signals is enough, no need to block
438      * SIG_STOP_FOR_GC because the child process is not linked onto
439      * all_threads until it's ready. */
440     sigaddset_deferrable(&newset);
441     thread_sigmask(SIG_BLOCK, &newset, &oldset);
442
443 #if defined(LISP_FEATURE_DARWIN)
444 #define CONTROL_STACK_ADJUST 8192 /* darwin wants page-aligned stacks */
445 #else
446 #define CONTROL_STACK_ADJUST 16
447 #endif
448
449     if((initcode = pthread_attr_init(&attr)) ||
450        /* FIXME: why do we even have this in the first place? */
451        (pthread_attr_setstack(&attr,th->control_stack_start,
452                               THREAD_CONTROL_STACK_SIZE-CONTROL_STACK_ADJUST)) ||
453 #undef CONTROL_STACK_ADJUST
454        (retcode = pthread_create
455         (kid_tid,&attr,(void *(*)(void *))new_thread_trampoline,th))) {
456         FSHOW_SIGNAL((stderr, "init, size, addr = %d, %d, %d\n", initcode, sizecode, addrcode));
457         FSHOW_SIGNAL((stderr, printf("pthread_create returned %d, errno %d\n", retcode, errno)));
458         FSHOW_SIGNAL((stderr, "wanted stack size %d, min stack size %d\n",
459                       THREAD_CONTROL_STACK_SIZE-16, PTHREAD_STACK_MIN));
460         if(retcode < 0) {
461             perror("create_os_thread");
462         }
463         r=0;
464     }
465 #ifdef QUEUE_FREEABLE_THREAD_STACKS
466     free_freeable_stacks();
467 #endif
468     thread_sigmask(SIG_SETMASK,&oldset,0);
469     return r;
470 }
471
472 os_thread_t create_thread(lispobj initial_function) {
473     struct thread *th;
474     os_thread_t kid_tid;
475
476     /* Assuming that a fresh thread struct has no lisp objects in it,
477      * linking it to all_threads can be left to the thread itself
478      * without fear of gc lossage. initial_function violates this
479      * assumption and must stay pinned until the child starts up. */
480     th = create_thread_struct(initial_function);
481     if(th==0) return 0;
482
483     if (create_os_thread(th,&kid_tid)) {
484         return kid_tid;
485     } else {
486         free_thread_struct(th);
487         return 0;
488     }
489 }
490
491 /* Send the signo to os_thread, retry if the rt signal queue is
492  * full. */
493 int
494 kill_thread_safely(os_thread_t os_thread, int signo)
495 {
496     int r;
497     /* The man page does not mention EAGAIN as a valid return value
498      * for either pthread_kill or kill. But that's theory, this is
499      * practice. By waiting here we assume that the delivery of this
500      * signal is not necessary for the delivery of the signals in the
501      * queue. In other words, we _assume_ there are no deadlocks. */
502     while ((r=pthread_kill(os_thread,signo))==EAGAIN) {
503         /* wait a bit then try again in the hope of the rt signal
504          * queue not being full */
505         FSHOW_SIGNAL((stderr,"/rt signal queue full\n"));
506         /* FIXME: some kind of backoff (random, exponential) would be
507          * nice. */
508         sleep(1);
509     }
510     return r;
511 }
512
513 int signal_interrupt_thread(os_thread_t os_thread)
514 {
515     int status = kill_thread_safely(os_thread, SIG_INTERRUPT_THREAD);
516     if (status == 0) {
517         return 0;
518     } else if (status == ESRCH) {
519         return -1;
520     } else {
521         lose("cannot send SIG_INTERRUPT_THREAD to thread=%lu: %d, %s\n",
522              os_thread, status, strerror(status));
523     }
524 }
525
526 /* stopping the world is a two-stage process.  From this thread we signal
527  * all the others with SIG_STOP_FOR_GC.  The handler for this signal does
528  * the usual pseudo-atomic checks (we don't want to stop a thread while
529  * it's in the middle of allocation) then waits for another SIG_STOP_FOR_GC.
530  */
531
532 /* To avoid deadlocks when gc stops the world all clients of each
533  * mutex must enable or disable SIG_STOP_FOR_GC for the duration of
534  * holding the lock, but they must agree on which. */
535 void gc_stop_the_world()
536 {
537     struct thread *p,*th=arch_os_get_current_thread();
538     int status, lock_ret;
539     FSHOW_SIGNAL((stderr,"/gc_stop_the_world:waiting on lock, thread=%lu\n",
540                   th->os_thread));
541     /* keep threads from starting while the world is stopped. */
542     lock_ret = pthread_mutex_lock(&all_threads_lock);      \
543     gc_assert(lock_ret == 0);
544
545     FSHOW_SIGNAL((stderr,"/gc_stop_the_world:got lock, thread=%lu\n",
546                   th->os_thread));
547     /* stop all other threads by sending them SIG_STOP_FOR_GC */
548     for(p=all_threads; p; p=p->next) {
549         gc_assert(p->os_thread != 0);
550         FSHOW_SIGNAL((stderr,"/gc_stop_the_world: p->state: %x\n", p->state));
551         if((p!=th) && ((p->state==STATE_RUNNING))) {
552             FSHOW_SIGNAL((stderr,"/gc_stop_the_world: suspending %x, os_thread %x\n",
553                           p, p->os_thread));
554             status=kill_thread_safely(p->os_thread,SIG_STOP_FOR_GC);
555             if (status==ESRCH) {
556                 /* This thread has exited. */
557                 gc_assert(p->state==STATE_DEAD);
558             } else if (status) {
559                 lose("cannot send suspend thread=%lu: %d, %s\n",
560                      p->os_thread,status,strerror(status));
561             }
562         }
563     }
564     FSHOW_SIGNAL((stderr,"/gc_stop_the_world:signals sent\n"));
565     /* wait for the running threads to stop or finish */
566     for(p=all_threads;p;) {
567         FSHOW_SIGNAL((stderr,"/gc_stop_the_world: th: %p, p: %p\n", th, p));
568         if((p!=th) && (p->state==STATE_RUNNING)) {
569             sched_yield();
570         } else {
571             p=p->next;
572         }
573     }
574     FSHOW_SIGNAL((stderr,"/gc_stop_the_world:end\n"));
575 }
576
577 void gc_start_the_world()
578 {
579     struct thread *p,*th=arch_os_get_current_thread();
580     int status, lock_ret;
581     /* if a resumed thread creates a new thread before we're done with
582      * this loop, the new thread will get consed on the front of
583      * all_threads, but it won't have been stopped so won't need
584      * restarting */
585     FSHOW_SIGNAL((stderr,"/gc_start_the_world:begin\n"));
586     for(p=all_threads;p;p=p->next) {
587         gc_assert(p->os_thread!=0);
588         if((p!=th) && (p->state!=STATE_DEAD)) {
589             if(p->state!=STATE_SUSPENDED) {
590                 lose("gc_start_the_world: wrong thread state is %d\n",
591                      fixnum_value(p->state));
592             }
593             FSHOW_SIGNAL((stderr, "/gc_start_the_world: resuming %lu\n",
594                           p->os_thread));
595             p->state=STATE_RUNNING;
596
597 #if defined(SIG_RESUME_FROM_GC)
598             status=kill_thread_safely(p->os_thread,SIG_RESUME_FROM_GC);
599 #else
600             status=kill_thread_safely(p->os_thread,SIG_STOP_FOR_GC);
601 #endif
602             if (status) {
603                 lose("cannot resume thread=%lu: %d, %s\n",
604                      p->os_thread,status,strerror(status));
605             }
606         }
607     }
608     /* If we waited here until all threads leave STATE_SUSPENDED, then
609      * SIG_STOP_FOR_GC wouldn't need to be a rt signal. That has some
610      * performance implications, but does away with the 'rt signal
611      * queue full' problem. */
612
613     lock_ret = pthread_mutex_unlock(&all_threads_lock);
614     gc_assert(lock_ret == 0);
615
616     FSHOW_SIGNAL((stderr,"/gc_start_the_world:end\n"));
617 }
618 #endif