Adding @atomic operations to Java
How might atomic operations work in Java, and is there a current alternative in OpenJDK/Hotspot it could translate to.
FeedbackIn my previous article on Making operations on volatile fields atomic. it was pointed out a few times that "fixing" previous behaviour is unlikely to go ahead regardless of good intentions.
An alternative to this is to add an @atomic annotation. This has the advantage of only applying to new code and not risk breaking old code.
Note: The use of a lower case name is intentional as it *doesn't* follow current coding conventions.
Any field listed with an @atomic would make the whole expression atomic. Variables which are non-volatile and non-atomic could be read at the start, or set after the completion of the expression. The expression itself may require locking on some platforms, CAS operations or TSX depending on the CPU technology.
If fields are only read, or only one is written too, this would be the same as volatile.
Currently the AtomicBoolean uses 4 bytes, plus an object header, with possible padding (as well as a reference) If the field was inlined it could look like this
@atomic boolean flag;
// toggle the flag.
this.flag = !this.flag;
But how would it work? Not all platforms support 1 byte atomic operations e.g. Unsafe does have a 1 byte CAS operations. This can be done with masking.
// possible replacement.
int num = Unsafe.getUnsafe().getVolatileInt(this, FLAG_OFFSET & ~3); // word align the access.
int value ^= 1 << ~(0xFF << (FLAG_OFFSET & 3) * 8) ;
if (Unsafe.getUnsafe().compareAndSwapInt(this, FLAG_OFFSET & ~3, num, value))
A type which is not supported is AtomicDouble, but this is a variation on AtomicLong. Consider this example.
@atomic double a = 1;
volatile double b = 2;
a += b;
How might it be implemented today?
double _b = Unsafe.getUnsafe().getVolatileDouble(this, B_OFFSET);
double _a = Unsafe.getUnsafe().getVolatileDouble(this, A_OFFSET);
long aAsLong = Double.doubleToRawLongBits(_a);
double _sum = _a + _b;
long sumAsLong = Double.doubleToRawLongBits(_a);
if (Unsafe.getUnsafe().compareAndSwapLong(this, A_OFFSET, aAsLong, sumAsLong))
Two Atomic Fields
Using Intel TSX, you can wrap a hardware transaction around a number of fields, but what if you don't have TSX, could it still be done, without resorting to a lock.
@atomic int a = 1, b = 2;
a += b * (b % 2 == 0 ? 2 : 1);
This can still be done with the CAS if the fields are together. There is a CAS2 operation planned to be able to check two 64-bit values. For now, this example will use two 4-byte values.
assert A_OFFSET + 4 == B_OFFSET;
long _ab = Unsafe.getUnsafe().getVolatileLong(this, A_OFFSET);
int _a = getLowerInt(_ab);
int _b = getHigherInt(_ab);
int _sum = _a + _b * (_b % 2 == 0 ? 2 : 1);
int _sum_ab = setLowerIntFor(_ab, _sum);
if (Unsafe.getUnsafe().compareAndSwapLong(this, A_OFFSET, _ab, _sum_ab))
Note: This operation can handle the change of either a, or b or both in an atomic way.
A common use case operations on immutable objects such as BigDecimal.
@atomic BigDecimal a;
a = a.add(b);
could be implemented this way on systems with CompressedOops or 32-bit JVMs.
BigDecimal _b = this.b;
BigDecimal _a = (BigDecimal) Unsafe.getUnsafe().getVolatileObject(this, A_OFFSET);
BigDecimal _sum = _a.add(_b);
if (Unsafe.getUnsafe().compareAndSwapLong(this, A_OFFSET, _a, _sum))
More complex examples
There will always be examples which are too complex for your platform. They might be fine on a System with TSX or HotSpot supported systems, however you need a fall back.
@atomic long a, b, c, d;
a = (b = (c = d + 4) + 5 ) + 6;
This is not support currently as it set multiple long values in one expression. However, a fall back could be to use the existing lock.
a = (b = (c = d + 4) + 5 ) + 6;
By adding an annotation, we could add atomic operations to regular fields, without the need to change the syntax. This would be a natural extension to the language without breaking backward comparability.