Fix bug with two reader and two writer processes.
authorStefan Vogel <sv@exept.de>
Tue, 31 Oct 2000 19:37:23 +0100
changeset 916 63fddba933d6
parent 915 3dc0e3cd838a
child 917 df608391baa5
Fix bug with two reader and two writer processes. Code is now simpler and faster.
SharedQueue.st
--- a/SharedQueue.st	Thu Oct 26 18:54:40 2000 +0200
+++ b/SharedQueue.st	Tue Oct 31 19:37:23 2000 +0100
@@ -10,8 +10,10 @@
  hereby transferred.
 "
 
+"{ Package: 'stx:libbasic2' }"
+
 Queue subclass:#SharedQueue
-	instanceVariableNames:'accessProtect dataAvailable spaceAvailable'
+	instanceVariableNames:'dataAvailable spaceAvailable'
 	classVariableNames:''
 	poolDictionaries:''
 	category:'Kernel-Processes'
@@ -115,58 +117,23 @@
      When the datum has been removed, signal space-availability to
      writers"
 
-    |value ok|
+    |value|
 
-    ok := false.
-    [ok] whileFalse:[
-	[tally == 0] whileTrue:[
-	    dataAvailable wait
-	].
-	accessProtect critical:[
-	    "
-	     this check is needed, since another process may
-	     have read the value in the meantime ...
-	    "
-	    tally == 0 ifFalse:[
-		value := super next.
-		tally == (contentsArray size - 1) ifTrue:[
-		    spaceAvailable signal
-		].
-		ok := true
-	    ]
-	]
-    ].
-    ^ value
-!
+    dataAvailable wait.
+    value := super next.
+    spaceAvailable signal.
+
+    ^ value.!
 
 nextPut:anObject
     "enter anObject into the queue; wait for available space, if
      the queue is full. After the put, signal availablity of a datum
      to readers."
 
-    |ok|
-
-    ok := false.
-    [ok] whileFalse:[
-        [tally == contentsArray size] whileTrue:[
-            spaceAvailable wait
-        ].
-        accessProtect critical:[
-            "
-             this check is needed, since another process may
-             have written a value in the meantime ...
-            "
-            tally == contentsArray size ifFalse:[
-                super nextPut:anObject.
-                tally == 1 ifTrue:[
-                    dataAvailable signal
-                ].
-                ok := true
-            ]
-        ]
-    ].
-    ^ anObject
-!
+    spaceAvailable wait.
+    super nextPut:anObject.
+    dataAvailable signal.
+    ^ anObject.!
 
 readSemaphore
     "return the semaphore which is signalled when data is available
@@ -184,17 +151,14 @@
      This can be used to flush queues in multi-process applications,
      when cleanup is required."
 
-    accessProtect critical:[
-        |oldCount|
+    |count|
 
-        oldCount := tally.
-        super removeAll.
-        oldCount == (contentsArray size) ifTrue:[
-            spaceAvailable signal
-        ].
-    ].
-
-!
+    count := 0.
+    [(dataAvailable waitWithTimeout:0) notNil] whileTrue:[
+        count := count + 1.
+    ].    
+    super removeAll.
+    count timesRepeat:[spaceAvailable signal].!
 
 removeLast
     "return the last value in the queue; if it its empty, wait 'til
@@ -202,31 +166,13 @@
      When the datum has been removed, signal space-availability to
      writers"
 
-    |value ok|
+    |value|
 
-    ok := false.
-    [ok] whileFalse:[
-        [tally == 0] whileTrue:[
-            dataAvailable wait
-        ].
-        accessProtect critical:[
-            "
-             this check is needed, since another process may
-             have read the value in the meantime ...
-            "
-            tally == 0 ifFalse:[
-                value := super removeLast.
-                tally == (contentsArray size - 1) ifTrue:[
-                    spaceAvailable signal
-                ].
-                ok := true
-            ]
-        ]
-    ].
-    ^ value
+    dataAvailable wait.
+    value := super removeLast.
+    spaceAvailable signal.
 
-    "Created: 22.6.1996 / 18:50:12 / cg"
-!
+    ^ value.!
 
 writeSemaphore
     "return the semaphore which is signalled when the queue has space
@@ -243,15 +189,13 @@
     "initialize the receiver for size entries"
 
     super init:size.
-    accessProtect := Semaphore forMutualExclusion name:'shared q-access'.
     dataAvailable := Semaphore new name:'shared q-read'.
-    spaceAvailable := Semaphore new name:'shared q-write'
+    spaceAvailable := (Semaphore new:size) name:'shared q-write'
 
-    "Modified: 25.1.1997 / 00:19:45 / cg"
-! !
+    "Modified: 25.1.1997 / 00:19:45 / cg"! !
 
 !SharedQueue class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic2/SharedQueue.st,v 1.21 1999-11-29 18:24:01 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic2/SharedQueue.st,v 1.22 2000-10-31 18:37:23 stefan Exp $'
 ! !