changeset 14068:a9c330f8a4cd

Atomic array value stores
author dsimms
date Thu, 21 Jan 2016 13:41:43 +0100
parents 8b42a880b426
children d1b18cbc666d
files src/java.base/share/classes/java/util/Arrays.java src/java.base/share/classes/jdk/internal/misc/Unsafe.java src/java.base/share/classes/sun/misc/Unsafe.java
diffstat 3 files changed, 46 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/share/classes/java/util/Arrays.java	Mon Dec 07 13:03:39 2015 -0500
+++ b/src/java.base/share/classes/java/util/Arrays.java	Thu Jan 21 13:41:43 2016 +0100
@@ -26,6 +26,7 @@
 package java.util;
 
 import jdk.internal.HotSpotIntrinsicCandidate;
+import jdk.internal.misc.Unsafe;
 
 import java.lang.reflect.Array;
 import java.util.concurrent.ForkJoinPool;
@@ -8883,4 +8884,40 @@
 
         return aLength != bLength ? length : -1;
     }
-}
\ No newline at end of file
+
+
+    // Valhalla prototype hacks...
+    private static final Unsafe unsafe = Unsafe.getUnsafe();
+
+    /**
+     * Query whether the given array supports atomic access.
+     *
+     * Since value type elements can be larger than the plaform supported
+     * atomic load/store size and array access is by default <i>"relaxed"</i>
+     * for types larger than <code>int</code>.
+     *
+     * @param array An array.
+     * @return true if setting or getting elements are preformed atomically.
+     */
+    public static /*<any T>*/ boolean isAccessAtomic(/*T[]*/Object array) {
+        return unsafe.isArrayAccessAtomic(array);
+    }
+
+    /**
+     * Enable atomic array access.
+     *
+     * Ensures operations which read from or written elements to the array
+     * may never interleave with other threads.
+     *
+     * Note: this may hurt performance.
+     *
+     * @param array An array.
+     * @return an array, whose set or get elements will never interleave with
+     *         other threads. Possibly a copy of the given <code>array</code>,
+     *         possible the same array.
+     */
+    public static /*<any T> T[]*/ Object ensureAtomicAccess(/*T[]*/Object array) {
+        return /*(T[])*/ unsafe.ensureArrayAtomicAccess(array);
+    }
+
+}
--- a/src/java.base/share/classes/jdk/internal/misc/Unsafe.java	Mon Dec 07 13:03:39 2015 -0500
+++ b/src/java.base/share/classes/jdk/internal/misc/Unsafe.java	Thu Jan 21 13:41:43 2016 +0100
@@ -1390,4 +1390,9 @@
     private static short convEndian(boolean big, short n) { return big == BE ? n : Short.reverseBytes(n)    ; }
     private static int convEndian(boolean big, int n)     { return big == BE ? n : Integer.reverseBytes(n)  ; }
     private static long convEndian(boolean big, long n)   { return big == BE ? n : Long.reverseBytes(n)     ; }
+
+    // Valhalla prototype hacks...
+
+    public final native boolean isArrayAccessAtomic(Object array);
+    public final native Object ensureArrayAtomicAccess(Object array);
 }
--- a/src/java.base/share/classes/sun/misc/Unsafe.java	Mon Dec 07 13:03:39 2015 -0500
+++ b/src/java.base/share/classes/sun/misc/Unsafe.java	Thu Jan 21 13:41:43 2016 +0100
@@ -1043,4 +1043,7 @@
     private native boolean unalignedAccess0();
     private native boolean isBigEndian0();
 
+    // Valhalla prototype hacks...
+    public final native boolean isArrayAccessAtomic(Object array);
+    public final native Object ensureArrayAtomicAccess(Object array);
 }