On 6 December 2012 16:15, Igor Stasenko <siguctua@gmail.com> wrote:
On 6 December 2012 16:12, Igor Stasenko <siguctua@gmail.com> wrote:
this is not a bug, but limitation. delay object(s) are not reentrant: means that you shall not use same delay instance multiple times, i.e.
delay := 1 seconds asDelay. 10 timesRepeat: [ [ delay wait ] fork ].
instead you shall create a fresh instance for each "delay" intent:
10 timesRepeat: [ [ 1 seconds asDelay wait ] fork ].
if Delay could implement #copy, then you could write like:
delay := 1 seconds asDelay. 10 timesRepeat: [ [ delay copy wait ] fork ].
this is because a Delay has 1:1 correspondence with its semaphore object. and when you scheduling it multiple times, it will end up using same semaphore for all instances of it... and that's why you get weird behavior.
err.. sorry.. not for "all instances".. since there's only one instance.. so single semaphore will be used to schedule multiple delays (but using single Delay object). and when that semaphore get signaled.. you'll get strange results . if you want to fix that, then it will require disassociating delay object from its semaphore.. the delay will no longer need to hold a 'semaphore' directly by itself but then without it, it will not require to hold any more state than simple Duration. so, to do it right, we will need to make quite some changes in delay scheduling code.. basicaly we will need to create a private object, which will hold 'semaphore + delay object ' pair and put that object in schedule. which at the end will just make same as following: delay := 1 seconds. 10 timesRepeat: [ [ delay asDelay wait ] fork ]. so, to make you happy, just add #wait protocol to Duration: wait ^ self asDelay wait so you can write: delay := 1 seconds. 10 timesRepeat: [ [ delay wait ] fork ]. end of story :) -- Best regards, Igor Stasenko.