Changeset 95
- Timestamp:
- 02/23/07 00:18:57 (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified branches/home/psmith/restructure/src/utils/concurrent-queue.lisp ¶
r94 r95 48 48 49 49 50 51 (defmacro pop-elt(a-buffer loc)52 `(if ,a-buffer53 (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 60 50 ;Do an (optionally blocking) remove of the element at the head of this queue 61 51 (defmethod take ((queue concurrent-queue) &key (blocking-call t)) 62 #+nio-debug (format- threadsafet "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)) 63 53 (sb-thread:with-mutex ((buffer-lock queue)) 64 #+nio-debug (format- threadsafet "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)) 65 55 ;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))))))))) 74 65 75 66 ;Append the element to the tail of this queue 76 67 (defmethod add ((queue concurrent-queue) elt) 77 #+nio-debug (format- threadsafet "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) 78 69 (sb-thread:with-mutex ((buffer-lock queue)) 79 70 (setf (buffer queue) (append (buffer queue) (list elt)) ) 80 (sb-thread:condition- broadcast(buffer-queue queue))))71 (sb-thread:condition-notify (buffer-queue queue)))) 81 72 82 73 … … 84 75 (loop for i from 0 to 100 do 85 76 ; (sleep (random 0.1)) 86 (format- threadsafet "Adding ~A~%" i)77 (format-log t "Adding ~A~%" i) 87 78 (add queue i))) 88 79 89 80 (defun test-reader(queue results) 90 (format- threadsafet "Started reader ~A~%" sb-thread:*current-thread*)81 (format-log t "Started reader ~A~%" sb-thread:*current-thread*) 91 82 (loop 92 83 (let ((elt (take queue))) 93 84 (push elt results) 94 (format- threadsafet "reader on ~A got elt ~A~%"85 (format-log t "reader on ~A got elt ~A~%" 95 86 sb-thread:*current-thread* 96 87 results)))) … … 106 97 ;(t2 (sb-thread:make-thread #'(lambda()(test-reader queue *results2*))))) 107 98 (sleep 5) ;;wait for it to probably complete 108 (format- threadsafet "t1 got: ~A~%" *results1*)109 (format- threadsafet "t2 got: ~A~%" *results2*)99 (format-log t "t1 got: ~A~%" *results1*) 100 (format-log t "t2 got: ~A~%" *results2*) 110 101 (sb-thread:destroy-thread t1) 111 102 ; (sb-thread:destroy-thread t2) … … 121 112 (sb-thread:make-thread #'(lambda()(test-writer queue))) 122 113 (sleep 10) 123 (format- threadsafet "running asserts")114 (format-log t "running asserts") 124 115 (sb-thread:with-mutex ((buffer-lock queue)) 125 116 (assert (eql (length (buffer queue)) 0)))))
Note: See TracChangeset
for help on using the changeset viewer.