diff --git a/cffi-epoll.asd b/cffi-epoll.asd
deleted file mode 100644
index c3bfeba..0000000
--- a/cffi-epoll.asd
+++ /dev/null
@@ -1,36 +0,0 @@
-;;
-;; cffi-epoll - Common Lisp wrapper for Linux epoll syscall
-;;
-;; Copyright 2017 Thomas de Grivel <thoxdg@gmail.com>
-;;
-;; Permission to use, copy, modify, and distribute this software for any
-;; purpose with or without fee is hereby granted, provided that the above
-;; copyright notice and this permission notice appear in all copies.
-;;
-;; THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
-;; WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
-;; MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
-;; ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
-;; WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
-;; ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
-;; OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-;;
-
-(in-package :common-lisp-user)
-
-(defpackage :cffi-epoll.system
- (:use :common-lisp :asdf))
-
-(in-package :cffi-epoll.system)
-
-(defsystem :cffi-epoll
- :name "cffi-epoll"
- :author "Thomas de Grivel <thoxdg@gmail.com>"
- :version "0.1"
- :description "Common Lisp wrapper for Linux epoll syscall"
- :defsystem-depends-on ("cffi-grovel")
- :depends-on ("cffi" "cffi-errno" "cffi-unistd")
- :components
- ((:file "package")
- (:cffi-grovel-file "grovel-epoll" :depends-on ("package"))
- (:file "cffi-epoll" :depends-on ("grovel-epoll"))))
diff --git a/cffi-epoll.lisp b/cffi-epoll.lisp
deleted file mode 100644
index 20137ca..0000000
--- a/cffi-epoll.lisp
+++ /dev/null
@@ -1,108 +0,0 @@
-;;
-;; cffi-epoll - Common Lisp wrapper for Linux epoll syscall
-;;
-;; Copyright 2017 Thomas de Grivel <thoxdg@gmail.com>
-;;
-;; Permission to use, copy, modify, and distribute this software for any
-;; purpose with or without fee is hereby granted, provided that the above
-;; copyright notice and this permission notice appear in all copies.
-;;
-;; THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
-;; WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
-;; MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
-;; ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
-;; WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
-;; ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
-;; OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-;;
-
-(in-package :cffi-epoll)
-
-(defcfun ("epoll_create" c-epoll-create) :int
- (size :int))
-
-(defun create (&optional (size 10))
- (let ((fd (c-epoll-create size)))
- (when (< fd 0)
- (error-errno "epoll_create"))
- fd))
-
-(defmacro with ((fdvar &optional (size 10)) &body body)
- (let ((fd (gensym "FD-")))
- `(let ((,fd (create ,size)))
- (unwind-protect (let ((,fdvar ,fd)) ,@body)
- (unistd:close ,fd)))))
-
-(defcfun ("epoll_ctl" c-epoll-ctl) :int
- (epfd :int)
- (op :int)
- (fd :int)
- (event (:pointer (:struct epoll-event))))
-
-(defun ctl (epfd op fd events &key data-ptr data-fd data-u32 data-u64)
- (with-foreign-object (evt '(:struct epoll-event))
- (setf (foreign-slot-value evt '(:struct epoll-event) 'events) events)
- (let ((data (foreign-slot-value evt '(:struct epoll-event) 'data)))
- (with-foreign-slots ((ptr fd u32 u64) data (:union epoll-data))
- (cond (data-ptr (setf ptr data-ptr))
- (data-fd (setf fd data-fd))
- (data-u32 (setf u32 data-u32))
- (data-u64 (setf u64 data-u64)))))
- (let ((r (c-epoll-ctl epfd op fd evt)))
- (when (< r 0)
- (error-errno "epoll_ctl"))
- r)))
-
-(defun add (epfd fd events &key data-ptr data-fd data-u32 data-u64)
- (ctl epfd +ctl-add+ fd events
- :data-ptr data-ptr
- :data-fd data-fd
- :data-u32 data-u32
- :data-u64 data-u64))
-
-(defun mod (epfd fd events &key data-ptr data-fd data-u32 data-u64)
- (ctl epfd +ctl-mod+ fd events
- :data-ptr data-ptr
- :data-fd data-fd
- :data-u32 data-u32
- :data-u64 data-u64))
-
-(defun del (epfd fd)
- (ctl epfd +ctl-del+ fd 0))
-
-(defcfun ("epoll_wait" c-epoll-wait) :int
- (epfd :int)
- (events (:pointer (:struct epoll-event)))
- (maxevents :int)
- (timeout :int))
-
-(defmacro wait ((events-var fd-var epfd &optional
- (max-events 1024)
- (timeout -1))
- &body body)
- (let ((events (gensym "EVENTS-"))
- (evt (gensym "EVT-"))
- (e-data (gensym "E-DATA-"))
- (n (gensym "N-"))
- (i (gensym "I-"))
- (g-max-events (gensym "MAX-EVENTS-")))
- `(let ((,g-max-events ,max-events))
- (with-foreign-object (,events '(:struct epoll-event) ,g-max-events)
- (loop
- (let ((,n (c-epoll-wait ,epfd ,events ,g-max-events ,timeout)))
- (when (< ,n 0)
- (handler-case
- (error-errno "epoll_wait")
- (errno-error (condition)
- (when (= +eintr+ (errno-error-errno condition))
- (continue)))))
- (dotimes (,i ,n)
- (let* ((,evt (mem-aptr ,events '(:struct epoll-event) ,i))
- (,events-var (foreign-slot-value
- ,evt '(:struct epoll-event) 'events))
- (,e-data (foreign-slot-value
- ,evt '(:struct epoll-event) 'data))
- (,fd-var (foreign-slot-value
- ,e-data '(:union epoll-data) 'fd)))
- ,@body))
- (return)))))))
diff --git a/cffi-kqueue.asd b/cffi-kqueue.asd
new file mode 100644
index 0000000..4f1ea4b
--- /dev/null
+++ b/cffi-kqueue.asd
@@ -0,0 +1,36 @@
+;;
+;; cffi-kqueue - Common Lisp wrapper for BSD kqueue syscall
+;;
+;; Copyright 2017,2018 Thomas de Grivel <thoxdg@gmail.com>
+;;
+;; Permission to use, copy, modify, and distribute this software for any
+;; purpose with or without fee is hereby granted, provided that the above
+;; copyright notice and this permission notice appear in all copies.
+;;
+;; THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+;; WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+;; MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+;; ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+;; WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+;; ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+;; OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+;;
+
+(in-package :common-lisp-user)
+
+(defpackage :cffi-kqueue.system
+ (:use :common-lisp :asdf))
+
+(in-package :cffi-kqueue.system)
+
+(defsystem :cffi-kqueue
+ :name "cffi-kqueue"
+ :author "Thomas de Grivel <thoxdg@gmail.com>"
+ :version "0.1"
+ :description "Common Lisp wrapper for BSD kqueue syscall"
+ :defsystem-depends-on ("cffi-grovel")
+ :depends-on ("cffi" "cffi-errno" "cffi-unistd")
+ :components
+ ((:file "package")
+ (:cffi-grovel-file "grovel-kqueue" :depends-on ("package"))
+ (:file "cffi-kqueue" :depends-on ("grovel-kqueue"))))
diff --git a/cffi-kqueue.lisp b/cffi-kqueue.lisp
new file mode 100644
index 0000000..c145b14
--- /dev/null
+++ b/cffi-kqueue.lisp
@@ -0,0 +1,69 @@
+;;
+;; cffi-kqueue - Common Lisp wrapper for BSD kqueue syscall
+;;
+;; Copyright 2017,2018 Thomas de Grivel <thoxdg@gmail.com>
+;;
+;; Permission to use, copy, modify, and distribute this software for any
+;; purpose with or without fee is hereby granted, provided that the above
+;; copyright notice and this permission notice appear in all copies.
+;;
+;; THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+;; WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+;; MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+;; ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+;; WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+;; ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+;; OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+;;
+
+(in-package :cffi-kqueue)
+
+(defcfun ("kqueue" c-kqueue) :int)
+
+(defun kqueue ()
+ (let ((fd (c-kqueue)))
+ (when (< fd 0)
+ (error-errno "kqueue"))
+ fd))
+
+(defcfun ("kevent" c-kevent) :int
+ (kq :int)
+ (changelist (:pointer (:struct kevent)))
+ (nchanges :int)
+ (eventlist (:pointer (:struct kevent)))
+ (nevents :int)
+ (timeout (:pointer (:struct timespec))))
+
+(defun kevent-ident (kev)
+ (foreign-slot-value kev '(:struct kevent) 'ident))
+
+(defun kevent-filter (kev)
+ (foreign-slot-value kev '(:struct kevent) 'filter))
+
+(defun kevent-flags (kev)
+ (foreign-slot-value kev '(:struct kevent) 'flags))
+
+(defun kevent-fflags (kev)
+ (foreign-slot-value kev '(:struct kevent) 'fflags))
+
+(defun kevent-data (kev)
+ (foreign-slot-value kev '(:struct kevent) 'data))
+
+(defun kevent-udata (kev)
+ (foreign-slot-value kev '(:struct kevent) 'udata))
+
+(defun kevent (kq &key changes n-changes
+ on-event (max-events 10)
+ (timeout 0))
+ (with-foreign-objects ((timespec '(:struct timespec))
+ (events '(:struct kevent) max-events))
+ (setf (foreign-slot-value timespec '(:struct timespec) 'tv-sec)
+ (floor timeout)
+ (foreign-slot-value timespec '(:struct timespec) 'tv-nsec)
+ (floor (- timeout (floor timeout)) 1/1000000000))
+ (let ((n (c-kevent kq changes n-changes events max-events timespec)))
+ (when (< n 0)
+ (error-errno "kevent"))
+ (when on-event
+ (dotimes (i n)
+ (funcall on-event (mem-aref events '(:struct kevent) i)))))))
diff --git a/grovel-epoll.lisp b/grovel-epoll.lisp
deleted file mode 100644
index 21e0bb9..0000000
--- a/grovel-epoll.lisp
+++ /dev/null
@@ -1,47 +0,0 @@
-;;
-;; cffi-epoll - Common Lisp wrapper for Linux epoll syscall
-;;
-;; Copyright 2017 Thomas de Grivel <thoxdg@gmail.com>
-;;
-;; Permission to use, copy, modify, and distribute this software for any
-;; purpose with or without fee is hereby granted, provided that the above
-;; copyright notice and this permission notice appear in all copies.
-;;
-;; THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
-;; WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
-;; MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
-;; ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
-;; WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
-;; ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
-;; OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-;;
-
-(in-package :cffi-epoll)
-
-(include "sys/epoll.h")
-
-(ctype uint32-t "uint32_t")
-(ctype uint64-t "uint64_t")
-
-(constant (+ctl-add+ "EPOLL_CTL_ADD"))
-(constant (+ctl-mod+ "EPOLL_CTL_MOD"))
-(constant (+ctl-del+ "EPOLL_CTL_DEL"))
-
-(constant (+in+ "EPOLLIN"))
-(constant (+out+ "EPOLLOUT"))
-(constant (+rdhup+ "EPOLLRDHUP"))
-(constant (+pri+ "EPOLLPRI"))
-(constant (+err+ "EPOLLERR"))
-(constant (+hup+ "EPOLLHUP"))
-(constant (+et+ "EPOLLET"))
-(constant (+exclusive+ "EPOLLEXCLUSIVE"))
-
-(cunion epoll-data "union epoll_data"
- (ptr "ptr" :type :pointer)
- (fd "fd" :type :int)
- (u32 "u32" :type uint32-t)
- (u64 "u64" :type uint64-t))
-
-(cstruct epoll-event "struct epoll_event"
- (events "events" :type uint32-t)
- (data "data" :type (:union epoll-data)))
diff --git a/grovel-kqueue.lisp b/grovel-kqueue.lisp
new file mode 100644
index 0000000..4d92464
--- /dev/null
+++ b/grovel-kqueue.lisp
@@ -0,0 +1,78 @@
+;;
+;; cffi-kqueue - Common Lisp wrapper for BSD kqueue syscall
+;;
+;; Copyright 2017,2018 Thomas de Grivel <thoxdg@gmail.com>
+;;
+;; Permission to use, copy, modify, and distribute this software for any
+;; purpose with or without fee is hereby granted, provided that the above
+;; copyright notice and this permission notice appear in all copies.
+;;
+;; THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+;; WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+;; MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+;; ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+;; WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+;; ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+;; OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+;;
+
+(in-package :cffi-kqueue)
+
+(include "sys/types.h")
+(include "sys/event.h")
+(include "sys/time.h")
+
+(constant (+ev-add+ "EV_ADD"))
+(constant (+ev-enable+ "EV_ENABLE"))
+(constant (+ev-disable+ "EV_DISABLE"))
+(constant (+ev-dispatch+ "EV_DISPATCH"))
+(constant (+ev-delete+ "EV_DELETE"))
+(constant (+ev-receipt+ "EV_RECEIPT"))
+(constant (+ev-oneshot+ "EV_ONESHOT"))
+(constant (+ev-clear+ "EV_CLEAR"))
+(constant (+ev-eof+ "EV_EOF"))
+(constant (+ev-error+ "EV_ERROR"))
+
+(constant (+evfilt-read+ "EVFILT_READ"))
+(constant (+evfilt-write+ "EVFILT_WRITE"))
+(constant (+evfilt-vnode+ "EVFILT_VNODE"))
+
+(constant (+note-delete+ "NOTE_DELETE"))
+(constant (+note-write+ "NOTE_WRITE"))
+(constant (+note-extend+ "NOTE_EXTEND"))
+(constant (+note-truncate+ "NOTE_TRUNCATE"))
+(constant (+note-attrib+ "NOTE_ATTRIB"))
+(constant (+note-link+ "NOTE_LINK"))
+(constant (+note-rename+ "NOTE_RENAME"))
+(constant (+note-revoke+ "NOTE_REVOKE"))
+
+(constant (+evfilt-proc+ "EVFILT_PROC"))
+
+(constant (+note-exit+ "NOTE_EXIT"))
+(constant (+note-fork+ "NOTE_FORK"))
+(constant (+note-exec+ "NOTE_EXEC"))
+(constant (+note-track+ "NOTE_TRACK"))
+(constant (+note-trackerr+ "NOTE_TRACKERR"))
+
+(constant (+evfilt-signal+ "EVFILT_SIGNAL"))
+(constant (+evfilt-timer+ "EVFILT_TIMER"))
+(constant (+evfilt-device+ "EVFILT_DEVICE"))
+
+(ctype uintptr-t "uintptr_t")
+(ctype u-short "u_short")
+(ctype u-int "u_int")
+(ctype int64-t "int64_t")
+
+(cstruct kevent "struct kevent"
+ (ident "ident" :type uintptr-t)
+ (filter "filter" :type :short)
+ (flags "flags" :type u-short)
+ (fflags "fflags" :type u-int)
+ (data "data" :type int64-t)
+ (udata "udata" :type :pointer))
+
+(ctype time-t "time_t")
+
+(cstruct timespec "struct timespec"
+ (tv-sec "tv_sec" :type time-t)
+ (tv-nsec "tv_nsec" :type :long))
diff --git a/package.lisp b/package.lisp
index 75d82c0..d9205d2 100644
--- a/package.lisp
+++ b/package.lisp
@@ -1,7 +1,7 @@
;;
-;; cffi-epoll - Common Lisp wrapper for Linux epoll syscall
+;; cffi-kqueue - Common Lisp wrapper for BSD kqueue syscall
;;
-;; Copyright 2017 Thomas de Grivel <thoxdg@gmail.com>
+;; Copyright 2017,2018 Thomas de Grivel <thoxdg@gmail.com>
;;
;; Permission to use, copy, modify, and distribute this software for any
;; purpose with or without fee is hereby granted, provided that the above
@@ -18,8 +18,8 @@
(in-package :common-lisp)
-(defpackage :cffi-epoll
- (:nicknames :epoll)
+(defpackage :cffi-kqueue
+ (:nicknames :kqueue)
(:use
:cffi
:common-lisp
@@ -27,21 +27,38 @@
(:shadow
#:mod)
(:export
- #:create
- #:with
- #:ctl
- #:add
- #:mod
- #:del
- #:wait
- #:+ctl-add+
- #:+ctl-mod+
- #:+ctl-del+
- #:+in+
- #:+out+
- #:+rdhup+
- #:+pri+
- #:+err+
- #:+hup+
- #:+et+
- #:+exclusive+))
+ #:+ev-add+
+ #:+ev-enable+
+ #:+ev-disable+
+ #:+ev-dispatch+
+ #:+ev-delete+
+ #:+ev-receipt+
+ #:+ev-oneshot+
+ #:+ev-clear+
+ #:+ev-eof+
+ #:+ev-error+
+ #:+evfilt-read+
+ #:+evfilt-write+
+ #:+evfilt-vnode+
+ #:+note-delete+
+ #:+note-write+
+ #:+note-extend+
+ #:+note-truncate+
+ #:+note-attrib+
+ #:+note-link+
+ #:+note-rename+
+ #:+note-revoke+
+ #:+evfilt-proc+
+ #:+note-exit+
+ #:+note-fork+
+ #:+note-exec+
+ #:+note-track+
+ #:+note-trackerr+
+ #:+evfilt-signal+
+ #:+evfilt-timer+
+ #:+evfilt-device+
+ #:c-kevent
+ #:c-kqueue
+ #:kevent
+ #:kqueue
+ #:ev-set))