+void
+block_deferrable_signals(sigset_t *where, sigset_t *old)
+{
+#if !defined(LISP_FEATURE_WIN32) || defined(LISP_FEATURE_SB_THREAD)
+ block_signals(&deferrable_sigset, where, old);
+#endif
+}
+
+void
+block_blockable_signals(sigset_t *where, sigset_t *old)
+{
+#if !defined(LISP_FEATURE_WIN32) || defined(LISP_FEATURE_SB_THREAD)
+ block_signals(&blockable_sigset, where, old);
+#endif
+}
+
+#ifndef LISP_FEATURE_SB_SAFEPOINT
+void
+block_gc_signals(sigset_t *where, sigset_t *old)
+{
+#if !defined(LISP_FEATURE_WIN32) || defined(LISP_FEATURE_SB_THREAD)
+ block_signals(&gc_sigset, where, old);
+#endif
+}
+#endif
+
+void
+unblock_deferrable_signals(sigset_t *where, sigset_t *old)
+{
+#if !defined(LISP_FEATURE_WIN32) || defined(LISP_FEATURE_SB_THREAD)
+ if (interrupt_handler_pending_p())
+ lose("unblock_deferrable_signals: losing proposition\n");
+#ifndef LISP_FEATURE_SB_SAFEPOINT
+ check_gc_signals_unblocked_or_lose(where);
+#endif
+ unblock_signals(&deferrable_sigset, where, old);
+#endif
+}
+
+void
+unblock_blockable_signals(sigset_t *where, sigset_t *old)
+{
+#if !defined(LISP_FEATURE_WIN32) || defined(LISP_FEATURE_SB_THREAD)
+ unblock_signals(&blockable_sigset, where, old);
+#endif
+}
+
+#ifndef LISP_FEATURE_SB_SAFEPOINT
+void
+unblock_gc_signals(sigset_t *where, sigset_t *old)
+{
+#ifndef LISP_FEATURE_WIN32
+ unblock_signals(&gc_sigset, where, old);
+#endif
+}
+#endif
+
+void
+unblock_signals_in_context_and_maybe_warn(os_context_t *context)
+{
+#if !defined(LISP_FEATURE_WIN32) || defined(LISP_FEATURE_SB_THREAD)
+ sigset_t *sigset = os_context_sigmask_addr(context);
+#ifndef LISP_FEATURE_SB_SAFEPOINT
+ if (all_signals_blocked_p(sigset, &gc_sigset, "gc")) {
+ corruption_warning_and_maybe_lose(
+"Enabling blocked gc signals to allow returning to Lisp without risking\n\
+gc deadlocks. Since GC signals are only blocked in signal handlers when \n\
+they are not safe to interrupt at all, this is a pretty severe occurrence.\n");
+ unblock_gc_signals(sigset, 0);
+ }
+#endif
+ if (!interrupt_handler_pending_p()) {
+ unblock_deferrable_signals(sigset, 0);
+ }
+#endif
+}
+\f
+
+inline static void
+check_interrupts_enabled_or_lose(os_context_t *context)
+{
+ struct thread *thread=arch_os_get_current_thread();
+ if (SymbolValue(INTERRUPTS_ENABLED,thread) == NIL)
+ lose("interrupts not enabled\n");
+ if (arch_pseudo_atomic_atomic(context))
+ lose ("in pseudo atomic section\n");
+}
+
+/* Save sigset (or the current sigmask if 0) if there is no pending
+ * handler, because that means that deferabbles are already blocked.
+ * The purpose is to avoid losing the pending gc signal if a
+ * deferrable interrupt async unwinds between clearing the pseudo
+ * atomic and trapping to GC.*/
+#ifndef LISP_FEATURE_SB_SAFEPOINT
+void
+maybe_save_gc_mask_and_block_deferrables(sigset_t *sigset)
+{
+#ifndef LISP_FEATURE_WIN32
+ struct thread *thread = arch_os_get_current_thread();
+ struct interrupt_data *data = thread->interrupt_data;
+ sigset_t oldset;
+ /* Obviously, this function is called when signals may not be
+ * blocked. Let's make sure we are not interrupted. */
+ block_blockable_signals(0, &oldset);
+#ifndef LISP_FEATURE_SB_THREAD
+ /* With threads a SIG_STOP_FOR_GC and a normal GC may also want to
+ * block. */
+ if (data->gc_blocked_deferrables)
+ lose("gc_blocked_deferrables already true\n");
+#endif
+ if ((!data->pending_handler) &&
+ (!data->gc_blocked_deferrables)) {
+ FSHOW_SIGNAL((stderr,"/setting gc_blocked_deferrables\n"));
+ data->gc_blocked_deferrables = 1;
+ if (sigset) {
+ /* This is the sigmask of some context. */
+ sigcopyset(&data->pending_mask, sigset);
+ sigaddset_deferrable(sigset);
+ thread_sigmask(SIG_SETMASK,&oldset,0);
+ return;
+ } else {
+ /* Operating on the current sigmask. Save oldset and
+ * unblock gc signals. In the end, this is equivalent to
+ * blocking the deferrables. */
+ sigcopyset(&data->pending_mask, &oldset);
+ thread_sigmask(SIG_UNBLOCK, &gc_sigset, 0);
+ return;
+ }
+ }
+ thread_sigmask(SIG_SETMASK,&oldset,0);
+#endif
+}
+#endif