1.0.5.9: experimental semi-synchronous deadlines
[sbcl.git] / src / code / deadline.lisp
1 ;;;; global deadlines for blocking functions: a threadsafe alternative
2 ;;;; to asynch timeouts
3
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.
12
13 (in-package "SB!IMPL")
14
15 ;;; Current deadline as internal time units or NIL.
16 (defvar *deadline* nil)
17 (declaim (type (or unsigned-byte null) *deadline*))
18
19 ;;; The relative number of seconds the current deadline corresponds
20 ;;; to. Used for continuing from TIMEOUT conditions.
21 (defvar *deadline-seconds* nil)
22
23 (declaim (inline seconds-to-internal-time))
24 (defun seconds-to-internal-time (seconds)
25   (truncate (* seconds sb!xc:internal-time-units-per-second)))
26
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.
32
33 Currently only blocking IO operations, GET-MUTEX, and CONDITION-WAIT respect
34 deadlines, but this includes their implicit uses inside SBCL itself.
35
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))))
52
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))))
60
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   (let ((*deadline* nil))
66     (apply #'error datum arguments)))
67
68 (defun signal-deadline ()
69   #!+sb-doc
70   "Signal a DEADLINE-TIMEOUT condition. Implementors of blocking functions
71 are responsible for calling this when a deadline is reached."
72   (signal-timeout 'deadline-timeout :seconds *deadline-seconds*))
73
74 ;;; Returns TIMEOUT-SEC, TIMEOUT-USEC, DEADLINE-SEC, DEADLINE-USEC, SIGNALP
75 ;;;
76 ;;; Takes *DEADLINE* into account: if it occurs before given SECONDS,
77 ;;; the values are based on it, and DEADLINEP is true -- and the
78 ;;; receipent of the values should call SIGNAL-TIMEOUT if the decoded
79 ;;; timeout is reached.
80 ;;;
81 ;;; If SECONDS is NIL and there is no *DEADLINE* all returned values
82 ;;; are NIL.
83 (defun decode-timeout (seconds)
84   #!+sb-doc
85   "Decodes a relative timeout in SECONDS into five values, taking any
86 global deadlines into account: TO-SEC, TO-USEC, STOP-SEC, STOP-USEC,
87 DEADLINEP.
88
89 TO-SEC and TO-USEC indicate the relative timeout in seconds and microsconds.
90 STOP-SEC and STOP-USEC indicate the absolute timeout in seconds and
91 microseconds. DEADLINEP is true if the returned values reflect a global
92 deadline instead of the local timeout indicated by SECONDS.
93
94 If SECONDS is null and there is no global timeout all returned values will be
95 null. If a global deadline has already passed when DECODE-TIMEOUT is called,
96 it will signal a timeout condition."
97   (let* ((timeout (when seconds (seconds-to-internal-time seconds)))
98          (now (get-internal-real-time))
99          (deadline *deadline*)
100          (deadline-timeout
101           (when deadline
102             (let ((time-left (- deadline now)))
103               (if (plusp time-left)
104                   time-left
105                   (signal-deadline))))))
106     (multiple-value-bind (final-timeout final-deadline signalp)
107         ;; Use either *DEADLINE* or TIMEOUT to produce both a timeout
108         ;; and deadline in internal-time units
109         (cond ((and deadline timeout)
110                (if (< timeout deadline-timeout)
111                    (values timeout (+ timeout now) nil)
112                    (values deadline-timeout deadline t)))
113               (deadline
114                (values deadline-timeout deadline t))
115               (timeout
116                (values timeout (+ timeout now) nil))
117               (t
118                (values nil nil nil)))
119       (if final-timeout
120           (multiple-value-bind (to-sec to-usec)
121               (decode-internal-time final-timeout)
122             (multiple-value-bind (stop-sec stop-usec)
123                 (decode-internal-time final-deadline)
124               (values to-sec to-usec stop-sec stop-usec signalp)))
125           (values nil nil nil nil nil)))))