unsafe.compareAndSwapInt first argument menanig

466 Views Asked by At

I am investigating java.util.concurrent.locks.AbstractQueuedSynchronizersource code.

From severals place invokes compareAndSetState method.

/**
 * Atomically sets synchronization state to the given updated
 * value if the current state value equals the expected value.
 * This operation has memory semantics of a {@code volatile} read
 * and write.
 *
 * @param expect the expected value
 * @param update the new value
 * @return {@code true} if successful. False return indicates that the actual
 *         value was not equal to the expected value.
 */
protected final boolean compareAndSetState(int expect, int update) {
    // See below for intrinsics setup to support this
    return unsafe.compareAndSwapInt(this, stateOffset, expect, update);
}

Parameters expect and update are obvious and correspond the atomics parameters. But this is Object(rather than int).
How does this compares with expect?

1

There are 1 best solutions below

4
On

It's the instance whose field state will be CAS-ed. The value is stored in that field. The instance-offset pair is a way to translate the field descriptor to a memory address, just like you need to supply the instance when using Field::set, Field::get or Method::invoke.

Btw, these sun classes' source is available online at openjdk's mercury repository.