Skip to content

Instantly share code, notes, and snippets.

@ashkrit
Created March 14, 2014 17:19
Show Gist options
  • Save ashkrit/9552435 to your computer and use it in GitHub Desktop.
Save ashkrit/9552435 to your computer and use it in GitHub Desktop.
public boolean increment() {
long orignalValue = readVolatile(startAddress);
long value = convert(orignalValue);
return UnsafeUtils.unsafe.compareAndSwapLong(null,
startAddress,orignalValue, convert(value + 1));
}
public long get() {
long orignalValue = readVolatile(startAddress);
return convert(orignalValue);
}
// Only unaligned is implemented
private static long readVolatile(long position) {
if (UnsafeUtils.unaligned()) {
return UnsafeUtils.unsafe.getLongVolatile(null, position);
}
throw new UnsupportedOperationException();
}
private static long convert(long a) {
if (UnsafeUtils.unaligned()) {
return (UnsafeUtils.nativeByteOrder ? a : Long.reverseBytes(a));
}
throw new UnsupportedOperationException();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment