#BUGFIX by stefan
class: SharedQueue
fix bug in #nextIfEmpty: if exceptionblock returns.
add missing methods
added:
#remove:ifAbsent:
#reverseDo:
comment/format in: #commonWriteWith:
changed:
#do:
#next
#nextIfEmpty:
#nextPut:
#nextPutFirst:
#nextWithTimeout:
#removeIdentical:ifAbsent:
#removeLast
--- a/SharedQueue.st Wed Feb 22 15:32:46 2017 +0100
+++ b/SharedQueue.st Wed Feb 22 15:33:50 2017 +0100
@@ -123,6 +123,22 @@
!SharedQueue methodsFor:'accessing'!
+remove:anElement ifAbsent:exceptionalValue
+ |retVal noSuchElement|
+
+ noSuchElement := false.
+ retVal := accessLock critical:[
+ super remove:anElement ifAbsent:[noSuchElement := true]
+ ].
+ noSuchElement ifTrue:[
+ ^ exceptionalValue value.
+ ].
+ spaceAvailable signal.
+ ^ retVal.
+
+ "Created: / 22-02-2017 / 14:53:13 / stefan"
+!
+
removeAll
"remove all elements in the queue; do not wait, but
synchronize access to the queue.
@@ -146,14 +162,16 @@
|retVal noSuchElement|
noSuchElement := false.
- accessLock critical:[
- retVal := super removeIdentical:anElement ifAbsent:[noSuchElement := true]
+ retVal := accessLock critical:[
+ super removeIdentical:anElement ifAbsent:[noSuchElement := true]
].
noSuchElement ifTrue:[
^ exceptionalValue value.
].
spaceAvailable signal.
^ retVal.
+
+ "Modified: / 22-02-2017 / 14:39:40 / stefan"
!
removeLast
@@ -165,12 +183,12 @@
|retVal|
dataAvailable wait.
- accessLock critical:[
- retVal := super removeLast.
- ].
+ retVal := accessLock critical:[super removeLast].
spaceAvailable signal.
^ retVal.
+
+ "Modified: / 22-02-2017 / 14:40:02 / stefan"
! !
!SharedQueue methodsFor:'accessing-internals'!
@@ -234,32 +252,34 @@
|retVal|
dataAvailable wait.
- accessLock critical:[
- retVal := super nextOrNil.
- ].
+ retVal := accessLock critical:[super nextOrNil].
spaceAvailable signal.
^ retVal.
+
+ "Modified: / 22-02-2017 / 14:40:32 / stefan"
!
-nextIfEmpty:exceptionValue
+nextIfEmpty:exceptionBlock
"return the next value in the queue; if it its empty do not wait, but return
- the value of exceptionValue.
+ the value of exceptionBlock.
When a datum has been removed, signal space-availability to writers"
- |retVal anyRemoved|
+ |retVal isEmpty|
- accessLock critical:[
- self isEmpty ifTrue:[
- retVal := exceptionValue value
- ] ifFalse:[
- retVal := super nextOrNil.
- anyRemoved := true.
+ retVal := accessLock critical:[
+ isEmpty := self isEmpty.
+ isEmpty ifFalse:[
+ super nextOrNil
].
].
- anyRemoved == true ifTrue:[spaceAvailable signal].
+ isEmpty ifTrue:[
+ ^ exceptionBlock value
+ ].
+ spaceAvailable signal.
+ ^ retVal.
- ^ retVal.
+ "Modified (comment): / 22-02-2017 / 15:29:30 / stefan"
!
nextOrNil
@@ -280,12 +300,12 @@
(dataAvailable waitWithTimeout:seconds) isNil ifTrue:[
^ nil
].
- accessLock critical:[
- retVal := super nextOrNil.
- ].
+ retVal := accessLock critical:[super nextOrNil].
spaceAvailable signal.
^ retVal.
+
+ "Modified: / 22-02-2017 / 14:35:09 / stefan"
!
peek
@@ -299,21 +319,23 @@
nextPut:anObject
"enter anObject to the end of the queue;
- do NOT wait for available space, if the queue is full; instead resize as required.
+ Wait for available space, if the queue is full.
After the put, signal availablity of a datum to readers."
self commonWriteWith:[self superNextPut:anObject].
- ^ self.
+
+ "Modified (comment): / 22-02-2017 / 15:18:36 / stefan"
!
nextPutFirst:anObject
"insert anObject at the beginning of the queue;
- do NOT wait for available space, if the queue is full, instead resize as required.
+ Wait for available space, if the queue is full.
After the put, signal availablity of a datum to readers.
Insertion at the beginning may be useful to add hi-prio elements (for example, in a job-scheduler)"
self commonWriteWith:[self superNextPutFirst:anObject].
- ^ self
+
+ "Modified (comment): / 22-02-2017 / 15:18:42 / stefan"
! !
!SharedQueue methodsFor:'enumerating'!
@@ -321,12 +343,17 @@
do:anObject
"evaluate the argument, aBlock for each element in the queue"
- |retVal|
+ ^ accessLock critical:[super do:anObject].
+
+ "Modified: / 22-02-2017 / 14:54:45 / stefan"
+!
- accessLock critical:[
- retVal := super do:anObject.
- ].
- ^ retVal.
+reverseDo:anObject
+ "evaluate the argument, aBlock for each element in the queue"
+
+ ^ accessLock critical:[super reverseDo:anObject].
+
+ "Created: / 22-02-2017 / 14:54:22 / stefan"
! !
!SharedQueue methodsFor:'initialization'!
@@ -348,7 +375,7 @@
commonWriteWith:aBlock
"common code for nextPut / nextPutFirst;
- do NOT wait for available space, if the queue is full; instead resize as required.
+ wait for available space, if the queue is full.
After the put, signal availablity of a datum to readers."
spaceAvailable wait.
@@ -356,6 +383,8 @@
aBlock value.
dataAvailable signal.
].
+
+ "Modified (comment): / 22-02-2017 / 15:18:03 / stefan"
! !
!SharedQueue class methodsFor:'documentation'!