Moved REPEAT-*-DECREASING-TIMEOUT and GETTIME into IOLIB-POSIX.
[iolib.git] / io-multiplex / select.lisp
blob6f93e41ee063bb4b1d0f574df3be17c60bc285ae
1 ;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp -*-
3 ;; Copyright (C) 2006, 2007 Stelian Ionescu
4 ;;
5 ;; This code is free software; you can redistribute it and/or
6 ;; modify it under the terms of the version 2.1 of
7 ;; the GNU Lesser General Public License as published by
8 ;; the Free Software Foundation, as clarified by the
9 ;; preamble found here:
10 ;; http://opensource.franz.com/preamble.html
12 ;; This program is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU Lesser General
18 ;; Public License along with this library; if not, write to the
19 ;; Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20 ;; Boston, MA 02110-1301, USA
22 (in-package :io.multiplex)
24 (defconstant +select-priority+ 3)
27 (define-multiplexer select-multiplexer +select-priority+ (multiplexer)
28 ((max-fd :initform 0
29 :accessor max-fd-of)
30 (read-fd-set :initform (allocate-fd-set)
31 :reader read-fd-set-of)
32 (write-fd-set :initform (allocate-fd-set)
33 :reader write-fd-set-of)
34 (except-fd-set :initform (allocate-fd-set)
35 :reader except-fd-set-of))
36 (:default-initargs :fd-limit (1- et:fd-setsize)))
39 (defun allocate-fd-set ()
40 (et:fd-zero (foreign-alloc 'et:fd-set)))
43 (defmethod print-object ((mux select-multiplexer) stream)
44 (print-unreadable-object (mux stream :type nil :identity nil)
45 (format stream "select(2) multiplexer")))
48 (defmethod close-multiplexer progn ((mux select-multiplexer))
49 (foreign-free (read-fd-set-of mux))
50 (foreign-free (write-fd-set-of mux))
51 (foreign-free (except-fd-set-of mux))
52 (mapc #'(lambda (slot)
53 (setf (slot-value mux slot) nil))
54 '(max-fd read-fd-set write-fd-set except-fd-set)))
57 (defun find-max-fd (fd-set end)
58 (loop :for i :downfrom end :to 0
59 :do (when (et:fd-isset i fd-set)
60 (return-from find-max-fd i)))
61 ;; this means no fd <= end is set
62 -1)
65 (defun recalc-fd-masks (mux fd read write)
66 (with-accessors ((rs read-fd-set-of) (ws write-fd-set-of)
67 (es except-fd-set-of) (max-fd max-fd-of)) mux
68 (if read
69 (progn
70 (et:fd-set fd rs)
71 (et:fd-set fd es))
72 (progn
73 (et:fd-clr fd rs)
74 (et:fd-clr fd es)))
75 (if write
76 (et:fd-set fd ws)
77 (et:fd-clr fd ws))
78 (setf max-fd (max (find-max-fd rs fd)
79 (find-max-fd ws fd)))
80 t))
83 (defmethod monitor-fd ((mux select-multiplexer) fd-entry)
84 (recalc-fd-masks mux (fd-entry-fd fd-entry)
85 (not (queue-empty-p (fd-entry-read-events fd-entry)))
86 (not (queue-empty-p (fd-entry-write-events fd-entry)))))
89 (defmethod update-fd ((mux select-multiplexer) fd-entry)
90 (recalc-fd-masks mux (fd-entry-fd fd-entry)
91 (not (queue-empty-p (fd-entry-read-events fd-entry)))
92 (not (queue-empty-p (fd-entry-write-events fd-entry)))))
95 (defmethod unmonitor-fd ((mux select-multiplexer) fd-entry)
96 (recalc-fd-masks mux (fd-entry-fd fd-entry) nil nil))
99 (defmethod harvest-events ((mux select-multiplexer) timeout)
100 (with-accessors ((rs read-fd-set-of) (ws write-fd-set-of)
101 (es except-fd-set-of) (max-fd max-fd-of)) mux
102 ;; if there are no fds set and timeout is NULL
103 ;; select() blocks forever
104 (when (and (minusp max-fd)
105 (null timeout))
106 (warn "Non fds to monitor and no timeout set !")
107 (return-from harvest-events nil))
109 (with-foreign-objects ((read-fds 'et:fd-set)
110 (write-fds 'et:fd-set)
111 (except-fds 'et:fd-set))
112 (et:copy-fd-set rs read-fds)
113 (et:copy-fd-set ws write-fds)
114 (et:copy-fd-set es except-fds)
116 (handler-case
117 (with-foreign-object (tv 'et:timeval)
118 (et:repeat-upon-condition-decreasing-timeout ((et:unix-error-intr)
119 tmp-timeout timeout)
120 (when tmp-timeout
121 (timeout->timeval tmp-timeout tv))
122 (et:select (1+ max-fd)
123 read-fds
124 write-fds
125 except-fds
126 (if tmp-timeout tv (null-pointer)))))
127 (et:unix-error-badf (err)
128 (declare (ignore err))
129 (return-from harvest-events
130 (harvest-select-fd-errors rs ws max-fd))))
132 (harvest-select-events max-fd read-fds write-fds except-fds))))
135 (defun harvest-select-events (max-fd read-fds write-fds except-fds)
136 (loop :for fd :upto max-fd
137 :for event := () :then ()
138 :when (or (et:fd-isset fd read-fds)
139 (et:fd-isset fd except-fds)) :do (push :read event)
140 :when (et:fd-isset fd write-fds) :do (push :write event)
141 :when event :collect (list fd event)))
144 ;; FIXME: I don't know whether on all *nix systems select()
145 ;; returns EBADF only when a given FD present in some fd-set
146 ;; is closed(as the POSIX docs say) or if some other kinds of
147 ;; errors are reported too(as the Linux manpages seem to suggest)
148 (defun fd-error-p (fd)
149 (not (et:fd-open-p fd)))
151 (defun harvest-select-fd-errors (read-fds write-fds max-fd)
152 (loop :for fd :upto max-fd
153 :when (and (or (et:fd-isset fd read-fds)
154 (et:fd-isset fd write-fds))
155 (fd-error-p fd))
156 :collect (cons fd :error)))