+/* initialized in interrupt_init */
+sigset_t deferrable_sigset;
+sigset_t blockable_sigset;
+sigset_t gc_sigset;
+
+#endif
+
+#if !defined(LISP_FEATURE_WIN32) || defined(LISP_FEATURE_SB_THREAD)
+boolean
+deferrables_blocked_p(sigset_t *sigset)
+{
+ return all_signals_blocked_p(sigset, &deferrable_sigset, "deferrable");
+}
+#endif
+
+void
+check_deferrables_unblocked_or_lose(sigset_t *sigset)
+{
+#if !defined(LISP_FEATURE_WIN32) || defined(LISP_FEATURE_SB_THREAD)
+ if (deferrables_blocked_p(sigset))
+ lose("deferrables blocked\n");
+#endif
+}
+
+void
+check_deferrables_blocked_or_lose(sigset_t *sigset)
+{
+#if !defined(LISP_FEATURE_WIN32) || defined(LISP_FEATURE_SB_THREAD)
+ if (!deferrables_blocked_p(sigset))
+ lose("deferrables unblocked\n");
+#endif
+}
+
+#if !defined(LISP_FEATURE_WIN32) || defined(LISP_FEATURE_SB_THREAD)
+boolean
+blockables_blocked_p(sigset_t *sigset)
+{
+ return all_signals_blocked_p(sigset, &blockable_sigset, "blockable");
+}
+#endif
+
+void
+check_blockables_unblocked_or_lose(sigset_t *sigset)
+{
+#if !defined(LISP_FEATURE_WIN32) || defined(LISP_FEATURE_SB_THREAD)
+ if (blockables_blocked_p(sigset))
+ lose("blockables blocked\n");
+#endif
+}
+
+void
+check_blockables_blocked_or_lose(sigset_t *sigset)
+{
+#if !defined(LISP_FEATURE_WIN32)
+ /* On Windows, there are no actual signals, but since the win32 port
+ * tracks the sigmask and checks it explicitly, some functions are
+ * still required to keep the mask set up properly. (After all, the
+ * goal of the sigmask emulation is to not have to change all the
+ * call sites in the first place.)
+ *
+ * However, this does not hold for all signals equally: While
+ * deferrables matter ("is interrupt-thread okay?"), it is not worth
+ * having to set up blockables properly (which include the
+ * non-existing GC signals).
+ *
+ * Yet, as the original comment explains it:
+ * Adjusting FREE-INTERRUPT-CONTEXT-INDEX* and other aspecs of
+ * fake_foreign_function_call machinery are sometimes useful here[...].
+ *
+ * So we merely skip this assertion.
+ * -- DFL, trying to expand on a comment by AK.
+ */
+ if (!blockables_blocked_p(sigset))
+ lose("blockables unblocked\n");
+#endif
+}
+
+#ifndef LISP_FEATURE_SB_SAFEPOINT
+#if !defined(LISP_FEATURE_WIN32)
+boolean
+gc_signals_blocked_p(sigset_t *sigset)
+{
+ return all_signals_blocked_p(sigset, &gc_sigset, "gc");
+}
+#endif
+
+void
+check_gc_signals_unblocked_or_lose(sigset_t *sigset)
+{
+#if !defined(LISP_FEATURE_WIN32)
+ if (gc_signals_blocked_p(sigset))
+ lose("gc signals blocked\n");
+#endif
+}
+
+void
+check_gc_signals_blocked_or_lose(sigset_t *sigset)
+{
+#if !defined(LISP_FEATURE_WIN32)
+ if (!gc_signals_blocked_p(sigset))
+ lose("gc signals unblocked\n");
+#endif
+}
+#endif
+
+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