| 1 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 2 |
;; |
| 3 |
;; This file provides a port of the SBCL/CMUCL 'serve-event' extension |
| 4 |
;; to ECL. serve-event provides a lispy abstraction of unix select(2) |
| 5 |
;; non-blocking IO (and potentially other variants such as epoll). It |
| 6 |
;; works with Unix-level file-descriptors, which can be retrieved from |
| 7 |
;; the sockets module using the socket-file-descriptor slot. |
| 8 |
;; |
| 9 |
;; As this file is based on SBCL's serve-event module it is being |
| 10 |
;; released under the same (non) license as SBCL (i.e. public-domain). |
| 11 |
;; |
| 12 |
;; The original port was made by Steve Smith ([email protected]) |
| 13 |
;; |
| 14 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 15 |
;; Test Example |
| 16 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 17 |
;; |
| 18 |
;; (defun test-stdin (&aux exit) |
| 19 |
;; (format t "DOING STDIN. Type Q to exit.~%") |
| 20 |
;; (serve-event:with-fd-handler |
| 21 |
;; (0 :input #'(lambda (fd) |
| 22 |
;; (declare (ignore fd)) |
| 23 |
;; (let ((ch (read-char))) |
| 24 |
;; (format t "Got data ~s~%" ch) |
| 25 |
;; (when (char= ch #\Q) |
| 26 |
;; (setf exit t))))) |
| 27 |
;; (loop until exit |
| 28 |
;; do (format t "Entering serve-all-events...~%") |
| 29 |
;; (force-output) |
| 30 |
;; (serve-event:serve-all-events 5) |
| 31 |
;; (format t "Events served~%")))) |
| 32 |
;; |
| 33 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 34 |
;; |
| 35 |
;; A more advanced example using sockets is available here: |
| 36 |
;; |
| 37 |
;; https://web.archive.org/web/20161203154152/http://haltcondition.net/?p=2232 |
| 38 |
;; |
| 39 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 40 |
(defpackage #:serve-event |
| 41 |
(:use #:cl #:ffi) |
| 42 |
(:export #:with-fd-handler #:add-fd-handler #:remove-fd-handler |
| 43 |
#:serve-event #:serve-all-events)) |
| 44 |
(in-package #:serve-event) |
| 45 |
|
| 46 |
(clines |
| 47 |
"#include <errno.h>" |
| 48 |
"#include <sys/select.h>") |
| 49 |
|
| 50 |
(eval-when (:compile-toplevel :execute) |
| 51 |
(defmacro c-constant (c-name) |
| 52 |
`(c-inline () () :int ,c-name :one-liner t)) |
| 53 |
(defmacro define-c-constants (&rest args) |
| 54 |
`(let () |
| 55 |
,@(loop |
| 56 |
for (lisp-name c-name) on args by #'cddr |
| 57 |
collect `(defconstant ,lisp-name (c-constant ,c-name)))))) |
| 58 |
|
| 59 |
(define-c-constants |
| 60 |
+eintr+ "EINTR") |
| 61 |
|
| 62 |
(defstruct (handler |
| 63 |
(:constructor make-handler (descriptor direction function)) |
| 64 |
(:copier nil)) |
| 65 |
;; Reading or writing... |
| 66 |
(direction nil :type (member :input :output)) |
| 67 |
;; File descriptor this handler is tied to. |
| 68 |
;; FIXME: Should be based on FD_SETSIZE |
| 69 |
(descriptor 0) |
| 70 |
;; Function to call. |
| 71 |
(function nil :type function) |
| 72 |
;; thread in which the handler was established |
| 73 |
#+threads |
| 74 |
(thread mp:*current-process*)) |
| 75 |
|
| 76 |
(defvar *descriptor-handlers* nil |
| 77 |
"List of all the currently active handlers for file descriptors") |
| 78 |
|
| 79 |
(defun coerce-to-descriptor (stream-or-fd direction) |
| 80 |
(etypecase stream-or-fd |
| 81 |
(fixnum stream-or-fd) |
| 82 |
(file-stream (si:file-stream-fd stream-or-fd)) |
| 83 |
(two-way-stream |
| 84 |
(coerce-to-descriptor |
| 85 |
(case direction |
| 86 |
(:input (two-way-stream-input-stream stream-or-fd)) |
| 87 |
(:output (two-way-stream-output-stream stream-or-fd))) |
| 88 |
direction)) |
| 89 |
#+clos-streams |
| 90 |
(stream (gray::stream-file-descriptor stream-or-fd direction)))) |
| 91 |
|
| 92 |
|
| 93 |
;;; serve-event calls only handlers which are established in the current thread |
| 94 |
(defmacro do-handlers ((handler-symbol handler-list) &body body) |
| 95 |
`(dolist (,handler-symbol ,handler-list) |
| 96 |
#+threads |
| 97 |
(when (eq mp:*current-process* (handler-thread ,handler-symbol)) |
| 98 |
,@body) |
| 99 |
#-threads |
| 100 |
,@body)) |
| 101 |
|
| 102 |
;;; Add a new handler to *descriptor-handlers*. |
| 103 |
(defun add-fd-handler (stream-or-fd direction function) |
| 104 |
"Arrange to call FUNCTION whenever the fd designated by STREAM-OR-FD |
| 105 |
is usable. DIRECTION should be either :INPUT or :OUTPUT. The value |
| 106 |
returned should be passed to SYSTEM:REMOVE-FD-HANDLER when it is no |
| 107 |
longer needed." |
| 108 |
(unless (member direction '(:input :output)) |
| 109 |
(error 'simple-type-error |
| 110 |
:format-control "Invalid direction ~S, must be either ~ |
| 111 |
:INPUT or :OUTPUT." |
| 112 |
:format-arguments (list direction) |
| 113 |
:datum direction |
| 114 |
:expected-type '(member :input :output))) |
| 115 |
(let ((handler (make-handler (coerce-to-descriptor stream-or-fd direction) |
| 116 |
direction |
| 117 |
function))) |
| 118 |
#+threads (mp:atomic-push handler *descriptor-handlers*) |
| 119 |
#-threads (push handler *descriptor-handlers*) |
| 120 |
handler)) |
| 121 |
|
| 122 |
;;; Remove an old handler from *descriptor-handlers*. |
| 123 |
(defun remove-fd-handler (handler) |
| 124 |
"Removes HANDLER from the list of active handlers." |
| 125 |
#+threads (mp:atomic-update *descriptor-handlers* |
| 126 |
#'(lambda (all-handlers) |
| 127 |
(remove handler all-handlers))) |
| 128 |
#-threads (setf *descriptor-handlers* |
| 129 |
(delete handler *descriptor-handlers*))) |
| 130 |
|
| 131 |
;;; Add the handler to *descriptor-handlers* for the duration of BODY. |
| 132 |
(defmacro with-fd-handler ((fd direction function) &rest body) |
| 133 |
"Establish a handler with SYSTEM:ADD-FD-HANDLER for the duration of BODY. |
| 134 |
DIRECTION should be either :INPUT or :OUTPUT, FD is the file descriptor to |
| 135 |
use, and FUNCTION is the function to call whenever FD is usable." |
| 136 |
(let ((handler (gensym))) |
| 137 |
`(let (,handler) |
| 138 |
(unwind-protect |
| 139 |
(progn |
| 140 |
(setf ,handler (add-fd-handler ,fd ,direction ,function)) |
| 141 |
,@body) |
| 142 |
(when ,handler |
| 143 |
(remove-fd-handler ,handler)))))) |
| 144 |
|
| 145 |
|
| 146 |
(defmacro fd-zero(fdset) |
| 147 |
`(c-inline (,fdset) (:object) :void |
| 148 |
"FD_ZERO((fd_set*)#0->foreign.data)" |
| 149 |
:one-liner t |
| 150 |
:side-effects t)) |
| 151 |
|
| 152 |
(defmacro fd-set (fd fdset) |
| 153 |
`(c-inline (,fd ,fdset) (:int :object) :void |
| 154 |
"FD_SET(#0, (fd_set*)#1->foreign.data);" |
| 155 |
:one-liner t |
| 156 |
:side-effects t)) |
| 157 |
|
| 158 |
(defmacro fd-isset (fd fdset) |
| 159 |
`(c-inline (,fd ,fdset) (:int :object) :int |
| 160 |
"FD_ISSET(#0, (fd_set*)#1->foreign.data)" |
| 161 |
:one-liner t |
| 162 |
:side-effects t)) |
| 163 |
|
| 164 |
(defun fdset-size () |
| 165 |
(c-inline () () :int "sizeof(fd_set)" :one-liner t :side-effects nil)) |
| 166 |
|
| 167 |
|
| 168 |
(defun serve-event (&optional (seconds nil)) |
| 169 |
"Receive pending events on all FD-STREAMS and dispatch to the appropriate |
| 170 |
handler functions. If timeout is specified, server will wait the specified |
| 171 |
time (in seconds) and then return, otherwise it will wait until something |
| 172 |
happens. Server returns T if something happened and NIL otherwise. Timeout |
| 173 |
0 means polling without waiting." |
| 174 |
|
| 175 |
;; fd_set is an opaque typedef, so we can't declare it locally. |
| 176 |
;; However we can fine out its size and allocate a char array of |
| 177 |
;; the same size which can be used in its place. |
| 178 |
(let ((fsize (fdset-size))) |
| 179 |
(with-foreign-objects ((rfd `(:array :unsigned-char ,fsize)) |
| 180 |
(wfd `(:array :unsigned-char ,fsize))) |
| 181 |
(fd-zero rfd) |
| 182 |
(fd-zero wfd) |
| 183 |
|
| 184 |
(let ((maxfd 0)) |
| 185 |
;; Load the descriptors into the relevant set |
| 186 |
(do-handlers (handler *descriptor-handlers*) |
| 187 |
(let ((fd (handler-descriptor handler))) |
| 188 |
(ecase (handler-direction handler) |
| 189 |
(:input (fd-set fd rfd)) |
| 190 |
(:output (fd-set fd wfd))) |
| 191 |
(when (> fd maxfd) |
| 192 |
(setf maxfd fd)))) |
| 193 |
|
| 194 |
(multiple-value-bind (retval errno) |
| 195 |
(if (null seconds) |
| 196 |
;; No timeout |
| 197 |
(c-inline (rfd wfd (1+ maxfd)) |
| 198 |
(:object :object :int) (values :int :int) |
| 199 |
"{ @(return 0) = select(#2, (fd_set*)#0->foreign.data, |
| 200 |
(fd_set*)#1->foreign.data, |
| 201 |
NULL, NULL); |
| 202 |
@(return 1) = errno; }" |
| 203 |
:one-liner nil |
| 204 |
:side-effects t) |
| 205 |
(c-inline (rfd wfd (1+ maxfd) seconds) |
| 206 |
(:object :object :int :double) (values :int :int) |
| 207 |
"{ struct timeval tv; |
| 208 |
double seconds = #3; |
| 209 |
tv.tv_sec = seconds; |
| 210 |
tv.tv_usec = (seconds * 1e6); |
| 211 |
@(return 0) = select(#2, (fd_set*)#0->foreign.data, |
| 212 |
(fd_set*)#1->foreign.data, |
| 213 |
NULL, &tv); |
| 214 |
@(return 1) = errno; }" |
| 215 |
:one-liner nil |
| 216 |
:side-effects t)) |
| 217 |
|
| 218 |
(cond ((zerop retval) |
| 219 |
nil) |
| 220 |
((minusp retval) |
| 221 |
(if (= errno +eintr+) |
| 222 |
;; suppress EINTR |
| 223 |
nil |
| 224 |
;; otherwise error |
| 225 |
(error "Error during select"))) |
| 226 |
((plusp retval) |
| 227 |
(do-handlers (handler *descriptor-handlers*) |
| 228 |
(let ((fd (handler-descriptor handler))) |
| 229 |
(if (plusp (ecase (handler-direction handler) |
| 230 |
(:input (fd-isset fd rfd)) |
| 231 |
(:output (fd-isset fd wfd)))) |
| 232 |
(funcall (handler-function handler) |
| 233 |
(handler-descriptor handler))))) |
| 234 |
t))))))) |
| 235 |
|
| 236 |
|
| 237 |
;;; Wait for up to timeout seconds for an event to happen. Make sure all |
| 238 |
;;; pending events are processed before returning. |
| 239 |
(defun serve-all-events (&optional (timeout nil)) |
| 240 |
"SERVE-ALL-EVENTS calls SERVE-EVENT with the specified timeout. If |
| 241 |
SERVE-EVENT does something (returns T) it loops over SERVE-EVENT with a |
| 242 |
timeout of 0 until there are no more events to serve. SERVE-ALL-EVENTS returns |
| 243 |
T if SERVE-EVENT did something and NIL if not." |
| 244 |
(do ((res nil) |
| 245 |
(sval (serve-event timeout) (serve-event 0))) |
| 246 |
((null sval) res) |
| 247 |
(setq res t))) |
| 248 |
|
| 249 |
(provide '#:serve-event) |