1 ;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp -*-
3 ;; Copyright (C) 2006, 2007 Stelian Ionescu
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 +kqueue-priority
+ 1)
26 (define-multiplexer kqueue-multiplexer
+kqueue-priority
+ (multiplexer)
29 (defmethod print-object ((mux kqueue-multiplexer
) stream
)
30 (print-unreadable-object (mux stream
:type nil
:identity nil
)
31 (format stream
"kqueue(2) multiplexer")))
33 (defvar *kqueue-max-events
* 200)
35 (defmethod initialize-instance :after
((mux kqueue-multiplexer
) &key
)
36 (setf (slot-value mux
'fd
) (et:kqueue
)))
38 (defun do-kqueue-event-request (kqueue-fd fd-entry filter request-type
)
39 (let ((fd (fd-entry-fd fd-entry
)))
40 (with-foreign-object (kev 'et
:kevent
)
41 (et:memset kev
0 et
:size-of-kevent
)
42 (et:ev-set kev fd filter request-type
0 0 (null-pointer))
48 (defun calc-kqueue-monitor-filter (fd-entry)
49 (if (queue-empty-p (fd-entry-read-events fd-entry
))
53 (defmethod monitor-fd ((mux kqueue-multiplexer
) fd-entry
)
56 (do-kqueue-event-request (fd-of mux
) fd-entry
57 (calc-kqueue-monitor-filter fd-entry
)
60 (warn "FD ~A is invalid, cannot monitor it." (fd-entry-fd fd-entry
)))))
62 (defun calc-kqueue-update-filter-and-flags (edge-change)
64 (:read-add
(values et
:evfilt-read et
:ev-add
))
65 (:read-del
(values et
:evfilt-read et
:ev-delete
))
66 (:write-add
(values et
:evfilt-write et
:ev-add
))
67 (:write-del
(values et
:evfilt-write et
:ev-delete
))))
69 (defmethod update-fd ((mux kqueue-multiplexer
) fd-entry
)
72 (multiple-value-bind (filter change
)
73 (calc-kqueue-update-filter-and-flags (fd-entry-edge-change fd-entry
))
74 (do-kqueue-event-request (fd-of mux
) fd-entry filter change
))
76 (warn "FD ~A is invalid, cannot update its status." (fd-entry-fd fd-entry
)))
78 (warn "FD ~A was not monitored, cannot update its status." (fd-entry-fd fd-entry
)))))
80 (defun calc-kqueue-unmonitor-filter (fd-entry)
81 (if (queue-empty-p (fd-entry-read-events fd-entry
))
85 (defmethod unmonitor-fd ((mux kqueue-multiplexer
) fd-entry
)
87 (do-kqueue-event-request (fd-of mux
) fd-entry
88 (calc-kqueue-unmonitor-filter fd-entry
)
91 (warn "FD ~A is invalid, cannot unmonitor it." (fd-entry-fd fd-entry
)))
93 (warn "FD ~A was not monitored, cannot unmonitor it." (fd-entry-fd fd-entry
)))))
95 (defmethod harvest-events ((mux kqueue-multiplexer
) timeout
)
96 (with-foreign-objects ((events 'et
:kevent
*kqueue-max-events
*)
98 (et:memset events
0 (* *kqueue-max-events
* et
:size-of-kevent
))
100 (et:repeat-upon-condition-decreasing-timeout
((et:eintr
)
103 (timeout->timespec tmp-timeout ts
))
105 (et:kevent
(fd-of mux
) (null-pointer) 0
106 events
*kqueue-max-events
*
107 (if tmp-timeout ts
(null-pointer)))))
108 (macrolet ((kevent-slot (slot-name)
109 `(foreign-slot-value (mem-aref events
'et
:kevent i
)
110 'et
:kevent
',slot-name
)))
112 :for i
:below ready-fds
113 :for fd
:= (kevent-slot et
:ident
)
114 :for flags
:= (kevent-slot et
:flags
)
115 :for filter
:= (kevent-slot et
:filter
)
116 :for data
:= (kevent-slot et
:data
)
117 :for kqueue-event
:= (make-kqueue-event fd flags filter data
)
118 :when kqueue-event
:collect kqueue-event
)))))
120 ;; TODO: do something with DATA
121 (defun make-kqueue-event (fd flags filter data
)
122 (declare (ignore data
))
125 (#.et
:evfilt-write
(push :write event
))
126 (#.et
:evfilt-read
(push :read event
)))
128 ;; TODO: check what exactly EV_EOF means
129 ;; (et:ev-eof (pushnew :read event))
130 (et:ev-error
(push :error event
)))