Changeset 95


Ignore:
Timestamp:
02/23/07 00:18:57 (18 years ago)
Author:
psmith
Message:

Fixed problem with NIL being read from queue and then waiting when we should have returned

File:
1 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified branches/home/psmith/restructure/src/utils/concurrent-queue.lisp

    r94 r95  
    4848
    4949
    50 
    51 (defmacro pop-elt(a-buffer loc)
    52   `(if ,a-buffer
    53        (let ((head (car ,a-buffer)))
    54          (setf ,a-buffer (cdr ,a-buffer))
    55 #+nio-debug (format-threadsafe t "concurent-queue:take - (~A,~A) read ~A at ~A~%" sb-thread:*current-thread* (length (buffer queue)) head ,loc)
    56          head)
    57        nil))
    58 
    59 
    6050;Do an (optionally blocking) remove of the element at the head of this queue
    6151(defmethod take ((queue concurrent-queue) &key (blocking-call t))
    62 #+nio-debug (format-threadsafe t "concurent-queue:take - (~A) attempting to obtain mutex ~A~%" sb-thread:*current-thread* (buffer-lock queue))
     52#+nio-debug (format-log t "concurent-queue:take - (~A) attempting to obtain mutex ~A~%" sb-thread:*current-thread* (buffer-lock queue))
    6353  (sb-thread:with-mutex ((buffer-lock queue))
    64 #+nio-debug (format-threadsafe t "concurent-queue:take - (~A) aquired mutex mutex ~A~%" sb-thread:*current-thread* (buffer-lock queue))
     54#+nio-debug (format-log t "concurent-queue:take - (~A) aquired mutex mutex ~A~%" sb-thread:*current-thread* (buffer-lock queue))
    6555    ;if its there, pop it
    66     (let ((ret (pop-elt (buffer queue) "1sttry")))
    67       (if (or ret (not blocking-call))
    68           ret
    69           (progn
    70 #+nio-debug (format-threadsafe t "concurent-queue:take - (~A) about to wait on queue~%" sb-thread:*current-thread*)
    71             (sb-thread:condition-wait (buffer-queue queue) (buffer-lock queue))
    72 #+nio-debug (format-threadsafe t "concurent-queue:take - (~A) notified on queue~%" sb-thread:*current-thread*)
    73             (pop-elt (buffer queue) "2ndtry"))))))
     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)))))))))
    7465
    7566;Append the element to the tail of this queue
    7667(defmethod add ((queue concurrent-queue) elt)
    77 #+nio-debug (format-threadsafe t "concurent-queue:add - (~A) adding ~A~%" sb-thread:*current-thread* elt)
     68#+nio-debug (format-log t "concurent-queue:add - (~A) adding ~A~%" sb-thread:*current-thread* elt)
    7869  (sb-thread:with-mutex ((buffer-lock queue))
    7970    (setf (buffer queue) (append (buffer queue) (list elt)) )
    80     (sb-thread:condition-broadcast (buffer-queue queue))))
     71    (sb-thread:condition-notify (buffer-queue queue))))
    8172
    8273
     
    8475  (loop for i from 0 to 100 do
    8576;       (sleep (random 0.1))
    86        (format-threadsafe t "Adding ~A~%" i)
     77       (format-log t "Adding ~A~%" i)
    8778       (add queue i)))
    8879       
    8980(defun test-reader(queue results)
    90   (format-threadsafe t "Started reader ~A~%" sb-thread:*current-thread*)
     81  (format-log t "Started reader ~A~%" sb-thread:*current-thread*)
    9182  (loop
    9283     (let ((elt (take queue)))
    9384       (push elt results)
    94        (format-threadsafe t "reader on ~A got elt ~A~%"
     85       (format-log t "reader on ~A got elt ~A~%"
    9586               sb-thread:*current-thread*
    9687               results))))
     
    10697          ;(t2 (sb-thread:make-thread #'(lambda()(test-reader queue *results2*)))))
    10798      (sleep 5) ;;wait for it to probably complete
    108       (format-threadsafe t "t1 got: ~A~%" *results1*)
    109       (format-threadsafe t "t2 got: ~A~%" *results2*)
     99      (format-log t "t1 got: ~A~%" *results1*)
     100      (format-log t "t2 got: ~A~%" *results2*)
    110101      (sb-thread:destroy-thread t1)
    111102;      (sb-thread:destroy-thread t2)
     
    121112    (sb-thread:make-thread #'(lambda()(test-writer queue)))
    122113    (sleep 10)
    123     (format-threadsafe t "running asserts")
     114    (format-log t "running asserts")
    124115    (sb-thread:with-mutex ((buffer-lock queue))
    125116      (assert (eql (length (buffer queue)) 0)))))
Note: See TracChangeset for help on using the changeset viewer.