BackgroundQueueProcessingJob.st
changeset 2696 689bb9231455
parent 2637 41e0362d5631
child 2697 0090025b8aa7
--- a/BackgroundQueueProcessingJob.st	Tue Dec 06 01:49:34 2011 +0100
+++ b/BackgroundQueueProcessingJob.st	Tue Dec 06 19:03:05 2011 +0100
@@ -26,7 +26,7 @@
 "{ Package: 'stx:libbasic2' }"
 
 BackgroundJob subclass:#BackgroundQueueProcessingJob
-	instanceVariableNames:'queue queueAccessLock'
+	instanceVariableNames:'queue queueAccessLock queueProcessedSema'
 	classVariableNames:''
 	poolDictionaries:''
 	category:'System-Support'
@@ -81,7 +81,8 @@
         queue       <OrderedCollection>     the queue of items to be procesed
         queueAccessLock<RecursionLock>      a lock used to synchronize
                                             access to the queue
-
+        queueProcessedSema<Semaphore>       a semaphore signaled when the queue is
+                                            processed (and therefore empty).
     [class variables:]
 
     [see also:]
@@ -144,8 +145,9 @@
     super initialize.
     queue := OrderedCollection new.
     queueAccessLock := RecursionLock new.
+    queueProcessedSema := Semaphore new.
 
-    "Modified: / 29-07-2011 / 10:22:42 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 06-12-2011 / 15:50:03 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
 !BackgroundQueueProcessingJob methodsFor:'processing'!
@@ -164,11 +166,12 @@
             self processItem: item
         ].
         item ~~ cookie.
-    ] whileTrue
+    ] whileTrue.
+    queueProcessedSema signalForAll.
 
     "Created: / 28-04-2011 / 20:36:35 / Jan Vrany <jan.vrany@fit.cvut.cz>"
     "Modified (format): / 03-08-2011 / 16:43:21 / cg"
-    "Modified: / 07-09-2011 / 13:02:23 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 06-12-2011 / 15:50:39 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 processItem: item
@@ -178,14 +181,43 @@
     "Created: / 28-04-2011 / 20:36:51 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
+!BackgroundQueueProcessingJob methodsFor:'queries'!
+
+numberIfItemsInQueue
+    "Returns number of items waiting to be processed. 
+     May not be accurate as there is no synchronization
+     (intentionally, for performance reasons)"
+
+    ^queue size
+
+    "Created: / 06-12-2011 / 16:01:53 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+
+!BackgroundQueueProcessingJob methodsFor:'utilities'!
+
+waitUntilProcessed
+    "Blocks the receiver until all items from the queue are processed.
+    NOTE, that it may block forever if another thread is filling queue
+    fadt enough."
+
+    self scheduled ifFalse:[ ^ self ].
+    queueProcessedSema wait.
+
+    "
+    queueProcessedSema signalForAll    
+    "
+
+    "Created: / 06-12-2011 / 15:58:49 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+
 !BackgroundQueueProcessingJob class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic2/BackgroundQueueProcessingJob.st,v 1.9 2011-09-07 12:10:12 vrany Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic2/BackgroundQueueProcessingJob.st,v 1.10 2011-12-06 18:03:05 vrany Exp $'
 !
 
 version_CVS
-    ^ '$Header: /cvs/stx/stx/libbasic2/BackgroundQueueProcessingJob.st,v 1.9 2011-09-07 12:10:12 vrany Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic2/BackgroundQueueProcessingJob.st,v 1.10 2011-12-06 18:03:05 vrany Exp $'
 !
 
 version_SVN