I've seen the argument that the reson why it is named "weak", is because the "strong" version has happens-before "orderings" behavior.
In my understanding "Happens before" is a term that denotes memory consistency, NOT sequential execution OR strong memory ordering, it always confuses me since people talk interchangeably about these things.
If you think deeply about reordering and how such an interaction would relate to the LOCK prefix, it simply makes little sense.
There are degrees of reordering, one that happens at compile and the other at runtime. If some sort of mechanism would kick at the LOCK prefix... How would that look like?... a runtime inlining of the spinlock while fencing the entire while loop for each of the concurrent lines of code accessing it??
My understanding of what the Happens-Before behavior entails, is that it ENSURES to tell you what happened before your current action... BUT this current action MAY OR MAY NOT follow a sequential consistency of execution among other concurrent actions AND the code the actions is comprised of may have been reordered prior to such execution... meaning that race conditions still apply and... NONE OF THIS INVOLVES reordering.
If we Have 2 twin threads: same code, same shared memories, their code may been reordered FROM THE START, even before reaching the atomic pressure point, and a strong compareAndSet
will not prevent this reordering(?), it will simply ensure the happens-before as in:
"A losing Thread can ATTEST that this was the memory's state (witness) before it arrived"-type of Happens-Before... the only type of happens-before.
This is NOT reordering; this is NOT sequential consistency of execution.
According to the LOCK prefix, once the instruction attempts a BUS entrance and sees it as being locked, it will NOT re-attempt to acquire it(?), it will simply escape with a failure(?).
If this is true, then the retry-acquire behavior IS NOT INTRINSIC to the LOCK prefix. and it becomes a responsibility of the higher level to attempt a retry.
If the LOCK + cmpxchg
combo applied by the Java's strong compareAndSet
ensures a "happens-before" it means that when the LOCK acquiring fails, a native spin will RETRY to acquire the lock, and only when acquired, it is able to stablish a mutually exclusive read and write coupling... the term (LL/SC) seems to be an abstraction describing the resulting behavior, but NOT the "HOW ITS ACHIEVED", and I haven't seen literature explaining it at depth.
But the Happens Before is NOT intrinsic to the cmpxchng
it is a consequence of the RETRY attempt spin that, without it, it would fail and escape to your side (the side of the programmer) with no witness, just a failure.
And so, could we say that "strong" CAS-sing is contentious, because it spins until the BUS is freed(??).
If we analyze the implication that weakCompareAndSet
may fail EVEN if the expected value matches, this would fit into this interpretation since, the instruction fails simply by encountering contention (a locked BUS), If the bus is locked it doesn't matter which state it's memory is or will be because it will not reattempt to acquire the lock, so if it's future memory state matches the expected value, it won't matter because what matters for the weak version is that it ENCOUNTERED CONTENTION.
And so, could we say that "weak" CASing is non-contentious, because it will return if the BUS is locked(??).
IMO the best evidence in favor of this argument is that EVERY part of the Java source code that uses the weakCompareAdnSet
is ALWAYS done on superficial spinlock situations that DO NOT read a constant value, like a true
, a false
, an enum
or constant... instead it is always done on a variable that is read via high-level volatile read, and which failure will always be followed up by a retry high-level spinlock.
This means that they ARE SURE that the value of the expected
obtained via high-level volatile read, can surely be dismissed when contention is met, so they escape to the superficial level (programmer's side) as soon as possible and reattempt... instead of natively spinning to acquire a LOCK on a cmpxchg
that will surely return a false on the comparison check.
Is my interpretation of this true? If you have some literature that I could read, it would be greatly appreciated, thanks.