Adding @atomic operations to Java

Overview

How might atomic operations work in Java, and is there a current alternative in OpenJDK/Hotspot it could translate to.

Feedback

In 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.

Atomic operations

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.

Atomic Boolean

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.
while(true) {
     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))
           break;
}

Atomic Double

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?

while(true) {
    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))
        break;
}

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;
while(true) {
    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))
        break;
}

Note: This operation can handle the change of either a, or b or both in an atomic way.

AtomicReferences

A common use case operations on immutable objects such as BigDecimal.

 @atomic BigDecimal a;
BigDecimal b;

a = a.add(b);

could be implemented this way on systems with CompressedOops or 32-bit JVMs.

BigDecimal _b = this.b;
while(true) {
    BigDecimal _a = (BigDecimal) Unsafe.getUnsafe().getVolatileObject(this, A_OFFSET);
    BigDecimal _sum = _a.add(_b);
    if (Unsafe.getUnsafe().compareAndSwapLong(this, A_OFFSET, _a, _sum))
        break;
}

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.

 synchronized(this) {
    a = (b = (c = d + 4) +  5 ) + 6;
}

Conclusion

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.

Reference: Adding @atomic operations to Java from our JCG partner Peter Lawrey at the Vanilla Java blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

Leave a Reply


7 − seven =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close