#| Copyright (c) 2007 All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |# (in-package :nio-utils) (declaim (optimize (debug 3) (speed 3) (space 0))) ;Implements a threadsafe queue where readers wait for elements of a FIFO queue to appear using a waitqueue ;Modified from sbcl manual example (defclass concurrent-queue() ((buffer-queue :initform (sb-thread:make-waitqueue) :reader buffer-queue) ; (buffer-queue-mutex :initform (sb-thread:make-mutex :name "buffer queue mutex") ; :reader buffer-queue-mutex) (buffer-lock :initform (sb-thread:make-mutex :name "buffer lock") :reader buffer-lock) (buffer :initform nil :accessor buffer))) (defun concurrent-queue() (make-instance 'concurrent-queue)) ;Do an (optionally blocking) remove of the element at the head of this queue (defmethod take ((queue concurrent-queue) &key (blocking-call t)) #+nio-debug (format-log t "concurent-queue:take - (~A) attempting to obtain mutex ~A~%" sb-thread:*current-thread* (buffer-lock queue)) (sb-thread:with-mutex ((buffer-lock queue)) #+nio-debug (format-log t "concurent-queue:take - (~A) aquired mutex mutex ~A~%" sb-thread:*current-thread* (buffer-lock queue)) ;if its there, pop it (if (> (length (buffer queue)) 0) (pop (buffer queue)) (when blocking-call (loop #+nio-debug (format-log t "concurent-queue:take - (~A) about to wait on queue~%" sb-thread:*current-thread*) (sb-thread:condition-wait (buffer-queue queue) (buffer-lock queue)) #+nio-debug (format-log t "concurent-queue:take - (~A) notified on queue~%" sb-thread:*current-thread*) (if (> (length (buffer queue)) 0) (return-from take (pop (buffer queue))))))))) ;Append the element to the tail of this queue (defmethod add ((queue concurrent-queue) elt) #+nio-debug (format-log t "concurent-queue:add - (~A) adding ~A to ~A~%" sb-thread:*current-thread* elt queue) (sb-thread:with-mutex ((buffer-lock queue)) (setf (buffer queue) (append (buffer queue) (list elt)) ) (sb-thread:condition-notify (buffer-queue queue)))) (defun test-writer(queue) (loop for i from 0 to 100 do ; (sleep (random 0.1)) (format-log t "Adding ~A~%" i) (add queue i))) (defun test-reader(queue results) (format-log t "Started reader ~A~%" sb-thread:*current-thread*) (loop (let ((elt (take queue))) (push elt results) (format-log t "reader on ~A got elt ~A~%" sb-thread:*current-thread* results)))) (defparameter *results1* (list 999999)) (defparameter *results2* (list 888888)) (defun test-queue() (let ((queue (make-instance 'concurrent-queue))) (sb-thread:make-thread #'(lambda()(test-writer queue))) ; (sleep 10) (let ((t1 (sb-thread:make-thread #'(lambda()(test-reader queue *results1*))))) ;(t2 (sb-thread:make-thread #'(lambda()(test-reader queue *results2*))))) (sleep 5) ;;wait for it to probably complete (format-log t "t1 got: ~A~%" *results1*) (format-log t "t2 got: ~A~%" *results2*) (sb-thread:destroy-thread t1) ; (sb-thread:destroy-thread t2) ) (sb-thread:with-mutex ((buffer-lock queue)) (assert (eql (length (buffer queue)) 0))))) (defun test-queue2() (let ((queue (make-instance 'concurrent-queue))) (sb-thread:make-thread #'(lambda()(test-reader queue))) (sb-thread:make-thread #'(lambda()(test-writer queue))) (sb-thread:make-thread #'(lambda()(test-reader queue))) (sb-thread:make-thread #'(lambda()(test-writer queue))) (sleep 10) (format-log t "running asserts") (sb-thread:with-mutex ((buffer-lock queue)) (assert (eql (length (buffer queue)) 0)))))