[concurrency-interest] Can ordered store be reordered with subsequent volatile load?

Vladimir Ozerov ppozerov at gmail.com
Sat Mar 26 17:32:31 EDT 2016


Hi,

I am trying to wrap my head around the following problem. Consider we have
a kind of lightweight locking algorithm between two threads which somewhat
resembles classical Peterson's lock. One thread declares intent to acquire
a lock and then checks whether state of another thread allows it.

The question is: provided very subtle "putOrdered" semantics and no data
dependency between "a" and "b", do I have any guarantees that store and
load will not be reordered in the code below?

int a, b;

boolean tryLock() {
    UNSAFE.putOrdered(a, 1); // Declare intent.

    // No StoreLoad here as store is not volatile.

    if (UNSAFE.getVolatile(b) == 1)) {
        // Reset intent and return false;
    }

    return true;
}

My hope is that ordering could be enforced by the fact that both store and
load in this example are synchronization actions (see old comment from
Doug:
http://cs.oswego.edu/pipermail/concurrency-interest/2011-October/008324.html
).

Vladimir.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20160327/6b6ccfd7/attachment.html>


More information about the Concurrency-interest mailing list