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

Andreas Raab andreas.raab at gmx.de
Tue Oct 12 20:51:56 UTC 2010


On 10/12/2010 6:14 AM, 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.

Actually, this is thread-safe. Process switches happen only on sends and 
backwards branches.

Cheers,
   - Andreas


>
> In order to make it thread safe, you must add even more boilerplate:
>
> | var1 var2 temp |
>
> semaphore critical: [
>    temp := var1.
>    var1 := var2.
>    var2 := temp.
> ]
>
> 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.
>
>




More information about the Squeak-dev mailing list