- get_spinlock(&all_threads_lock,th->pid);
- if(countdown_to_gc>0) countdown_to_gc--;
- if(th==all_threads)
- all_threads=th->next;
- else {
- struct thread *th1=all_threads;
- while(th1->next!=th) th1=th1->next;
- th1->next=th->next; /* unlink */
+ get_spinlock(&thread_start_lock,arch_os_get_current_thread()->pid);
+#ifdef QSHOW_SIGNALS
+ SHOW("create_thread:got lock");
+#endif
+ kid_pid=clone(new_thread_trampoline,
+ (((void*)th->control_stack_start)+
+ THREAD_CONTROL_STACK_SIZE-16),
+ CLONE_FILES|SIG_THREAD_EXIT|CLONE_VM,th);
+
+ if(kid_pid>0) {
+ link_thread(th,kid_pid);
+ /* wait here until our thread is started: see new_thread_trampoline */
+ while(th->state==STATE_STARTING) sched_yield();
+ /* it's started and initialized, it's safe to gc */
+ release_spinlock(&thread_start_lock);
+#ifdef QSHOW_SIGNALS
+ SHOW("create_thread:released lock");
+#endif
+ return th->pid;
+ } else {
+ release_spinlock(&thread_start_lock);
+#ifdef QSHOW_SIGNALS
+ SHOW("create_thread:released lock(failure)");
+#endif
+ os_invalidate((os_vm_address_t) th->control_stack_start,
+ ((sizeof (lispobj))
+ * (th->control_stack_end-th->control_stack_start)) +
+ BINDING_STACK_SIZE+ALIEN_STACK_SIZE+dynamic_values_bytes+
+ 32*SIGSTKSZ);
+ return 0;