JLS and Fence Intrinsics

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

JLS and Fence Intrinsics

vikas
I am trying to write a very basic blog entry on JEP 171: Fence Intrinsics.
http://ipenneddown.blogspot.com/2015/04/case-of-fence-intrinsics.html.
So i am trying to verify if my understanding of the subject is ok.

From JLS Example below


T1

r1 = X;
if(r1 != 0) {
  Y = 1;
}
-----------------------

T2
r2 = Y;
if(r2 != 0) {
   X = 1;
}

 x == 1 and y == 1 is possible because values may appear out of thin air
It gives an example of how happens before consistency is not sufficient.
If we try to fix the above using Intrinsic Fences api then will below code work?


T1

r1 = X;
if(r1 != 0) {
  U.storeFence();
  Y = 1;
}
-----------------------

T2
r2 = Y;
if(r2 != 0) {
   U.storeFence();
   X = 1;
}

So now X == 1 and Y == 1 is not allowed, is this right?.


Similarly another example from JLS  Behavior allowed by happens-before consistency, but not sequential consistency. 

T1

B = 1;
r2 = A;
-----------------------

T2
A = 2;
r1 = B;

So  r1 == 0 and r2 == 0 is possible (Not Sequential Consistent).
Using Fences API can we try something like this

T1

B = 1;
U.fullFence();
r2 = A;
-----------------------

T2
A = 2;
U.fullFence();
r1 = B;

U.fullFence() will provide the necessary StoreLoad barrier and hence r1 == 0 and r2 == 0 is not possible.
And hence the above code will work.

Do my both assumptions are fine?