+ }
+}
+
+/* this is the first thing that runs in the child (which is why the
+ * silly calling convention). Basically it calls the user's requested
+ * lisp function after doing arch_os_thread_init and whatever other
+ * bookkeeping needs to be done
+ */
+int
+new_thread_trampoline(struct thread *th)
+{
+ lispobj function;
+ int result, lock_ret;
+
+ FSHOW((stderr,"/creating thread %lu\n", thread_self()));
+ function = th->no_tls_value_marker;
+ th->no_tls_value_marker = NO_TLS_VALUE_MARKER_WIDETAG;
+ if(arch_os_thread_init(th)==0) {
+ /* FIXME: handle error */
+ lose("arch_os_thread_init failed\n");
+ }
+
+ th->os_thread=thread_self();
+ protect_control_stack_guard_page(1);
+ /* Since GC can only know about this thread from the all_threads
+ * list and we're just adding this thread to it there is no danger
+ * of deadlocking even with SIG_STOP_FOR_GC blocked (which it is
+ * not). */
+ lock_ret = pthread_mutex_lock(&all_threads_lock);
+ gc_assert(lock_ret == 0);
+ link_thread(th);
+ lock_ret = pthread_mutex_unlock(&all_threads_lock);
+ gc_assert(lock_ret == 0);
+
+ result = funcall0(function);
+
+ /* Block GC */
+ block_blockable_signals();
+ th->state=STATE_DEAD;
+
+ /* SIG_STOP_FOR_GC is blocked and GC might be waiting for this
+ * thread, but since we are already dead it won't wait long. */
+ lock_ret = pthread_mutex_lock(&all_threads_lock);
+ gc_assert(lock_ret == 0);
+
+ gc_alloc_update_page_tables(0, &th->alloc_region);
+ unlink_thread(th);
+ pthread_mutex_unlock(&all_threads_lock);
+ gc_assert(lock_ret == 0);
+
+ if(th->tls_cookie>=0) arch_os_thread_cleanup(th);
+ os_invalidate((os_vm_address_t)th->interrupt_data,
+ (sizeof (struct interrupt_data)));
+
+#ifdef LISP_FEATURE_MACH_EXCEPTION_HANDLER
+ FSHOW((stderr, "Deallocating mach port %x\n", THREAD_STRUCT_TO_EXCEPTION_PORT(th)));
+ mach_port_move_member(mach_task_self(),
+ THREAD_STRUCT_TO_EXCEPTION_PORT(th),
+ MACH_PORT_NULL);
+ mach_port_deallocate(mach_task_self(),
+ THREAD_STRUCT_TO_EXCEPTION_PORT(th));
+ mach_port_destroy(mach_task_self(),
+ THREAD_STRUCT_TO_EXCEPTION_PORT(th));
+#endif
+
+ schedule_thread_post_mortem(th);
+ FSHOW((stderr,"/exiting thread %p\n", thread_self()));
+ return result;
+}
+
+#endif /* LISP_FEATURE_SB_THREAD */
+
+static void
+free_thread_struct(struct thread *th)
+{
+ if (th->interrupt_data)
+ os_invalidate((os_vm_address_t) th->interrupt_data,
+ (sizeof (struct interrupt_data)));
+ os_invalidate((os_vm_address_t) th->os_address,
+ THREAD_STRUCT_SIZE);