[squeak-dev] Re: [Pharo-project] How about atomic value-swap bytecode?

Igor Stasenko siguctua at gmail.com
Wed Oct 13 22:11:27 UTC 2010


>
> Lets get back to my LIFO queue, its seems i found how to make it right
> using ASWP:
>
> Populating:
>
> | temp |
> temp := newItem := QueueItem new.
> newItem next: newItem. "note this"
> queueHead :=: newItem.
> temp next: newItem.
>
>
> Flushing and processing the queue:
>
> | oldHead |
> oldHead := nil.
> queueHead :=: oldHead.
>
> [ oldHead notNil ] whileTrue: [
>  oldHead next == oldHead ifFalse: [
>    "process the queue item here"
>  oldHead := oldHead next. ]
> ]
>

Popping just one item:

| head next rest |

 "DummyQueueItem same as QueueItem, but carrying no operation"
 rest := head := DummyQueueItem new.

 "same trick, point to itself "
 head next: head.

 head :=: queueHead.   "swap"

 head ifNil: [ rest next: nil. ^ self ]. "okay, queue was empty"

"spin, to make sure we're not in the middle of populating/extracting an item"
 [ head next == head ] whileTrue.

 "skip over dummies"
 [ head notNil and: [ head isDummy ]] whileTrue: [ head := head next. ].

 head ifNil: [ rest next: nil. ^ self ]. "okay, there was just dummies"

 rest next: head next.  "append queue tail back to head"

 self processSingleItem: head


So, in summary:
 - population requires no spin loops.
 - extraction requires 1 spin loop.

Of course it may be incorrect. Please check.

And i am really interested to compare it with implementation which
using CAS instead.

-- 
Best regards,
Igor Stasenko AKA sig.



More information about the Squeak-dev mailing list