Better %SYS-GETTID.
[iolib.git] / io.multiplex / multiplexer.lisp
blob4c0a5433808fbedea2cf31bc1c62d6fef4cce414
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; indent-tabs-mode: nil -*-
2 ;;;
3 ;;; --- Base class for multiplexers.
4 ;;;
6 (in-package :io.multiplex)
8 (defvar *available-multiplexers* nil
9 "An alist of (PRIORITY . MULTIPLEXER). Smaller values mean higher priority.")
11 (defvar *default-multiplexer* nil
12 "The default multiplexer for the current OS.")
14 (defun get-fd-limit ()
15 "Return the maximum number of FDs available for the current process."
16 (let ((fd-limit (isys:%sys-getrlimit isys:rlimit-nofile)))
17 (unless (eql fd-limit isys:rlim-infinity)
18 (1- fd-limit))))
20 (defclass multiplexer ()
21 ((fd :reader fd-of)
22 (fd-limit :initform (get-fd-limit)
23 :initarg :fd-limit
24 :reader fd-limit-of)
25 (closedp :accessor multiplexer-closedp
26 :initform nil))
27 (:documentation "Base class for I/O multiplexers."))
29 (defgeneric close-multiplexer (mux)
30 (:method-combination progn :most-specific-last)
31 (:documentation "Close multiplexer MUX, calling close() on the multiplexer's FD if bound."))
33 (defgeneric monitor-fd (mux fd-entry)
34 (:documentation "Add the descriptor reppresented by FD-ENTRY to multiplexer MUX.
35 Must return NIL on failure, T otherwise."))
37 (defgeneric update-fd (mux fd-entry event-type edge-change)
38 (:documentation "Update the status of the descriptor reppresented by FD-ENTRY in multiplexer MUX.
39 Must return NIL on failure, T otherwise."))
41 (defgeneric unmonitor-fd (mux fd-entry)
42 (:documentation "Remove the descriptor reppresented by FD-ENTRY from multiplexer MUX.
43 Must return NIL on failure, T otherwise."))
45 (defgeneric harvest-events (mux timeout)
46 (:documentation "Wait for events on multiplexer MUX for a maximum time of TIMEOUT seconds.
47 Returns a list of fd/result pairs which have one of these forms:
48 (fd (:read))
49 (fd (:write))
50 (fd (:read :write))
51 (fd . :error)"))
53 (defmethod close-multiplexer :around ((mux multiplexer))
54 (unless (multiplexer-closedp mux)
55 (call-next-method)
56 (setf (multiplexer-closedp mux) t)))
58 (defmethod close-multiplexer progn ((mux multiplexer))
59 (when (and (slot-boundp mux 'fd) (not (null (fd-of mux))))
60 (isys:%sys-close (fd-of mux))
61 (setf (slot-value mux 'fd) nil))
62 (values mux))
64 (defmethod monitor-fd :before ((mux multiplexer) fd-entry)
65 (with-accessors ((fd-limit fd-limit-of))
66 mux
67 (let ((fd (fd-entry-fd fd-entry)))
68 (when (and fd-limit (> fd fd-limit))
69 (error "Cannot add such a large FD: ~A" fd)))))
71 (defmacro define-multiplexer (name priority superclasses slots &rest options)
72 `(progn
73 (defclass ,name ,superclasses ,slots ,@options)
74 (pushnew (cons ,priority ',name) *available-multiplexers*
75 :test #'equal)))