Preliminary work towards threads on win32
[sbcl.git] / src / runtime / thread.c
blobcc9eebd0ed3cec0c8a8385be10b5f5a843d574d6
1 /*
2 * This software is part of the SBCL system. See the README file for
3 * more information.
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.
12 #include "sbcl.h"
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 "runtime.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
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
34 #include "runtime.h"
35 #include "validate.h" /* for BINDING_STACK_SIZE etc */
36 #include "thread.h"
37 #include "arch.h"
38 #include "target-arch-os.h"
39 #include "os.h"
40 #include "globals.h"
41 #include "dynbind.h"
42 #include "genesis/cons.h"
43 #include "genesis/fdefn.h"
44 #include "interr.h" /* for lose() */
45 #include "alloc.h"
46 #include "gc-internal.h"
47 #include "cpputil.h"
48 #include "pseudo-atomic.h"
49 #include "interrupt.h"
50 #include "lispregs.h"
52 #if defined(LISP_FEATURE_WIN32) && defined(LISP_FEATURE_SB_THREAD)
53 # define IMMEDIATE_POST_MORTEM
54 #endif
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
61 #ifdef LISP_FEATURE_FREEBSD
62 #define CREATE_CLEANUP_THREAD
63 #define LOCK_CREATE_THREAD
64 #endif
66 #ifdef LISP_FEATURE_SB_THREAD
67 struct thread_post_mortem {
68 #ifdef DELAY_THREAD_POST_MORTEM
69 struct thread_post_mortem *next;
70 #endif
71 os_thread_t os_thread;
72 pthread_attr_t *os_attr;
73 os_vm_address_t os_address;
76 #ifdef DELAY_THREAD_POST_MORTEM
77 static int pending_thread_post_mortem_count = 0;
78 pthread_mutex_t thread_post_mortem_lock = PTHREAD_MUTEX_INITIALIZER;
79 #endif
80 static struct thread_post_mortem * volatile pending_thread_post_mortem = 0;
81 #endif
83 int dynamic_values_bytes=TLS_SIZE*sizeof(lispobj); /* same for all threads */
84 struct thread *all_threads;
85 extern struct interrupt_data * global_interrupt_data;
87 #ifdef LISP_FEATURE_SB_THREAD
88 pthread_mutex_t all_threads_lock = PTHREAD_MUTEX_INITIALIZER;
89 #ifdef LOCK_CREATE_THREAD
90 static pthread_mutex_t create_thread_lock = PTHREAD_MUTEX_INITIALIZER;
91 #endif
92 #ifdef LISP_FEATURE_GCC_TLS
93 __thread struct thread *current_thread;
94 #endif
95 pthread_key_t lisp_thread = 0;
96 #endif
98 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
99 extern lispobj call_into_lisp_first_time(lispobj fun, lispobj *args, int nargs);
100 #endif
102 static void
103 link_thread(struct thread *th)
105 if (all_threads) all_threads->prev=th;
106 th->next=all_threads;
107 th->prev=0;
108 all_threads=th;
111 #ifdef LISP_FEATURE_SB_THREAD
112 static void
113 unlink_thread(struct thread *th)
115 if (th->prev)
116 th->prev->next = th->next;
117 else
118 all_threads = th->next;
119 if (th->next)
120 th->next->prev = th->prev;
123 /* Only access thread state with blockables blocked. */
124 lispobj
125 thread_state(struct thread *thread)
127 lispobj state;
128 sigset_t old;
129 block_blockable_signals(NULL, &old);
130 os_sem_wait(thread->state_sem, "thread_state");
131 state = thread->state;
132 os_sem_post(thread->state_sem, "thread_state");
133 thread_sigmask(SIG_SETMASK, &old, NULL);
134 return state;
137 void
138 set_thread_state(struct thread *thread, lispobj state)
140 int i, waitcount = 0;
141 sigset_t old;
142 block_blockable_signals(NULL, &old);
143 os_sem_wait(thread->state_sem, "set_thread_state");
144 if (thread->state != state) {
145 if ((STATE_STOPPED==state) ||
146 (STATE_DEAD==state)) {
147 waitcount = thread->state_not_running_waitcount;
148 thread->state_not_running_waitcount = 0;
149 for (i=0; i<waitcount; i++)
150 os_sem_post(thread->state_not_running_sem, "set_thread_state (not running)");
152 if ((STATE_RUNNING==state) ||
153 (STATE_DEAD==state)) {
154 waitcount = thread->state_not_stopped_waitcount;
155 thread->state_not_stopped_waitcount = 0;
156 for (i=0; i<waitcount; i++)
157 os_sem_post(thread->state_not_stopped_sem, "set_thread_state (not stopped)");
159 thread->state = state;
161 os_sem_post(thread->state_sem, "set_thread_state");
162 thread_sigmask(SIG_SETMASK, &old, NULL);
165 void
166 wait_for_thread_state_change(struct thread *thread, lispobj state)
168 sigset_t old;
169 os_sem_t *wait_sem;
170 block_blockable_signals(NULL, &old);
171 start:
172 os_sem_wait(thread->state_sem, "wait_for_thread_state_change");
173 if (thread->state == state) {
174 switch (state) {
175 case STATE_RUNNING:
176 wait_sem = thread->state_not_running_sem;
177 thread->state_not_running_waitcount++;
178 break;
179 case STATE_STOPPED:
180 wait_sem = thread->state_not_stopped_sem;
181 thread->state_not_stopped_waitcount++;
182 break;
183 default:
184 lose("Invalid state in wait_for_thread_state_change: "OBJ_FMTX"\n", state);
186 } else {
187 wait_sem = NULL;
189 os_sem_post(thread->state_sem, "wait_for_thread_state_change");
190 if (wait_sem) {
191 os_sem_wait(wait_sem, "wait_for_thread_state_change");
192 goto start;
194 thread_sigmask(SIG_SETMASK, &old, NULL);
196 #endif
198 static int
199 initial_thread_trampoline(struct thread *th)
201 lispobj function;
202 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
203 lispobj *args = NULL;
204 #endif
205 #ifdef LISP_FEATURE_SB_THREAD
206 pthread_setspecific(lisp_thread, (void *)1);
207 #endif
208 #if defined(THREADS_USING_GCSIGNAL) && defined(LISP_FEATURE_PPC)
209 /* SIG_STOP_FOR_GC defaults to blocked on PPC? */
210 unblock_gc_signals(0,0);
211 #endif
212 function = th->no_tls_value_marker;
213 th->no_tls_value_marker = NO_TLS_VALUE_MARKER_WIDETAG;
214 if(arch_os_thread_init(th)==0) return 1;
215 #ifdef LISP_FEATURE_SB_SAFEPOINT
216 pthread_mutex_lock(thread_qrl(th));
217 #endif
218 link_thread(th);
219 th->os_thread=thread_self();
220 #ifndef LISP_FEATURE_WIN32
221 protect_control_stack_hard_guard_page(1, NULL);
222 #endif
223 protect_binding_stack_hard_guard_page(1, NULL);
224 protect_alien_stack_hard_guard_page(1, NULL);
225 #ifndef LISP_FEATURE_WIN32
226 protect_control_stack_guard_page(1, NULL);
227 #endif
228 protect_binding_stack_guard_page(1, NULL);
229 protect_alien_stack_guard_page(1, NULL);
231 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
232 return call_into_lisp_first_time(function,args,0);
233 #else
234 return funcall0(function);
235 #endif
238 #ifdef LISP_FEATURE_SB_THREAD
240 # if defined(IMMEDIATE_POST_MORTEM)
243 * If this feature is set, we are running on a stack managed by the OS,
244 * and no fancy delays are required for anything. Just do it.
246 static void
247 schedule_thread_post_mortem(struct thread *corpse)
249 pthread_detach(pthread_self());
250 gc_assert(!pthread_attr_destroy(corpse->os_attr));
251 free(corpse->os_attr);
252 #if defined(LISP_FEATURE_WIN32)
253 os_invalidate_free(corpse->os_address, THREAD_STRUCT_SIZE);
254 #else
255 os_invalidate(corpse->os_address, THREAD_STRUCT_SIZE);
256 #endif
259 # else
261 /* THREAD POST MORTEM CLEANUP
263 * Memory allocated for the thread stacks cannot be reclaimed while
264 * the thread is still alive, so we need a mechanism for post mortem
265 * cleanups. FIXME: We actually have three, for historical reasons as
266 * the saying goes. Do we really need three? Nikodemus guesses that
267 * not anymore, now that we properly call pthread_attr_destroy before
268 * freeing the stack. */
270 static struct thread_post_mortem *
271 plan_thread_post_mortem(struct thread *corpse)
273 if (corpse) {
274 struct thread_post_mortem *post_mortem = malloc(sizeof(struct thread_post_mortem));
275 gc_assert(post_mortem);
276 post_mortem->os_thread = corpse->os_thread;
277 post_mortem->os_attr = corpse->os_attr;
278 post_mortem->os_address = corpse->os_address;
279 #ifdef DELAY_THREAD_POST_MORTEM
280 post_mortem->next = NULL;
281 #endif
282 return post_mortem;
283 } else {
284 /* FIXME: When does this happen? */
285 return NULL;
289 static void
290 perform_thread_post_mortem(struct thread_post_mortem *post_mortem)
292 #ifdef CREATE_POST_MORTEM_THREAD
293 pthread_detach(pthread_self());
294 #endif
295 if (post_mortem) {
296 gc_assert(!pthread_join(post_mortem->os_thread, NULL));
297 gc_assert(!pthread_attr_destroy(post_mortem->os_attr));
298 free(post_mortem->os_attr);
299 os_invalidate(post_mortem->os_address, THREAD_STRUCT_SIZE);
300 free(post_mortem);
304 static void
305 schedule_thread_post_mortem(struct thread *corpse)
307 struct thread_post_mortem *post_mortem = NULL;
308 if (corpse) {
309 post_mortem = plan_thread_post_mortem(corpse);
311 #ifdef DELAY_THREAD_POST_MORTEM
312 pthread_mutex_lock(&thread_post_mortem_lock);
313 /* First stick the new post mortem to the end of the queue. */
314 if (pending_thread_post_mortem) {
315 struct thread_post_mortem *next = pending_thread_post_mortem;
316 while (next->next) {
317 next = next->next;
319 next->next = post_mortem;
320 } else {
321 pending_thread_post_mortem = post_mortem;
323 /* Then, if there are enough things in the queue, clean up one
324 * from the head -- or increment the count, and null out the
325 * post_mortem we have. */
326 if (pending_thread_post_mortem_count > DELAY_THREAD_POST_MORTEM) {
327 post_mortem = pending_thread_post_mortem;
328 pending_thread_post_mortem = post_mortem->next;
329 } else {
330 pending_thread_post_mortem_count++;
331 post_mortem = NULL;
333 pthread_mutex_unlock(&thread_post_mortem_lock);
334 /* Finally run, the cleanup, if any. */
335 perform_thread_post_mortem(post_mortem);
336 #elif defined(CREATE_POST_MORTEM_THREAD)
337 gc_assert(!pthread_create(&thread, NULL, perform_thread_post_mortem, post_mortem));
338 #else
339 post_mortem = (struct thread_post_mortem *)
340 swap_lispobjs((lispobj *)(void *)&pending_thread_post_mortem,
341 (lispobj)post_mortem);
342 perform_thread_post_mortem(post_mortem);
343 #endif
347 # endif /* !IMMEDIATE_POST_MORTEM */
349 /* this is the first thing that runs in the child (which is why the
350 * silly calling convention). Basically it calls the user's requested
351 * lisp function after doing arch_os_thread_init and whatever other
352 * bookkeeping needs to be done
355 new_thread_trampoline(struct thread *th)
357 lispobj function;
358 int result, lock_ret;
360 FSHOW((stderr,"/creating thread %lu\n", thread_self()));
361 check_deferrables_blocked_or_lose(0);
362 #ifndef LISP_FEATURE_SB_SAFEPOINT
363 check_gc_signals_unblocked_or_lose(0);
364 #endif
365 pthread_setspecific(lisp_thread, (void *)1);
366 function = th->no_tls_value_marker;
367 th->no_tls_value_marker = NO_TLS_VALUE_MARKER_WIDETAG;
368 if(arch_os_thread_init(th)==0) {
369 /* FIXME: handle error */
370 lose("arch_os_thread_init failed\n");
373 th->os_thread=thread_self();
374 protect_control_stack_guard_page(1, NULL);
375 protect_binding_stack_guard_page(1, NULL);
376 protect_alien_stack_guard_page(1, NULL);
377 /* Since GC can only know about this thread from the all_threads
378 * list and we're just adding this thread to it, there is no
379 * danger of deadlocking even with SIG_STOP_FOR_GC blocked (which
380 * it is not). */
381 #ifdef LISP_FEATURE_SB_SAFEPOINT
382 *th->csp_around_foreign_call = (lispobj)&function;
383 pthread_mutex_lock(thread_qrl(th));
384 #endif
385 lock_ret = pthread_mutex_lock(&all_threads_lock);
386 gc_assert(lock_ret == 0);
387 link_thread(th);
388 lock_ret = pthread_mutex_unlock(&all_threads_lock);
389 gc_assert(lock_ret == 0);
391 /* Kludge: Changed the order of some steps between the safepoint/
392 * non-safepoint versions of this code. Can we unify this more?
394 #ifdef LISP_FEATURE_SB_SAFEPOINT
395 WITH_GC_AT_SAFEPOINTS_ONLY() {
396 result = funcall0(function);
397 block_blockable_signals(0, 0);
398 gc_alloc_update_page_tables(BOXED_PAGE_FLAG, &th->alloc_region);
400 lock_ret = pthread_mutex_lock(&all_threads_lock);
401 gc_assert(lock_ret == 0);
402 unlink_thread(th);
403 lock_ret = pthread_mutex_unlock(&all_threads_lock);
404 gc_assert(lock_ret == 0);
405 pthread_mutex_unlock(thread_qrl(th));
406 set_thread_state(th,STATE_DEAD);
407 #else
408 result = funcall0(function);
410 /* Block GC */
411 block_blockable_signals(0, 0);
412 set_thread_state(th, STATE_DEAD);
414 /* SIG_STOP_FOR_GC is blocked and GC might be waiting for this
415 * thread, but since we are already dead it won't wait long. */
416 lock_ret = pthread_mutex_lock(&all_threads_lock);
417 gc_assert(lock_ret == 0);
419 gc_alloc_update_page_tables(BOXED_PAGE_FLAG, &th->alloc_region);
420 unlink_thread(th);
421 pthread_mutex_unlock(&all_threads_lock);
422 gc_assert(lock_ret == 0);
423 #endif
425 if(th->tls_cookie>=0) arch_os_thread_cleanup(th);
426 os_sem_destroy(th->state_sem);
427 os_sem_destroy(th->state_not_running_sem);
428 os_sem_destroy(th->state_not_stopped_sem);
430 #if defined(LISP_FEATURE_WIN32)
431 free((os_vm_address_t)th->interrupt_data);
432 #else
433 os_invalidate((os_vm_address_t)th->interrupt_data,
434 (sizeof (struct interrupt_data)));
435 #endif
437 #ifdef LISP_FEATURE_MACH_EXCEPTION_HANDLER
438 mach_lisp_thread_destroy(th);
439 #endif
441 #if defined(LISP_FEATURE_WIN32)
442 int i;
443 for (i = 0; i<
444 (int) (sizeof(th->private_events.events)/
445 sizeof(th->private_events.events[0])); ++i) {
446 CloseHandle(th->private_events.events[i]);
448 TlsSetValue(OUR_TLS_INDEX,NULL);
449 #endif
451 schedule_thread_post_mortem(th);
452 FSHOW((stderr,"/exiting thread %lu\n", thread_self()));
453 return result;
456 #endif /* LISP_FEATURE_SB_THREAD */
458 static void
459 free_thread_struct(struct thread *th)
461 #if defined(LISP_FEATURE_WIN32)
462 if (th->interrupt_data) {
463 os_invalidate_free((os_vm_address_t) th->interrupt_data,
464 (sizeof (struct interrupt_data)));
466 os_invalidate_free((os_vm_address_t) th->os_address,
467 THREAD_STRUCT_SIZE);
468 #else
469 if (th->interrupt_data)
470 os_invalidate((os_vm_address_t) th->interrupt_data,
471 (sizeof (struct interrupt_data)));
472 os_invalidate((os_vm_address_t) th->os_address,
473 THREAD_STRUCT_SIZE);
474 #endif
477 #ifdef LISP_FEATURE_SB_THREAD
478 /* FIXME: should be MAX_INTERRUPTS -1 ? */
479 const unsigned int tls_index_start =
480 MAX_INTERRUPTS + sizeof(struct thread)/sizeof(lispobj);
481 #endif
483 /* this is called from any other thread to create the new one, and
484 * initialize all parts of it that can be initialized from another
485 * thread
488 static struct thread *
489 create_thread_struct(lispobj initial_function) {
490 union per_thread_data *per_thread;
491 struct thread *th=0; /* subdue gcc */
492 void *spaces=0;
493 void *aligned_spaces=0;
494 #if defined(LISP_FEATURE_SB_THREAD) || defined(LISP_FEATURE_WIN32)
495 unsigned int i;
496 #endif
498 /* May as well allocate all the spaces at once: it saves us from
499 * having to decide what to do if only some of the allocations
500 * succeed. SPACES must be appropriately aligned, since the GC
501 * expects the control stack to start at a page boundary -- and
502 * the OS may have even more rigorous requirements. We can't rely
503 * on the alignment passed from os_validate, since that might
504 * assume the current (e.g. 4k) pagesize, while we calculate with
505 * the biggest (e.g. 64k) pagesize allowed by the ABI. */
506 spaces=os_validate(0, THREAD_STRUCT_SIZE);
507 if(!spaces)
508 return NULL;
509 /* Aligning up is safe as THREAD_STRUCT_SIZE has
510 * THREAD_ALIGNMENT_BYTES padding. */
511 aligned_spaces = (void *)((((unsigned long)(char *)spaces)
512 + THREAD_ALIGNMENT_BYTES-1)
513 &~(unsigned long)(THREAD_ALIGNMENT_BYTES-1));
514 void* csp_page=
515 (aligned_spaces+
516 thread_control_stack_size+
517 BINDING_STACK_SIZE+
518 ALIEN_STACK_SIZE);
519 per_thread=(union per_thread_data *)
520 (csp_page + THREAD_CSP_PAGE_SIZE);
521 struct nonpointer_thread_data *nonpointer_data
522 = (void *) &per_thread->dynamic_values[TLS_SIZE];
524 #ifdef LISP_FEATURE_SB_THREAD
525 for(i = 0; i < (dynamic_values_bytes / sizeof(lispobj)); i++)
526 per_thread->dynamic_values[i] = NO_TLS_VALUE_MARKER_WIDETAG;
527 if (all_threads == 0) {
528 if(SymbolValue(FREE_TLS_INDEX,0)==UNBOUND_MARKER_WIDETAG) {
529 SetSymbolValue(FREE_TLS_INDEX,tls_index_start << WORD_SHIFT,0);
530 SetSymbolValue(TLS_INDEX_LOCK,make_fixnum(0),0);
532 #define STATIC_TLS_INIT(sym,field) \
533 ((struct symbol *)(sym-OTHER_POINTER_LOWTAG))->tls_index= \
534 (THREAD_SLOT_OFFSET_WORDS(field) << WORD_SHIFT)
536 STATIC_TLS_INIT(BINDING_STACK_START,binding_stack_start);
537 #ifdef BINDING_STACK_POINTER
538 STATIC_TLS_INIT(BINDING_STACK_POINTER,binding_stack_pointer);
539 #endif
540 STATIC_TLS_INIT(CONTROL_STACK_START,control_stack_start);
541 STATIC_TLS_INIT(CONTROL_STACK_END,control_stack_end);
542 #ifdef ALIEN_STACK
543 STATIC_TLS_INIT(ALIEN_STACK,alien_stack_pointer);
544 #endif
545 #if defined(LISP_FEATURE_X86) || defined (LISP_FEATURE_X86_64)
546 STATIC_TLS_INIT(PSEUDO_ATOMIC_BITS,pseudo_atomic_bits);
547 #endif
548 #undef STATIC_TLS_INIT
550 #endif
552 th=&per_thread->thread;
553 th->os_address = spaces;
554 th->control_stack_start = aligned_spaces;
555 th->binding_stack_start=
556 (lispobj*)((void*)th->control_stack_start+thread_control_stack_size);
557 th->control_stack_end = th->binding_stack_start;
558 th->control_stack_guard_page_protected = T;
559 th->alien_stack_start=
560 (lispobj*)((void*)th->binding_stack_start+BINDING_STACK_SIZE);
561 set_binding_stack_pointer(th,th->binding_stack_start);
562 th->this=th;
563 th->os_thread=0;
565 #ifdef LISP_FEATURE_SB_SAFEPOINT
566 th->pc_around_foreign_call = 0;
567 th->csp_around_foreign_call = csp_page;
568 #endif
570 #ifdef LISP_FEATURE_SB_THREAD
571 /* Contrary to the "allocate all the spaces at once" comment above,
572 * the os_attr is allocated separately. We cannot put it into the
573 * nonpointer data, because it's used for post_mortem and freed
574 * separately */
575 th->os_attr=malloc(sizeof(pthread_attr_t));
576 th->nonpointer_data = nonpointer_data;
577 th->state_sem=&nonpointer_data->state_sem;
578 th->state_not_running_sem=&nonpointer_data->state_not_running_sem;
579 th->state_not_stopped_sem=&nonpointer_data->state_not_stopped_sem;
580 th->state_not_running_waitcount = 0;
581 th->state_not_stopped_waitcount = 0;
582 os_sem_init(th->state_sem, 1);
583 os_sem_init(th->state_not_running_sem, 0);
584 os_sem_init(th->state_not_stopped_sem, 0);
585 # ifdef LISP_FEATURE_SB_SAFEPOINT
586 pthread_mutex_init(thread_qrl(th), NULL);
587 # endif
588 #endif
589 th->state=STATE_RUNNING;
590 #ifdef LISP_FEATURE_STACK_GROWS_DOWNWARD_NOT_UPWARD
591 th->alien_stack_pointer=((void *)th->alien_stack_start
592 + ALIEN_STACK_SIZE-N_WORD_BYTES);
593 #else
594 th->alien_stack_pointer=((void *)th->alien_stack_start);
595 #endif
596 #if defined(LISP_FEATURE_X86) || defined (LISP_FEATURE_X86_64) || defined(LISP_FEATURE_SB_THREAD)
597 th->pseudo_atomic_bits=0;
598 #endif
599 #ifdef LISP_FEATURE_GENCGC
600 gc_set_region_empty(&th->alloc_region);
601 #endif
602 #ifdef LISP_FEATURE_SB_THREAD
603 /* This parallels the same logic in globals.c for the
604 * single-threaded foreign_function_call_active, KLUDGE and
605 * all. */
606 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
607 th->foreign_function_call_active = 0;
608 #else
609 th->foreign_function_call_active = 1;
610 #endif
611 #endif
613 #ifndef LISP_FEATURE_SB_THREAD
614 /* the tls-points-into-struct-thread trick is only good for threaded
615 * sbcl, because unithread sbcl doesn't have tls. So, we copy the
616 * appropriate values from struct thread here, and make sure that
617 * we use the appropriate SymbolValue macros to access any of the
618 * variable quantities from the C runtime. It's not quite OAOOM,
619 * it just feels like it */
620 SetSymbolValue(BINDING_STACK_START,(lispobj)th->binding_stack_start,th);
621 SetSymbolValue(CONTROL_STACK_START,(lispobj)th->control_stack_start,th);
622 SetSymbolValue(CONTROL_STACK_END,(lispobj)th->control_stack_end,th);
623 #if defined(LISP_FEATURE_X86) || defined (LISP_FEATURE_X86_64)
624 SetSymbolValue(ALIEN_STACK,(lispobj)th->alien_stack_pointer,th);
625 SetSymbolValue(PSEUDO_ATOMIC_BITS,(lispobj)th->pseudo_atomic_bits,th);
626 #endif
627 #endif
628 bind_variable(CURRENT_CATCH_BLOCK,make_fixnum(0),th);
629 bind_variable(CURRENT_UNWIND_PROTECT_BLOCK,make_fixnum(0),th);
630 bind_variable(FREE_INTERRUPT_CONTEXT_INDEX,make_fixnum(0),th);
631 bind_variable(INTERRUPT_PENDING, NIL,th);
632 bind_variable(INTERRUPTS_ENABLED,T,th);
633 bind_variable(ALLOW_WITH_INTERRUPTS,T,th);
634 bind_variable(GC_PENDING,NIL,th);
635 bind_variable(ALLOC_SIGNAL,NIL,th);
636 #ifdef PINNED_OBJECTS
637 bind_variable(PINNED_OBJECTS,NIL,th);
638 #endif
639 #ifdef LISP_FEATURE_SB_THREAD
640 bind_variable(STOP_FOR_GC_PENDING,NIL,th);
641 #endif
642 #if defined(LISP_FEATURE_SB_SAFEPOINT)
643 bind_variable(GC_SAFE,NIL,th);
644 bind_variable(IN_SAFEPOINT,NIL,th);
645 #endif
646 #ifdef LISP_FEATURE_SB_THRUPTION
647 bind_variable(THRUPTION_PENDING,NIL,th);
648 bind_variable(RESTART_CLUSTERS,NIL,th);
649 #endif
650 #ifndef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
651 access_control_stack_pointer(th)=th->control_stack_start;
652 #endif
654 #if defined(LISP_FEATURE_WIN32)
655 th->interrupt_data = (struct interrupt_data *)
656 calloc((sizeof (struct interrupt_data)),1);
657 #else
658 th->interrupt_data = (struct interrupt_data *)
659 os_validate(0,(sizeof (struct interrupt_data)));
660 #endif
661 if (!th->interrupt_data) {
662 free_thread_struct(th);
663 return 0;
665 th->interrupt_data->pending_handler = 0;
666 th->interrupt_data->gc_blocked_deferrables = 0;
667 #ifdef GENCGC_IS_PRECISE
668 th->interrupt_data->allocation_trap_context = 0;
669 #endif
670 th->no_tls_value_marker=initial_function;
672 #if defined(LISP_FEATURE_WIN32)
673 for (i = 0; i<sizeof(th->private_events.events)/
674 sizeof(th->private_events.events[0]); ++i) {
675 th->private_events.events[i] = CreateEvent(NULL,FALSE,FALSE,NULL);
677 #endif
678 th->stepping = NIL;
679 return th;
682 void create_initial_thread(lispobj initial_function) {
683 struct thread *th=create_thread_struct(initial_function);
684 #ifdef LISP_FEATURE_SB_THREAD
685 pthread_key_create(&lisp_thread, 0);
686 #endif
687 if(th) {
688 initial_thread_trampoline(th); /* no return */
689 } else lose("can't create initial thread\n");
692 #ifdef LISP_FEATURE_SB_THREAD
694 #ifndef __USE_XOPEN2K
695 extern int pthread_attr_setstack (pthread_attr_t *__attr, void *__stackaddr,
696 size_t __stacksize);
697 #endif
699 boolean create_os_thread(struct thread *th,os_thread_t *kid_tid)
701 /* The new thread inherits the restrictive signal mask set here,
702 * and enables signals again when it is set up properly. */
703 sigset_t oldset;
704 boolean r=1;
705 int retcode = 0, initcode;
707 FSHOW_SIGNAL((stderr,"/create_os_thread: creating new thread\n"));
709 /* Blocking deferrable signals is enough, no need to block
710 * SIG_STOP_FOR_GC because the child process is not linked onto
711 * all_threads until it's ready. */
712 block_deferrable_signals(0, &oldset);
714 #ifdef LOCK_CREATE_THREAD
715 retcode = pthread_mutex_lock(&create_thread_lock);
716 gc_assert(retcode == 0);
717 FSHOW_SIGNAL((stderr,"/create_os_thread: got lock\n"));
718 #endif
720 if((initcode = pthread_attr_init(th->os_attr)) ||
721 /* call_into_lisp_first_time switches the stack for the initial
722 * thread. For the others, we use this. */
723 #if defined(LISP_FEATURE_WIN32)
724 (pthread_attr_setstacksize(th->os_attr, thread_control_stack_size)) ||
725 #else
726 (pthread_attr_setstack(th->os_attr,th->control_stack_start,
727 thread_control_stack_size)) ||
728 #endif
729 (retcode = pthread_create
730 (kid_tid,th->os_attr,(void *(*)(void *))new_thread_trampoline,th))) {
731 FSHOW_SIGNAL((stderr, "init = %d\n", initcode));
732 FSHOW_SIGNAL((stderr, "pthread_create returned %d, errno %d\n",
733 retcode, errno));
734 if(retcode < 0) {
735 perror("create_os_thread");
737 r=0;
740 #ifdef LOCK_CREATE_THREAD
741 retcode = pthread_mutex_unlock(&create_thread_lock);
742 gc_assert(retcode == 0);
743 FSHOW_SIGNAL((stderr,"/create_os_thread: released lock\n"));
744 #endif
745 thread_sigmask(SIG_SETMASK,&oldset,0);
746 return r;
749 os_thread_t create_thread(lispobj initial_function) {
750 struct thread *th, *thread = arch_os_get_current_thread();
751 os_thread_t kid_tid = 0;
753 /* Must defend against async unwinds. */
754 if (SymbolValue(INTERRUPTS_ENABLED, thread) != NIL)
755 lose("create_thread is not safe when interrupts are enabled.\n");
757 /* Assuming that a fresh thread struct has no lisp objects in it,
758 * linking it to all_threads can be left to the thread itself
759 * without fear of gc lossage. initial_function violates this
760 * assumption and must stay pinned until the child starts up. */
761 th = create_thread_struct(initial_function);
762 if (th && !create_os_thread(th,&kid_tid)) {
763 free_thread_struct(th);
764 kid_tid = 0;
766 return kid_tid;
769 /* stopping the world is a two-stage process. From this thread we signal
770 * all the others with SIG_STOP_FOR_GC. The handler for this signal does
771 * the usual pseudo-atomic checks (we don't want to stop a thread while
772 * it's in the middle of allocation) then waits for another SIG_STOP_FOR_GC.
775 * (With SB-SAFEPOINT, see the definitions in safepoint.c instead.)
777 #ifndef LISP_FEATURE_SB_SAFEPOINT
779 /* To avoid deadlocks when gc stops the world all clients of each
780 * mutex must enable or disable SIG_STOP_FOR_GC for the duration of
781 * holding the lock, but they must agree on which. */
782 void gc_stop_the_world()
784 struct thread *p,*th=arch_os_get_current_thread();
785 int status, lock_ret;
786 #ifdef LOCK_CREATE_THREAD
787 /* KLUDGE: Stopping the thread during pthread_create() causes deadlock
788 * on FreeBSD. */
789 FSHOW_SIGNAL((stderr,"/gc_stop_the_world:waiting on create_thread_lock\n"));
790 lock_ret = pthread_mutex_lock(&create_thread_lock);
791 gc_assert(lock_ret == 0);
792 FSHOW_SIGNAL((stderr,"/gc_stop_the_world:got create_thread_lock\n"));
793 #endif
794 FSHOW_SIGNAL((stderr,"/gc_stop_the_world:waiting on lock\n"));
795 /* keep threads from starting while the world is stopped. */
796 lock_ret = pthread_mutex_lock(&all_threads_lock); \
797 gc_assert(lock_ret == 0);
799 FSHOW_SIGNAL((stderr,"/gc_stop_the_world:got lock\n"));
800 /* stop all other threads by sending them SIG_STOP_FOR_GC */
801 for(p=all_threads; p; p=p->next) {
802 gc_assert(p->os_thread != 0);
803 FSHOW_SIGNAL((stderr,"/gc_stop_the_world: thread=%lu, state=%x\n",
804 p->os_thread, thread_state(p)));
805 if((p!=th) && ((thread_state(p)==STATE_RUNNING))) {
806 FSHOW_SIGNAL((stderr,"/gc_stop_the_world: suspending thread %lu\n",
807 p->os_thread));
808 /* We already hold all_thread_lock, P can become DEAD but
809 * cannot exit, ergo it's safe to use pthread_kill. */
810 status=pthread_kill(p->os_thread,SIG_STOP_FOR_GC);
811 if (status==ESRCH) {
812 /* This thread has exited. */
813 gc_assert(thread_state(p)==STATE_DEAD);
814 } else if (status) {
815 lose("cannot send suspend thread=%lu: %d, %s\n",
816 p->os_thread,status,strerror(status));
820 FSHOW_SIGNAL((stderr,"/gc_stop_the_world:signals sent\n"));
821 for(p=all_threads;p;p=p->next) {
822 if (p!=th) {
823 FSHOW_SIGNAL
824 ((stderr,
825 "/gc_stop_the_world: waiting for thread=%lu: state=%x\n",
826 p->os_thread, thread_state(p)));
827 wait_for_thread_state_change(p, STATE_RUNNING);
828 if (p->state == STATE_RUNNING)
829 lose("/gc_stop_the_world: unexpected state");
832 FSHOW_SIGNAL((stderr,"/gc_stop_the_world:end\n"));
835 void gc_start_the_world()
837 struct thread *p,*th=arch_os_get_current_thread();
838 int lock_ret;
839 /* if a resumed thread creates a new thread before we're done with
840 * this loop, the new thread will get consed on the front of
841 * all_threads, but it won't have been stopped so won't need
842 * restarting */
843 FSHOW_SIGNAL((stderr,"/gc_start_the_world:begin\n"));
844 for(p=all_threads;p;p=p->next) {
845 gc_assert(p->os_thread!=0);
846 if (p!=th) {
847 lispobj state = thread_state(p);
848 if (state != STATE_DEAD) {
849 if(state != STATE_STOPPED) {
850 lose("gc_start_the_world: wrong thread state is %d\n",
851 fixnum_value(state));
853 FSHOW_SIGNAL((stderr, "/gc_start_the_world: resuming %lu\n",
854 p->os_thread));
855 set_thread_state(p, STATE_RUNNING);
860 lock_ret = pthread_mutex_unlock(&all_threads_lock);
861 gc_assert(lock_ret == 0);
862 #ifdef LOCK_CREATE_THREAD
863 lock_ret = pthread_mutex_unlock(&create_thread_lock);
864 gc_assert(lock_ret == 0);
865 #endif
867 FSHOW_SIGNAL((stderr,"/gc_start_the_world:end\n"));
870 #endif /* !LISP_FEATURE_SB_SAFEPOINT */
871 #endif /* !LISP_FEATURE_SB_THREAD */
874 thread_yield()
876 #ifdef LISP_FEATURE_SB_THREAD
877 return sched_yield();
878 #else
879 return 0;
880 #endif
884 wake_thread(os_thread_t os_thread)
886 #if defined(LISP_FEATURE_WIN32)
887 return kill_safely(os_thread, 1);
888 #elif !defined(LISP_FEATURE_SB_THRUPTION)
889 return kill_safely(os_thread, SIGPIPE);
890 #else
891 return wake_thread_posix(os_thread);
892 #endif
895 /* If the thread id given does not belong to a running thread (it has
896 * exited or never even existed) pthread_kill _may_ fail with ESRCH,
897 * but it is also allowed to just segfault, see
898 * <http://udrepper.livejournal.com/16844.html>.
900 * Relying on thread ids can easily backfire since ids are recycled
901 * (NPTL recycles them extremely fast) so a signal can be sent to
902 * another process if the one it was sent to exited.
904 * For these reasons, we must make sure that the thread is still alive
905 * when the pthread_kill is called and return if the thread is
906 * exiting.
908 * Note (DFL, 2011-06-22): At the time of writing, this function is only
909 * used for INTERRUPT-THREAD, hence the wake_thread special-case for
910 * Windows is OK. */
912 kill_safely(os_thread_t os_thread, int signal)
914 FSHOW_SIGNAL((stderr,"/kill_safely: %lu, %d\n", os_thread, signal));
916 #ifdef LISP_FEATURE_SB_THREAD
917 sigset_t oldset;
918 struct thread *thread;
919 /* Frequent special case: resignalling to self. The idea is
920 * that leave_region safepoint will acknowledge the signal, so
921 * there is no need to take locks, roll thread to safepoint
922 * etc. */
923 /* Kludge (on safepoint builds): At the moment, this isn't just
924 * an optimization; rather it masks the fact that
925 * gc_stop_the_world() grabs the all_threads mutex without
926 * releasing it, and since we're not using recursive pthread
927 * mutexes, the pthread_mutex_lock() around the all_threads loop
928 * would go wrong. Why are we running interruptions while
929 * stopping the world though? Test case is (:ASYNC-UNWIND
930 * :SPECIALS), especially with s/10/100/ in both loops. */
931 if (os_thread == pthread_self()) {
932 pthread_kill(os_thread, signal);
933 #ifdef LISP_FEATURE_WIN32
934 check_pending_thruptions(NULL);
935 #endif
936 return 0;
939 /* pthread_kill is not async signal safe and we don't want to be
940 * interrupted while holding the lock. */
941 block_deferrable_signals(0, &oldset);
942 pthread_mutex_lock(&all_threads_lock);
943 for (thread = all_threads; thread; thread = thread->next) {
944 if (thread->os_thread == os_thread) {
945 int status = pthread_kill(os_thread, signal);
946 if (status)
947 lose("kill_safely: pthread_kill failed with %d\n", status);
948 #if defined(LISP_FEATURE_WIN32) && defined(LISP_FEATURE_SB_THRUPTION)
949 wake_thread_win32(thread);
950 #endif
951 break;
954 pthread_mutex_unlock(&all_threads_lock);
955 thread_sigmask(SIG_SETMASK,&oldset,0);
956 if (thread)
957 return 0;
958 else
959 return -1;
960 #elif defined(LISP_FEATURE_WIN32)
961 return 0;
962 #else
963 int status;
964 if (os_thread != 0)
965 lose("kill_safely: who do you want to kill? %d?\n", os_thread);
966 /* Dubious (as in don't know why it works) workaround for the
967 * signal sometimes not being generated on darwin. */
968 #ifdef LISP_FEATURE_DARWIN
970 sigset_t oldset;
971 sigprocmask(SIG_BLOCK, &deferrable_sigset, &oldset);
972 status = raise(signal);
973 sigprocmask(SIG_SETMASK,&oldset,0);
975 #else
976 status = raise(signal);
977 #endif
978 if (status == 0) {
979 return 0;
980 } else {
981 lose("cannot raise signal %d, %d %s\n",
982 signal, status, strerror(errno));
984 #endif