<commit_before>(defpackage :mailbox
  (:use :cl)
  (:export #:mailboxp
           #:make-mailbox
           #:post-mail
           #:read-mail))

(in-package :mailbox)

(defclass mailbox ()
  ((mailbox :initform (cons nil nil))
   (tail)
   (lock :initform (bordeaux-threads:make-lock))
   (emptyp :initform t)))

(defmethod initialize-instance :after ((the mailbox) &rest initargs)
  (declare (ignore initargs))
  (setf (slot-value the 'tail) (slot-value the 'mailbox)))

(defun make-mailbox ()
  (make-instance 'mailbox))

(defmethod mailboxp ((object mailbox)) t)

(defmethod post-mail (object (mailbox mailbox))
  (with-slots (lock emptyp mailbox tail)
      mailbox
    (bordeaux-threads:with-lock-held (lock)
      (let ((mail (cons object nil)))
        (if emptyp
            (setf mailbox mail
                  tail mailbox
                  emptyp nil)
            (setf (cdr tail) mail
                  tail mail))
        object))))

(defmethod read-mail ((mailbox mailbox))
  (with-slots (lock emptyp mailbox tail)
      mailbox
    (bordeaux-threads:with-lock-held (lock)
      (if emptyp
          (values nil nil)
          (values (prog1 (first mailbox)
                    (if (endp (rest mailbox))
                        (let ((empty (cons nil nil)))
                          (setf mailbox empty
                                tail empty
                                emptyp t))
                        (setf mailbox (rest mailbox))))
                  t)))))
<commit_msg>docstrings and explicit defgenerics<commit_after>(defpackage :mailbox
  (:use :cl)
  (:export #:mailboxp
           #:make-mailbox
           #:post-mail
           #:read-mail))

(in-package :mailbox)

(defclass mailbox ()
  ((mailbox :initform (cons nil nil))
   (tail)
   (lock :initform (bordeaux-threads:make-lock))
   (emptyp :initform t)))

(defmethod initialize-instance :after ((the mailbox) &rest initargs)
  (declare (ignore initargs))
  (setf (slot-value the 'tail) (slot-value the 'mailbox)))

(defun make-mailbox ()
  (make-instance 'mailbox))

(defgeneric mailboxp (object)
  (:documentation "Return T if OBJECT is a mailbox.")
  (:method (object) nil)
  (:method ((object mailbox)) t))

(defgeneric post-mail (object mailbox)
  (:documentation "Add OBJECT to MAILBOX.")
  (:method (object (mailbox mailbox))
    (with-slots (lock emptyp mailbox tail)
        mailbox
      (bordeaux-threads:with-lock-held (lock)
        (let ((mail (cons object nil)))
          (if emptyp
              (setf mailbox mail
                    tail mailbox
                    emptyp nil)
              (setf (cdr tail) mail
                    tail mail))
          object)))))

(defgeneric read-mail (mailbox)
  (:documentation "Retrieve oldest object from MAILBOX.

If the mailbox is not empty return multiple values the object and T,
otherwise NIL and NIL.")
  (:method ((mailbox mailbox))
    (with-slots (lock emptyp mailbox tail)
        mailbox
      (bordeaux-threads:with-lock-held (lock)
        (if emptyp
            (values nil nil)
            (values (prog1 (first mailbox)
                      (if (endp (rest mailbox))
                          (let ((empty (cons nil nil)))
                            (setf mailbox empty
                                  tail empty
                                  emptyp t))
                          (setf mailbox (rest mailbox))))
                    t))))))

{'fraction_non_alphanumeric': 0.11478770131771596, 'fraction_numerical': 0.0005856515373352855, 'mean_word_length': 1.868178001679261, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '288378', 'n_tokens_mistral': 1039, 'n_tokens_neox': 997, 'n_words': 303}