+static void
+link_thread(struct thread *th)
+{
+ if (all_threads) all_threads->prev=th;
+ th->next=all_threads;
+ th->prev=0;
+ all_threads=th;
+}
+
+#ifdef LISP_FEATURE_SB_THREAD
+static void
+unlink_thread(struct thread *th)
+{
+ if (th->prev)
+ th->prev->next = th->next;
+ else
+ all_threads = th->next;
+ if (th->next)
+ th->next->prev = th->prev;
+}
+
+#ifndef LISP_FEATURE_SB_SAFEPOINT
+/* Only access thread state with blockables blocked. */
+lispobj
+thread_state(struct thread *thread)
+{
+ lispobj state;
+ sigset_t old;
+ block_blockable_signals(NULL, &old);
+ os_sem_wait(thread->state_sem, "thread_state");
+ state = thread->state;
+ os_sem_post(thread->state_sem, "thread_state");
+ thread_sigmask(SIG_SETMASK, &old, NULL);
+ return state;
+}
+
+void
+set_thread_state(struct thread *thread, lispobj state)
+{
+ int i, waitcount = 0;
+ sigset_t old;
+ block_blockable_signals(NULL, &old);
+ os_sem_wait(thread->state_sem, "set_thread_state");
+ if (thread->state != state) {
+ if ((STATE_STOPPED==state) ||
+ (STATE_DEAD==state)) {
+ waitcount = thread->state_not_running_waitcount;
+ thread->state_not_running_waitcount = 0;
+ for (i=0; i<waitcount; i++)
+ os_sem_post(thread->state_not_running_sem, "set_thread_state (not running)");
+ }
+ if ((STATE_RUNNING==state) ||
+ (STATE_DEAD==state)) {
+ waitcount = thread->state_not_stopped_waitcount;
+ thread->state_not_stopped_waitcount = 0;
+ for (i=0; i<waitcount; i++)
+ os_sem_post(thread->state_not_stopped_sem, "set_thread_state (not stopped)");
+ }
+ thread->state = state;
+ }
+ os_sem_post(thread->state_sem, "set_thread_state");
+ thread_sigmask(SIG_SETMASK, &old, NULL);
+}
+
+void
+wait_for_thread_state_change(struct thread *thread, lispobj state)
+{
+ sigset_t old;
+ os_sem_t *wait_sem;
+ block_blockable_signals(NULL, &old);
+ start:
+ os_sem_wait(thread->state_sem, "wait_for_thread_state_change");
+ if (thread->state == state) {
+ switch (state) {
+ case STATE_RUNNING:
+ wait_sem = thread->state_not_running_sem;
+ thread->state_not_running_waitcount++;
+ break;
+ case STATE_STOPPED:
+ wait_sem = thread->state_not_stopped_sem;
+ thread->state_not_stopped_waitcount++;
+ break;
+ default:
+ lose("Invalid state in wait_for_thread_state_change: "OBJ_FMTX"\n", state);
+ }
+ } else {
+ wait_sem = NULL;
+ }
+ os_sem_post(thread->state_sem, "wait_for_thread_state_change");
+ if (wait_sem) {
+ os_sem_wait(wait_sem, "wait_for_thread_state_change");
+ goto start;
+ }
+ thread_sigmask(SIG_SETMASK, &old, NULL);
+}
+#endif /* sb-safepoint */
+#endif /* sb-thread */
+
+static int
+initial_thread_trampoline(struct thread *th)
+{
+ lispobj function;
+#if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
+ lispobj *args = NULL;
+#endif
+#ifdef LISP_FEATURE_SB_THREAD
+ pthread_setspecific(lisp_thread, (void *)1);
+#endif
+#if defined(THREADS_USING_GCSIGNAL) && defined(LISP_FEATURE_PPC)
+ /* SIG_STOP_FOR_GC defaults to blocked on PPC? */
+ unblock_gc_signals(0,0);
+#endif
+ function = th->no_tls_value_marker;
+ th->no_tls_value_marker = NO_TLS_VALUE_MARKER_WIDETAG;
+ if(arch_os_thread_init(th)==0) return 1;
+ link_thread(th);
+ th->os_thread=thread_self();
+#ifndef LISP_FEATURE_WIN32
+ protect_control_stack_hard_guard_page(1, NULL);
+#endif
+ protect_binding_stack_hard_guard_page(1, NULL);
+ protect_alien_stack_hard_guard_page(1, NULL);
+#ifndef LISP_FEATURE_WIN32
+ protect_control_stack_guard_page(1, NULL);
+#endif
+ protect_binding_stack_guard_page(1, NULL);
+ protect_alien_stack_guard_page(1, NULL);
+
+#if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
+ return call_into_lisp_first_time(function,args,0);
+#else
+ return funcall0(function);
+#endif
+}
+
+#ifdef LISP_FEATURE_SB_THREAD
+
+# if defined(IMMEDIATE_POST_MORTEM)
+
+/*
+ * If this feature is set, we are running on a stack managed by the OS,
+ * and no fancy delays are required for anything. Just do it.