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