Unsafe compareAnd*

Paul Sandoz paul.sandoz at oracle.com
Thu Nov 17 18:48:50 UTC 2016


Hi Trevor,

The compareAndSwapShort (non-instrinsic) implementation defers to the compareAndExchangeShortVolatile implementation, and the weakCompareAndSwapShortVolatile implementation defers to (the stronger) compareAndSwapShort implementation [*]:

i.e. weakCompareAndSwapShortVolatile -> compareAndSwapShort -> compareAndExchangeShortVolatile

@HotSpotIntrinsicCandidate
public final short compareAndExchangeShortVolatile(Object o, long offset,
                                         short expected,
                                         short x) {
    if ((offset & 3) == 3) {
        throw new IllegalArgumentException("Update spans the word, not supported");
    }

    …
}

@HotSpotIntrinsicCandidate
public final boolean compareAndSwapShort(Object o, long offset,
                                         short expected,
                                         short x) {
    return compareAndExchangeShortVolatile(o, offset, expected, x) == expected;
}

@HotSpotIntrinsicCandidate
public final boolean weakCompareAndSwapShortVolatile(Object o, long offset,
                                                     short expected,
                                                     short x) {
    return compareAndSwapShort(o, offset, expected, x);
}

I think that explains why you are observing failing Unsafe.weakCompareAndSwapShort() tests.

Paul.

[*] Note, we really need to change the names here to be consistent with the schema on VarHandles


> On 17 Nov 2016, at 09:29, Trevor Watson <trevor.d.watson at oracle.com> wrote:
> 
> I'm working on an implementation of the C2 code for compareAndExchangeShort on SPARC.
> 
> I've only implemented this function so far, and no compareAndSwapShort equivalent.
> 
> When I run the test in hotspot/test/compiler/unsafe/JdkInternalMiscUnsafeAccessTestShort.java it fails because Unsafe.compareAndSwapShort() returns an incorrect value. This test passes without my implementation of compareAndExchangeShort.
> 
> If I comment out the Unsafe.compareAndSwapShort() tests, the Unsafe.compareAndExchangeShort tests run successfully but the Unsafe.weakCompareAndSwapShort() tests subsequently fail.
> 
> Can anyone tell me why it might be that an implementation for CompareAndExchangeS would trigger a failure in Unsafe.compareAndSwapShort()?
> 
> Thanks,
> Trevor



More information about the hotspot-dev mailing list