[jmm-dev] bitwise RMW operators, specifically testAndSetBit/BTS

Andrew Haley aph at redhat.com
Mon Jul 25 13:50:36 UTC 2016

On 25/07/16 14:24, Doug Lea wrote:
> On 07/25/2016 04:54 AM, Andrew Haley wrote:
>> On 20/07/16 20:16, Doug Lea wrote:
>>> Or, in pseudo-VarHandle style using "getM" (for varying Ms):
>>> static VarHandle PX = MethodHandles.lookup().findVarHandle(Point.class, "x",
>>> int.class);
>>> void f(Point a, Point b) {
>>>    int r1 = PX.getM(a);
>>>    int r2 = PX.getM(b);
>>>    int r3 = PX.getM(a); // *
>>>    use (r1, r2, r3);
>>> }
>>> Can you simplify (*) to "r3 = r1" ? It depends on M:
>>> * Java-Plain and C++-Plain: yes.
>>> * Java Opaque: no.
>>> * C++-Relaxed: only if a != b.
>>> * (And, for the record, other modes: no)
>>> This is one reason "opaque" mode is needed.
>> But the processor hardware is allowed to simplify (*) to "r3 = r1" even
>> if Opaque is used.  So, again, why does it matter?
> The existence of one case where it may not matter doesn't mean
> that it is always OK (consider loops), and so the best practical
> answer for compilers is "no" here.

Well, OK, but I'm trying to think of one case where a Java program
could tell the difference between the two, and I'm coming up empty.
One could argue (and I would argue) that if it's not possible to write
such a test case then perhaps such a thing doesn't belong in a
language specification.

> Coming up with a formal model and spec that clearly delineates legal
> transformations hits a lot of "little" issues along these lines.

It sure does!


More information about the jmm-dev mailing list