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

Doug Lea dl at cs.oswego.edu
Wed Jul 20 12:49:14 UTC 2016

On 07/20/2016 04:25 AM, Paul Sandoz wrote:

>> C++ relaxed atomics are (perhaps!) stronger than "plain" in two senses: truly atomic (!) and single-memory-location-sequentially-consistent.
> Yes, it’s the latter that seems harder to apply.

To illustrate the main consequence (also showing how Java-Plain vs C++relaxed
differences are so small and subtle), in C++-relaxed, compilers cannot perform
some forms of  common subexpression elimination in the presence of possible
aliasing, but for Java-plain (and C++-plain), they can.  As in:

class Point ( int x, y; }

void f(Point a, Point b) {
   int r1 = a.x;
   int r2 = b.x;
   int r3 = a.x; // simplify to: int r3 = r1 ?
   use (r1, r2, r3);

If the accesses were C++-relaxed, then the transformation could not be applied
if a and b are the same point because the r3 read might be older than r2 if
some other thread wrote between the reads. But C++-plain and Java-Plain both
allow this to be done anyway. Intuitively, because the per view (a vs b)
reads are "coherent", which is spec'ed as OK even though the per-location
rule need not hold.

(Mostly unrelatedly, note that if a and b were known to be aliased, then you
could apply this transformation if you first simplified the "r2 = b.x" to
"r2 = r1".)


More information about the jmm-dev mailing list