pthread_mutex_t all_threads_lock = PTHREAD_MUTEX_INITIALIZER;
 
+/* When trying to get all_threads_lock one should make sure that
+ * SIG_STOP_FOR_GC is not blocked. Else there would be a possible
+ * deadlock: gc locks it, other thread blocks signals, gc sends stop
+ * request to other thread and waits, other thread blocks on lock. */
+void check_sig_stop_for_gc_can_arrive_or_lose()
+{
+    /* Get the current sigmask, by blocking the empty set. */
+    sigset_t empty,current;
+    sigemptyset(&empty);
+    thread_sigmask(SIG_BLOCK, &empty, ¤t);
+    if (sigismember(¤t,SIG_STOP_FOR_GC))
+        lose("SIG_STOP_FOR_GC cannot arrive: it is blocked\n");
+    if (SymbolValue(GC_INHIBIT,arch_os_get_current_thread()) != NIL)
+        lose("SIG_STOP_FOR_GC cannot arrive: gc is inhibited\n");
+    if (arch_pseudo_atomic_atomic(NULL))
+        lose("SIG_STOP_FOR_GC cannot arrive: in pseudo atomic\n");
+}
+
+#define GET_ALL_THREADS_LOCK(name) \
+    { \
+        sigset_t _newset,_oldset; \
+        sigemptyset(&_newset); \
+        sigaddset_deferrable(&_newset); \
+        thread_sigmask(SIG_BLOCK, &_newset, &_oldset); \
+        check_sig_stop_for_gc_can_arrive_or_lose(); \
+        FSHOW_SIGNAL((stderr,"/%s:waiting on lock=%ld, thread=%lu\n",name, \
+               all_threads_lock,arch_os_get_current_thread()->os_thread)); \
+        pthread_mutex_lock(&all_threads_lock); \
+        FSHOW_SIGNAL((stderr,"/%s:got lock, thread=%lu\n", \
+               name,arch_os_get_current_thread()->os_thread));
+
+#define RELEASE_ALL_THREADS_LOCK(name) \
+        FSHOW_SIGNAL((stderr,"/%s:released lock\n",name)); \
+        pthread_mutex_unlock(&all_threads_lock); \
+        thread_sigmask(SIG_SETMASK,&_oldset,0); \
+    }
+
 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
 extern lispobj call_into_lisp_first_time(lispobj fun, lispobj *args, int nargs);
 #endif
 static void
 link_thread(struct thread *th)
 {
-    th->os_thread=thread_self();
     if (all_threads) all_threads->prev=th;
     th->next=all_threads;
     th->prev=0;
     all_threads=th;
-    protect_control_stack_guard_page(1);
 }
 
 #ifdef LISP_FEATURE_SB_THREAD
     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();
+    protect_control_stack_guard_page(1);
+    th->state = STATE_RUNNING;
 
 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
     return call_into_lisp_first_time(function,args,0);
         lose("arch_os_thread_init failed\n");
     }
 
-    /* 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. */
+    th->os_thread=thread_self();
+    protect_control_stack_guard_page(1);
+    /* This thread is in STATE_STARTING so the GC is not sending it
+     * SIG_STOP_FOR_GC => no danger of deadlocking even with
+     * SIG_STOP_FOR_GC blocked. The lock is acquired in order not to
+     * enter running state with the gc running. */
     pthread_mutex_lock(&all_threads_lock);
-    link_thread(th);
+    th->state = STATE_RUNNING;
     pthread_mutex_unlock(&all_threads_lock);
+    /* Now that we entered STATE_RUNNING let the gc suspend this
+     * thread. */
+    {
+        sigset_t sigset;
+        sigemptyset(&sigset);
+        sigaddset(&sigset, SIG_STOP_FOR_GC);
+        thread_sigmask(SIG_UNBLOCK, &sigset, 0);
+    }
 
     result = funcall0(function);
     th->state=STATE_DEAD;
     th->binding_stack_pointer=th->binding_stack_start;
     th->this=th;
     th->os_thread=0;
-    th->state=STATE_RUNNING;
+    th->state=STATE_STARTING;
 #ifdef LISP_FEATURE_STACK_GROWS_DOWNWARD_NOT_UPWARD
     th->alien_stack_pointer=((void *)th->alien_stack_start
                              + ALIEN_STACK_SIZE-N_WORD_BYTES);
     sigset_t newset,oldset;
     boolean r=1;
     sigemptyset(&newset);
-    /* Blocking deferrable signals is enough, no need to block
-     * SIG_STOP_FOR_GC because the child process is not linked onto
-     * all_threads until it's ready. */
-    sigaddset_deferrable(&newset);
+    sigaddset_blockable(&newset);
     thread_sigmask(SIG_BLOCK, &newset, &oldset);
 
     if((pthread_attr_init(&attr)) ||
 
     if(linux_no_threads_p) return 0;
 
-    th=create_thread_struct(initial_function);
+    /* The new thread must be linked immediately onto all_threads for
+     * gc. */
+    GET_ALL_THREADS_LOCK("create_thread")
+    /* If it is too slow most of the allocation/initialization can
+     * be done without the lock. */
+    th = create_thread_struct(initial_function);
+    if (th)
+        link_thread(th);
+    RELEASE_ALL_THREADS_LOCK("create_thread")
     if(th==0) return 0;
 
     if (create_os_thread(th,&kid_tid)) {
         return kid_tid;
     } else {
+        GET_ALL_THREADS_LOCK("create_thread")
+        unlink_thread(th);
+        RELEASE_ALL_THREADS_LOCK("create_thread")
         free_thread_struct(th);
         return 0;
     }
     FSHOW_SIGNAL((stderr,"/gc_stop_the_world:signals sent\n"));
     /* wait for the running threads to stop or finish */
     for(p=all_threads;p;) {
-        gc_assert(p->os_thread!=0);
-        if((p==th) || (p->state==STATE_SUSPENDED) ||
-           (p->state==STATE_DEAD)) {
-            p=p->next;
-        } else {
+        if((p!=th) && (p->state==STATE_RUNNING)) {
+            gc_assert(p->os_thread!=0);
             sched_yield();
+        } else {
+            p=p->next;
         }
     }
     FSHOW_SIGNAL((stderr,"/gc_stop_the_world:end\n"));
      * restarting */
     FSHOW_SIGNAL((stderr,"/gc_start_the_world:begin\n"));
     for(p=all_threads;p;p=p->next) {
-        gc_assert(p->os_thread!=0);
-        if((p!=th) && (p->state!=STATE_DEAD)) {
+        if((p!=th) && (p->state!=STATE_STARTING) && (p->state!=STATE_DEAD)) {
+            gc_assert(p->os_thread!=0);
             if(p->state!=STATE_SUSPENDED) {
                 lose("gc_start_the_world: wrong thread state is %d\n",
                      fixnum_value(p->state));