Using CFFI's FOREIGN-POINTER type.
[iolib.git] / io-multiplex / kqueue.lisp
blob1226b8f49ae959817798e2826c7136718b0f5bf1
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 +kqueue-priority+ 1)
26 (define-multiplexer kqueue-multiplexer +kqueue-priority+ (multiplexer)
27 ())
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))
43 (et:kevent kqueue-fd
44 kev 1
45 (null-pointer) 0
46 (null-pointer)))))
48 (defun calc-kqueue-monitor-filter (fd-entry)
49 (if (queue-empty-p (fd-entry-read-events fd-entry))
50 et:evfilt-write
51 et:evfilt-read))
53 (defmethod monitor-fd ((mux kqueue-multiplexer) fd-entry)
54 (assert fd-entry)
55 (handler-case
56 (do-kqueue-event-request (fd-of mux) fd-entry
57 (calc-kqueue-monitor-filter fd-entry)
58 et:ev-add)
59 (et:ebadf ()
60 (warn "FD ~A is invalid, cannot monitor it." (fd-entry-fd fd-entry)))))
62 (defun calc-kqueue-update-filter-and-flags (edge-change)
63 (case 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)
70 (assert fd-entry)
71 (handler-case
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))
75 (et:ebadf ()
76 (warn "FD ~A is invalid, cannot update its status." (fd-entry-fd fd-entry)))
77 (et:enoent ()
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))
82 et:evfilt-read
83 et:evfilt-write))
85 (defmethod unmonitor-fd ((mux kqueue-multiplexer) fd-entry)
86 (handler-case
87 (do-kqueue-event-request (fd-of mux) fd-entry
88 (calc-kqueue-unmonitor-filter fd-entry)
89 et:ev-delete)
90 (et:ebadf ()
91 (warn "FD ~A is invalid, cannot unmonitor it." (fd-entry-fd fd-entry)))
92 (et:enoent ()
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*)
97 (ts 'et:timespec))
98 (et:memset events 0 (* *kqueue-max-events* et:size-of-kevent))
99 (let (ready-fds)
100 (et:repeat-upon-condition-decreasing-timeout ((et:eintr)
101 tmp-timeout timeout)
102 (when tmp-timeout
103 (timeout->timespec tmp-timeout ts))
104 (setf ready-fds
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)))
111 (loop
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))
123 (let ((event ()))
124 (case filter
125 (#.et:evfilt-write (push :write event))
126 (#.et:evfilt-read (push :read event)))
127 (flags-case flags
128 ;; TODO: check what exactly EV_EOF means
129 ;; (et:ev-eof (pushnew :read event))
130 (et:ev-error (push :error event)))
131 (when event
132 (list fd event))))