1 | #| |
---|
2 | Copyright (c) 2007 |
---|
3 | All rights reserved. |
---|
4 | |
---|
5 | Redistribution and use in source and binary forms, with or without |
---|
6 | modification, are permitted provided that the following conditions |
---|
7 | are met: |
---|
8 | 1. Redistributions of source code must retain the above copyright |
---|
9 | notice, this list of conditions and the following disclaimer. |
---|
10 | 2. Redistributions in binary form must reproduce the above copyright |
---|
11 | notice, this list of conditions and the following disclaimer in the |
---|
12 | documentation and/or other materials provided with the distribution. |
---|
13 | 3. The name of the author may not be used to endorse or promote products |
---|
14 | derived from this software without specific prior written permission. |
---|
15 | |
---|
16 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
---|
17 | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
---|
18 | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
---|
19 | IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
---|
20 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
---|
21 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
---|
22 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
---|
23 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
24 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
---|
25 | THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
26 | |# |
---|
27 | |
---|
28 | (in-package :nio-utils) |
---|
29 | |
---|
30 | (declaim (optimize (debug 3) (speed 3) (space 0))) |
---|
31 | |
---|
32 | ;Implements a threadsafe queue where readers wait for elements of a FIFO queue to appear using a waitqueue |
---|
33 | ;Modified from sbcl manual example |
---|
34 | |
---|
35 | |
---|
36 | (defclass concurrent-queue() |
---|
37 | ((buffer-queue :initform (sb-thread:make-waitqueue) |
---|
38 | :reader buffer-queue) |
---|
39 | ; (buffer-queue-mutex :initform (sb-thread:make-mutex :name "buffer queue mutex") |
---|
40 | ; :reader buffer-queue-mutex) |
---|
41 | (buffer-lock :initform (sb-thread:make-mutex :name "buffer lock") |
---|
42 | :reader buffer-lock) |
---|
43 | (buffer :initform nil |
---|
44 | :accessor buffer))) |
---|
45 | |
---|
46 | (defun concurrent-queue() |
---|
47 | (make-instance 'concurrent-queue)) |
---|
48 | |
---|
49 | |
---|
50 | ;Do an (optionally blocking) remove of the element at the head of this queue |
---|
51 | (defmethod take ((queue concurrent-queue) &key (blocking-call t)) |
---|
52 | #+nio-debug (format-log t "concurent-queue:take - (~A) attempting to obtain mutex ~A~%" sb-thread:*current-thread* (buffer-lock queue)) |
---|
53 | (sb-thread:with-mutex ((buffer-lock queue)) |
---|
54 | #+nio-debug (format-log t "concurent-queue:take - (~A) aquired mutex mutex ~A~%" sb-thread:*current-thread* (buffer-lock queue)) |
---|
55 | ;if its there, pop it |
---|
56 | (if (> (length (buffer queue)) 0) |
---|
57 | (pop (buffer queue)) |
---|
58 | (when blocking-call |
---|
59 | (loop |
---|
60 | #+nio-debug (format-log t "concurent-queue:take - (~A) about to wait on queue~%" sb-thread:*current-thread*) |
---|
61 | (sb-thread:condition-wait (buffer-queue queue) (buffer-lock queue)) |
---|
62 | #+nio-debug (format-log t "concurent-queue:take - (~A) notified on queue~%" sb-thread:*current-thread*) |
---|
63 | (if (> (length (buffer queue)) 0) |
---|
64 | (return-from take (pop (buffer queue))))))))) |
---|
65 | |
---|
66 | ;Append the element to the tail of this queue |
---|
67 | (defmethod add ((queue concurrent-queue) elt) |
---|
68 | #+nio-debug (format-log t "concurent-queue:add - (~A) adding ~A to ~A~%" sb-thread:*current-thread* elt queue) |
---|
69 | (sb-thread:with-mutex ((buffer-lock queue)) |
---|
70 | (setf (buffer queue) (append (buffer queue) (list elt)) ) |
---|
71 | (sb-thread:condition-notify (buffer-queue queue)))) |
---|
72 | |
---|
73 | |
---|
74 | (defun test-writer(queue) |
---|
75 | (loop for i from 0 to 100 do |
---|
76 | ; (sleep (random 0.1)) |
---|
77 | (format-log t "Adding ~A~%" i) |
---|
78 | (add queue i))) |
---|
79 | |
---|
80 | (defun test-reader(queue results) |
---|
81 | (format-log t "Started reader ~A~%" sb-thread:*current-thread*) |
---|
82 | (loop |
---|
83 | (let ((elt (take queue))) |
---|
84 | (push elt results) |
---|
85 | (format-log t "reader on ~A got elt ~A~%" |
---|
86 | sb-thread:*current-thread* |
---|
87 | results)))) |
---|
88 | |
---|
89 | (defparameter *results1* (list 999999)) |
---|
90 | (defparameter *results2* (list 888888)) |
---|
91 | |
---|
92 | (defun test-queue() |
---|
93 | (let ((queue (make-instance 'concurrent-queue))) |
---|
94 | (sb-thread:make-thread #'(lambda()(test-writer queue))) |
---|
95 | ; (sleep 10) |
---|
96 | (let ((t1 (sb-thread:make-thread #'(lambda()(test-reader queue *results1*))))) |
---|
97 | ;(t2 (sb-thread:make-thread #'(lambda()(test-reader queue *results2*))))) |
---|
98 | (sleep 5) ;;wait for it to probably complete |
---|
99 | (format-log t "t1 got: ~A~%" *results1*) |
---|
100 | (format-log t "t2 got: ~A~%" *results2*) |
---|
101 | (sb-thread:destroy-thread t1) |
---|
102 | ; (sb-thread:destroy-thread t2) |
---|
103 | ) |
---|
104 | (sb-thread:with-mutex ((buffer-lock queue)) |
---|
105 | (assert (eql (length (buffer queue)) 0))))) |
---|
106 | |
---|
107 | (defun test-queue2() |
---|
108 | (let ((queue (make-instance 'concurrent-queue))) |
---|
109 | (sb-thread:make-thread #'(lambda()(test-reader queue))) |
---|
110 | (sb-thread:make-thread #'(lambda()(test-writer queue))) |
---|
111 | (sb-thread:make-thread #'(lambda()(test-reader queue))) |
---|
112 | (sb-thread:make-thread #'(lambda()(test-writer queue))) |
---|
113 | (sleep 10) |
---|
114 | (format-log t "running asserts") |
---|
115 | (sb-thread:with-mutex ((buffer-lock queue)) |
---|
116 | (assert (eql (length (buffer queue)) 0))))) |
---|