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

Bert Freudenberg bert at freudenbergs.de
Tue Oct 12 21:35:35 UTC 2010


On 12.10.2010, at 14:25, Lawson English wrote:

> On 10/12/10 1:51 PM, Andreas Raab wrote:
>> 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.
>> 
>> 
> So code within blocks methods is thread safe but code that accesses data outside of the block/method's scope can't be sure that the external vars are consistent?

No. Whenever you send a message, your process can be interrupted. Has nothing to do with blocks.

In addition to sends, a loop can be interrupted, too, at its end (when the instruction pointer is decreased, a.k.a. backwards branching).

As Stefan pointed out, it is unwise to rely on this in application code (it is okay in very low-level code like the scheduling/process implementation itself).

- Bert -





More information about the Squeak-dev mailing list