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

Igor Stasenko siguctua at gmail.com
Tue Oct 12 09:14:24 EDT 2010

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.

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.

More information about the Pharo-dev mailing list