[Pharo-project] How about atomic value-swap bytecode?

Levente Uzonyi leves at elte.hu
Tue Oct 12 09:51:13 EDT 2010


On Tue, 12 Oct 2010, Igor Stasenko wrote:

> Hello, i just thought, that it would be cool to have a special bytecode,
> which guarantees atomicity for swapping values between two variables.
>
> To swap two values, you usually do:
>
> | var1 var2 temp |
>
> temp := var1.
> var1 := var2.
> var2 := temp.
>
> But since its non-atomic, a process can be interrupted and such operation
> is not thread-safe.
>
> In order to make it thread safe, you must add even more boilerplate:
>
> | var1 var2 temp |
>
> semaphore critical: [
>  temp := var1.
>  var1 := var2.
>  var2 := temp.
> ]

An alternative solution:

| a b |
a := 1.
b := 2.
[
 	| tmp |
 	tmp := a.
 	a := b.
 	b := tmp ] valueUnpreemptively


Levente

>
> So, i thought , if we could have a special bytecode, then we could
> write something like:
>
> | var1 var2 |
>
> var1 :=: var2.
>
> So, VM will guarantee that values of var1 and var2 will be swapped
> atomically, and completely thread-safe.
>
> var1 and var2 could be either temps or instance variables, but of
> course not arguments, since they are not assignable.
>
> One thing i don't like, that it will need to introduce a new syntax
> for atomic-value-swap operator -  :=:
>
> Or, maybe just reserve a special keyword selector (which is recognized
> by compiler), so
>
> var1 __atomicSwapWith: var2
>
> so it will look like a regular smalltalk syntax, except that its not a
> message send, but a low-level operation instead.
>
>
> -- 
> Best regards,
> Igor Stasenko AKA sig.
>
> _______________________________________________
> Pharo-project mailing list
> Pharo-project at lists.gforge.inria.fr
> http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
>




More information about the Pharo-dev mailing list