Created
March 14, 2014 17:19
-
-
Save ashkrit/9552435 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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