Fix installation of SERIES' reader macros.
[iolib.git] / io-multiplex / select.lisp
blob48f1deb6fb30fbaec3ec7c2656c1599945bbd69a
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Indent-tabs-mode: NIL -*-
2 ;;;
3 ;;; select.lisp --- select()-based multiplexer implementation.
4 ;;;
5 ;;; Copyright (C) 2006-2007, Stelian Ionescu <sionescu@common-lisp.net>
6 ;;;
7 ;;; This code is free software; you can redistribute it and/or
8 ;;; modify it under the terms of the version 2.1 of
9 ;;; the GNU Lesser General Public License as published by
10 ;;; the Free Software Foundation, as clarified by the
11 ;;; preamble found here:
12 ;;; http://opensource.franz.com/preamble.html
13 ;;;
14 ;;; This program is distributed in the hope that it will be useful,
15 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;;; GNU General Public License for more details.
18 ;;;
19 ;;; You should have received a copy of the GNU Lesser General
20 ;;; Public License along with this library; if not, write to the
21 ;;; Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22 ;;; Boston, MA 02110-1301, USA
24 (in-package :io.multiplex)
26 (defconstant +select-priority+ 3)
28 (define-multiplexer select-multiplexer +select-priority+ (multiplexer)
29 ((max-fd :initform 0
30 :accessor max-fd-of)
31 (read-fd-set :initform (allocate-fd-set)
32 :reader read-fd-set-of)
33 (write-fd-set :initform (allocate-fd-set)
34 :reader write-fd-set-of)
35 (except-fd-set :initform (allocate-fd-set)
36 :reader except-fd-set-of))
37 (:default-initargs :fd-limit (1- nix:fd-setsize)))
39 (defun allocate-fd-set ()
40 (nix:fd-zero (foreign-alloc 'nix:fd-set)))
42 (defmethod print-object ((mux select-multiplexer) stream)
43 (print-unreadable-object (mux stream :type nil :identity nil)
44 (format stream "select(2) multiplexer")))
46 (defmethod close-multiplexer progn ((mux select-multiplexer))
47 (foreign-free (read-fd-set-of mux))
48 (foreign-free (write-fd-set-of mux))
49 (foreign-free (except-fd-set-of mux))
50 (dolist (slot '(max-fd read-fd-set write-fd-set except-fd-set))
51 (setf (slot-value mux slot) nil)))
53 (defun find-max-fd (fd-set end)
54 (loop :for i :downfrom end :to 0
55 :do (when (nix:fd-isset i fd-set)
56 (return-from find-max-fd i)))
57 ;; this means no fd <= end is set
58 -1)
60 (defun recalc-fd-masks (mux fd read write)
61 (with-accessors ((rs read-fd-set-of) (ws write-fd-set-of)
62 (es except-fd-set-of) (max-fd max-fd-of)) mux
63 (cond (read
64 (nix:fd-set fd rs)
65 (nix:fd-set fd es))
67 (nix:fd-clr fd rs)
68 (nix:fd-clr fd es)))
69 (if write
70 (nix:fd-set fd ws)
71 (nix:fd-clr fd ws))
72 (setf max-fd (max (find-max-fd rs fd)
73 (find-max-fd ws fd)))
74 t))
76 (defmethod monitor-fd ((mux select-multiplexer) fd-entry)
77 (recalc-fd-masks mux (fd-entry-fd fd-entry)
78 (fd-entry-read-event fd-entry)
79 (fd-entry-write-event fd-entry)))
81 (defmethod update-fd ((mux select-multiplexer) fd-entry event-type edge-change)
82 (declare (ignore event-type edge-change))
83 (recalc-fd-masks mux (fd-entry-fd fd-entry)
84 (fd-entry-read-event fd-entry)
85 (fd-entry-write-event fd-entry)))
87 (defmethod unmonitor-fd ((mux select-multiplexer) fd-entry)
88 (recalc-fd-masks mux (fd-entry-fd fd-entry) nil nil))
90 (defmethod harvest-events ((mux select-multiplexer) timeout)
91 (with-accessors ((rs read-fd-set-of) (ws write-fd-set-of)
92 (es except-fd-set-of) (max-fd max-fd-of)) mux
93 ;; if there are no fds set and timeout is NULL
94 ;; select() blocks forever
95 (when (and (minusp max-fd)
96 (null timeout))
97 (warn "Non fds to monitor and no timeout set !")
98 (return-from harvest-events nil))
99 (with-foreign-objects ((read-fds 'nix:fd-set)
100 (write-fds 'nix:fd-set)
101 (except-fds 'nix:fd-set))
102 (nix:copy-fd-set rs read-fds)
103 (nix:copy-fd-set ws write-fds)
104 (nix:copy-fd-set es except-fds)
105 (handler-case
106 (with-foreign-object (tv 'nix::timeval)
107 (nix:repeat-upon-condition-decreasing-timeout
108 ((nix:eintr) tmp-timeout timeout)
109 (when tmp-timeout
110 (timeout->timeval tmp-timeout tv))
111 (nix:select (1+ max-fd)
112 read-fds
113 write-fds
114 except-fds
115 (if tmp-timeout tv (null-pointer)))))
116 (nix:ebadf ()
117 (return-from harvest-events
118 (harvest-select-fd-errors rs ws max-fd))))
119 (harvest-select-events max-fd read-fds write-fds except-fds))))
121 (defun harvest-select-events (max-fd read-fds write-fds except-fds)
122 (loop :for fd :upto max-fd
123 :for event := () :then ()
124 :when (or (nix:fd-isset fd read-fds)
125 (nix:fd-isset fd except-fds)) :do (push :read event)
126 :when (nix:fd-isset fd write-fds) :do (push :write event)
127 :when event :collect (list fd event)))
129 ;;; FIXME: I don't know whether on all *nix systems select()
130 ;;; returns EBADF only when a given FD present in some fd-set
131 ;;; is closed(as the POSIX docs say) or if some other kinds of
132 ;;; errors are reported too(as the Linux manpages seem to suggest)
133 (defun fd-error-p (fd)
134 (not (nix:fd-open-p fd)))
136 (defun harvest-select-fd-errors (read-fds write-fds max-fd)
137 (loop :for fd :upto max-fd
138 :when (and (or (nix:fd-isset fd read-fds)
139 (nix:fd-isset fd write-fds))
140 (fd-error-p fd))
141 :collect (cons fd :error)))