2 * This software is part of the SBCL system. See the README file for
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.
17 #ifndef LISP_FEATURE_WIN32
22 #include <sys/types.h>
23 #ifndef LISP_FEATURE_WIN32
27 #ifdef LISP_FEATURE_MACH_EXCEPTION_HANDLER
28 #include <mach/mach.h>
29 #include <mach/mach_error.h>
30 #include <mach/mach_types.h>
34 #include "validate.h" /* for BINDING_STACK_SIZE etc */
37 #include "target-arch-os.h"
41 #include "genesis/cons.h"
42 #include "genesis/fdefn.h"
43 #include "interr.h" /* for lose() */
45 #include "gc-internal.h"
47 #include "pseudo-atomic.h"
48 #include "interrupt.h"
51 #ifdef LISP_FEATURE_SB_THREAD
53 #ifdef LISP_FEATURE_OPENBSD
54 #include <pthread_np.h>
57 #ifdef LISP_FEATURE_SUNOS
61 #ifdef LISP_FEATURE_WIN32
62 # define IMMEDIATE_POST_MORTEM
65 #ifdef LISP_FEATURE_DARWIN
66 #define DELAY_THREAD_POST_MORTEM 5
67 #define LOCK_CREATE_THREAD
72 #if defined(LISP_FEATURE_FREEBSD) || defined(LISP_FEATURE_DRAGONFLY)
73 #define LOCK_CREATE_THREAD
76 #ifdef LISP_FEATURE_SB_THREAD
77 struct thread_post_mortem
{
78 #ifdef DELAY_THREAD_POST_MORTEM
79 struct thread_post_mortem
*next
;
81 os_thread_t os_thread
;
82 pthread_attr_t
*os_attr
;
83 os_vm_address_t os_address
;
86 #ifdef DELAY_THREAD_POST_MORTEM
87 static int pending_thread_post_mortem_count
= 0;
88 pthread_mutex_t thread_post_mortem_lock
= PTHREAD_MUTEX_INITIALIZER
;
90 static struct thread_post_mortem
* volatile pending_thread_post_mortem
= 0;
93 int dynamic_values_bytes
=TLS_SIZE
*sizeof(lispobj
); /* same for all threads */
94 struct thread
*all_threads
;
96 #ifdef LISP_FEATURE_SB_THREAD
97 pthread_mutex_t all_threads_lock
= PTHREAD_MUTEX_INITIALIZER
;
98 #ifdef LOCK_CREATE_THREAD
99 static pthread_mutex_t create_thread_lock
= PTHREAD_MUTEX_INITIALIZER
;
101 #ifdef LISP_FEATURE_GCC_TLS
102 __thread
struct thread
*current_thread
;
104 pthread_key_t lisp_thread
= 0;
107 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
108 extern lispobj
call_into_lisp_first_time(lispobj fun
, lispobj
*args
, int nargs
)
109 # ifdef LISP_FEATURE_X86_64
110 __attribute__((sysv_abi
))
116 link_thread(struct thread
*th
)
118 if (all_threads
) all_threads
->prev
=th
;
119 th
->next
=all_threads
;
124 #ifdef LISP_FEATURE_SB_THREAD
126 unlink_thread(struct thread
*th
)
129 th
->prev
->next
= th
->next
;
131 all_threads
= th
->next
;
133 th
->next
->prev
= th
->prev
;
136 #ifndef LISP_FEATURE_SB_SAFEPOINT
137 /* Only access thread state with blockables blocked. */
139 thread_state(struct thread
*thread
)
143 block_blockable_signals(NULL
, &old
);
144 os_sem_wait(thread
->state_sem
, "thread_state");
145 state
= thread
->state
;
146 os_sem_post(thread
->state_sem
, "thread_state");
147 thread_sigmask(SIG_SETMASK
, &old
, NULL
);
152 set_thread_state(struct thread
*thread
, lispobj state
)
154 int i
, waitcount
= 0;
156 block_blockable_signals(NULL
, &old
);
157 os_sem_wait(thread
->state_sem
, "set_thread_state");
158 if (thread
->state
!= state
) {
159 if ((STATE_STOPPED
==state
) ||
160 (STATE_DEAD
==state
)) {
161 waitcount
= thread
->state_not_running_waitcount
;
162 thread
->state_not_running_waitcount
= 0;
163 for (i
=0; i
<waitcount
; i
++)
164 os_sem_post(thread
->state_not_running_sem
, "set_thread_state (not running)");
166 if ((STATE_RUNNING
==state
) ||
167 (STATE_DEAD
==state
)) {
168 waitcount
= thread
->state_not_stopped_waitcount
;
169 thread
->state_not_stopped_waitcount
= 0;
170 for (i
=0; i
<waitcount
; i
++)
171 os_sem_post(thread
->state_not_stopped_sem
, "set_thread_state (not stopped)");
173 thread
->state
= state
;
175 os_sem_post(thread
->state_sem
, "set_thread_state");
176 thread_sigmask(SIG_SETMASK
, &old
, NULL
);
180 wait_for_thread_state_change(struct thread
*thread
, lispobj state
)
184 block_blockable_signals(NULL
, &old
);
186 os_sem_wait(thread
->state_sem
, "wait_for_thread_state_change");
187 if (thread
->state
== state
) {
190 wait_sem
= thread
->state_not_running_sem
;
191 thread
->state_not_running_waitcount
++;
194 wait_sem
= thread
->state_not_stopped_sem
;
195 thread
->state_not_stopped_waitcount
++;
198 lose("Invalid state in wait_for_thread_state_change: "OBJ_FMTX
"\n", state
);
203 os_sem_post(thread
->state_sem
, "wait_for_thread_state_change");
205 os_sem_wait(wait_sem
, "wait_for_thread_state_change");
208 thread_sigmask(SIG_SETMASK
, &old
, NULL
);
210 #endif /* sb-safepoint */
211 #endif /* sb-thread */
214 initial_thread_trampoline(struct thread
*th
)
217 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
218 lispobj
*args
= NULL
;
220 #ifdef LISP_FEATURE_SB_THREAD
221 pthread_setspecific(lisp_thread
, (void *)1);
223 #if defined(THREADS_USING_GCSIGNAL) && (defined(LISP_FEATURE_PPC) || defined(LISP_FEATURE_ARM64))
224 /* SIG_STOP_FOR_GC defaults to blocked on PPC? */
225 unblock_gc_signals(0,0);
227 function
= th
->no_tls_value_marker
;
228 th
->no_tls_value_marker
= NO_TLS_VALUE_MARKER_WIDETAG
;
229 if(arch_os_thread_init(th
)==0) return 1;
231 th
->os_thread
=thread_self();
232 #ifndef LISP_FEATURE_WIN32
233 protect_control_stack_hard_guard_page(1, NULL
);
235 protect_binding_stack_hard_guard_page(1, NULL
);
236 protect_alien_stack_hard_guard_page(1, NULL
);
237 #ifndef LISP_FEATURE_WIN32
238 protect_control_stack_guard_page(1, NULL
);
240 protect_binding_stack_guard_page(1, NULL
);
241 protect_alien_stack_guard_page(1, NULL
);
243 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
244 return call_into_lisp_first_time(function
,args
,0);
246 return funcall0(function
);
250 #ifdef LISP_FEATURE_SB_THREAD
252 # if defined(IMMEDIATE_POST_MORTEM)
255 * If this feature is set, we are running on a stack managed by the OS,
256 * and no fancy delays are required for anything. Just do it.
259 schedule_thread_post_mortem(struct thread
*corpse
)
261 pthread_detach(pthread_self());
262 gc_assert(!pthread_attr_destroy(corpse
->os_attr
));
263 #if defined(LISP_FEATURE_WIN32)
264 os_invalidate_free(corpse
->os_address
, THREAD_STRUCT_SIZE
);
266 os_invalidate(corpse
->os_address
, THREAD_STRUCT_SIZE
);
272 /* THREAD POST MORTEM CLEANUP
274 * Memory allocated for the thread stacks cannot be reclaimed while
275 * the thread is still alive, so we need a mechanism for post mortem
276 * cleanups. FIXME: We actually have three, for historical reasons as
277 * the saying goes. Do we really need three? Nikodemus guesses that
278 * not anymore, now that we properly call pthread_attr_destroy before
279 * freeing the stack. */
281 static struct thread_post_mortem
*
282 plan_thread_post_mortem(struct thread
*corpse
)
285 struct thread_post_mortem
*post_mortem
= malloc(sizeof(struct thread_post_mortem
));
286 gc_assert(post_mortem
);
287 post_mortem
->os_thread
= corpse
->os_thread
;
288 post_mortem
->os_attr
= corpse
->os_attr
;
289 post_mortem
->os_address
= corpse
->os_address
;
290 #ifdef DELAY_THREAD_POST_MORTEM
291 post_mortem
->next
= NULL
;
295 /* FIXME: When does this happen? */
301 perform_thread_post_mortem(struct thread_post_mortem
*post_mortem
)
303 #ifdef CREATE_POST_MORTEM_THREAD
304 pthread_detach(pthread_self());
308 if ((result
= pthread_join(post_mortem
->os_thread
, NULL
))) {
309 lose("Error calling pthread_join in perform_thread_post_mortem:\n%s",
312 if ((result
= pthread_attr_destroy(post_mortem
->os_attr
))) {
313 lose("Error calling pthread_attr_destroy in perform_thread_post_mortem:\n%s",
316 os_invalidate(post_mortem
->os_address
, THREAD_STRUCT_SIZE
);
322 schedule_thread_post_mortem(struct thread
*corpse
)
324 struct thread_post_mortem
*post_mortem
= NULL
;
326 post_mortem
= plan_thread_post_mortem(corpse
);
328 #ifdef DELAY_THREAD_POST_MORTEM
329 pthread_mutex_lock(&thread_post_mortem_lock
);
330 /* First stick the new post mortem to the end of the queue. */
331 if (pending_thread_post_mortem
) {
332 struct thread_post_mortem
*next
= pending_thread_post_mortem
;
336 next
->next
= post_mortem
;
338 pending_thread_post_mortem
= post_mortem
;
340 /* Then, if there are enough things in the queue, clean up one
341 * from the head -- or increment the count, and null out the
342 * post_mortem we have. */
343 if (pending_thread_post_mortem_count
> DELAY_THREAD_POST_MORTEM
) {
344 post_mortem
= pending_thread_post_mortem
;
345 pending_thread_post_mortem
= post_mortem
->next
;
347 pending_thread_post_mortem_count
++;
350 pthread_mutex_unlock(&thread_post_mortem_lock
);
351 /* Finally run, the cleanup, if any. */
352 perform_thread_post_mortem(post_mortem
);
353 #elif defined(CREATE_POST_MORTEM_THREAD)
354 gc_assert(!pthread_create(&thread
, NULL
, perform_thread_post_mortem
, post_mortem
));
356 post_mortem
= (struct thread_post_mortem
*)
357 swap_lispobjs((lispobj
*)(void *)&pending_thread_post_mortem
,
358 (lispobj
)post_mortem
);
359 perform_thread_post_mortem(post_mortem
);
364 # endif /* !IMMEDIATE_POST_MORTEM */
366 /* Note: scribble must be stack-allocated */
368 init_new_thread(struct thread
*th
, init_thread_data
*scribble
, int guardp
)
372 pthread_setspecific(lisp_thread
, (void *)1);
373 if(arch_os_thread_init(th
)==0) {
374 /* FIXME: handle error */
375 lose("arch_os_thread_init failed\n");
378 th
->os_thread
=thread_self();
380 protect_control_stack_guard_page(1, NULL
);
381 protect_binding_stack_guard_page(1, NULL
);
382 protect_alien_stack_guard_page(1, NULL
);
383 /* Since GC can only know about this thread from the all_threads
384 * list and we're just adding this thread to it, there is no
385 * danger of deadlocking even with SIG_STOP_FOR_GC blocked (which
387 #ifdef LISP_FEATURE_SB_SAFEPOINT
388 *th
->csp_around_foreign_call
= (lispobj
)scribble
;
390 lock_ret
= pthread_mutex_lock(&all_threads_lock
);
391 gc_assert(lock_ret
== 0);
393 lock_ret
= pthread_mutex_unlock(&all_threads_lock
);
394 gc_assert(lock_ret
== 0);
396 /* Kludge: Changed the order of some steps between the safepoint/
397 * non-safepoint versions of this code. Can we unify this more?
399 #ifdef LISP_FEATURE_SB_SAFEPOINT
401 gc_state_wait(GC_NONE
);
403 push_gcing_safety(&scribble
->safety
);
408 undo_init_new_thread(struct thread
*th
, init_thread_data
*scribble
)
412 /* Kludge: Changed the order of some steps between the safepoint/
413 * non-safepoint versions of this code. Can we unify this more?
415 #ifdef LISP_FEATURE_SB_SAFEPOINT
416 block_blockable_signals(0, 0);
417 gc_alloc_update_page_tables(BOXED_PAGE_FLAG
, &th
->alloc_region
);
418 #if defined(LISP_FEATURE_SB_SAFEPOINT_STRICTLY) && !defined(LISP_FEATURE_WIN32)
419 gc_alloc_update_page_tables(BOXED_PAGE_FLAG
, &th
->sprof_alloc_region
);
421 pop_gcing_safety(&scribble
->safety
);
422 lock_ret
= pthread_mutex_lock(&all_threads_lock
);
423 gc_assert(lock_ret
== 0);
425 lock_ret
= pthread_mutex_unlock(&all_threads_lock
);
426 gc_assert(lock_ret
== 0);
429 block_blockable_signals(0, 0);
430 set_thread_state(th
, STATE_DEAD
);
432 /* SIG_STOP_FOR_GC is blocked and GC might be waiting for this
433 * thread, but since we are already dead it won't wait long. */
434 lock_ret
= pthread_mutex_lock(&all_threads_lock
);
435 gc_assert(lock_ret
== 0);
437 gc_alloc_update_page_tables(BOXED_PAGE_FLAG
, &th
->alloc_region
);
438 #if defined(LISP_FEATURE_SB_SAFEPOINT_STRICTLY) && !defined(LISP_FEATURE_WIN32)
439 gc_alloc_update_page_tables(BOXED_PAGE_FLAG
, &th
->sprof_alloc_region
);
442 pthread_mutex_unlock(&all_threads_lock
);
443 gc_assert(lock_ret
== 0);
446 arch_os_thread_cleanup(th
);
448 #ifndef LISP_FEATURE_SB_SAFEPOINT
449 os_sem_destroy(th
->state_sem
);
450 os_sem_destroy(th
->state_not_running_sem
);
451 os_sem_destroy(th
->state_not_stopped_sem
);
455 #ifdef LISP_FEATURE_MACH_EXCEPTION_HANDLER
456 mach_lisp_thread_destroy(th
);
459 #if defined(LISP_FEATURE_WIN32)
462 (int) (sizeof(th
->private_events
.events
)/
463 sizeof(th
->private_events
.events
[0])); ++i
) {
464 CloseHandle(th
->private_events
.events
[i
]);
466 TlsSetValue(OUR_TLS_INDEX
,NULL
);
469 /* Undo the association of the current pthread to its `struct thread',
470 * such that we can call arch_os_get_current_thread() later in this
471 * thread and cleanly get back NULL. */
472 #ifdef LISP_FEATURE_GCC_TLS
473 current_thread
= NULL
;
475 pthread_setspecific(specials
, NULL
);
479 /* this is the first thing that runs in the child (which is why the
480 * silly calling convention). Basically it calls the user's requested
481 * lisp function after doing arch_os_thread_init and whatever other
482 * bookkeeping needs to be done
485 new_thread_trampoline(struct thread
*th
)
488 init_thread_data scribble
;
490 FSHOW((stderr
,"/creating thread %lu\n", thread_self()));
491 check_deferrables_blocked_or_lose(0);
492 #ifndef LISP_FEATURE_SB_SAFEPOINT
493 check_gc_signals_unblocked_or_lose(0);
496 lispobj function
= th
->no_tls_value_marker
;
497 th
->no_tls_value_marker
= NO_TLS_VALUE_MARKER_WIDETAG
;
498 init_new_thread(th
, &scribble
, 1);
499 result
= funcall0(function
);
500 undo_init_new_thread(th
, &scribble
);
502 schedule_thread_post_mortem(th
);
504 FSHOW((stderr
,"/exiting thread %lu\n", thread_self()));
508 static struct thread
*create_thread_struct(lispobj
);
511 attach_os_thread(init_thread_data
*scribble
)
513 os_thread_t os
= pthread_self();
514 odxprint(misc
, "attach_os_thread: attaching to %p", os
);
516 struct thread
*th
= create_thread_struct(NIL
);
517 block_deferrable_signals(0, &scribble
->oldset
);
518 th
->no_tls_value_marker
= NO_TLS_VALUE_MARKER_WIDETAG
;
519 /* We don't actually want a pthread_attr here, but rather than add
520 * `if's to the post-mostem, let's just keep that code happy by
521 * keeping it initialized: */
522 pthread_attr_init(th
->os_attr
);
524 #ifndef LISP_FEATURE_WIN32
525 /* On windows, arch_os_thread_init will take care of finding the
529 #ifdef LISP_FEATURE_OPENBSD
531 pthread_stackseg_np(os
, &stack
);
532 stack_size
= stack
.ss_size
;
533 stack_addr
= (void*)((size_t)stack
.ss_sp
- stack_size
);
534 #elif defined LISP_FEATURE_SUNOS
536 thr_stksegment(&stack
);
537 stack_size
= stack
.ss_size
;
538 stack_addr
= (void*)((size_t)stack
.ss_sp
- stack_size
);
539 #elif defined(LISP_FEATURE_DARWIN)
540 stack_addr
= pthread_get_stackaddr_np(os
);
541 stack_size
= pthread_get_stacksize_np(os
);
544 #ifdef LISP_FEATURE_FREEBSD
545 pthread_attr_get_np(os
, &attr
);
547 int pthread_getattr_np(pthread_t
, pthread_attr_t
*);
548 pthread_getattr_np(os
, &attr
);
550 pthread_attr_getstack(&attr
, &stack_addr
, &stack_size
);
553 th
->control_stack_start
= stack_addr
;
554 th
->control_stack_end
= (void *) (((uintptr_t) stack_addr
) + stack_size
);
557 init_new_thread(th
, scribble
, 0);
559 /* We will be calling into Lisp soon, and the functions being called
560 * recklessly ignore the comment in target-thread which says that we
561 * must be careful to not cause GC while initializing a new thread.
562 * Since we first need to create a fresh thread object, it's really
563 * tempting to just perform such unsafe allocation though. So let's
564 * at least try to suppress GC before consing, and hope that it
566 bind_variable(GC_INHIBIT
, T
, th
);
569 = (uword_t
) th
->control_stack_end
- (uword_t
) th
->control_stack_start
;
570 odxprint(misc
, "attach_os_thread: attached %p as %p (0x%lx bytes stack)",
571 os
, th
, (long) stacksize
);
575 detach_os_thread(init_thread_data
*scribble
)
577 struct thread
*th
= arch_os_get_current_thread();
578 odxprint(misc
, "detach_os_thread: detaching");
580 undo_init_new_thread(th
, scribble
);
582 odxprint(misc
, "deattach_os_thread: detached");
583 pthread_setspecific(lisp_thread
, (void *)0);
584 thread_sigmask(SIG_SETMASK
, &scribble
->oldset
, 0);
588 callback_wrapper_trampoline(
589 #if !(defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64))
590 /* On the x86oid backends, the assembly wrapper happens to not pass
591 * in ENTER_ALIEN_CALLBACK explicitly for safepoints. However, the
592 * platforms with precise GC are tricky enough already, and I want
593 * to minimize the read-time conditionals. For those platforms, I'm
594 * only replacing funcall3 with callback_wrapper_trampoline while
595 * keeping the arguments unchanged. --DFL */
596 lispobj
__attribute__((__unused__
)) fun
,
598 lispobj arg0
, lispobj arg1
, lispobj arg2
)
600 #if defined(LISP_FEATURE_WIN32)
601 pthread_np_notice_thread();
603 struct thread
* th
= arch_os_get_current_thread();
604 if (!th
) { /* callback invoked in non-lisp thread */
605 init_thread_data scribble
;
606 attach_os_thread(&scribble
);
607 funcall3(StaticSymbolFunction(ENTER_FOREIGN_CALLBACK
), arg0
,arg1
,arg2
);
608 detach_os_thread(&scribble
);
612 #ifdef LISP_FEATURE_WIN32
613 /* arg2 is the pointer to a return value, which sits on the stack */
614 th
->carried_base_pointer
= (os_context_register_t
) *(((void**)arg2
)-1);
617 #ifdef LISP_FEATURE_SB_SAFEPOINT
618 WITH_GC_AT_SAFEPOINTS_ONLY()
621 funcall3(SymbolValue(ENTER_ALIEN_CALLBACK
, 0), arg0
, arg1
, arg2
);
625 #endif /* LISP_FEATURE_SB_THREAD */
628 free_thread_struct(struct thread
*th
)
630 #if defined(LISP_FEATURE_WIN32)
631 os_invalidate_free((os_vm_address_t
) th
->os_address
, THREAD_STRUCT_SIZE
);
633 os_invalidate((os_vm_address_t
) th
->os_address
, THREAD_STRUCT_SIZE
);
637 #ifdef LISP_FEATURE_SB_THREAD
638 /* FIXME: should be MAX_INTERRUPTS -1 ? */
639 const unsigned int tls_index_start
=
640 MAX_INTERRUPTS
+ sizeof(struct thread
)/sizeof(lispobj
);
643 /* this is called from any other thread to create the new one, and
644 * initialize all parts of it that can be initialized from another
648 static struct thread
*
649 create_thread_struct(lispobj initial_function
) {
650 union per_thread_data
*per_thread
;
651 struct thread
*th
=0; /* subdue gcc */
653 void *aligned_spaces
=0;
654 #if defined(LISP_FEATURE_SB_THREAD) || defined(LISP_FEATURE_WIN32)
658 /* May as well allocate all the spaces at once: it saves us from
659 * having to decide what to do if only some of the allocations
660 * succeed. SPACES must be appropriately aligned, since the GC
661 * expects the control stack to start at a page boundary -- and
662 * the OS may have even more rigorous requirements. We can't rely
663 * on the alignment passed from os_validate, since that might
664 * assume the current (e.g. 4k) pagesize, while we calculate with
665 * the biggest (e.g. 64k) pagesize allowed by the ABI. */
666 spaces
=os_validate(0, THREAD_STRUCT_SIZE
);
669 /* Aligning up is safe as THREAD_STRUCT_SIZE has
670 * THREAD_ALIGNMENT_BYTES padding. */
671 aligned_spaces
= (void *)((((uword_t
)(char *)spaces
)
672 + THREAD_ALIGNMENT_BYTES
-1)
673 &~(uword_t
)(THREAD_ALIGNMENT_BYTES
-1));
676 thread_control_stack_size
+
679 per_thread
=(union per_thread_data
*)
680 (csp_page
+ THREAD_CSP_PAGE_SIZE
);
682 #ifdef LISP_FEATURE_SB_THREAD
683 for(i
= 0; i
< (dynamic_values_bytes
/ sizeof(lispobj
)); i
++)
684 per_thread
->dynamic_values
[i
] = NO_TLS_VALUE_MARKER_WIDETAG
;
687 th
=&per_thread
->thread
;
688 th
->os_address
= spaces
;
689 th
->control_stack_start
= aligned_spaces
;
690 th
->binding_stack_start
=
691 (lispobj
*)((void*)th
->control_stack_start
+thread_control_stack_size
);
692 th
->control_stack_end
= th
->binding_stack_start
;
693 th
->control_stack_guard_page_protected
= T
;
694 th
->alien_stack_start
=
695 (lispobj
*)((void*)th
->binding_stack_start
+BINDING_STACK_SIZE
);
696 set_binding_stack_pointer(th
,th
->binding_stack_start
);
700 #ifdef LISP_FEATURE_SB_SAFEPOINT
701 # ifdef LISP_FEATURE_WIN32
702 th
->carried_base_pointer
= 0;
704 # ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
705 th
->pc_around_foreign_call
= 0;
707 th
->csp_around_foreign_call
= csp_page
;
710 struct nonpointer_thread_data
*nonpointer_data
711 = (void *) &per_thread
->dynamic_values
[TLS_SIZE
];
713 th
->interrupt_data
= &nonpointer_data
->interrupt_data
;
715 #ifdef LISP_FEATURE_SB_THREAD
716 th
->os_attr
= &nonpointer_data
->os_attr
;
717 # ifndef LISP_FEATURE_SB_SAFEPOINT
718 th
->state_sem
= &nonpointer_data
->state_sem
;
719 th
->state_not_running_sem
= &nonpointer_data
->state_not_running_sem
;
720 th
->state_not_stopped_sem
= &nonpointer_data
->state_not_stopped_sem
;
721 os_sem_init(th
->state_sem
, 1);
722 os_sem_init(th
->state_not_running_sem
, 0);
723 os_sem_init(th
->state_not_stopped_sem
, 0);
724 th
->state_not_running_waitcount
= 0;
725 th
->state_not_stopped_waitcount
= 0;
729 th
->state
=STATE_RUNNING
;
730 #ifdef ALIEN_STACK_GROWS_DOWNWARD
731 th
->alien_stack_pointer
=((void *)th
->alien_stack_start
732 + ALIEN_STACK_SIZE
-N_WORD_BYTES
);
734 th
->alien_stack_pointer
=((void *)th
->alien_stack_start
);
737 #ifdef LISP_FEATURE_SB_THREAD
738 th
->pseudo_atomic_bits
=0;
739 #elif defined LISP_FEATURE_GENCGC
740 clear_pseudo_atomic_atomic(th
);
741 clear_pseudo_atomic_interrupted(th
);
744 #ifdef LISP_FEATURE_GENCGC
745 gc_set_region_empty(&th
->alloc_region
);
746 # if defined(LISP_FEATURE_SB_SAFEPOINT_STRICTLY) && !defined(LISP_FEATURE_WIN32)
747 gc_set_region_empty(&th
->sprof_alloc_region
);
750 #ifdef LISP_FEATURE_SB_THREAD
751 /* This parallels the same logic in globals.c for the
752 * single-threaded foreign_function_call_active, KLUDGE and
754 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
755 th
->foreign_function_call_active
= 0;
757 th
->foreign_function_call_active
= 1;
761 #ifndef LISP_FEATURE_SB_THREAD
762 /* the tls-points-into-struct-thread trick is only good for threaded
763 * sbcl, because unithread sbcl doesn't have tls. So, we copy the
764 * appropriate values from struct thread here, and make sure that
765 * we use the appropriate SymbolValue macros to access any of the
766 * variable quantities from the C runtime. It's not quite OAOOM,
767 * it just feels like it */
768 SetSymbolValue(BINDING_STACK_START
,(lispobj
)th
->binding_stack_start
,th
);
769 SetSymbolValue(CONTROL_STACK_START
,(lispobj
)th
->control_stack_start
,th
);
770 SetSymbolValue(CONTROL_STACK_END
,(lispobj
)th
->control_stack_end
,th
);
771 #if defined(LISP_FEATURE_X86) || defined (LISP_FEATURE_X86_64)
772 SetSymbolValue(ALIEN_STACK_POINTER
,(lispobj
)th
->alien_stack_pointer
,th
);
775 bind_variable(CURRENT_CATCH_BLOCK
,make_fixnum(0),th
);
776 bind_variable(CURRENT_UNWIND_PROTECT_BLOCK
,make_fixnum(0),th
);
777 bind_variable(FREE_INTERRUPT_CONTEXT_INDEX
,make_fixnum(0),th
);
778 bind_variable(INTERRUPT_PENDING
, NIL
,th
);
779 bind_variable(INTERRUPTS_ENABLED
,T
,th
);
780 bind_variable(ALLOW_WITH_INTERRUPTS
,T
,th
);
781 bind_variable(GC_PENDING
,NIL
,th
);
782 bind_variable(ALLOC_SIGNAL
,NIL
,th
);
783 #ifdef PINNED_OBJECTS
784 bind_variable(PINNED_OBJECTS
,NIL
,th
);
786 #ifdef LISP_FEATURE_SB_THREAD
787 bind_variable(STOP_FOR_GC_PENDING
,NIL
,th
);
789 #if defined(LISP_FEATURE_SB_SAFEPOINT)
790 bind_variable(GC_SAFE
,NIL
,th
);
791 bind_variable(IN_SAFEPOINT
,NIL
,th
);
793 #ifdef LISP_FEATURE_SB_THRUPTION
794 bind_variable(THRUPTION_PENDING
,NIL
,th
);
795 bind_variable(RESTART_CLUSTERS
,NIL
,th
);
797 #ifndef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
798 access_control_stack_pointer(th
)=th
->control_stack_start
;
801 th
->interrupt_data
->pending_handler
= 0;
802 th
->interrupt_data
->gc_blocked_deferrables
= 0;
803 #ifdef GENCGC_IS_PRECISE
804 th
->interrupt_data
->allocation_trap_context
= 0;
806 th
->no_tls_value_marker
=initial_function
;
808 #if defined(LISP_FEATURE_WIN32)
809 for (i
= 0; i
<sizeof(th
->private_events
.events
)/
810 sizeof(th
->private_events
.events
[0]); ++i
) {
811 th
->private_events
.events
[i
] = CreateEvent(NULL
,FALSE
,FALSE
,NULL
);
813 th
->synchronous_io_handle_and_flag
= 0;
819 void create_initial_thread(lispobj initial_function
) {
820 struct thread
*th
=create_thread_struct(initial_function
);
821 #ifdef LISP_FEATURE_SB_THREAD
822 pthread_key_create(&lisp_thread
, 0);
825 initial_thread_trampoline(th
); /* no return */
826 } else lose("can't create initial thread\n");
829 #ifdef LISP_FEATURE_SB_THREAD
831 #ifndef __USE_XOPEN2K
832 extern int pthread_attr_setstack (pthread_attr_t
*__attr
, void *__stackaddr
,
836 boolean
create_os_thread(struct thread
*th
,os_thread_t
*kid_tid
)
838 /* The new thread inherits the restrictive signal mask set here,
839 * and enables signals again when it is set up properly. */
842 int retcode
= 0, initcode
;
844 FSHOW_SIGNAL((stderr
,"/create_os_thread: creating new thread\n"));
846 /* Blocking deferrable signals is enough, no need to block
847 * SIG_STOP_FOR_GC because the child process is not linked onto
848 * all_threads until it's ready. */
849 block_deferrable_signals(0, &oldset
);
851 #ifdef LOCK_CREATE_THREAD
852 retcode
= pthread_mutex_lock(&create_thread_lock
);
853 gc_assert(retcode
== 0);
854 FSHOW_SIGNAL((stderr
,"/create_os_thread: got lock\n"));
857 if((initcode
= pthread_attr_init(th
->os_attr
)) ||
858 /* call_into_lisp_first_time switches the stack for the initial
859 * thread. For the others, we use this. */
860 #if defined(LISP_FEATURE_WIN32)
861 (pthread_attr_setstacksize(th
->os_attr
, thread_control_stack_size
)) ||
863 # if defined(LISP_FEATURE_C_STACK_IS_CONTROL_STACK)
864 (pthread_attr_setstack(th
->os_attr
,th
->control_stack_start
,
865 thread_control_stack_size
)) ||
867 (pthread_attr_setstack(th
->os_attr
,th
->alien_stack_start
,
868 ALIEN_STACK_SIZE
)) ||
871 (retcode
= pthread_create
872 (kid_tid
,th
->os_attr
,(void *(*)(void *))new_thread_trampoline
,th
))) {
873 FSHOW_SIGNAL((stderr
, "init = %d\n", initcode
));
874 FSHOW_SIGNAL((stderr
, "pthread_create returned %d, errno %d\n",
877 perror("create_os_thread");
882 #ifdef LOCK_CREATE_THREAD
883 retcode
= pthread_mutex_unlock(&create_thread_lock
);
884 gc_assert(retcode
== 0);
885 FSHOW_SIGNAL((stderr
,"/create_os_thread: released lock\n"));
887 thread_sigmask(SIG_SETMASK
,&oldset
,0);
891 os_thread_t
create_thread(lispobj initial_function
) {
892 struct thread
*th
, *thread
= arch_os_get_current_thread();
893 os_thread_t kid_tid
= 0;
895 /* Must defend against async unwinds. */
896 if (SymbolValue(INTERRUPTS_ENABLED
, thread
) != NIL
)
897 lose("create_thread is not safe when interrupts are enabled.\n");
899 /* Assuming that a fresh thread struct has no lisp objects in it,
900 * linking it to all_threads can be left to the thread itself
901 * without fear of gc lossage. initial_function violates this
902 * assumption and must stay pinned until the child starts up. */
903 th
= create_thread_struct(initial_function
);
904 if (th
&& !create_os_thread(th
,&kid_tid
)) {
905 free_thread_struct(th
);
911 /* stopping the world is a two-stage process. From this thread we signal
912 * all the others with SIG_STOP_FOR_GC. The handler for this signal does
913 * the usual pseudo-atomic checks (we don't want to stop a thread while
914 * it's in the middle of allocation) then waits for another SIG_STOP_FOR_GC.
917 * (With SB-SAFEPOINT, see the definitions in safepoint.c instead.)
919 #ifndef LISP_FEATURE_SB_SAFEPOINT
921 /* To avoid deadlocks when gc stops the world all clients of each
922 * mutex must enable or disable SIG_STOP_FOR_GC for the duration of
923 * holding the lock, but they must agree on which. */
924 void gc_stop_the_world()
926 struct thread
*p
,*th
=arch_os_get_current_thread();
927 int status
, lock_ret
;
928 #ifdef LOCK_CREATE_THREAD
929 /* KLUDGE: Stopping the thread during pthread_create() causes deadlock
931 FSHOW_SIGNAL((stderr
,"/gc_stop_the_world:waiting on create_thread_lock\n"));
932 lock_ret
= pthread_mutex_lock(&create_thread_lock
);
933 gc_assert(lock_ret
== 0);
934 FSHOW_SIGNAL((stderr
,"/gc_stop_the_world:got create_thread_lock\n"));
936 FSHOW_SIGNAL((stderr
,"/gc_stop_the_world:waiting on lock\n"));
937 /* keep threads from starting while the world is stopped. */
938 lock_ret
= pthread_mutex_lock(&all_threads_lock
); \
939 gc_assert(lock_ret
== 0);
941 FSHOW_SIGNAL((stderr
,"/gc_stop_the_world:got lock\n"));
942 /* stop all other threads by sending them SIG_STOP_FOR_GC */
943 for(p
=all_threads
; p
; p
=p
->next
) {
944 gc_assert(p
->os_thread
!= 0);
945 FSHOW_SIGNAL((stderr
,"/gc_stop_the_world: thread=%lu, state=%x\n",
946 p
->os_thread
, thread_state(p
)));
947 if((p
!=th
) && ((thread_state(p
)==STATE_RUNNING
))) {
948 FSHOW_SIGNAL((stderr
,"/gc_stop_the_world: suspending thread %lu\n",
950 /* We already hold all_thread_lock, P can become DEAD but
951 * cannot exit, ergo it's safe to use pthread_kill. */
952 status
=pthread_kill(p
->os_thread
,SIG_STOP_FOR_GC
);
954 /* This thread has exited. */
955 gc_assert(thread_state(p
)==STATE_DEAD
);
957 lose("cannot send suspend thread=%lu: %d, %s\n",
958 p
->os_thread
,status
,strerror(status
));
962 FSHOW_SIGNAL((stderr
,"/gc_stop_the_world:signals sent\n"));
963 for(p
=all_threads
;p
;p
=p
->next
) {
967 "/gc_stop_the_world: waiting for thread=%lu: state=%x\n",
968 p
->os_thread
, thread_state(p
)));
969 wait_for_thread_state_change(p
, STATE_RUNNING
);
970 if (p
->state
== STATE_RUNNING
)
971 lose("/gc_stop_the_world: unexpected state");
974 FSHOW_SIGNAL((stderr
,"/gc_stop_the_world:end\n"));
977 void gc_start_the_world()
979 struct thread
*p
,*th
=arch_os_get_current_thread();
981 /* if a resumed thread creates a new thread before we're done with
982 * this loop, the new thread will get consed on the front of
983 * all_threads, but it won't have been stopped so won't need
985 FSHOW_SIGNAL((stderr
,"/gc_start_the_world:begin\n"));
986 for(p
=all_threads
;p
;p
=p
->next
) {
987 gc_assert(p
->os_thread
!=0);
989 lispobj state
= thread_state(p
);
990 if (state
!= STATE_DEAD
) {
991 if(state
!= STATE_STOPPED
) {
992 lose("gc_start_the_world: wrong thread state is %d\n",
993 fixnum_value(state
));
995 FSHOW_SIGNAL((stderr
, "/gc_start_the_world: resuming %lu\n",
997 set_thread_state(p
, STATE_RUNNING
);
1002 lock_ret
= pthread_mutex_unlock(&all_threads_lock
);
1003 gc_assert(lock_ret
== 0);
1004 #ifdef LOCK_CREATE_THREAD
1005 lock_ret
= pthread_mutex_unlock(&create_thread_lock
);
1006 gc_assert(lock_ret
== 0);
1009 FSHOW_SIGNAL((stderr
,"/gc_start_the_world:end\n"));
1012 #endif /* !LISP_FEATURE_SB_SAFEPOINT */
1013 #endif /* !LISP_FEATURE_SB_THREAD */
1018 #ifdef LISP_FEATURE_SB_THREAD
1019 return sched_yield();
1026 wake_thread(os_thread_t os_thread
)
1028 #if defined(LISP_FEATURE_WIN32)
1029 return kill_safely(os_thread
, 1);
1030 #elif !defined(LISP_FEATURE_SB_THRUPTION)
1031 return kill_safely(os_thread
, SIGPIPE
);
1033 return wake_thread_posix(os_thread
);
1037 /* If the thread id given does not belong to a running thread (it has
1038 * exited or never even existed) pthread_kill _may_ fail with ESRCH,
1039 * but it is also allowed to just segfault, see
1040 * <http://udrepper.livejournal.com/16844.html>.
1042 * Relying on thread ids can easily backfire since ids are recycled
1043 * (NPTL recycles them extremely fast) so a signal can be sent to
1044 * another process if the one it was sent to exited.
1046 * For these reasons, we must make sure that the thread is still alive
1047 * when the pthread_kill is called and return if the thread is
1050 * Note (DFL, 2011-06-22): At the time of writing, this function is only
1051 * used for INTERRUPT-THREAD, hence the wake_thread special-case for
1054 kill_safely(os_thread_t os_thread
, int signal
)
1056 FSHOW_SIGNAL((stderr
,"/kill_safely: %lu, %d\n", os_thread
, signal
));
1058 #ifdef LISP_FEATURE_SB_THREAD
1060 struct thread
*thread
;
1061 /* Frequent special case: resignalling to self. The idea is
1062 * that leave_region safepoint will acknowledge the signal, so
1063 * there is no need to take locks, roll thread to safepoint
1065 /* Kludge (on safepoint builds): At the moment, this isn't just
1066 * an optimization; rather it masks the fact that
1067 * gc_stop_the_world() grabs the all_threads mutex without
1068 * releasing it, and since we're not using recursive pthread
1069 * mutexes, the pthread_mutex_lock() around the all_threads loop
1070 * would go wrong. Why are we running interruptions while
1071 * stopping the world though? Test case is (:ASYNC-UNWIND
1072 * :SPECIALS), especially with s/10/100/ in both loops. */
1073 if (os_thread
== pthread_self()) {
1074 pthread_kill(os_thread
, signal
);
1075 #ifdef LISP_FEATURE_WIN32
1076 check_pending_thruptions(NULL
);
1081 /* pthread_kill is not async signal safe and we don't want to be
1082 * interrupted while holding the lock. */
1083 block_deferrable_signals(0, &oldset
);
1084 pthread_mutex_lock(&all_threads_lock
);
1085 for (thread
= all_threads
; thread
; thread
= thread
->next
) {
1086 if (thread
->os_thread
== os_thread
) {
1087 int status
= pthread_kill(os_thread
, signal
);
1089 lose("kill_safely: pthread_kill failed with %d\n", status
);
1090 #if defined(LISP_FEATURE_WIN32) && defined(LISP_FEATURE_SB_THRUPTION)
1091 wake_thread_win32(thread
);
1096 pthread_mutex_unlock(&all_threads_lock
);
1097 thread_sigmask(SIG_SETMASK
,&oldset
,0);
1102 #elif defined(LISP_FEATURE_WIN32)
1107 lose("kill_safely: who do you want to kill? %d?\n", os_thread
);
1108 /* Dubious (as in don't know why it works) workaround for the
1109 * signal sometimes not being generated on darwin. */
1110 #ifdef LISP_FEATURE_DARWIN
1113 sigprocmask(SIG_BLOCK
, &deferrable_sigset
, &oldset
);
1114 status
= raise(signal
);
1115 sigprocmask(SIG_SETMASK
,&oldset
,0);
1118 status
= raise(signal
);
1123 lose("cannot raise signal %d, %d %s\n",
1124 signal
, status
, strerror(errno
));