Tolerate non-simple strings when checking arguments to CERROR.
[sbcl.git] / src / code / deadline.lisp
blob2245dad01f8efa06fdd2446d1aa2fda7a7d065e4
1 ;;;; global deadlines for blocking functions: a threadsafe alternative
2 ;;;; to asynch timeouts
4 ;;;; This software is part of the SBCL system. See the README file for
5 ;;;; more information.
6 ;;;;
7 ;;;; This software is derived from the CMU CL system, which was
8 ;;;; written at Carnegie Mellon University and released into the
9 ;;;; public domain. The software is in the public domain and is
10 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
11 ;;;; files for more information.
13 (in-package "SB!IMPL")
15 (declaim (inline make-deadline))
16 (defstruct (deadline
17 (:constructor make-deadline (internal-time seconds))
18 (:copier nil))
19 ;; The absolute deadline in internal time.
20 (internal-time nil :type internal-time :read-only t)
21 ;; A relative representation of the deadline in seconds relative to
22 ;; the time this deadline was established. This is used in error
23 ;; message and when extended the deadline by the original amount of
24 ;; time.
25 (seconds nil :type (real 0) :read-only t))
26 (declaim (freeze-type deadline))
28 ;;; Current DEADLINE or NIL.
29 (declaim (type (or deadline null) *deadline*))
30 (!define-thread-local *deadline* nil)
32 (declaim (inline seconds-to-internal-time))
33 (defun seconds-to-internal-time (seconds)
34 (the internal-time
35 (values (truncate (* seconds sb!xc:internal-time-units-per-second)))))
37 (declaim (inline seconds-to-maybe-internal-time))
38 (defun seconds-to-maybe-internal-time (seconds)
39 (declare (optimize (speed 3)))
40 (typecase seconds
41 ((integer 0 #.internal-seconds-limit)
42 (locally ; FIXME compiler should learn to figure that out
43 (declare (type (integer 0 #.internal-seconds-limit) seconds))
44 (seconds-to-internal-time seconds)))
45 ((single-float 0.0f0 #.(float safe-internal-seconds-limit 1.0f0))
46 (seconds-to-internal-time seconds))
47 ((and (not single-float) (real 0 #.safe-internal-seconds-limit))
48 (seconds-to-internal-time seconds))))
50 (declaim (inline seconds-to-internal-time-deadline))
51 (defun seconds-to-internal-time-deadline (seconds)
52 (let ((internal-time (when seconds
53 (seconds-to-maybe-internal-time seconds))))
54 (when internal-time
55 (+ internal-time (get-internal-real-time)))))
57 (defmacro with-deadline ((&key seconds override)
58 &body body)
59 "Arranges for a TIMEOUT condition to be signalled if an operation
60 respecting deadlines occurs either after the deadline has passed, or
61 would take longer than the time left to complete.
63 Currently only SLEEP, blocking IO operations, GET-MUTEX, and
64 CONDITION-WAIT respect deadlines, but this includes their implicit
65 uses inside SBCL itself.
67 Unless OVERRIDE is true, existing deadlines can only be restricted,
68 not extended. Deadlines are per thread: children are unaffected by
69 their parent's deadlines.
71 Experimental."
72 (once-only ((seconds seconds))
73 (with-unique-names (deadline)
74 `(labels ((with-deadline-thunk ()
75 ,@body)
76 (bind-deadline-and-call (deadline)
77 (let ((*deadline* deadline))
78 (with-deadline-thunk)))
79 (bind-new-deadline-and-call (deadline-internal-time seconds)
80 (dx-let ((deadline (make-deadline
81 deadline-internal-time seconds)))
82 (bind-deadline-and-call deadline))))
83 (let ((,deadline (when ,seconds
84 (seconds-to-internal-time-deadline ,seconds))))
85 (cond
86 ((and ,override ,deadline)
87 (bind-new-deadline-and-call ,deadline ,seconds))
88 (,override
89 (bind-deadline-and-call nil))
90 (,deadline
91 (let ((old *deadline*))
92 (if (and old (< (deadline-internal-time old)
93 ,deadline))
94 (bind-deadline-and-call old)
95 (bind-new-deadline-and-call ,deadline ,seconds))))
97 (bind-deadline-and-call nil))))))))
99 (declaim (inline decode-internal-time))
100 (defun decode-internal-time (time)
101 "Returns internal time value TIME decoded into seconds and microseconds."
102 (declare (type sb!kernel:internal-time time))
103 (multiple-value-bind (sec frac)
104 (truncate time sb!xc:internal-time-units-per-second)
105 (values sec (* frac sb!unix::micro-seconds-per-internal-time-unit))))
107 (defun signal-timeout (datum &rest arguments)
108 "Signals a timeout condition while inhibiting further timeouts due to
109 deadlines while the condition is being handled."
110 ;; FIXME: Maybe we should make ERROR do WITH-INTERRUPTS instead of
111 ;; putting it all over the place (now that we have ALLOW-WITH-INTERRUPTS.)
112 (with-interrupts
113 ;; Don't signal a deadline while handling a non-deadline timeout.
114 (let ((*deadline* nil))
115 (apply #'error datum arguments))))
117 (defun signal-deadline ()
118 "Signal a DEADLINE-TIMEOUT condition, and associate a DEFER-DEADLINE
119 restart with it. Implementors of blocking functions are responsible
120 for calling this when a deadline is reached."
121 ;; Make sure we don't signal the same deadline twice. LET is not good
122 ;; enough: we might catch the same deadline again while unwinding.
123 (let ((deadline *deadline*))
124 (when deadline
125 (setf *deadline* nil))
126 (with-interrupts
127 (let ((seconds (when deadline
128 (deadline-seconds deadline))))
129 (restart-case
130 (error 'deadline-timeout :seconds seconds)
131 (defer-deadline (&optional (seconds seconds))
132 :report "Defer the deadline for SECONDS more."
133 :interactive (lambda ()
134 (sb!int:read-evaluated-form
135 "By how many seconds shall the deadline ~
136 be deferred?: "))
137 (setf *deadline*
138 (let ((deadline (when seconds
139 (seconds-to-internal-time-deadline
140 seconds))))
141 (when deadline
142 (make-deadline deadline seconds)))))
143 (cancel-deadline ()
144 :report "Cancel the deadline and continue."
145 (setf *deadline* nil))))))
146 nil)
148 (defun defer-deadline (seconds &optional condition)
149 "Find the DEFER-DEADLINE restart associated with CONDITION, and
150 invoke it with SECONDS as argument (deferring the deadline by that many
151 seconds.) Otherwise return NIL if the restart is not found."
152 (try-restart 'defer-deadline condition seconds))
154 (defun cancel-deadline (&optional condition)
155 "Find and invoke the CANCEL-DEADLINE restart associated with
156 CONDITION, or return NIL if the restart is not found."
157 (try-restart 'cancel-deadline condition))
159 (declaim (inline relative-decoded-times))
160 (defun relative-decoded-times (abs-sec abs-usec)
161 "Returns relative decoded time as two values: difference between
162 ABS-SEC and ABS-USEC and current real time.
164 If ABS-SEC and ABS-USEC are in the past, 0 0 is returned."
165 (declare (type sb!kernel:internal-seconds abs-sec)
166 (type (mod 1000000) abs-usec))
167 (binding* (((now-sec now-usec)
168 (decode-internal-time (get-internal-real-time)))
169 (rel-sec (- abs-sec now-sec))
170 (rel-usec (- abs-usec now-usec)))
171 (when (minusp rel-usec)
172 (decf rel-sec)
173 (incf rel-usec 1000000))
174 (if (minusp rel-sec)
175 (values 0 0)
176 (values rel-sec rel-usec))))
178 ;;; Returns TIMEOUT-SEC, TIMEOUT-USEC, DEADLINE-SEC, DEADLINE-USEC, SIGNALP
180 ;;; Takes *DEADLINE* into account: if it occurs before given SECONDS,
181 ;;; the values are based on it, and DEADLINEP is true -- and the
182 ;;; receipent of the values should call SIGNAL-TIMEOUT if the decoded
183 ;;; timeout is reached.
185 ;;; If SECONDS is NIL and there is no *DEADLINE* all returned values
186 ;;; are NIL.
187 (declaim (ftype (function ((or null (real 0)))
188 (values (or null sb!kernel:internal-seconds)
189 (or null (mod 1000000))
190 (or null sb!kernel:internal-seconds)
191 (or null (mod 1000000))
193 decode-timeout))
194 (defun decode-timeout (seconds)
195 "Decodes a relative timeout in SECONDS into five values, taking any
196 global deadlines into account: TO-SEC, TO-USEC, STOP-SEC, STOP-USEC,
197 DEADLINEP.
199 TO-SEC and TO-USEC indicate the relative timeout in seconds and microseconds.
200 STOP-SEC and STOP-USEC indicate the absolute timeout in seconds and
201 microseconds. DEADLINEP is true if the returned values reflect a global
202 deadline instead of the local timeout indicated by SECONDS.
204 If SECONDS is null and there is no global timeout all returned values will be
205 null. If a global deadline has already passed when DECODE-TIMEOUT is called,
206 it will signal a timeout condition."
207 (declare (optimize speed)
208 (explicit-check))
209 (flet ((return-timeout (timeout deadline signalp)
210 (binding* (((to-sec to-usec)
211 (decode-internal-time timeout))
212 ((stop-sec stop-usec)
213 (decode-internal-time deadline)))
214 (values to-sec to-usec stop-sec stop-usec signalp)))
215 (return-no-timeout ()
216 (values nil nil nil nil nil)))
217 (let ((deadline *deadline*))
218 ;; Use either TIMEOUT or DEADLINE to produce both a timeout and
219 ;; deadline in internal-time units.
220 (if (or seconds deadline)
221 (locally
222 (declare (type (or null (real 0)) seconds))
223 (let ((timeout (and seconds
224 (seconds-to-maybe-internal-time seconds))))
225 (tagbody
226 :restart
227 (let* ((now (get-internal-real-time))
228 (deadline-internal-time (when deadline
229 (deadline-internal-time deadline)))
230 (deadline-timeout
231 (when deadline-internal-time
232 (let ((time-left (- deadline-internal-time now)))
233 (when (plusp time-left) time-left)))))
234 (return-from decode-timeout
235 (cond
236 ;; We have a timeout and a non-expired deadline. Use the
237 ;; one that expires earlier.
238 ((and timeout deadline-timeout)
239 (if (< timeout deadline-timeout)
240 (return-timeout timeout (+ timeout now) nil)
241 (return-timeout deadline-timeout deadline-internal-time t)))
242 ;; Non-expired deadline.
243 (deadline-timeout
244 (return-timeout deadline-timeout deadline-internal-time t))
245 ;; Expired deadline. Signal the DEADLINE-TIMEOUT
246 ;; condition. In case we return here (i.e. the deadline
247 ;; has been deferred or canceled), pick up the new value
248 ;; of *DEADLINE*.
249 (deadline-internal-time
250 (signal-deadline)
251 (setf deadline *deadline*)
252 (go :restart))
253 ;; There is no deadline but a timeout.
254 (timeout
255 (return-timeout timeout (+ timeout now) nil))
257 (return-no-timeout))))))))
258 (return-no-timeout)))))