1.0.6.47: small fixes
[sbcl/lichteblau.git] / src / code / deadline.lisp
blob4ea6e72df23bfe0da9a79d775abb2b47d86bd3e4
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 (defvar *deadline* nil)
17 (declaim (type (or unsigned-byte null) *deadline*))
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 respecting
30 deadlines occurs either after the deadline has passed, or would take longer
31 than the time left to complete.
33 Currently only blocking IO operations, GET-MUTEX, and CONDITION-WAIT respect
34 deadlines, but this includes their implicit uses inside SBCL itself.
36 Experimental."
37 (with-unique-names (deadline-seconds deadline)
38 ;; We're operating on a millisecond precision, so a single-float
39 ;; is enough, and is an immediate on 64bit platforms.
40 `(let* ((,deadline-seconds (coerce ,seconds 'single-float))
41 (,deadline
42 (+ (seconds-to-internal-time ,deadline-seconds)
43 (get-internal-real-time))))
44 (multiple-value-bind (*deadline* *deadline-seconds*)
45 (if ,override
46 (values ,deadline ,deadline-seconds)
47 (let ((old *deadline*))
48 (if (and old (< old ,deadline))
49 (values old *deadline-seconds*)
50 (values ,deadline ,deadline-seconds))))
51 ,@body))))
53 (declaim (inline decode-internal-time))
54 (defun decode-internal-time (time)
55 #!+sb-doc
56 "Returns internal time value TIME decoded into seconds and microseconds."
57 (multiple-value-bind (sec frac)
58 (truncate time sb!xc:internal-time-units-per-second)
59 (values sec (* frac sb!unix::micro-seconds-per-internal-time-unit))))
61 (defun signal-timeout (datum &rest arguments)
62 #!+sb-doc
63 "Signals a timeout condition while inhibiting further timeouts due to
64 deadlines while the condition is being handled."
65 ;; FIXME: Maybe we should make ERROR do WITH-INTERRUPTS instead of
66 ;; putting it all over the place (now that we have ALLOW-WITH-INTERRUPTS.)
67 (with-interrupts
68 ;; Don't signal a deadline while handling a non-deadline timeout.
69 (let ((*deadline* nil))
70 (apply #'error datum arguments))))
72 (defun signal-deadline ()
73 #!+sb-doc
74 "Signal a DEADLINE-TIMEOUT condition. Implementors of blocking functions
75 are responsible for calling this when a deadline is reached."
76 ;; Make sure we don't signal the same deadline twice. LET is not good
77 ;; enough: we might catch the same deadline again while unwinding.
78 (when *deadline*
79 (setf *deadline* nil))
80 (signal-timeout 'deadline-timeout :seconds *deadline-seconds*))
82 ;;; Returns TIMEOUT-SEC, TIMEOUT-USEC, DEADLINE-SEC, DEADLINE-USEC, SIGNALP
83 ;;;
84 ;;; Takes *DEADLINE* into account: if it occurs before given SECONDS,
85 ;;; the values are based on it, and DEADLINEP is true -- and the
86 ;;; receipent of the values should call SIGNAL-TIMEOUT if the decoded
87 ;;; timeout is reached.
88 ;;;
89 ;;; If SECONDS is NIL and there is no *DEADLINE* all returned values
90 ;;; are NIL.
91 (defun decode-timeout (seconds)
92 #!+sb-doc
93 "Decodes a relative timeout in SECONDS into five values, taking any
94 global deadlines into account: TO-SEC, TO-USEC, STOP-SEC, STOP-USEC,
95 DEADLINEP.
97 TO-SEC and TO-USEC indicate the relative timeout in seconds and microsconds.
98 STOP-SEC and STOP-USEC indicate the absolute timeout in seconds and
99 microseconds. DEADLINEP is true if the returned values reflect a global
100 deadline instead of the local timeout indicated by SECONDS.
102 If SECONDS is null and there is no global timeout all returned values will be
103 null. If a global deadline has already passed when DECODE-TIMEOUT is called,
104 it will signal a timeout condition."
105 (let* ((timeout (when seconds (seconds-to-internal-time seconds)))
106 (now (get-internal-real-time))
107 (deadline *deadline*)
108 (deadline-timeout
109 (when deadline
110 (let ((time-left (- deadline now)))
111 (if (plusp time-left)
112 time-left
113 (signal-deadline))))))
114 (multiple-value-bind (final-timeout final-deadline signalp)
115 ;; Use either *DEADLINE* or TIMEOUT to produce both a timeout
116 ;; and deadline in internal-time units
117 (cond ((and deadline timeout)
118 (if (< timeout deadline-timeout)
119 (values timeout (+ timeout now) nil)
120 (values deadline-timeout deadline t)))
121 (deadline
122 (values deadline-timeout deadline t))
123 (timeout
124 (values timeout (+ timeout now) nil))
126 (values nil nil nil)))
127 (if final-timeout
128 (multiple-value-bind (to-sec to-usec)
129 (decode-internal-time final-timeout)
130 (multiple-value-bind (stop-sec stop-usec)
131 (decode-internal-time final-deadline)
132 (values to-sec to-usec stop-sec stop-usec signalp)))
133 (values nil nil nil nil nil)))))