1.0.25.18: it's only SHOW
[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 #ifdef LISP_FEATURE_MACH_EXCEPTION_HANDLER
29 #include <mach/mach.h>
30 #include <mach/mach_error.h>
31 #include <mach/mach_types.h>
32 #endif
33
34 #include "runtime.h"
35 #include "validate.h"           /* for BINDING_STACK_SIZE etc */
36 #include "alloc.h"
37 #include "thread.h"
38 #include "arch.h"
39 #include "target-arch-os.h"
40 #include "os.h"
41 #include "globals.h"
42 #include "dynbind.h"
43 #include "genesis/cons.h"
44 #include "genesis/fdefn.h"
45 #include "interr.h"             /* for lose() */
46 #include "gc-internal.h"
47
48 #ifdef LISP_FEATURE_WIN32
49 /*
50  * Win32 doesn't have SIGSTKSZ, and we're not switching stacks anyway,
51  * so define it arbitrarily
52  */
53 #define SIGSTKSZ 1024
54 #endif
55
56 #if defined(LISP_FEATURE_DARWIN) && defined(LISP_FEATURE_SB_THREAD)
57 #define DELAY_THREAD_POST_MORTEM 5
58 #define LOCK_CREATE_THREAD
59 #endif
60
61 #ifdef LISP_FEATURE_FREEBSD
62 #define CREATE_CLEANUP_THREAD
63 #define LOCK_CREATE_THREAD
64 #endif
65
66 #define ALIEN_STACK_SIZE (1*1024*1024) /* 1Mb size chosen at random */
67
68 #ifdef LISP_FEATURE_SB_THREAD
69 struct thread_post_mortem {
70 #ifdef DELAY_THREAD_POST_MORTEM
71     struct thread_post_mortem *next;
72 #endif
73     os_thread_t os_thread;
74     pthread_attr_t *os_attr;
75     os_vm_address_t os_address;
76 };
77
78 #ifdef DELAY_THREAD_POST_MORTEM
79 static int pending_thread_post_mortem_count = 0;
80 pthread_mutex_t thread_post_mortem_lock = PTHREAD_MUTEX_INITIALIZER;
81 #endif
82 static struct thread_post_mortem * volatile pending_thread_post_mortem = 0;
83 #endif
84
85 int dynamic_values_bytes=TLS_SIZE*sizeof(lispobj);  /* same for all threads */
86 struct thread * volatile all_threads;
87 extern struct interrupt_data * global_interrupt_data;
88
89 #ifdef LISP_FEATURE_SB_THREAD
90 pthread_mutex_t all_threads_lock = PTHREAD_MUTEX_INITIALIZER;
91 #ifdef LOCK_CREATE_THREAD
92 static pthread_mutex_t create_thread_lock = PTHREAD_MUTEX_INITIALIZER;
93 #endif
94 #ifdef LISP_FEATURE_GCC_TLS
95 __thread struct thread *current_thread;
96 #endif
97 #endif
98
99 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
100 extern lispobj call_into_lisp_first_time(lispobj fun, lispobj *args, int nargs);
101 #endif
102
103 static void
104 link_thread(struct thread *th)
105 {
106     if (all_threads) all_threads->prev=th;
107     th->next=all_threads;
108     th->prev=0;
109     all_threads=th;
110 }
111
112 #ifdef LISP_FEATURE_SB_THREAD
113 static void
114 unlink_thread(struct thread *th)
115 {
116     if (th->prev)
117         th->prev->next = th->next;
118     else
119         all_threads = th->next;
120     if (th->next)
121         th->next->prev = th->prev;
122 }
123 #endif
124
125 static int
126 initial_thread_trampoline(struct thread *th)
127 {
128     lispobj function;
129 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
130     lispobj *args = NULL;
131 #endif
132     function = th->no_tls_value_marker;
133     th->no_tls_value_marker = NO_TLS_VALUE_MARKER_WIDETAG;
134     if(arch_os_thread_init(th)==0) return 1;
135     link_thread(th);
136     th->os_thread=thread_self();
137 #ifndef LISP_FEATURE_WIN32
138     protect_control_stack_guard_page(1);
139 #endif
140
141 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
142     return call_into_lisp_first_time(function,args,0);
143 #else
144     return funcall0(function);
145 #endif
146 }
147
148 #define THREAD_STRUCT_SIZE (thread_control_stack_size + BINDING_STACK_SIZE + \
149                             ALIEN_STACK_SIZE + dynamic_values_bytes +        \
150                             32 * SIGSTKSZ +                                  \
151                             THREAD_ALIGNMENT_BYTES)
152
153 #ifdef LISP_FEATURE_SB_THREAD
154 /* THREAD POST MORTEM CLEANUP
155  *
156  * Memory allocated for the thread stacks cannot be reclaimed while
157  * the thread is still alive, so we need a mechanism for post mortem
158  * cleanups. FIXME: We actually have three, for historical reasons as
159  * the saying goes. Do we really need three? Nikodemus guesses that
160  * not anymore, now that we properly call pthread_attr_destroy before
161  * freeing the stack. */
162
163 static struct thread_post_mortem *
164 plan_thread_post_mortem(struct thread *corpse)
165 {
166     if (corpse) {
167         struct thread_post_mortem *post_mortem = malloc(sizeof(struct thread_post_mortem));
168         gc_assert(post_mortem);
169         post_mortem->os_thread = corpse->os_thread;
170         post_mortem->os_attr = corpse->os_attr;
171         post_mortem->os_address = corpse->os_address;
172 #ifdef DELAY_THREAD_POST_MORTEM
173         post_mortem->next = NULL;
174 #endif
175         return post_mortem;
176     } else {
177         /* FIXME: When does this happen? */
178         return NULL;
179     }
180 }
181
182 static void
183 perform_thread_post_mortem(struct thread_post_mortem *post_mortem)
184 {
185 #ifdef CREATE_POST_MORTEM_THREAD
186     pthread_detach(pthread_self());
187 #endif
188     if (post_mortem) {
189         gc_assert(!pthread_join(post_mortem->os_thread, NULL));
190         gc_assert(!pthread_attr_destroy(post_mortem->os_attr));
191         free(post_mortem->os_attr);
192         os_invalidate(post_mortem->os_address, THREAD_STRUCT_SIZE);
193         free(post_mortem);
194     }
195 }
196
197 static void
198 schedule_thread_post_mortem(struct thread *corpse)
199 {
200     struct thread_post_mortem *post_mortem = NULL;
201     if (corpse) {
202         post_mortem = plan_thread_post_mortem(corpse);
203
204 #ifdef DELAY_THREAD_POST_MORTEM
205         pthread_mutex_lock(&thread_post_mortem_lock);
206         /* First stick the new post mortem to the end of the queue. */
207         if (pending_thread_post_mortem) {
208             struct thread_post_mortem *next = pending_thread_post_mortem;
209             while (next->next) {
210                 next = next->next;
211             }
212             next->next = post_mortem;
213         } else {
214             pending_thread_post_mortem = post_mortem;
215         }
216         /* Then, if there are enough things in the queue, clean up one
217          * from the head -- or increment the count, and null out the
218          * post_mortem we have. */
219         if (pending_thread_post_mortem_count > DELAY_THREAD_POST_MORTEM) {
220             post_mortem = pending_thread_post_mortem;
221             pending_thread_post_mortem = post_mortem->next;
222         } else {
223             pending_thread_post_mortem_count++;
224             post_mortem = NULL;
225         }
226         pthread_mutex_unlock(&thread_post_mortem_lock);
227         /* Finally run, the cleanup, if any. */
228         perform_thread_post_mortem(post_mortem);
229 #elif defined(CREATE_POST_MORTEM_THREAD)
230         gc_assert(!pthread_create(&thread, NULL, perform_thread_post_mortem, post_mortem));
231 #else
232         post_mortem = (struct thread_post_mortem *)
233             swap_lispobjs((lispobj *)(void *)&pending_thread_post_mortem,
234                           (lispobj)post_mortem);
235         perform_thread_post_mortem(post_mortem);
236 #endif
237     }
238 }
239
240 /* this is the first thing that runs in the child (which is why the
241  * silly calling convention).  Basically it calls the user's requested
242  * lisp function after doing arch_os_thread_init and whatever other
243  * bookkeeping needs to be done
244  */
245 int
246 new_thread_trampoline(struct thread *th)
247 {
248     lispobj function;
249     int result, lock_ret;
250
251     FSHOW((stderr,"/creating thread %lu\n", thread_self()));
252     function = th->no_tls_value_marker;
253     th->no_tls_value_marker = NO_TLS_VALUE_MARKER_WIDETAG;
254     if(arch_os_thread_init(th)==0) {
255         /* FIXME: handle error */
256         lose("arch_os_thread_init failed\n");
257     }
258
259     th->os_thread=thread_self();
260     protect_control_stack_guard_page(1);
261     /* Since GC can only know about this thread from the all_threads
262      * list and we're just adding this thread to it there is no danger
263      * of deadlocking even with SIG_STOP_FOR_GC blocked (which it is
264      * not). */
265     lock_ret = pthread_mutex_lock(&all_threads_lock);
266     gc_assert(lock_ret == 0);
267     link_thread(th);
268     lock_ret = pthread_mutex_unlock(&all_threads_lock);
269     gc_assert(lock_ret == 0);
270
271     result = funcall0(function);
272
273     /* Block GC */
274     block_blockable_signals();
275     th->state=STATE_DEAD;
276
277     /* SIG_STOP_FOR_GC is blocked and GC might be waiting for this
278      * thread, but since we are already dead it won't wait long. */
279     lock_ret = pthread_mutex_lock(&all_threads_lock);
280     gc_assert(lock_ret == 0);
281
282     gc_alloc_update_page_tables(BOXED_PAGE_FLAG, &th->alloc_region);
283     unlink_thread(th);
284     pthread_mutex_unlock(&all_threads_lock);
285     gc_assert(lock_ret == 0);
286
287     if(th->tls_cookie>=0) arch_os_thread_cleanup(th);
288     os_invalidate((os_vm_address_t)th->interrupt_data,
289                   (sizeof (struct interrupt_data)));
290
291 #ifdef LISP_FEATURE_MACH_EXCEPTION_HANDLER
292     FSHOW((stderr, "Deallocating mach port %x\n", THREAD_STRUCT_TO_EXCEPTION_PORT(th)));
293     mach_port_move_member(mach_task_self(),
294                           THREAD_STRUCT_TO_EXCEPTION_PORT(th),
295                           MACH_PORT_NULL);
296     mach_port_deallocate(mach_task_self(),
297                          THREAD_STRUCT_TO_EXCEPTION_PORT(th));
298     mach_port_destroy(mach_task_self(),
299                       THREAD_STRUCT_TO_EXCEPTION_PORT(th));
300 #endif
301
302     schedule_thread_post_mortem(th);
303     FSHOW((stderr,"/exiting thread %lu\n", thread_self()));
304     return result;
305 }
306
307 #endif /* LISP_FEATURE_SB_THREAD */
308
309 static void
310 free_thread_struct(struct thread *th)
311 {
312     if (th->interrupt_data)
313         os_invalidate((os_vm_address_t) th->interrupt_data,
314                       (sizeof (struct interrupt_data)));
315     os_invalidate((os_vm_address_t) th->os_address,
316                   THREAD_STRUCT_SIZE);
317 }
318
319 /* this is called from any other thread to create the new one, and
320  * initialize all parts of it that can be initialized from another
321  * thread
322  */
323
324 static struct thread *
325 create_thread_struct(lispobj initial_function) {
326     union per_thread_data *per_thread;
327     struct thread *th=0;        /*  subdue gcc */
328     void *spaces=0;
329     void *aligned_spaces=0;
330 #ifdef LISP_FEATURE_SB_THREAD
331     unsigned int i;
332 #endif
333
334     /* May as well allocate all the spaces at once: it saves us from
335      * having to decide what to do if only some of the allocations
336      * succeed. SPACES must be appropriately aligned, since the GC
337      * expects the control stack to start at a page boundary -- and
338      * the OS may have even more rigorous requirements. We can't rely
339      * on the alignment passed from os_validate, since that might
340      * assume the current (e.g. 4k) pagesize, while we calculate with
341      * the biggest (e.g. 64k) pagesize allowed by the ABI. */
342     spaces=os_validate(0, THREAD_STRUCT_SIZE);
343     if(!spaces)
344         return NULL;
345     /* Aligning up is safe as THREAD_STRUCT_SIZE has
346      * THREAD_ALIGNMENT_BYTES padding. */
347     aligned_spaces = (void *)((((unsigned long)(char *)spaces)
348                                + THREAD_ALIGNMENT_BYTES-1)
349                               &~(unsigned long)(THREAD_ALIGNMENT_BYTES-1));
350     per_thread=(union per_thread_data *)
351         (aligned_spaces+
352          thread_control_stack_size+
353          BINDING_STACK_SIZE+
354          ALIEN_STACK_SIZE);
355
356 #ifdef LISP_FEATURE_SB_THREAD
357     for(i = 0; i < (dynamic_values_bytes / sizeof(lispobj)); i++)
358         per_thread->dynamic_values[i] = NO_TLS_VALUE_MARKER_WIDETAG;
359     if (all_threads == 0) {
360         if(SymbolValue(FREE_TLS_INDEX,0)==UNBOUND_MARKER_WIDETAG) {
361             SetSymbolValue
362                 (FREE_TLS_INDEX,
363                  /* FIXME: should be MAX_INTERRUPTS -1 ? */
364                  make_fixnum(MAX_INTERRUPTS+
365                              sizeof(struct thread)/sizeof(lispobj)),
366                  0);
367             SetSymbolValue(TLS_INDEX_LOCK,make_fixnum(0),0);
368         }
369 #define STATIC_TLS_INIT(sym,field) \
370   ((struct symbol *)(sym-OTHER_POINTER_LOWTAG))->tls_index= \
371   make_fixnum(THREAD_SLOT_OFFSET_WORDS(field))
372
373         STATIC_TLS_INIT(BINDING_STACK_START,binding_stack_start);
374         STATIC_TLS_INIT(BINDING_STACK_POINTER,binding_stack_pointer);
375         STATIC_TLS_INIT(CONTROL_STACK_START,control_stack_start);
376         STATIC_TLS_INIT(CONTROL_STACK_END,control_stack_end);
377         STATIC_TLS_INIT(ALIEN_STACK,alien_stack_pointer);
378 #if defined(LISP_FEATURE_X86) || defined (LISP_FEATURE_X86_64)
379         STATIC_TLS_INIT(PSEUDO_ATOMIC_BITS,pseudo_atomic_bits);
380 #endif
381 #undef STATIC_TLS_INIT
382     }
383 #endif
384
385     th=&per_thread->thread;
386     th->os_address = spaces;
387     th->control_stack_start = aligned_spaces;
388     th->binding_stack_start=
389         (lispobj*)((void*)th->control_stack_start+thread_control_stack_size);
390     th->control_stack_end = th->binding_stack_start;
391     th->alien_stack_start=
392         (lispobj*)((void*)th->binding_stack_start+BINDING_STACK_SIZE);
393     th->binding_stack_pointer=th->binding_stack_start;
394     th->this=th;
395     th->os_thread=0;
396 #ifdef LISP_FEATURE_SB_THREAD
397     th->os_attr=malloc(sizeof(pthread_attr_t));
398 #endif
399     th->state=STATE_RUNNING;
400 #ifdef LISP_FEATURE_STACK_GROWS_DOWNWARD_NOT_UPWARD
401     th->alien_stack_pointer=((void *)th->alien_stack_start
402                              + ALIEN_STACK_SIZE-N_WORD_BYTES);
403 #else
404     th->alien_stack_pointer=((void *)th->alien_stack_start);
405 #endif
406 #if defined(LISP_FEATURE_X86) || defined (LISP_FEATURE_X86_64)
407     th->pseudo_atomic_bits=0;
408 #endif
409 #ifdef LISP_FEATURE_GENCGC
410     gc_set_region_empty(&th->alloc_region);
411 #endif
412
413 #ifndef LISP_FEATURE_SB_THREAD
414     /* the tls-points-into-struct-thread trick is only good for threaded
415      * sbcl, because unithread sbcl doesn't have tls.  So, we copy the
416      * appropriate values from struct thread here, and make sure that
417      * we use the appropriate SymbolValue macros to access any of the
418      * variable quantities from the C runtime.  It's not quite OAOOM,
419      * it just feels like it */
420     SetSymbolValue(BINDING_STACK_START,(lispobj)th->binding_stack_start,th);
421     SetSymbolValue(CONTROL_STACK_START,(lispobj)th->control_stack_start,th);
422     SetSymbolValue(CONTROL_STACK_END,(lispobj)th->control_stack_end,th);
423 #if defined(LISP_FEATURE_X86) || defined (LISP_FEATURE_X86_64)
424     SetSymbolValue(BINDING_STACK_POINTER,(lispobj)th->binding_stack_pointer,th);
425     SetSymbolValue(ALIEN_STACK,(lispobj)th->alien_stack_pointer,th);
426     SetSymbolValue(PSEUDO_ATOMIC_BITS,(lispobj)th->pseudo_atomic_bits,th);
427 #else
428     current_binding_stack_pointer=th->binding_stack_pointer;
429     current_control_stack_pointer=th->control_stack_start;
430 #endif
431 #endif
432     bind_variable(CURRENT_CATCH_BLOCK,make_fixnum(0),th);
433     bind_variable(CURRENT_UNWIND_PROTECT_BLOCK,make_fixnum(0),th);
434     bind_variable(FREE_INTERRUPT_CONTEXT_INDEX,make_fixnum(0),th);
435     bind_variable(INTERRUPT_PENDING, NIL,th);
436     bind_variable(INTERRUPTS_ENABLED,T,th);
437     bind_variable(ALLOW_WITH_INTERRUPTS,T,th);
438     bind_variable(GC_PENDING,NIL,th);
439     bind_variable(ALLOC_SIGNAL,NIL,th);
440 #ifdef LISP_FEATURE_SB_THREAD
441     bind_variable(STOP_FOR_GC_PENDING,NIL,th);
442 #endif
443
444     th->interrupt_data = (struct interrupt_data *)
445         os_validate(0,(sizeof (struct interrupt_data)));
446     if (!th->interrupt_data) {
447         free_thread_struct(th);
448         return 0;
449     }
450     th->interrupt_data->pending_handler = 0;
451     th->no_tls_value_marker=initial_function;
452
453     th->stepping = NIL;
454     return th;
455 }
456
457 #ifdef LISP_FEATURE_MACH_EXCEPTION_HANDLER
458 mach_port_t setup_mach_exception_handling_thread();
459 kern_return_t mach_thread_init(mach_port_t thread_exception_port);
460
461 #endif
462
463 void create_initial_thread(lispobj initial_function) {
464     struct thread *th=create_thread_struct(initial_function);
465     if(th) {
466 #ifdef LISP_FEATURE_MACH_EXCEPTION_HANDLER
467         setup_mach_exception_handling_thread();
468 #endif
469         initial_thread_trampoline(th); /* no return */
470     } else lose("can't create initial thread\n");
471 }
472
473 #ifdef LISP_FEATURE_SB_THREAD
474
475 #ifndef __USE_XOPEN2K
476 extern int pthread_attr_setstack (pthread_attr_t *__attr, void *__stackaddr,
477                                   size_t __stacksize);
478 #endif
479
480 boolean create_os_thread(struct thread *th,os_thread_t *kid_tid)
481 {
482     /* The new thread inherits the restrictive signal mask set here,
483      * and enables signals again when it is set up properly. */
484     sigset_t newset,oldset;
485     boolean r=1;
486     int retcode = 0, initcode;
487
488     FSHOW_SIGNAL((stderr,"/create_os_thread: creating new thread\n"));
489
490 #ifdef LOCK_CREATE_THREAD
491     retcode = pthread_mutex_lock(&create_thread_lock);
492     gc_assert(retcode == 0);
493     FSHOW_SIGNAL((stderr,"/create_os_thread: got lock\n"));
494 #endif
495     sigemptyset(&newset);
496     /* Blocking deferrable signals is enough, no need to block
497      * SIG_STOP_FOR_GC because the child process is not linked onto
498      * all_threads until it's ready. */
499     sigaddset_deferrable(&newset);
500     thread_sigmask(SIG_BLOCK, &newset, &oldset);
501
502     if((initcode = pthread_attr_init(th->os_attr)) ||
503        /* call_into_lisp_first_time switches the stack for the initial thread. For the
504         * others, we use this. */
505        (pthread_attr_setstack(th->os_attr,th->control_stack_start,thread_control_stack_size)) ||
506        (retcode = pthread_create
507         (kid_tid,th->os_attr,(void *(*)(void *))new_thread_trampoline,th))) {
508         FSHOW_SIGNAL((stderr, "init = %d\n", initcode));
509         FSHOW_SIGNAL((stderr, printf("pthread_create returned %d, errno %d\n", retcode, errno)));
510         if(retcode < 0) {
511             perror("create_os_thread");
512         }
513         r=0;
514     }
515
516     thread_sigmask(SIG_SETMASK,&oldset,0);
517 #ifdef LOCK_CREATE_THREAD
518     retcode = pthread_mutex_unlock(&create_thread_lock);
519     gc_assert(retcode == 0);
520     FSHOW_SIGNAL((stderr,"/create_os_thread: released lock\n"));
521 #endif
522     return r;
523 }
524
525 os_thread_t create_thread(lispobj initial_function) {
526     struct thread *th;
527     os_thread_t kid_tid;
528
529     /* Assuming that a fresh thread struct has no lisp objects in it,
530      * linking it to all_threads can be left to the thread itself
531      * without fear of gc lossage. initial_function violates this
532      * assumption and must stay pinned until the child starts up. */
533     th = create_thread_struct(initial_function);
534     if(th==0) return 0;
535
536     if (create_os_thread(th,&kid_tid)) {
537         return kid_tid;
538     } else {
539         free_thread_struct(th);
540         return 0;
541     }
542 }
543
544 /* Send the signo to os_thread, retry if the rt signal queue is
545  * full. */
546 int
547 kill_thread_safely(os_thread_t os_thread, int signo)
548 {
549     int r;
550     /* The man page does not mention EAGAIN as a valid return value
551      * for either pthread_kill or kill. But that's theory, this is
552      * practice. By waiting here we assume that the delivery of this
553      * signal is not necessary for the delivery of the signals in the
554      * queue. In other words, we _assume_ there are no deadlocks. */
555     while ((r=pthread_kill(os_thread,signo))==EAGAIN) {
556         /* wait a bit then try again in the hope of the rt signal
557          * queue not being full */
558         FSHOW_SIGNAL((stderr,"/rt signal queue full\n"));
559         /* FIXME: some kind of backoff (random, exponential) would be
560          * nice. */
561         sleep(1);
562     }
563     return r;
564 }
565
566 int signal_interrupt_thread(os_thread_t os_thread)
567 {
568     int status = kill_thread_safely(os_thread, SIG_INTERRUPT_THREAD);
569     FSHOW_SIGNAL((stderr,"/signal_interrupt_thread: %lu\n", os_thread));
570     if (status == 0) {
571         return 0;
572     } else if (status == ESRCH) {
573         return -1;
574     } else {
575         lose("cannot send SIG_INTERRUPT_THREAD to thread=%lu: %d, %s\n",
576              os_thread, status, strerror(status));
577     }
578 }
579
580 /* stopping the world is a two-stage process.  From this thread we signal
581  * all the others with SIG_STOP_FOR_GC.  The handler for this signal does
582  * the usual pseudo-atomic checks (we don't want to stop a thread while
583  * it's in the middle of allocation) then waits for another SIG_STOP_FOR_GC.
584  */
585
586 /* To avoid deadlocks when gc stops the world all clients of each
587  * mutex must enable or disable SIG_STOP_FOR_GC for the duration of
588  * holding the lock, but they must agree on which. */
589 void gc_stop_the_world()
590 {
591     struct thread *p,*th=arch_os_get_current_thread();
592     int status, lock_ret;
593 #ifdef LOCK_CREATE_THREAD
594     /* KLUDGE: Stopping the thread during pthread_create() causes deadlock
595      * on FreeBSD. */
596     FSHOW_SIGNAL((stderr,"/gc_stop_the_world:waiting on create_thread_lock\n"));
597     lock_ret = pthread_mutex_lock(&create_thread_lock);
598     gc_assert(lock_ret == 0);
599     FSHOW_SIGNAL((stderr,"/gc_stop_the_world:got create_thread_lock\n"));
600 #endif
601     FSHOW_SIGNAL((stderr,"/gc_stop_the_world:waiting on lock\n"));
602     /* keep threads from starting while the world is stopped. */
603     lock_ret = pthread_mutex_lock(&all_threads_lock);      \
604     gc_assert(lock_ret == 0);
605
606     FSHOW_SIGNAL((stderr,"/gc_stop_the_world:got lock\n"));
607     /* stop all other threads by sending them SIG_STOP_FOR_GC */
608     for(p=all_threads; p; p=p->next) {
609         gc_assert(p->os_thread != 0);
610         FSHOW_SIGNAL((stderr,"/gc_stop_the_world: thread=%lu, state=%x\n",
611                       p->os_thread, p->state));
612         if((p!=th) && ((p->state==STATE_RUNNING))) {
613             FSHOW_SIGNAL((stderr,"/gc_stop_the_world: suspending thread %lu\n",
614                           p->os_thread));
615             status=kill_thread_safely(p->os_thread,SIG_STOP_FOR_GC);
616             if (status==ESRCH) {
617                 /* This thread has exited. */
618                 gc_assert(p->state==STATE_DEAD);
619             } else if (status) {
620                 lose("cannot send suspend thread=%lu: %d, %s\n",
621                      p->os_thread,status,strerror(status));
622             }
623         }
624     }
625     FSHOW_SIGNAL((stderr,"/gc_stop_the_world:signals sent\n"));
626     /* wait for the running threads to stop or finish */
627     {
628 #if QSHOW_SIGNALS
629         struct thread *prev = 0;
630 #endif
631         for(p=all_threads;p;) {
632 #if QSHOW_SIGNALS
633             if ((p!=th)&&(p!=prev)&&(p->state==STATE_RUNNING)) {
634                 FSHOW_SIGNAL
635                     ((stderr,
636                       "/gc_stop_the_world: waiting for thread=%lu: state=%x\n",
637                       p->os_thread, p->state));
638                 prev=p;
639             }
640 #endif
641             if((p!=th) && (p->state==STATE_RUNNING)) {
642                 sched_yield();
643             } else {
644                 p=p->next;
645             }
646         }
647     }
648     FSHOW_SIGNAL((stderr,"/gc_stop_the_world:end\n"));
649 }
650
651 void gc_start_the_world()
652 {
653     struct thread *p,*th=arch_os_get_current_thread();
654     int status, lock_ret;
655     /* if a resumed thread creates a new thread before we're done with
656      * this loop, the new thread will get consed on the front of
657      * all_threads, but it won't have been stopped so won't need
658      * restarting */
659     FSHOW_SIGNAL((stderr,"/gc_start_the_world:begin\n"));
660     for(p=all_threads;p;p=p->next) {
661         gc_assert(p->os_thread!=0);
662         if((p!=th) && (p->state!=STATE_DEAD)) {
663             if(p->state!=STATE_SUSPENDED) {
664                 lose("gc_start_the_world: wrong thread state is %d\n",
665                      fixnum_value(p->state));
666             }
667             FSHOW_SIGNAL((stderr, "/gc_start_the_world: resuming %lu\n",
668                           p->os_thread));
669             p->state=STATE_RUNNING;
670
671 #if defined(SIG_RESUME_FROM_GC)
672             status=kill_thread_safely(p->os_thread,SIG_RESUME_FROM_GC);
673 #else
674             status=kill_thread_safely(p->os_thread,SIG_STOP_FOR_GC);
675 #endif
676             if (status) {
677                 lose("cannot resume thread=%lu: %d, %s\n",
678                      p->os_thread,status,strerror(status));
679             }
680         }
681     }
682     /* If we waited here until all threads leave STATE_SUSPENDED, then
683      * SIG_STOP_FOR_GC wouldn't need to be a rt signal. That has some
684      * performance implications, but does away with the 'rt signal
685      * queue full' problem. */
686
687     lock_ret = pthread_mutex_unlock(&all_threads_lock);
688     gc_assert(lock_ret == 0);
689 #ifdef LOCK_CREATE_THREAD
690     lock_ret = pthread_mutex_unlock(&create_thread_lock);
691     gc_assert(lock_ret == 0);
692 #endif
693
694     FSHOW_SIGNAL((stderr,"/gc_start_the_world:end\n"));
695 }
696 #endif
697
698 int
699 thread_yield()
700 {
701 #ifdef LISP_FEATURE_SB_THREAD
702     return sched_yield();
703 #else
704     return 0;
705 #endif
706 }