[jmm-dev] weakCompareAndSet memory semantics

Andrew Haley aph at redhat.com
Thu Apr 21 07:52:44 UTC 2016


On 21/04/16 00:10, David Holmes wrote:
> On 21/04/2016 8:59 AM, Justin Sampson wrote:
>> David Holmes wrote:
>>
>>> Side question: why would you replace compareAndSet with
>>> weakCompareAndSet when that would expose those ll/sc spurious
>>> failures?
>>
>> The spurious failures are irrelevant because the whole thing is
>> wrapped in a retry loop anyway.
> 
> And in the compareAndSet the spurious failures never escape because it 
> also has a retry loop. So my question stands: why would you switch to 
> weakCompareAndSet in "busy loops" and how would this be a benefit?

Say you're incrementing a heavily-contended counter.  There's no point
spinning in a loop if the store fails or if there is a spurious failure.

You want to do this:

loop:
    ldax x0, [addr]
    add x0, x0, #1
    stlx status, x0, [addr]
    cbnz status, loop

You can simulate this with weakCompareAndSet.

Andrew.


More information about the jmm-dev mailing list