Fix wrappers.
[iolib/alendvai.git] / io.multiplex / select.lisp
blob135e68091d7cdb623bb3074659fe5a46d52e0f78
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; indent-tabs-mode: nil -*-
2 ;;;
3 ;;; --- select(2) multiplexer implementation.
4 ;;;
6 (in-package :io.multiplex)
8 (defconstant +select-priority+ 3)
10 (define-multiplexer select-multiplexer +select-priority+ (multiplexer)
11 ((max-fd :initform 0
12 :accessor max-fd-of)
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
39 -1)
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
44 (cond (read
45 (fd-set fd rs)
46 (fd-set fd es))
48 (fd-clr fd rs)
49 (fd-clr fd es)))
50 (if write
51 (fd-set fd ws)
52 (fd-clr fd ws))
53 (setf max-fd (max (find-max-fd rs fd)
54 (find-max-fd ws fd)))
55 t))
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)
77 (null timeout))
78 (warn "Non fds to monitor and no timeout set !")
79 (return* nil))
80 (with-foreign-objects ((read-fds 'fd-set)
81 (write-fds 'fd-set)
82 (except-fds 'fd-set))
83 (copy-fd-set rs read-fds)
84 (copy-fd-set ws write-fds)
85 (copy-fd-set es except-fds)
86 (handler-case
87 (with-foreign-object (tv 'timeval)
88 (nix:repeat-upon-condition-decreasing-timeout
89 ((nix:eintr) tmp-timeout timeout)
90 (when tmp-timeout
91 (timeout->timeval tmp-timeout tv))
92 (select (1+ max-fd)
93 read-fds
94 write-fds
95 except-fds
96 (if tmp-timeout tv (null-pointer)))))
97 (nix:ebadf ()
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))
120 (fd-error-p fd))
121 :collect (cons fd :error)))