Replace sb-concurrency:queue implementation.
[sbcl.git] / contrib / sb-concurrency / queue.lisp
1 ;;;; Written by James M. Lawrence for SBCL.
2 ;;;; API and docstrings by Nikodemus Siivola.
3 ;;;;
4 ;;;; This software is part of the SBCL system. See the README file for
5 ;;;; more information.
6 ;;;;
7 ;;;; This software is derived from the CMU CL system, which was written at
8 ;;;; Carnegie Mellon University and released into the public domain. The
9 ;;;; software is in the public domain and is provided with absolutely no
10 ;;;; warranty. See the COPYING and CREDITS files for more information.
11
12 ;;; Singly-linked queue with compare-and-swap operations.
13 ;;;
14 ;;; The following invariants hold except during updates:
15 ;;;
16 ;;;   (car (queue-head queue)) == +dummy+
17 ;;;
18 ;;;   (cdr (queue-tail queue)) == nil
19 ;;;
20 ;;;   If the queue is empty, (queue-head queue) == (queue-tail queue).
21 ;;;
22 ;;;   If the queue is non-empty, (cadr (queue-head queue)) is the next
23 ;;;   value to be dequeued and (car (queue-tail queue)) is the most
24 ;;;   recently enqueued value.
25 ;;;
26 ;;; The CDR of a discarded node is set to +DEAD-END+. This flag must
27 ;;; be checked at each traversal.
28
29 (in-package :sb-concurrency)
30
31 (defconstant +dummy+ '.dummy.)
32 (defconstant +dead-end+ '.dead-end.)
33
34 (declaim (inline %make-queue))
35 (defstruct (queue (:constructor %make-queue (head tail name))
36                   (:copier nil)
37                   (:predicate queuep))
38   "Lock-free thread safe FIFO queue.
39
40 Use ENQUEUE to add objects to the queue, and DEQUEUE to remove them."
41   (head (error "No HEAD.") :type cons)
42   (tail (error "No TAIL.") :type cons)
43   (name nil))
44
45 (setf (documentation 'queuep 'function)
46       "Returns true if argument is a QUEUE, NIL otherwise."
47       (documentation 'queue-name 'function)
48       "Name of a QUEUE. Can be assingned to using SETF. Queue names
49 can be arbitrary printable objects, and need not be unique.")
50
51 (defun make-queue (&key name initial-contents)
52   "Returns a new QUEUE with NAME and contents of the INITIAL-CONTENTS
53 sequence enqueued."
54   (let* ((dummy (cons +dummy+ nil))
55          (queue (%make-queue dummy dummy name)))
56     (flet ((enc-1 (x)
57              (enqueue x queue)))
58       (declare (dynamic-extent #'enc-1))
59       (map nil #'enc-1 initial-contents))
60     queue))
61
62 (defun enqueue (value queue)
63   "Adds VALUE to the end of QUEUE. Returns VALUE."
64   ;; Attempt CAS, repeat upon failure. Upon success update QUEUE-TAIL.
65   (declare (optimize speed))
66   (let ((new (cons value nil)))
67     (loop (when (eq nil (sb-ext:compare-and-swap (cdr (queue-tail queue))
68                                                  nil new))
69             (setf (queue-tail queue) new)
70             (return value)))))
71
72 (defun dequeue (queue)
73   "Retrieves the oldest value in QUEUE and returns it as the primary value,
74 and T as secondary value. If the queue is empty, returns NIL as both primary
75 and secondary value."
76   ;; Attempt to CAS QUEUE-HEAD with the next node, repeat upon
77   ;; failure. Upon success, clear the discarded node and set the CAR
78   ;; of QUEUE-HEAD to +DUMMY+.
79   (declare (optimize speed))
80   (loop (let* ((head (queue-head queue))
81                (next (cdr head)))
82           ;; NEXT could be +DEAD-END+, whereupon we try again.
83           (typecase next
84             (null (return (values nil nil)))
85             (cons (when (eq head (sb-ext:compare-and-swap (queue-head queue)
86                                                           head next))
87                     (let ((value (car next)))
88                       ;; Clear the CDR, otherwise the conservative GC could
89                       ;; hoard long lists. (car head) is always +dummy+.
90                       (setf (cdr head) +dead-end+
91                             (car next) +dummy+)
92                       (return (values value t)))))))))
93
94 (defun try-walk-queue (fun queue)
95   (let ((node (queue-head queue)))
96     (loop
97        (let ((value (car node)))
98          (unless (eq value +dummy+)
99            (funcall fun value)))
100        (setf node (cdr node))
101        (cond ((eq node +dead-end+)
102               (return nil))
103              ((null node)
104               (return t))))))
105
106 (defun list-queue-contents (queue)
107   "Returns the contents of QUEUE as a list without removing them from the
108 QUEUE. Mainly useful for manual examination of queue state, as the list
109 may be out of date by the time it is returned."
110   (tagbody
111    :retry
112      (collect ((result))
113        (unless (try-walk-queue (lambda (elem) (result elem)) queue)
114          (go :retry))
115        (return-from list-queue-contents (result)))))
116
117 (defun queue-count (queue)
118   "Returns the number of objects in QUEUE. Mainly useful for manual
119 examination of queue state, and in PRINT-OBJECT methods: inefficient as it
120 must walk the entire queue."
121   (tagbody
122    :retry
123      (let ((count 0))
124        (unless (try-walk-queue (lambda (elem)
125                                  (declare (ignore elem))
126                                  (incf count))
127                                queue)
128          (go :retry))
129        (return-from queue-count count))))
130
131 (defun queue-empty-p (queue)
132   "Returns T if QUEUE is empty, NIL otherwise."
133   (null (cdr (queue-head queue))))