1.0.5.9: experimental semi-synchronous deadlines
[sbcl.git] / src / code / serve-event.lisp
1 ;;;; This software is part of the SBCL system. See the README file for
2 ;;;; more information.
3 ;;;;
4 ;;;; This software is derived from the CMU CL system, which was
5 ;;;; written at Carnegie Mellon University and released into the
6 ;;;; public domain. The software is in the public domain and is
7 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
8 ;;;; files for more information.
9
10 (in-package "SB!IMPL")
11
12 ;;;; file descriptor I/O noise
13
14 (defstruct (handler
15             (:constructor make-handler (direction descriptor function))
16             (:copier nil))
17   ;; Reading or writing...
18   (direction nil :type (member :input :output))
19   ;; File descriptor this handler is tied to.
20   (descriptor 0 :type (mod #.sb!unix:fd-setsize))
21   ;; T iff this handler is running.
22   ;;
23   ;; FIXME: unused. At some point this used to be set to T
24   ;; around the call to the handler-function, but that was commented
25   ;; out with the verbose explantion "Doesn't work -- ACK".
26   active
27   ;; Function to call.
28   (function nil :type function)
29   ;; T if this descriptor is bogus.
30   bogus)
31
32 (def!method print-object ((handler handler) stream)
33   (print-unreadable-object (handler stream :type t)
34     (format stream
35             "~A on ~:[~;BOGUS ~]descriptor ~W: ~S"
36             (handler-direction handler)
37             (handler-bogus handler)
38             (handler-descriptor handler)
39             (handler-function handler))))
40
41 (defvar *descriptor-handlers* nil
42   #!+sb-doc
43   "List of all the currently active handlers for file descriptors")
44
45 (sb!xc:defmacro with-descriptor-handlers (&body forms)
46   ;; FD-STREAM functionality can add and remove descriptors on it's
47   ;; own, so getting an interrupt while modifying this and the
48   ;; starting to recursively modify it could lose...
49   `(without-interrupts ,@forms))
50
51 (defun list-all-descriptor-handlers ()
52   (with-descriptor-handlers
53     (copy-list *descriptor-handlers*)))
54
55 (defun select-descriptor-handlers (function)
56   (declare (function function))
57   (with-descriptor-handlers
58     (remove-if-not function *descriptor-handlers*)))
59
60 (defun map-descriptor-handlers (function)
61   (declare (function function))
62   (with-descriptor-handlers
63     (dolist (handler *descriptor-handlers*)
64       (funcall function handler))))
65
66 ;;; Add a new handler to *descriptor-handlers*.
67 (defun add-fd-handler (fd direction function)
68   #!+sb-doc
69   "Arange to call FUNCTION whenever FD is usable. DIRECTION should be
70   either :INPUT or :OUTPUT. The value returned should be passed to
71   SYSTEM:REMOVE-FD-HANDLER when it is no longer needed."
72   (unless (member direction '(:input :output))
73     ;; FIXME: should be TYPE-ERROR?
74     (error "Invalid direction ~S, must be either :INPUT or :OUTPUT" direction))
75   (let ((handler (make-handler direction fd function)))
76     (with-descriptor-handlers
77       (push handler *descriptor-handlers*))
78     handler))
79
80 ;;; Remove an old handler from *descriptor-handlers*.
81 (defun remove-fd-handler (handler)
82   #!+sb-doc
83   "Removes HANDLER from the list of active handlers."
84   (with-descriptor-handlers
85     (setf *descriptor-handlers*
86           (delete handler *descriptor-handlers*))))
87
88 ;;; Search *descriptor-handlers* for any reference to fd, and nuke 'em.
89 (defun invalidate-descriptor (fd)
90   #!+sb-doc
91   "Remove any handers refering to fd. This should only be used when attempting
92   to recover from a detected inconsistancy."
93   (with-descriptor-handlers
94     (setf *descriptor-handlers*
95           (delete fd *descriptor-handlers*
96                   :key #'handler-descriptor))))
97
98 ;;; Add the handler to *descriptor-handlers* for the duration of BODY.
99 (defmacro with-fd-handler ((fd direction function) &rest body)
100   #!+sb-doc
101   "Establish a handler with SYSTEM:ADD-FD-HANDLER for the duration of BODY.
102    DIRECTION should be either :INPUT or :OUTPUT, FD is the file descriptor to
103    use, and FUNCTION is the function to call whenever FD is usable."
104   (let ((handler (gensym)))
105     `(let (,handler)
106        (unwind-protect
107            (progn
108              (setf ,handler (add-fd-handler ,fd ,direction ,function))
109              ,@body)
110          (when ,handler
111            (remove-fd-handler ,handler))))))
112
113 ;;; First, get a list and mark bad file descriptors. Then signal an error
114 ;;; offering a few restarts.
115 (defun handler-descriptors-error ()
116   (let ((bogus-handlers nil))
117     (dolist (handler (list-all-descriptor-handlers))
118       (unless (or (handler-bogus handler)
119                   (sb!unix:unix-fstat (handler-descriptor handler)))
120         (setf (handler-bogus handler) t)
121         (push handler bogus-handlers)))
122     (restart-case (error "~S ~[have~;has a~:;have~] bad file descriptor~:P."
123                          bogus-handlers (length bogus-handlers))
124       (remove-them ()
125         :report "Remove bogus handlers."
126         (with-descriptor-handlers
127           (setf *descriptor-handlers*
128                 (delete-if #'handler-bogus *descriptor-handlers*))))
129       (retry-them ()
130         :report "Retry bogus handlers."
131        (dolist (handler bogus-handlers)
132          (setf (handler-bogus handler) nil)))
133       (continue ()
134         :report "Go on, leaving handlers marked as bogus.")))
135   nil)
136
137 \f
138 ;;;; SERVE-ALL-EVENTS, SERVE-EVENT, and friends
139
140 ;;; Wait until FD is usable for DIRECTION. The timeout given to serve-event is
141 ;;; recalculated each time through the loop so that WAIT-UNTIL-FD-USABLE will
142 ;;; timeout at the correct time irrespective of how many events are handled in
143 ;;; the meantime.
144 (defun wait-until-fd-usable (fd direction &optional timeout)
145   #!+sb-doc
146   "Wait until FD is usable for DIRECTION. DIRECTION should be either :INPUT or
147 :OUTPUT. TIMEOUT, if supplied, is the number of seconds to wait before giving
148 up."
149   (let (usable)
150     (multiple-value-bind (to-sec to-usec stop-sec stop-usec signalp)
151         (decode-timeout timeout)
152       (declare (type (or integer null) to-sec to-usec))
153       (with-fd-handler (fd direction (lambda (fd)
154                                        (declare (ignore fd))
155                                        (setf usable t)))
156         (loop
157            (sub-serve-event to-sec to-usec signalp)
158            (when usable
159              (return t))
160            (when to-sec
161              (multiple-value-bind (sec usec)
162                  (decode-internal-time (get-internal-real-time))
163                (setf to-sec (- stop-sec sec))
164                (cond ((> usec stop-usec)
165                       (decf to-sec)
166                       (setf to-usec (- (+ stop-usec 1000000) usec)))
167                      (t
168                       (setf to-usec (- stop-usec usec)))))
169              (when (or (minusp to-sec) (minusp to-usec))
170                (if signalp
171                    (signal-deadline)
172                    (return nil)))))))))
173 \f
174 ;;; Wait for up to timeout seconds for an event to happen. Make sure all
175 ;;; pending events are processed before returning.
176 (defun serve-all-events (&optional timeout)
177   #!+sb-doc
178   "SERVE-ALL-EVENTS calls SERVE-EVENT with the specified timeout. If
179 SERVE-EVENT does something (returns T) it loops over SERVE-EVENT with a
180 timeout of 0 until there are no more events to serve. SERVE-ALL-EVENTS returns
181 T if SERVE-EVENT did something and NIL if not."
182   (do ((res nil)
183        (sval (serve-event timeout) (serve-event 0)))
184       ((null sval) res)
185     (setq res t)))
186
187 ;;; Serve a single set of events.
188 (defun serve-event (&optional timeout)
189   #!+sb-doc
190   "Receive pending events on all FD-STREAMS and dispatch to the appropriate
191 handler functions. If timeout is specified, server will wait the specified
192 time (in seconds) and then return, otherwise it will wait until something
193 happens. Server returns T if something happened and NIL otherwise. Timeout
194 0 means polling without waiting."
195   (multiple-value-bind (to-sec to-usec stop-sec stop-usec signalp)
196       (decode-timeout timeout)
197     (declare (ignore stop-sec stop-usec))
198     (sub-serve-event to-sec to-usec signalp)))
199
200 ;;; Takes timeout broken into seconds and microseconds.
201 (defun sub-serve-event (to-sec to-usec deadlinep)
202   ;; Next, wait for something to happen.
203   (sb!alien:with-alien ((read-fds (sb!alien:struct sb!unix:fd-set))
204                         (write-fds (sb!alien:struct sb!unix:fd-set)))
205
206       (sb!unix:fd-zero read-fds)
207       (sb!unix:fd-zero write-fds)
208       (let ((count 0))
209         (declare (type index count))
210
211         ;; Initialize the fd-sets for UNIX-SELECT and return the active
212         ;; descriptor count.
213         (map-descriptor-handlers
214          (lambda (handler)
215            ;; FIXME: If HANDLER-ACTIVE ever is reinstanted, it needs
216            ;; to be checked here in addition to HANDLER-BOGUS
217            (unless (handler-bogus handler)
218              (let ((fd (handler-descriptor handler)))
219                (ecase (handler-direction handler)
220                  (:input (sb!unix:fd-set fd read-fds))
221                  (:output (sb!unix:fd-set fd write-fds)))
222                (when (> fd count)
223                  (setf count fd))))))
224         (incf count)
225
226       ;; Next, wait for something to happen.
227       (multiple-value-bind (value err)
228           (sb!unix:unix-fast-select count
229                                     (sb!alien:addr read-fds)
230                                     (sb!alien:addr write-fds)
231                                     nil to-sec to-usec)
232         #!+win32
233         (declare (ignore err))
234         ;; Now see what it was (if anything)
235         (cond ((not value)
236                ;; Interrupted or one of the file descriptors is bad.
237                ;; FIXME: Check for other errnos. Why do we return true
238                ;; when interrupted?
239                #!-win32
240                (if (eql err sb!unix:eintr)
241                    t
242                  (handler-descriptors-error))
243                #!+win32
244                (handler-descriptors-error))
245               ((plusp value)
246                ;; Got something. Call file descriptor handlers
247                ;; according to the readable and writable masks
248                ;; returned by select.
249                (dolist (handler
250                         (select-descriptor-handlers
251                          (lambda (handler)
252                            (let ((fd (handler-descriptor handler)))
253                              (ecase (handler-direction handler)
254                                (:input (sb!unix:fd-isset fd read-fds))
255                                (:output (sb!unix:fd-isset fd write-fds)))))))
256                  (funcall (handler-function handler)
257                           (handler-descriptor handler)))
258                t)
259               ((zerop value)
260                (when deadlinep
261                  (signal-deadline))
262                nil))))))