[concurrency-interest] Volatile stores in constructors, disallowed to see the default value

Oleksandr Otenko oleksandr.otenko at oracle.com
Wed Nov 27 09:08:16 EST 2013

Yes, the elimination of barriers between multiple stores is clear. I am 
only saying that here:

r2=new A();

we know store(a,r2) can go ahead of store(r2.f,42), even if 
store(r2.f,42) is a volatile store - which makes a.f==0 observable.

Adding final field semantics makes sense, I was only commenting on 
Alexey's proof. It wasn't particularly clear whether he was saying "this 
is how we want it to behave" or "look, this is how it already behaves". 
His statement about "we can't always use reordering logic" made it vague 
- did he mean there is a JLS statement that already precludes that 
"obvious" reordering.


On 27/11/2013 13:49, Doug Lea wrote:
> On 11/27/2013 08:13 AM, Oleksandr Otenko wrote:
>> If JLS already mentions that constructors with volatile stores in 
>> them are
>> treated differently, then there is no ambiguity already.
> It doesn't, but there are common cases where the effects can be
> made identical to how final fields are treated (which is
> convenient for JVMs).
> Using only the simple cookbook approximations to JMM rules,
> including that you cannot ever reorder volatile stores with
> any other stores, plus the basic weakening transformations
> (see "Removing Barriers" section in 
> http://gee.cs.oswego.edu/dl/jmm/cookbook.html), you can reason as 
> follows for the example:
> class C {
>   volatile int x, y;
>   C(int a, int b) { x = a; y = b; }
> }
> There are no previous accesses of x or y.
> And none except for the stores within the constructor.
> And none until after the second store,
> which cannot be reordered wrt other stores.
> In this case, you don't need any fencing between the stores
> to x and y. But you still need to prevent store reorderings
> by the caller of the constructor, which can be done
> in the same way it is done for classes with final fields:
> placing a store fence at end of constructor.
> -Doug
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest

More information about the Concurrency-interest mailing list