+#ifdef LISP_FEATURE_SB_THREAD
+
+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); \
+ }
+#endif
+
+
+#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 int