[BUG][FIX] SharedQueue flushing

Nathanael Schärli n.schaerli at gmx.net
Tue Jun 18 09:17:53 UTC 2002


Hey

I have noticed that there are bugs in the methods SharedQueue>>flush and
SharedQueue>>flushAllSuchThat:. The problem is that both of them do not
properly adjust the semaphore "readSynch" which is used to block readers
if the queue is empty. Look at the following examples:

q _ SharedQueue new.
q nextPut: 100.
q flush.
q next.

This example should block the active process because there is currently
no element in the queue. Instead, you get an error message ("Error in
SharedQueue synchronization").

q _ SharedQueue new.
q nextPut: 100.
q nextPut: 200.
q flushAllSuchThat: [:each | each > 150].
q next.
q next.

Also this example should block the active process because there is
currently no element in the queue. Instead, you get the same error
message ("Error in SharedQueue synchronization").


I have attached a little changeset that fixes this problem.

Cheers,
Nathanael
-------------- next part --------------
'From Squeak3.2gamma of 15 January 2002 [latest update: #4857] on 18 June 2002 at 11:15:55 am'!
"Change Set:		FixSharedQueue
Date:			18 June 2002
Author:			Nathanael Scharli

This fixes two bugs associated with (partially) flushing a SharedQueue. The problem was that the sempahore that is used to block a reader when the queue is empty did not get adjusted right."!


!SharedQueue methodsFor: 'accessing' stamp: 'NS 6/18/2002 11:04'!
flush
	"Throw out all pending contents"
	accessProtect critical: [
		readPosition _ 1.
		writePosition _ 1.
		"Reset the read synchronization semaphore"
		readSynch initSignals].! !

!SharedQueue methodsFor: 'accessing' stamp: 'NS 6/18/2002 11:15'!
flushAllSuchThat: aBlock
	"Remove from the queue all objects that satisfy aBlock."
	| value newReadPos |
	accessProtect critical: [
		newReadPos _ writePosition.
		writePosition-1 to: readPosition by: -1 do:
			[:i | value _ contentsArray at: i.
			contentsArray at: i put: nil.
			(aBlock value: value) ifTrue: [
				"We take an element out of the queue, and therefore, we need to decrement 
				the readSynch signals"
				readSynch wait.
			] ifFalse: [
				newReadPos _ newReadPos - 1.
				contentsArray at: newReadPos put: value]].
		readPosition _ newReadPos].
	^value
! !


More information about the Squeak-dev mailing list