1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; indent-tabs-mode: nil -*-
3 ;;; --- select(2) multiplexer implementation.
6 (in-package :io.multiplex
)
8 (defconstant +select-priority
+ 3)
10 (define-multiplexer select-multiplexer
+select-priority
+ (multiplexer)
13 (read-fd-set :initform
(allocate-fd-set)
14 :reader read-fd-set-of
)
15 (write-fd-set :initform
(allocate-fd-set)
16 :reader write-fd-set-of
)
17 (except-fd-set :initform
(allocate-fd-set)
18 :reader except-fd-set-of
))
19 (:default-initargs
:fd-limit
(1- fd-setsize
)))
21 (defun allocate-fd-set ()
22 (fd-zero (foreign-alloc 'fd-set
)))
24 (defmethod print-object ((mux select-multiplexer
) stream
)
25 (print-unreadable-object (mux stream
:type nil
:identity nil
)
26 (format stream
"select(2) multiplexer")))
28 (defmethod close-multiplexer progn
((mux select-multiplexer
))
29 (foreign-free (read-fd-set-of mux
))
30 (foreign-free (write-fd-set-of mux
))
31 (foreign-free (except-fd-set-of mux
))
32 (dolist (slot '(max-fd read-fd-set write-fd-set except-fd-set
))
33 (setf (slot-value mux slot
) nil
)))
35 (defun find-max-fd (fd-set end
)
36 (loop :for i
:downfrom end
:to
0
37 :do
(when (fd-isset i fd-set
) (return* i
)))
38 ;; this means no fd <= end is set
41 (defun recalc-fd-masks (mux fd read write
)
42 (with-accessors ((rs read-fd-set-of
) (ws write-fd-set-of
)
43 (es except-fd-set-of
) (max-fd max-fd-of
)) mux
53 (setf max-fd
(max (find-max-fd rs fd
)
57 (defmethod monitor-fd ((mux select-multiplexer
) fd-entry
)
58 (recalc-fd-masks mux
(fd-entry-fd fd-entry
)
59 (fd-entry-read-event fd-entry
)
60 (fd-entry-write-event fd-entry
)))
62 (defmethod update-fd ((mux select-multiplexer
) fd-entry event-type edge-change
)
63 (declare (ignore event-type edge-change
))
64 (recalc-fd-masks mux
(fd-entry-fd fd-entry
)
65 (fd-entry-read-event fd-entry
)
66 (fd-entry-write-event fd-entry
)))
68 (defmethod unmonitor-fd ((mux select-multiplexer
) fd-entry
)
69 (recalc-fd-masks mux
(fd-entry-fd fd-entry
) nil nil
))
71 (defmethod harvest-events ((mux select-multiplexer
) timeout
)
72 (with-accessors ((rs read-fd-set-of
) (ws write-fd-set-of
)
73 (es except-fd-set-of
) (max-fd max-fd-of
)) mux
74 ;; if there are no fds set and timeout is NULL
75 ;; select() blocks forever
76 (when (and (minusp max-fd
)
78 (warn "Non fds to monitor and no timeout set !")
80 (with-foreign-objects ((read-fds 'fd-set
)
83 (copy-fd-set rs read-fds
)
84 (copy-fd-set ws write-fds
)
85 (copy-fd-set es except-fds
)
87 (with-foreign-object (tv 'timeval
)
88 (nix:repeat-upon-condition-decreasing-timeout
89 ((nix:eintr
) tmp-timeout timeout
)
91 (timeout->timeval tmp-timeout tv
))
96 (if tmp-timeout tv
(null-pointer)))))
98 (return* (harvest-select-fd-errors rs ws max-fd
))))
99 (harvest-select-events max-fd read-fds write-fds except-fds
))))
101 (defun harvest-select-events (max-fd read-fds write-fds except-fds
)
102 (loop :for fd
:upto max-fd
103 :for event
:= () :then
()
104 :when
(or (fd-isset fd read-fds
)
105 (fd-isset fd except-fds
)) :do
(push :read event
)
106 :when
(fd-isset fd write-fds
) :do
(push :write event
)
107 :when event
:collect
(list fd event
)))
109 ;;; FIXME: I don't know whether on all *nix systems select()
110 ;;; returns EBADF only when a given FD present in some fd-set
111 ;;; is closed(as the POSIX docs say) or if some other kinds of
112 ;;; errors are reported too(as the Linux manpages seem to suggest)
113 (defun fd-error-p (fd)
114 (not (nix:fd-open-p fd
)))
116 (defun harvest-select-fd-errors (read-fds write-fds max-fd
)
117 (loop :for fd
:upto max-fd
118 :when
(and (or (fd-isset fd read-fds
)
119 (fd-isset fd write-fds
))
121 :collect
(cons fd
:error
)))