/[cmucl]/src/code/serve-event.lisp
ViewVC logotype

Contents of /src/code/serve-event.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.24 - (show annotations)
Thu Jul 6 04:31:17 2000 UTC (13 years, 9 months ago) by dtc
Branch: MAIN
Changes since 1.23: +2 -2 lines
Clarify an assertion on the result of truncate, firstly receiving the
only the first value.
1 ;;; -*- Log: code.log; Package: LISP -*-
2
3 ;;; **********************************************************************
4 ;;; This code was written as part of the CMU Common Lisp project at
5 ;;; Carnegie Mellon University, and has been placed in the public domain.
6 ;;;
7 (ext:file-comment
8 "$Header: /tiger/var/lib/cvsroots/cmucl/src/code/serve-event.lisp,v 1.24 2000/07/06 04:31:17 dtc Exp $")
9 ;;;
10 ;;; **********************************************************************
11 ;;;
12 ;;; SYSTEM:SERVE-EVENT, now in it's own file.
13 ;;;
14 ;;; Re-written by William Lott, July 1989 - January 1990.
15 ;;;
16 ;;; **********************************************************************
17
18 (in-package "SYSTEM")
19
20 (export '(with-fd-handler add-fd-handler remove-fd-handler invalidate-descriptor
21 serve-event serve-all-events wait-until-fd-usable
22 make-object-set object-set-operation *xwindow-table*
23 map-xwindow add-xwindow-object remove-xwindow-object))
24
25 (in-package "EXTENSIONS")
26
27 (export '(*display-event-handlers*))
28
29 (in-package "LISP")
30
31
32
33 ;;;; Object set stuff.
34
35 ;;;
36 ;;; Hashtable from ports to objects. Each entry is a cons (object . set).
37 ;;;
38 ;(defvar *port-table* (make-hash-table :test #'eql))
39
40 ;;; Hashtable from windows to objects. Each entry is a cons (object . set).
41 ;;;
42 (defvar *xwindow-table* (make-hash-table :test #'eql))
43
44
45 (defstruct (object-set
46 (:constructor make-object-set
47 (name &optional
48 (default-handler #'default-default-handler)))
49 (:print-function
50 (lambda (s stream d)
51 (declare (ignore d))
52 (format stream "#<Object Set ~S>" (object-set-name s)))))
53 name ; Name, for descriptive purposes.
54 (table (make-hash-table :test #'eq)) ; Message-ID or xevent-type --> handler fun.
55 default-handler)
56
57 (setf (documentation 'make-object-set 'function)
58 "Make an object set for use by a RPC/xevent server. Name is for
59 descriptive purposes only.")
60
61 ;;; Default-Default-Handler -- Internal
62 ;;;
63 ;;; If no such operation defined, signal an error.
64 ;;;
65 (defun default-default-handler (object)
66 (error "You lose, object: ~S" object))
67
68
69 ;;; MAP-XWINDOW and MAP-PORT return as multiple values the object and
70 ;;; object set mapped to by a xwindow or port in *xwindow-table* or
71 ;;; *port-table*.
72 ;;;
73 (macrolet ((defmapper (name table)
74 `(defun ,(intern (concatenate 'simple-string
75 "MAP-" (symbol-name name)))
76 (,name)
77 ,(format nil "Return as multiple values the object and ~
78 object-set mapped to by ~A."
79 (string-downcase (symbol-name name)))
80 (let ((temp (gethash ,name ,table)))
81 (if temp
82 (values (car temp) (cdr temp))
83 (values nil nil))))))
84 ;(defmapper port *port-table*)
85 (defmapper xwindow *xwindow-table*))
86
87
88 ;;; ADD-PORT-OBJECT and ADD-XWINDOW-OBJECT store an object/object-set pair
89 ;;; mapped to by a port or xwindow in either *port-table* or *xwindow-table*.
90 ;;;
91 (macrolet ((def-add-object (name table)
92 `(defun ,(intern (concatenate 'simple-string
93 "ADD-" (symbol-name name)
94 "-OBJECT"))
95 (,name object object-set)
96 ,(format nil "Add a new ~A/object/object-set association."
97 (string-downcase (symbol-name name)))
98 (check-type object-set object-set)
99 (setf (gethash ,name ,table) (cons object object-set))
100 object)))
101 ;(def-add-object port *port-table*)
102 (def-add-object xwindow *xwindow-table*))
103
104
105 ;;; REMOVE-PORT-OBJECT and REMOVE-XWINDOW-OBJECT remove a port or xwindow and
106 ;;; its associated object/object-set pair from *port-table* or *xwindow-table*.
107 ;;;
108 (macrolet ((def-remove-object (name table)
109 `(defun ,(intern (concatenate 'simple-string
110 "REMOVE-" (symbol-name name)
111 "-OBJECT"))
112 (,name)
113 ,(format nil
114 "Remove ~A and its associated object/object-set pair."
115 (string-downcase (symbol-name name)))
116 (remhash ,name ,table))))
117 ;(def-remove-object port *port-table*)
118 (def-remove-object xwindow *xwindow-table*))
119
120
121 ;;; Object-Set-Operation -- Public
122 ;;;
123 ;;; Look up the handler function for a given message ID.
124 ;;;
125 (defun object-set-operation (object-set message-id)
126 "Return the handler function in Object-Set for the operation specified by
127 Message-ID, if none, NIL is returned."
128 (check-type object-set object-set)
129 (check-type message-id fixnum)
130 (values (gethash message-id (object-set-table object-set))))
131
132 ;;; %Set-Object-Set-Operation -- Internal
133 ;;;
134 ;;; The setf inverse for Object-Set-Operation.
135 ;;;
136 (defun %set-object-set-operation (object-set message-id new-value)
137 (check-type object-set object-set)
138 (check-type message-id fixnum)
139 (setf (gethash message-id (object-set-table object-set)) new-value))
140 ;;;
141 (defsetf object-set-operation %set-object-set-operation
142 "Sets the handler function for an object set operation.")
143
144
145
146 ;;;; File descriptor IO noise.
147
148 (defstruct (handler
149 (:print-function %print-handler)
150 (:constructor make-handler (direction descriptor function)))
151 ;; Reading or writing...
152 (direction nil :type (member :input :output))
153 ;;
154 ;; File descriptor this handler is tied to.
155 (descriptor 0 :type (mod #.unix:fd-setsize))
156
157 active ; T iff this handler is running.
158 (function nil :type function) ; Function to call.
159 bogus ; T if this descriptor is bogus.
160 )
161
162 (defun %print-handler (handler stream depth)
163 (declare (ignore depth))
164 (format stream "#<Handler for ~A on ~:[~;BOGUS ~]descriptor ~D: ~S>"
165 (handler-direction handler)
166 (handler-bogus handler)
167 (handler-descriptor handler)
168 (handler-function handler)))
169
170 (defvar *descriptor-handlers* nil
171 "List of all the currently active handlers for file descriptors")
172
173 ;;; ADD-FD-HANDLER -- public
174 ;;;
175 ;;; Add a new handler to *descriptor-handlers*.
176 ;;;
177 (defun add-fd-handler (fd direction function)
178 "Arange to call FUNCTION whenever FD is usable. DIRECTION should be
179 either :INPUT or :OUTPUT. The value returned should be passed to
180 SYSTEM:REMOVE-FD-HANDLER when it is no longer needed."
181 (assert (member direction '(:input :output))
182 (direction)
183 "Invalid direction ~S, must be either :INPUT or :OUTPUT" direction)
184 (let ((handler (make-handler direction fd function)))
185 (push handler *descriptor-handlers*)
186 handler))
187
188 ;;; REMOVE-FD-HANDLER -- public
189 ;;;
190 ;;; Remove an old handler from *descriptor-handlers*.
191 ;;;
192 (defun remove-fd-handler (handler)
193 "Removes HANDLER from the list of active handlers."
194 (setf *descriptor-handlers*
195 (delete handler *descriptor-handlers*
196 :test #'eq)))
197
198 ;;; INVALIDATE-DESCRIPTOR -- public
199 ;;;
200 ;;; Search *descriptor-handlers* for any reference to fd, and nuke 'em.
201 ;;;
202 (defun invalidate-descriptor (fd)
203 "Remove any handers refering to fd. This should only be used when attempting
204 to recover from a detected inconsistancy."
205 (setf *descriptor-handlers*
206 (delete fd *descriptor-handlers*
207 :key #'handler-descriptor)))
208
209 ;;; WITH-FD-HANDLER -- Public.
210 ;;;
211 ;;; Add the handler to *descriptor-handlers* for the duration of BODY.
212 ;;;
213 (defmacro with-fd-handler ((fd direction function) &rest body)
214 "Establish a handler with SYSTEM:ADD-FD-HANDLER for the duration of BODY.
215 DIRECTION should be either :INPUT or :OUTPUT, FD is the file descriptor to
216 use, and FUNCTION is the function to call whenever FD is usable."
217 (let ((handler (gensym)))
218 `(let (,handler)
219 (unwind-protect
220 (progn
221 (setf ,handler (add-fd-handler ,fd ,direction ,function))
222 ,@body)
223 (when ,handler
224 (remove-fd-handler ,handler))))))
225
226
227 ;;; HANDLER-DESCRIPTORS-ERROR -- Internal.
228 ;;;
229 ;;; First, get a list and mark bad file descriptors. Then signal an error
230 ;;; offering a few restarts.
231 ;;;
232 (defun handler-descriptors-error ()
233 (let ((bogus-handlers nil))
234 (dolist (handler *descriptor-handlers*)
235 (unless (or (handler-bogus handler)
236 (unix:unix-fstat (handler-descriptor handler)))
237 (setf (handler-bogus handler) t)
238 (push handler bogus-handlers)))
239 (restart-case (error "~S ~[have~;has a~:;have~] bad file descriptor~:P."
240 bogus-handlers (length bogus-handlers))
241 (remove-them () :report "Remove bogus handlers."
242 (setf *descriptor-handlers*
243 (delete-if #'handler-bogus *descriptor-handlers*)))
244 (retry-them () :report "Retry bogus handlers."
245 (dolist (handler bogus-handlers)
246 (setf (handler-bogus handler) nil)))
247 (continue () :report "Go on, leaving handlers marked as bogus."))))
248
249
250
251 ;;;; Serve-all-events, serve-event, and friends.
252
253 (declaim (start-block wait-until-fd-usable serve-event serve-all-events))
254
255 ;;; DECODE-TIMEOUT -- Internal
256 ;;;
257 ;;; Break a real timeout into seconds and microseconds.
258 ;;;
259 (defun decode-timeout (timeout)
260 (declare (values (or index null) index))
261 (typecase timeout
262 (integer (values timeout 0))
263 (null (values nil 0))
264 (real
265 (multiple-value-bind (q r)
266 (truncate (coerce timeout 'single-float))
267 (declare (type index q) (single-float r))
268 (values q (the index (values (truncate (* r 1f6)))))))
269 (t
270 (error "Timeout is not a real number or NIL: ~S" timeout))))
271
272
273 ;;; WAIT-UNTIL-FD-USABLE -- Public.
274 ;;;
275 ;;; Wait until FD is usable for DIRECTION. The timeout given to serve-event is
276 ;;; recalculated each time through the loop so that WAIT-UNTIL-FD-USABLE will
277 ;;; timeout at the correct time irrespective of how many events are handled in
278 ;;; the meantime.
279 ;;;
280 (defun wait-until-fd-usable (fd direction &optional timeout)
281 "Wait until FD is usable for DIRECTION. DIRECTION should be either :INPUT or
282 :OUTPUT. TIMEOUT, if supplied, is the number of seconds to wait before giving
283 up."
284 (declare (type (or real null) timeout))
285 (let (usable)
286 (multiple-value-bind (to-sec to-usec)
287 (decode-timeout timeout)
288 (declare (type (or index null) to-sec to-usec))
289 (multiple-value-bind
290 (stop-sec stop-usec)
291 (if to-sec
292 (multiple-value-bind (okay start-sec start-usec)
293 (unix:unix-gettimeofday)
294 (declare (ignore okay))
295 (let ((usec (+ to-usec start-usec))
296 (sec (+ to-sec start-sec)))
297 (declare (type (unsigned-byte 31) usec sec))
298 (if (>= usec 1000000)
299 (values (1+ sec) (- usec 1000000))
300 (values sec usec))))
301 (values 0 0))
302 (declare (type (unsigned-byte 31) stop-sec stop-usec))
303 (with-fd-handler (fd direction #'(lambda (fd)
304 (declare (ignore fd))
305 (setf usable t)))
306 (loop
307 (sub-serve-event to-sec to-usec)
308
309 (when usable
310 (return t))
311
312 (when timeout
313 (multiple-value-bind (okay sec usec)
314 (unix:unix-gettimeofday)
315 (declare (ignore okay))
316 (when (or (> sec stop-sec)
317 (and (= sec stop-sec) (>= usec stop-usec)))
318 (return nil))
319 (setq to-sec (- stop-sec sec))
320 (cond ((> usec stop-usec)
321 (decf to-sec)
322 (setq to-usec (- (+ stop-usec 1000000) usec)))
323 (t
324 (setq to-usec (- stop-usec usec))))))))))))
325
326
327 (defvar *display-event-handlers* nil
328 "This is an alist mapping displays to user functions to be called when
329 SYSTEM:SERVE-EVENT notices input on a display connection. Do not modify
330 this directly; use EXT:ENABLE-CLX-EVENT-HANDLING. A given display
331 should be represented here only once.")
332
333 ;;; SERVE-ALL-EVENTS -- public
334 ;;;
335 ;;; Wait for up to timeout seconds for an event to happen. Make sure all
336 ;;; pending events are processed before returning.
337 ;;;
338 (defun serve-all-events (&optional timeout)
339 "SERVE-ALL-EVENTS calls SERVE-EVENT with the specified timeout. If
340 SERVE-EVENT does something (returns T) it loops over SERVE-EVENT with timeout
341 0 until all events have been served. SERVE-ALL-EVENTS returns T if
342 SERVE-EVENT did something and NIL if not."
343 (do ((res nil)
344 (sval (serve-event timeout) (serve-event 0)))
345 ((null sval) res)
346 (setq res t)))
347
348
349 ;;; SERVE-EVENT -- public
350 ;;;
351 ;;; Serve a single event.
352 ;;;
353 (defun serve-event (&optional timeout)
354 "Receive on all ports and Xevents and dispatch to the appropriate handler
355 function. If timeout is specified, server will wait the specified time (in
356 seconds) and then return, otherwise it will wait until something happens.
357 Server returns T if something happened and NIL otherwise."
358 (multiple-value-bind (to-sec to-usec)
359 (decode-timeout timeout)
360 (sub-serve-event to-sec to-usec)))
361
362
363 ;;; Check for any X displays with pending events.
364 ;;;
365 (defun handle-queued-clx-event ()
366 (dolist (d/h *display-event-handlers*)
367 (let* ((d (car d/h))
368 (disp-fd (fd-stream-fd (xlib::display-input-stream d))))
369 (declare (inline member))
370 ;;
371 ;; If in the *descriptor-handlers*, then we are already waiting for input
372 ;; on that display, and we don't want to do it recursively.
373 (when (and (dolist (hand *descriptor-handlers* t)
374 (when (and (eql (handler-descriptor hand) disp-fd)
375 (not (eq (handler-function hand)
376 #'ext::call-display-event-handler)))
377 (return nil)))
378 (xlib::event-listen d))
379 (handler-bind ((error #'(lambda (condx)
380 (declare (ignore condx))
381 (flush-display-events d))))
382 (unless (funcall (cdr d/h) d)
383 (disable-clx-event-handling d)
384 (error "Event-listen was true, but handler didn't handle: ~%~S"
385 d/h)))
386 (return-from handle-queued-clx-event t)))))
387
388
389 ;;; These macros are chunks of code from SUB-SERVE-EVENT. They randomly
390 ;;; reference the READ-FDS and WRITE-FDS Alien variables (which wold be consed
391 ;;; if passed as function arguments.)
392 ;;;
393 (eval-when (compile eval)
394
395 ;;; CALC-MASKS -- Internal.
396 ;;;
397 ;;; Initialize the fd-sets for UNIX-SELECT and return the active descriptor
398 ;;; count.
399 ;;;
400 (defmacro calc-masks ()
401 '(progn
402 (unix:fd-zero read-fds)
403 (unix:fd-zero write-fds)
404 (let ((count 0))
405 (declare (type index count))
406 (dolist (handler *descriptor-handlers*)
407 (unless (or (handler-active handler)
408 (handler-bogus handler))
409 (let ((fd (handler-descriptor handler)))
410 (ecase (handler-direction handler)
411 (:input (unix:fd-set fd read-fds))
412 (:output (unix:fd-set fd write-fds)))
413 (when (> fd count)
414 (setf count fd)))))
415 (1+ count))))
416
417
418 ;;; Call file descriptor handlers according to the readable and writable masks
419 ;;; returned by select.
420 ;;;
421 (defmacro call-fd-handler ()
422 '(let ((result nil))
423 (dolist (handler *descriptor-handlers*)
424 (let ((desc (handler-descriptor handler)))
425 (when (ecase (handler-direction handler)
426 (:input (unix:fd-isset desc read-fds))
427 (:output (unix:fd-isset desc write-fds)))
428 (unwind-protect
429 (progn
430 ;; Doesn't work -- ACK
431 ;(setf (handler-active handler) t)
432 (funcall (handler-function handler) desc))
433 (setf (handler-active handler) nil))
434 (ecase (handler-direction handler)
435 (:input (unix:fd-clr desc read-fds))
436 (:output (unix:fd-clr desc write-fds)))
437 (setf result t)))
438 result)))
439
440 ); eval-when (compile eval)
441
442 ;;; When a *periodic-polling-function* is defined the server will not
443 ;;; block for more than the maximum event timeout and will call the
444 ;;; polling function if it does times out. One important use of this
445 ;;; is to periodically call process-yield.
446 ;;;
447 (declaim (type (or null function) *periodic-polling-function*))
448 (defvar *periodic-polling-function*
449 #-mp nil #+mp #'mp:process-yield)
450 (declaim (type (unsigned-byte 29) *max-event-to-sec* *max-event-to-usec*))
451 (defvar *max-event-to-sec* 1)
452 (defvar *max-event-to-usec* 0)
453
454 ;;; SUB-SERVE-EVENT -- Internal
455 ;;;
456 ;;; Takes timeout broken into seconds and microseconds.
457 ;;;
458 (defun sub-serve-event (to-sec to-usec)
459 (declare (type (or null (unsigned-byte 29)) to-sec to-usec))
460
461 (when (handle-queued-clx-event) (return-from sub-serve-event t))
462
463 (let ((call-polling-fn nil))
464 (when (and *periodic-polling-function*
465 ;; Enforce a maximum timeout.
466 (or (null to-sec)
467 (> to-sec *max-event-to-sec*)
468 (and (= to-sec *max-event-to-sec*)
469 (> to-usec *max-event-to-usec*))))
470 (setf to-sec *max-event-to-sec*)
471 (setf to-usec *max-event-to-usec*)
472 (setf call-polling-fn t))
473
474 ;; Next, wait for something to happen.
475 (alien:with-alien ((read-fds (alien:struct unix:fd-set))
476 (write-fds (alien:struct unix:fd-set)))
477 (let ((count (calc-masks)))
478 (multiple-value-bind
479 (value err)
480 (unix:unix-fast-select
481 count
482 (alien:addr read-fds) (alien:addr write-fds)
483 nil to-sec to-usec)
484
485 ;; Now see what it was (if anything)
486 (cond (value
487 (cond ((zerop value)
488 ;; Timed out.
489 (when call-polling-fn
490 (funcall *periodic-polling-function*)))
491 (t
492 (call-fd-handler))))
493 ((eql err unix:eintr)
494 ;; We did an interrupt.
495 t)
496 (t
497 ;; One of the file descriptors is bad.
498 (handler-descriptors-error)
499 nil)))))))
500

  ViewVC Help
Powered by ViewVC 1.1.5