JDK-7023898 : Intrinsify AtomicLongFieldUpdater.getAndIncrement()
  • Type: Enhancement
  • Component: hotspot
  • Sub-Component: compiler
  • Affected Version: hs21
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: x86
  • Submitted: 2011-03-02
  • Updated: 2021-08-26
  • Resolved: 2012-09-21
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 7 JDK 8 Other
7u40Fixed 8Fixed hs24Fixed
Related Reports
Relates :  
Relates :  
Relates :  
Relates :  
Description
java.util.concurrent.atomic/Atomic*.getAndIncrement() and getAndAdd() are currently
implemented as CAS loops.  On x86/x64, these could be intrinsified into lock:xadd.
The latter is far less heavy-weight than cmpxchg.
After discussions with Doug Lea and Dave Dice, we should extend Unsafe a bit to provide intrinsifiable operations to take advantage of xadd and xchg.  Here's the rough interface addition.  These are slightly different than normal Unsafe since they start with Java implementations which are always valid.  They can be replaced with intrinsics if the platform provides a fast implementation.

   /**
    * Atomically update Java variable by <tt>delta</tt> returning
    * the previous value.
    * @return the previous value
    */
   public int getAndAddInt(Object o, long offset, int delta) {
       for (;;) {
           int current = getInt(o, offset);
           int next = current + delta;
           if (compareAndSwapInt(o, offset, current, next)) {
               return current;
           }
       }
   }

   /**
    * Atomically update Java variable by <tt>delta</tt> returning
    * the previous value.
    * @return the previous value
    */
   public long getAndAddLong(Object o, long offset, long delta) {
       for (;;) {
           long current = getLongVolatile(o, offset);
           long next = current + delta;
           if (compareAndSwapLong(o, offset, current, next)) {
               return current;
           }
       }
   }

   /**
    * Atomically sets the field of the given object at the given offset
    * to the given value and returns the old value.
    *
    * @param o An object whose field to get and set
    * @param newValue the new value
    * @return the previous value
    */
   public int getAndSet(Object o, long offset, int newValue) {
       for (;;) {
           int current = getInt(o, offset);
           if (compareAndSwapInt(o, offset, current, newValue)) {
               return current;
           }
       }
   }
   /**
    * Atomically sets the field of the given object at the given offset
    * to the given value and returns the old value.
    *
    * @param o An object whose field to get and set
    * @param newValue the new value
    * @return the previous value
    */
   public long getAndSet(Object o, long offset, long newValue) {
       for (;;) {
           long current = getLongVolatile(o, offset);
           if (compareAndSwapLong(o, offset, current, newValue)) {
               return current;
           }
       }
   }
   /**
    * Atomically sets the field of the given object at the given offset
    * to the given value and returns the old value.
    *
    * @param o An object whose field to get and set
    * @param newValue the new value
    * @return the previous value
    */
   public Object getAndSet(Object o, long offset, Object newValue) {
       for (;;) {
           Object current = getObject(o, offset);
           if (compareAndSwapObject(o, offset, current, newValue)) {
               return current;
           }
       }
   }

Comments
David, Roland, I had filed JDK-8004330 for this.
03-12-2012

David: no CR was created as far as I know.
03-12-2012

We seem to have lost the actual Unsafe.java changes that this work supports. Was CR/RFE for that ever created?
25-11-2012

URL: http://hg.openjdk.java.net/hsx/hotspot-main/hotspot/rev/7eca5de9e0b6 User: kvn Date: 2012-09-28 19:34:46 +0000
28-09-2012

EVALUATION http://hg.openjdk.java.net/hsx/hotspot-comp/hotspot/rev/7eca5de9e0b6
20-09-2012

EVALUATION See description.
20-03-2012

PUBLIC COMMENTS See http://blogs.oracle.com/dave/entry/atomic_fetch_and_add_vs
13-06-2011