<commit_before>(in-package #:messagepack-rpc.event-loop)


(defun init ()
  "Initialize a new event-loop and return it."
  (let ((loop-pointer (cffi:foreign-alloc :unsigned-char :count (uv:uv-loop-size))))
    (uv:uv-loop-init loop-pointer)
    (let ((event-loop (make-instance
                       'as::event-base
                       :c loop-pointer
                       :id 0  ; TODO: should id change for each new loop?
                       :catch-app-errors NIL
                       :send-errors-to-eventcb T)))
      (with-event-loop-bindings (event-loop)
        (as:signal-handler 2 #'(lambda (sig)
                                 (declare (ignore sig))
                                 (clean event-loop "Event loop exited.")))
        event-loop))))

(defun run-once (event-base)
  "Run event loop once, blocking the execution of the thread until a new message is received."
  (with-event-loop-bindings (event-base)
    (uv:uv-run (as::event-base-c event-base) (cffi:foreign-enum-value 'uv:uv-run-mode :run-once))))

(defun run-forever (event-base)
  "Run event loop forever, blocking the execution and processing all received messages."
  (with-event-loop-bindings (event-base)
    (uv:uv-run (as::event-base-c event-base) (cffi:foreign-enum-value 'uv:uv-run-mode :run-default))))

(defun clean (event-base &optional with-error)
  "Clean up and properly close the event-loop, and throw an error with-error if it is supplied."
  (with-event-loop-bindings (event-base)
    (as:exit-event-loop)
    (as::do-close-loop (as::event-base-c as::*event-base*))
    ; (static-vectors:free-static-vector as::*output-buffer*)
    ; (static-vectors:free-static-vector as::*input-buffer*)
    (as::free-pointer-data (as::event-base-c as::*event-base*) :preserve-pointer t)
    (if with-error (error 'transport-error :message with-error))))

(setf as::*output-buffer* (static-vectors:make-static-vector as::*buffer-size* :element-type 'octet))
(setf as::*input-buffer* (static-vectors:make-static-vector as::*buffer-size* :element-type 'octet))<commit_msg>Exit event loop on sigint and sigterm<commit_after>(in-package #:messagepack-rpc.event-loop)


(defun init ()
  "Initialize a new event-loop and return it."
  (let ((loop-pointer (cffi:foreign-alloc :unsigned-char :count (uv:uv-loop-size))))
    (uv:uv-loop-init loop-pointer)
    (let ((event-loop (make-instance
                       'as::event-base
                       :c loop-pointer
                       :id 0  ; TODO: should id change for each new loop?
                       :catch-app-errors NIL
                       :send-errors-to-eventcb T)))
      (with-event-loop-bindings (event-loop)
        (as:signal-handler as:+sigterm+ #'(lambda (sig)
                                            (declare (ignore sig))
                                            (clean event-loop "Received sigterm. Event loop exited.")))
        (as:signal-handler as:+sigint+ #'(lambda (sig)
                                           (declare (ignore sig))
                                           (clean event-loop "Received sigint. Event loop exited.")))
        event-loop))))

(defun run-once (event-base)
  "Run event loop once, blocking the execution of the thread until a new message is received."
  (with-event-loop-bindings (event-base)
    (uv:uv-run (as::event-base-c event-base) (cffi:foreign-enum-value 'uv:uv-run-mode :run-once))))

(defun run-forever (event-base)
  "Run event loop forever, blocking the execution and processing all received messages."
  (with-event-loop-bindings (event-base)
    (uv:uv-run (as::event-base-c event-base) (cffi:foreign-enum-value 'uv:uv-run-mode :run-default))))

(defun clean (event-base &optional with-error)
  "Clean up and properly close the event-loop, and throw an error with-error if it is supplied."
  (with-event-loop-bindings (event-base)
    (as:exit-event-loop)
    (as::do-close-loop (as::event-base-c as::*event-base*))
    ; (static-vectors:free-static-vector as::*output-buffer*)
    ; (static-vectors:free-static-vector as::*input-buffer*)
    (as::free-pointer-data (as::event-base-c as::*event-base*) :preserve-pointer t)
    (if with-error (error 'transport-error :message with-error))))

(setf as::*output-buffer* (static-vectors:make-static-vector as::*buffer-size* :element-type 'octet))
(setf as::*input-buffer* (static-vectors:make-static-vector as::*buffer-size* :element-type 'octet))
{'fraction_non_alphanumeric': 0.14963833634719712, 'fraction_numerical': 0.000678119349005425, 'mean_word_length': 2.6449752883031303, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7444785', 'n_tokens_mistral': 1402, 'n_tokens_neox': 1366, 'n_words': 337}