Eliminate style-warning about undefined type GLOBAL-VAR
[sbcl.git] / src / code / deadline.lisp
blob556dba1e5b08bf213a46b08247c3aad8a27d916f
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 ;;; Current deadline as internal time units or NIL.
16 (declaim (type (or unsigned-byte null) *deadline*))
17 (!defvar *deadline* nil)
19 ;;; The relative number of seconds the current deadline corresponds
20 ;;; to. Used for continuing from TIMEOUT conditions.
21 (!defvar *deadline-seconds* nil)
23 (declaim (inline seconds-to-internal-time))
24 (defun seconds-to-internal-time (seconds)
25 (truncate (* seconds sb!xc:internal-time-units-per-second)))
27 (defmacro with-deadline ((&key seconds override)
28 &body body)
29 #!+sb-doc
30 "Arranges for a TIMEOUT condition to be signalled if an operation
31 respecting deadlines occurs either after the deadline has passed, or
32 would take longer than the time left to complete.
34 Currently only blocking IO operations, GET-MUTEX, and CONDITION-WAIT
35 respect deadlines, but this includes their implicit uses inside SBCL
36 itself.
38 Unless OVERRIDE is true, existing deadlines can only be restricted,
39 not extended. Deadlines are per thread: children are unaffected by
40 their parent's deadlines.
42 Experimental."
43 (with-unique-names (tmp deadline-seconds deadline)
44 ;; We're operating on a millisecond precision, so a single-float
45 ;; is enough, and is an immediate on 64bit platforms.
46 `(let* ((,tmp ,seconds)
47 (,deadline-seconds
48 (when ,tmp
49 (coerce ,tmp 'single-float)))
50 (,deadline
51 (when ,deadline-seconds
52 (+ (seconds-to-internal-time ,deadline-seconds)
53 (get-internal-real-time)))))
54 (multiple-value-bind (*deadline* *deadline-seconds*)
55 (if ,override
56 (values ,deadline ,deadline-seconds)
57 (let ((old *deadline*))
58 (if (and old (or (not ,deadline) (< old ,deadline)))
59 (values old *deadline-seconds*)
60 (values ,deadline ,deadline-seconds))))
61 ,@body))))
63 (declaim (inline decode-internal-time))
64 (defun decode-internal-time (time)
65 #!+sb-doc
66 "Returns internal time value TIME decoded into seconds and microseconds."
67 (multiple-value-bind (sec frac)
68 (truncate time sb!xc:internal-time-units-per-second)
69 (values sec (* frac sb!unix::micro-seconds-per-internal-time-unit))))
71 (defun signal-timeout (datum &rest arguments)
72 #!+sb-doc
73 "Signals a timeout condition while inhibiting further timeouts due to
74 deadlines while the condition is being handled."
75 ;; FIXME: Maybe we should make ERROR do WITH-INTERRUPTS instead of
76 ;; putting it all over the place (now that we have ALLOW-WITH-INTERRUPTS.)
77 (with-interrupts
78 ;; Don't signal a deadline while handling a non-deadline timeout.
79 (let ((*deadline* nil))
80 (apply #'error datum arguments))))
82 (defun signal-deadline ()
83 #!+sb-doc
84 "Signal a DEADLINE-TIMEOUT condition, and associate a DEFER-DEADLINE
85 restart with it. Implementors of blocking functions are responsible
86 for calling this when a deadline is reached."
87 ;; Make sure we don't signal the same deadline twice. LET is not good
88 ;; enough: we might catch the same deadline again while unwinding.
89 (when *deadline*
90 (setf *deadline* nil))
91 (with-interrupts
92 (restart-case
93 (error 'deadline-timeout :seconds *deadline-seconds*)
94 (defer-deadline (&optional (seconds *deadline-seconds*))
95 :report "Defer the deadline for SECONDS more."
96 :interactive (lambda ()
97 (sb!int:read-evaluated-form
98 "By how many seconds shall the deadline ~
99 be deferred?: "))
100 (let* ((new-deadline-seconds (coerce seconds 'single-float))
101 (new-deadline (+ (seconds-to-internal-time new-deadline-seconds)
102 (get-internal-real-time))))
103 (setf *deadline* new-deadline
104 *deadline-seconds* new-deadline-seconds)))
105 (cancel-deadline ()
106 :report "Cancel the deadline and continue."
107 (setf *deadline* nil *deadline-seconds* nil))))
108 nil)
110 (defun defer-deadline (seconds &optional condition)
111 #!+sb-doc
112 "Find the DEFER-DEADLINE restart associated with CONDITION, and
113 invoke it with SECONDS as argument (deferring the deadline by that many
114 seconds.) Otherwise return NIL if the restart is not found."
115 (try-restart 'defer-deadline condition seconds))
117 (defun cancel-deadline (&optional condition)
118 #!+sb-doc
119 "Find and invoke the CANCEL-DEADLINE restart associated with
120 CONDITION, or return NIL if the restart is not found."
121 (try-restart 'cancel-deadline condition))
123 (declaim (inline relative-decoded-times))
124 (defun relative-decoded-times (abs-sec abs-usec)
125 #!+sb-doc
126 "Returns relative decoded times: difference between SEC and USEC and
127 current real time."
128 (multiple-value-bind (now-sec now-usec)
129 (decode-internal-time (get-internal-real-time))
130 (let ((rel-sec (- abs-sec now-sec)))
131 (cond ((> now-usec abs-usec)
132 (values (max 0 (1- rel-sec))
133 (- (+ abs-usec 1000000) now-usec)))
135 (values (max 0 rel-sec)
136 (- abs-usec now-usec)))))))
138 ;;; Returns TIMEOUT-SEC, TIMEOUT-USEC, DEADLINE-SEC, DEADLINE-USEC, SIGNALP
140 ;;; Takes *DEADLINE* into account: if it occurs before given SECONDS,
141 ;;; the values are based on it, and DEADLINEP is true -- and the
142 ;;; receipent of the values should call SIGNAL-TIMEOUT if the decoded
143 ;;; timeout is reached.
145 ;;; If SECONDS is NIL and there is no *DEADLINE* all returned values
146 ;;; are NIL.
147 (defun decode-timeout (seconds)
148 #!+sb-doc
149 "Decodes a relative timeout in SECONDS into five values, taking any
150 global deadlines into account: TO-SEC, TO-USEC, STOP-SEC, STOP-USEC,
151 DEADLINEP.
153 TO-SEC and TO-USEC indicate the relative timeout in seconds and microseconds.
154 STOP-SEC and STOP-USEC indicate the absolute timeout in seconds and
155 microseconds. DEADLINEP is true if the returned values reflect a global
156 deadline instead of the local timeout indicated by SECONDS.
158 If SECONDS is null and there is no global timeout all returned values will be
159 null. If a global deadline has already passed when DECODE-TIMEOUT is called,
160 it will signal a timeout condition."
161 (tagbody
162 :restart
163 (let* ((timeout (when seconds (seconds-to-internal-time seconds)))
164 (now (get-internal-real-time))
165 (deadline *deadline*)
166 (deadline-timeout
167 (when deadline
168 (let ((time-left (- deadline now)))
169 (if (plusp time-left)
170 time-left
171 (progn
172 (signal-deadline)
173 (go :restart)))))))
174 (return-from decode-timeout
175 (multiple-value-bind (final-timeout final-deadline signalp)
176 ;; Use either *DEADLINE* or TIMEOUT to produce both a timeout
177 ;; and deadline in internal-time units
178 (cond ((and deadline timeout)
179 (if (< timeout deadline-timeout)
180 (values timeout (+ timeout now) nil)
181 (values deadline-timeout deadline t)))
182 (deadline
183 (values deadline-timeout deadline t))
184 (timeout
185 (values timeout (+ timeout now) nil))
187 (values nil nil nil)))
188 (if final-timeout
189 (multiple-value-bind (to-sec to-usec)
190 (decode-internal-time final-timeout)
191 (multiple-value-bind (stop-sec stop-usec)
192 (decode-internal-time final-deadline)
193 (values (max 0 to-sec) (max 0 to-usec) stop-sec stop-usec signalp)))
194 (values nil nil nil nil nil)))))))