From 557c66cf4e6ca1ef0c4ecbeaf7a15da7babd2cdf Mon Sep 17 00:00:00 2001 From: Justin Ethier Date: Thu, 27 Jun 2019 12:58:27 -0400 Subject: [PATCH] Add wait count --- libs/cyclone/shared-queue.sld | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/libs/cyclone/shared-queue.sld b/libs/cyclone/shared-queue.sld index 524525d3..2d854be7 100644 --- a/libs/cyclone/shared-queue.sld +++ b/libs/cyclone/shared-queue.sld @@ -49,11 +49,12 @@ ;if start == end after an add, then vector is full, need to resize (define-record-type - (%make-queue store start end lock cv) + (%make-queue store start end wait-count lock cv) queue? (store q:store q:set-store!) (start q:start q:set-start!) (end q:end q:set-end!) + (wait-count q:wait-count q:set-wait-count) (lock q:lock q:set-lock!) (cv q:cv q:set-cv!) ) @@ -64,6 +65,7 @@ (make-vector *default-table-size* #f) 0 0 + 0 (make-mutex) (make-condition-variable) ))) @@ -118,14 +120,21 @@ ;; should we have a failsafe if the same thread that is doing adds, then ;; does a blocking remove?? (define (queue-remove! q) - (let loop () + (let loop ((waiting #f)) (mutex-lock! (q:lock q)) + ;; If thread was previously waiting, clear that status + (when waiting + (set! waiting #f) + (q:set-wait-count q (- (q:wait-count q) 1))) (cond ((= (q:start q) (q:end q)) + ;; Let Q know we are waiting + (set! waiting #t) + (q:set-wait-count q (+ (q:wait-count q) 1)) ;; Wait for CV, indicating data is ready (mutex-unlock! (q:lock q) (q:cv q)) - (loop)) + (loop waiting)) (else (let ((result (vector-ref (q:store q) (q:start q)))) (q:set-start! q (inc (q:start q) (vector-length (q:store q))))