<commit_before>;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp -*-

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;   Copyright (C) 2006 by <NAME>                                 ;
;                                                                         ;
;   This program is free software; you can redistribute it and/or modify  ;
;   it under the terms of the GNU General Public License as published by  ;
;   the Free Software Foundation; either version 2 of the License, or     ;
;   (at your option) any later version.                                   ;
;                                                                         ;
;   This program is distributed in the hope that it will be useful,       ;
;   but WITHOUT ANY WARRANTY; without even the implied warranty of        ;
;   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         ;
;   GNU General Public License for more details.                          ;
;                                                                         ;
;   You should have received a copy of the GNU General Public License     ;
;   along with this program; if not, write to the                         ;
;   Free Software Foundation, Inc.,                                       ;
;   51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA              ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; (declaim (optimize (speed 2) (safety 2) (space 1) (debug 2)))
(declaim (optimize (speed 0) (safety 2) (space 0) (debug 2)))

(in-package #:io.multiplex)

;;;
;;; Class definitions
;;;

(defstruct (handler
             (:constructor make-handler (fd read-func write-func except-func))
             (:copier nil))
  (fd            0 :type et:select-file-descriptor)
  (read-func   nil :type (or function null))
  (write-func  nil :type (or function null))
  (except-func nil :type (or function null)))

(defclass multiplex-interface ()
  ((fd-handlers :initform (make-hash-table :test 'eql) :reader fd-handlers)
   (fd-set-size :initform 0)))

(defmethod initialize-instance :after ((interface multiplex-interface)
                                       &key size)
  (setf (slot-value interface 'fd-set-size) size))

(defgeneric fd-handler (multiplex-interface fd)
  (:method ((interface multiplex-interface) fd)
    (gethash fd (fd-handlers interface))))

(defgeneric monitor-fd (multiplex-interface handler)
  (:method-combination progn :most-specific-last))

(defgeneric modify-fd (multiplex-interface fd
                       &key read-handler write-handler except-handler)
  (:method-combination progn :most-specific-last))

(defgeneric add-fd-handlers (multiplex-interface fd
                             &key read-handler write-handler except-handler)
  (:method-combination progn :most-specific-last))

(defgeneric unmonitor-fd (multiplex-interface handler)
  (:method-combination progn :most-specific-first))

(defgeneric remove-fd-handlers (multiplex-interface fd
                                &key read write except all)
  (:method-combination progn :most-specific-first))

(defgeneric serve-fd-events (multiplex-interface &key))

(defgeneric close-multiplex-interface (multiplex-interface fd)
  (:method ((interface multiplex-interface))
    t))

(eval-when (:compile-toplevel :load-toplevel :execute)
  (defvar *multiplex-available-interfaces* nil)
  (defvar *multiplex-best-interface* nil))

(defmacro define-iomux-interface (name priority)
  `(pushnew (cons ,priority ',name)
            *multiplex-available-interfaces*))

;; small utility
(defun fd-open-p (fd)
  (sb-alien:with-alien ((stat et:stat))
    (handler-case
        (progn (et:stat fd (sb-alien:addr stat)) t)
      (et:unix-error-badf (err)
        (declare (ignore err))
        nil))))<commit_msg>Small fix.<commit_after>;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp -*-

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;   Copyright (C) 2006 by <NAME>                                 ;
;                                                                         ;
;   This program is free software; you can redistribute it and/or modify  ;
;   it under the terms of the GNU General Public License as published by  ;
;   the Free Software Foundation; either version 2 of the License, or     ;
;   (at your option) any later version.                                   ;
;                                                                         ;
;   This program is distributed in the hope that it will be useful,       ;
;   but WITHOUT ANY WARRANTY; without even the implied warranty of        ;
;   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         ;
;   GNU General Public License for more details.                          ;
;                                                                         ;
;   You should have received a copy of the GNU General Public License     ;
;   along with this program; if not, write to the                         ;
;   Free Software Foundation, Inc.,                                       ;
;   51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA              ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; (declaim (optimize (speed 2) (safety 2) (space 1) (debug 2)))
(declaim (optimize (speed 0) (safety 2) (space 0) (debug 2)))

(in-package #:io.multiplex)

;;;
;;; Class definitions
;;;

(defstruct (handler
             (:constructor make-handler (fd read-func write-func except-func))
             (:copier nil))
  (fd            0 :type et:select-file-descriptor)
  (read-func   nil :type (or function null))
  (write-func  nil :type (or function null))
  (except-func nil :type (or function null)))

(defclass multiplex-interface ()
  ((fd-handlers :initform (make-hash-table :test 'eql) :reader fd-handlers)
   (fd-set-size :initform 0)))

(defmethod initialize-instance :after ((interface multiplex-interface)
                                       &key size)
  (setf (slot-value interface 'fd-set-size) size))

(defgeneric fd-handler (multiplex-interface fd)
  (:method ((interface multiplex-interface) fd)
    (gethash fd (fd-handlers interface))))

(defgeneric monitor-fd (multiplex-interface handler)
  (:method-combination progn :most-specific-last))

(defgeneric modify-fd (multiplex-interface fd
                       &key read-handler write-handler except-handler)
  (:method-combination progn :most-specific-last))

(defgeneric add-fd-handlers (multiplex-interface fd
                             &key read-handler write-handler except-handler)
  (:method-combination progn :most-specific-last))

(defgeneric unmonitor-fd (multiplex-interface handler)
  (:method-combination progn :most-specific-first))

(defgeneric remove-fd-handlers (multiplex-interface fd
                                &key read write except all)
  (:method-combination progn :most-specific-first))

(defgeneric serve-fd-events (multiplex-interface &key))

(defgeneric close-multiplex-interface (multiplex-interface)
  (:method ((interface multiplex-interface))
    t))

(eval-when (:compile-toplevel :load-toplevel :execute)
  (defvar *multiplex-available-interfaces* nil)
  (defvar *multiplex-best-interface* nil))

(defmacro define-iomux-interface (name priority)
  `(pushnew (cons ,priority ',name)
            *multiplex-available-interfaces*))

;; small utility
(defun fd-open-p (fd)
  (sb-alien:with-alien ((stat et:stat))
    (handler-case
        (progn (et:stat fd (sb-alien:addr stat)) t)
      (et:unix-error-badf (err)
        (declare (ignore err))
        nil))))
{'fraction_non_alphanumeric': 0.1431764396529056, 'fraction_numerical': 0.0068367078622140415, 'mean_word_length': 2.2536355859709154, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '779242', 'n_tokens_mistral': 2211, 'n_tokens_neox': 2042, 'n_words': 629}