'From etoys3.0 of 19 February 2008 [latest update: #2009] on 9 June 2008 at 7:48:22 pm'! "Change Set: fixSharedQueueAgain-bf Date: 9 June 2008 Author: Bert Freudenberg Fix SharedQueue>>nextOrNilSuchThat: to not throw away non-matching elements, and make #nextOrNil signal correctly"! !SharedQueue methodsFor: 'accessing' stamp: 'bf 6/9/2008 19:42'! nextOrNil "Answer the object that was sent through the receiver first and has not yet been received by anyone. If no object has been sent, answer ." | value | accessProtect critical: [ readPosition >= writePosition ifTrue: [ value := nil ] ifFalse: [ value := contentsArray at: readPosition. contentsArray at: readPosition put: nil. readPosition := readPosition + 1. readSynch wait. ]. ]. ^value! ! !SharedQueue methodsFor: 'accessing' stamp: 'bf 6/9/2008 19:47'! nextOrNilSuchThat: aBlock "Answer the next object that satisfies aBlock, or nil otherwise. Leave other enqueued objects in place." | i each | accessProtect critical: [ i := readPosition. [i < writePosition] whileTrue: [ each := contentsArray at: i. (aBlock value: each) ifTrue: [ [i > readPosition] whileTrue: [ contentsArray at: i put: (contentsArray at: i-1). i := i - 1]. contentsArray at: readPosition put: nil. readPosition := readPosition + 1. readSynch wait. ^each]. i := i + 1. ]. ]. ^nil "=== | q c v | q := SharedQueue new. 1 to: 10 do: [ :i | q nextPut: i]. c := OrderedCollection new. [ v := q nextOrNilSuchThat: [ :e | e odd]. v notNil ] whileTrue: [ c add: {v. q size} ]. {c. q} ==="! !