1 ;;;; handling UNIX signals
3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
12 (in-package "SB!UNIX")
14 ;;;; macros for dynamically enabling and disabling signal handling
16 ;;; Notes on how the without-interrupts/with-interrupts stuff works:
18 ;;; Before invoking the supplied handler for any of the signals that
19 ;;; can be blocked, the C interrupt support code checks to see whether
20 ;;; *interrupts-enabled* has been bound to NIL. If so, it saves the
21 ;;; signal number and the value of the signal mask (from the signal
22 ;;; context), sets the signal mask to block all blockable signals,
23 ;;; sets *interrupt-pending* and returns without handling the signal.
25 ;;; When we drop out the without interrupts, we check to see whether
26 ;;; *INTERRUPT-PENDING* has been set. If so, we call
27 ;;; RECEIVE-PENDING-INTERRUPT, which generates a SIGTRAP. The C code
28 ;;; invokes the handler for the saved signal instead of the SIGTRAP
29 ;;; after replacing the signal mask in the signal context with the
30 ;;; saved value. When that hander returns, the original signal mask is
31 ;;; installed, allowing any other pending signals to be handled.
33 ;;; This means that the cost of WITHOUT-INTERRUPTS is just a special
34 ;;; binding in the case when no signals are delivered (the normal
35 ;;; case). It's only when a signal is actually delivered that we use
36 ;;; any system calls, and by then the cost of the extra system calls
37 ;;; are lost in the noise when compared with the cost of delivering
38 ;;; the signal in the first place.
40 ;;; The conditional bindings done by this code here are worth the
41 ;;; trouble as binding is more expensive then read & test -- so
49 ;;; provided that the first branch is true "often enough".
51 (defvar *interrupts-enabled
* t
)
52 (defvar *interrupt-pending
* nil
)
53 (defvar *allow-with-interrupts
* t
)
55 (sb!xc
:defmacro without-interrupts
(&body body
)
57 "Executes BODY with all deferrable interrupts disabled. Deferrable
58 interrupts arriving during execution of the BODY take effect after BODY has
61 Deferrable interrupts include most blockable POSIX signals, and
62 SB-THREAD:INTERRUPT-THREAD. Does not interfere with garbage collection, and
63 unlike in many traditional Lisps using userspace threads, in SBCL
64 WITHOUT-INTERRUPTS does not inhibit scheduling of other threads.
66 Binds ALLOW-WITH-INTERRUPTS and WITH-LOCAL-INTERRUPTS as a local macros.
68 ALLOW-WITH-INTERRUPTS allows the WITH-INTERRUPTS to take effect during the
69 dynamic scope of its body, unless there is an outer WITHOUT-INTERRUPTS without
70 a corresponding ALLOW-WITH-INTERRUPTS.
72 WITH-LOCAL-INTERRUPTS executes its body with interrupts enabled provided that
73 for there is an ALLOW-WITH-INTERRUPTS for every WITHOUT-INTERRUPTS surrounding
74 the current one. WITH-LOCAL-INTERRUPTS is equivalent to:
76 (allow-with-interrupts (with-interrupts ...))
78 Care must be taken not to let either ALLOW-WITH-INTERRUPTS or
79 WITH-LOCAL-INTERRUPTS appear in a function that escapes from inside the
80 WITHOUT-INTERRUPTS in:
83 ;; The body of the lambda would be executed with WITH-INTERRUPTS allowed
84 ;; regardless of the interrupt policy in effect when it is called.
85 (lambda () (allow-with-interrupts ...)))
88 ;; The body of the lambda would be executed with interrupts enabled
89 ;; regardless of the interrupt policy in effect when it is called.
90 (lambda () (with-local-interrupts ...)))
92 (with-unique-names (outer-allow-with-interrupts without-interrupts-body
)
93 `(flet ((,without-interrupts-body
()
94 (declare (disable-package-locks allow-with-interrupts with-local-interrupts
))
95 (macrolet ((allow-with-interrupts (&body allow-forms
)
96 `(let ((*allow-with-interrupts
* ,',outer-allow-with-interrupts
))
98 (with-local-interrupts (&body with-forms
)
99 `(let ((*allow-with-interrupts
* ,',outer-allow-with-interrupts
)
100 (*interrupts-enabled
* ,',outer-allow-with-interrupts
))
101 (when (and ,',outer-allow-with-interrupts
*interrupt-pending
*)
102 (receive-pending-interrupt))
103 (locally ,@with-forms
))))
104 (let ((*interrupts-enabled
* nil
)
105 (,outer-allow-with-interrupts
*allow-with-interrupts
*)
106 (*allow-with-interrupts
* nil
))
107 (declare (ignorable ,outer-allow-with-interrupts
))
108 (declare (enable-package-locks allow-with-interrupts with-local-interrupts
))
110 (if *interrupts-enabled
*
112 (,without-interrupts-body
)
113 ;; If we were interrupted in the protected section,
114 ;; then the interrupts are still blocked and it remains
115 ;; so until the pending interrupt is handled.
117 ;; If we were not interrupted in the protected section,
118 ;; but here, then even if the interrupt handler enters
119 ;; another WITHOUT-INTERRUPTS, the pending interrupt will be
120 ;; handled immediately upon exit from said
121 ;; WITHOUT-INTERRUPTS, so it is as if nothing has happened.
122 (when *interrupt-pending
*
123 (receive-pending-interrupt)))
124 (,without-interrupts-body
)))))
126 (sb!xc
:defmacro with-interrupts
(&body body
)
128 "Executes BODY with deferrable interrupts conditionally enabled. If there
129 are pending interrupts they take effect prior to executing BODY.
131 As interrupts are normally allowed WITH-INTERRUPTS only makes sense if there
132 is an outer WITHOUT-INTERRUPTS with a corresponding ALLOW-WITH-INTERRUPTS:
133 interrupts are not enabled if any outer WITHOUT-INTERRUPTS is not accompanied
134 by ALLOW-WITH-INTERRUPTS."
135 (with-unique-names (allowp enablep
)
136 ;; We could manage without ENABLEP here, but that would require
137 ;; taking extra care not to ever have *ALLOW-WITH-INTERRUPTS* NIL
138 ;; and *INTERRUPTS-ENABLED* T -- instead of risking future breakage
139 ;; we take the tiny hit here.
140 `(let* ((,allowp
*allow-with-interrupts
*)
141 (,enablep
*interrupts-enabled
*)
142 (*interrupts-enabled
* (or ,enablep
,allowp
)))
143 (when (and (and ,allowp
(not ,enablep
)) *interrupt-pending
*)
144 (receive-pending-interrupt))
147 (defmacro allow-with-interrupts
(&body body
)
148 (declare (ignore body
))
149 (error "~S is valid only inside ~S." 'allow-with-interrupts
'without-interrupts
))
151 (defmacro with-local-interrupts
(&body body
)
152 (declare (ignore body
))
153 (error "~S is valid only inside ~S." 'with-local-interrupts
'without-interrupts
))
155 ;;; A low-level operation that assumes that *INTERRUPTS-ENABLED* is false,
156 ;;; and *ALLOW-WITH-INTERRUPTS* is true.
157 (defun %check-interrupts
()
158 ;; Here we check for pending interrupts first, because reading a special
159 ;; is faster then binding it!
160 (when *interrupt-pending
*
161 (let ((*interrupts-enabled
* t
))
162 (receive-pending-interrupt))))