8145485: Miscellaneous changes imported from jsr166 CVS 2016-02
authordl
Tue, 16 Feb 2016 09:52:49 -0800
changeset 35981 e3e89c0bb3d9
parent 35980 1fc60a70d779
child 35997 394957e542bc
8145485: Miscellaneous changes imported from jsr166 CVS 2016-02 Reviewed-by: martin, psandoz, chegar
jdk/src/java.base/share/classes/java/util/SplittableRandom.java
jdk/src/java.base/share/classes/java/util/concurrent/Exchanger.java
jdk/src/java.base/share/classes/java/util/concurrent/ForkJoinPool.java
jdk/src/java.base/share/classes/java/util/concurrent/ForkJoinTask.java
jdk/src/java.base/share/classes/java/util/concurrent/ThreadLocalRandom.java
jdk/src/java.base/share/classes/java/util/concurrent/atomic/Striped64.java
jdk/test/java/util/concurrent/ConcurrentQueues/RemovePollRace.java
jdk/test/java/util/concurrent/ExecutorCompletionService/ExecutorCompletionServiceLoops.java
--- a/jdk/src/java.base/share/classes/java/util/SplittableRandom.java	Tue Feb 16 09:49:14 2016 -0800
+++ b/jdk/src/java.base/share/classes/java/util/SplittableRandom.java	Tue Feb 16 09:52:49 2016 -0800
@@ -225,14 +225,13 @@
     private static final AtomicLong defaultGen = new AtomicLong(initialSeed());
 
     private static long initialSeed() {
-        String pp = java.security.AccessController.doPrivileged(
-                new sun.security.action.GetPropertyAction(
-                        "java.util.secureRandomSeed"));
-        if (pp != null && pp.equalsIgnoreCase("true")) {
+        java.security.PrivilegedAction<Boolean> action =
+            () -> Boolean.getBoolean("java.util.secureRandomSeed");
+        if (java.security.AccessController.doPrivileged(action)) {
             byte[] seedBytes = java.security.SecureRandom.getSeed(8);
-            long s = (long)(seedBytes[0]) & 0xffL;
+            long s = (long)seedBytes[0] & 0xffL;
             for (int i = 1; i < 8; ++i)
-                s = (s << 8) | ((long)(seedBytes[i]) & 0xffL);
+                s = (s << 8) | ((long)seedBytes[i] & 0xffL);
             return s;
         }
         return (mix64(System.currentTimeMillis()) ^
--- a/jdk/src/java.base/share/classes/java/util/concurrent/Exchanger.java	Tue Feb 16 09:49:14 2016 -0800
+++ b/jdk/src/java.base/share/classes/java/util/concurrent/Exchanger.java	Tue Feb 16 09:52:49 2016 -0800
@@ -155,8 +155,8 @@
      * a value that is enough for common platforms.  Additionally,
      * extra care elsewhere is taken to avoid other false/unintended
      * sharing and to enhance locality, including adding padding (via
-     * jdk.internal.vm.annotation.Contended) to Nodes, embedding "bound" as an Exchanger
-     * field, and reworking some park/unpark mechanics compared to
+     * @Contended) to Nodes, embedding "bound" as an Exchanger field,
+     * and reworking some park/unpark mechanics compared to
      * LockSupport versions.
      *
      * The arena starts out with only one used slot. We expand the
@@ -304,8 +304,7 @@
 
     /**
      * Nodes hold partially exchanged data, plus other per-thread
-     * bookkeeping. Padded via @jdk.internal.vm.annotation.Contended to reduce memory
-     * contention.
+     * bookkeeping. Padded via @Contended to reduce memory contention.
      */
     @jdk.internal.vm.annotation.Contended static final class Node {
         int index;              // Arena index
--- a/jdk/src/java.base/share/classes/java/util/concurrent/ForkJoinPool.java	Tue Feb 16 09:49:14 2016 -0800
+++ b/jdk/src/java.base/share/classes/java/util/concurrent/ForkJoinPool.java	Tue Feb 16 09:52:49 2016 -0800
@@ -818,8 +818,9 @@
         final ForkJoinPool pool;   // the containing pool (may be null)
         final ForkJoinWorkerThread owner; // owning thread or null if shared
         volatile Thread parker;    // == owner during call to park; else null
-        volatile ForkJoinTask<?> currentJoin;  // task being joined in awaitJoin
-        @jdk.internal.vm.annotation.Contended("group2") // separate from other fields
+        volatile ForkJoinTask<?> currentJoin; // task being joined in awaitJoin
+
+        @jdk.internal.vm.annotation.Contended("group2") // segregate
         volatile ForkJoinTask<?> currentSteal; // nonnull when running some task
 
         WorkQueue(ForkJoinPool pool, ForkJoinWorkerThread owner) {
--- a/jdk/src/java.base/share/classes/java/util/concurrent/ForkJoinTask.java	Tue Feb 16 09:49:14 2016 -0800
+++ b/jdk/src/java.base/share/classes/java/util/concurrent/ForkJoinTask.java	Tue Feb 16 09:52:49 2016 -0800
@@ -541,16 +541,16 @@
     }
 
     /**
-     * Returns a rethrowable exception for the given task, if
-     * available. To provide accurate stack traces, if the exception
-     * was not thrown by the current thread, we try to create a new
-     * exception of the same type as the one thrown, but with the
-     * recorded exception as its cause. If there is no such
-     * constructor, we instead try to use a no-arg constructor,
-     * followed by initCause, to the same effect. If none of these
-     * apply, or any fail due to other exceptions, we return the
-     * recorded exception, which is still correct, although it may
-     * contain a misleading stack trace.
+     * Returns a rethrowable exception for this task, if available.
+     * To provide accurate stack traces, if the exception was not
+     * thrown by the current thread, we try to create a new exception
+     * of the same type as the one thrown, but with the recorded
+     * exception as its cause. If there is no such constructor, we
+     * instead try to use a no-arg constructor, followed by initCause,
+     * to the same effect. If none of these apply, or any fail due to
+     * other exceptions, we return the recorded exception, which is
+     * still correct, although it may contain a misleading stack
+     * trace.
      *
      * @return the exception, or null if none
      */
@@ -572,26 +572,20 @@
         if (e == null || (ex = e.ex) == null)
             return null;
         if (e.thrower != Thread.currentThread().getId()) {
-            Class<? extends Throwable> ec = ex.getClass();
             try {
                 Constructor<?> noArgCtor = null;
-                Constructor<?>[] cs = ec.getConstructors();// public ctors only
-                for (int i = 0; i < cs.length; ++i) {
-                    Constructor<?> c = cs[i];
+                // public ctors only
+                for (Constructor<?> c : ex.getClass().getConstructors()) {
                     Class<?>[] ps = c.getParameterTypes();
                     if (ps.length == 0)
                         noArgCtor = c;
-                    else if (ps.length == 1 && ps[0] == Throwable.class) {
-                        Throwable wx = (Throwable)c.newInstance(ex);
-                        return (wx == null) ? ex : wx;
-                    }
+                    else if (ps.length == 1 && ps[0] == Throwable.class)
+                        return (Throwable)c.newInstance(ex);
                 }
                 if (noArgCtor != null) {
-                    Throwable wx = (Throwable)(noArgCtor.newInstance());
-                    if (wx != null) {
-                        wx.initCause(ex);
-                        return wx;
-                    }
+                    Throwable wx = (Throwable)noArgCtor.newInstance();
+                    wx.initCause(ex);
+                    return wx;
                 }
             } catch (Exception ignore) {
             }
--- a/jdk/src/java.base/share/classes/java/util/concurrent/ThreadLocalRandom.java	Tue Feb 16 09:49:14 2016 -0800
+++ b/jdk/src/java.base/share/classes/java/util/concurrent/ThreadLocalRandom.java	Tue Feb 16 09:52:49 2016 -0800
@@ -134,14 +134,13 @@
     private static final AtomicLong seeder = new AtomicLong(initialSeed());
 
     private static long initialSeed() {
-        String pp = java.security.AccessController.doPrivileged(
-                new sun.security.action.GetPropertyAction(
-                        "java.util.secureRandomSeed"));
-        if (pp != null && pp.equalsIgnoreCase("true")) {
+        java.security.PrivilegedAction<Boolean> action =
+            () -> Boolean.getBoolean("java.util.secureRandomSeed");
+        if (java.security.AccessController.doPrivileged(action)) {
             byte[] seedBytes = java.security.SecureRandom.getSeed(8);
-            long s = (long)(seedBytes[0]) & 0xffL;
+            long s = (long)seedBytes[0] & 0xffL;
             for (int i = 1; i < 8; ++i)
-                s = (s << 8) | ((long)(seedBytes[i]) & 0xffL);
+                s = (s << 8) | ((long)seedBytes[i] & 0xffL);
             return s;
         }
         return (mix64(System.currentTimeMillis()) ^
--- a/jdk/src/java.base/share/classes/java/util/concurrent/atomic/Striped64.java	Tue Feb 16 09:49:14 2016 -0800
+++ b/jdk/src/java.base/share/classes/java/util/concurrent/atomic/Striped64.java	Tue Feb 16 09:52:49 2016 -0800
@@ -55,13 +55,13 @@
      * accessed directly by subclasses.
      *
      * Table entries are of class Cell; a variant of AtomicLong padded
-     * (via @jdk.internal.vm.annotation.Contended) to reduce cache contention. Padding
-     * is overkill for most Atomics because they are usually
-     * irregularly scattered in memory and thus don't interfere much
-     * with each other. But Atomic objects residing in arrays will
-     * tend to be placed adjacent to each other, and so will most
-     * often share cache lines (with a huge negative performance
-     * impact) without this precaution.
+     * (via @Contended) to reduce cache contention. Padding is
+     * overkill for most Atomics because they are usually irregularly
+     * scattered in memory and thus don't interfere much with each
+     * other. But Atomic objects residing in arrays will tend to be
+     * placed adjacent to each other, and so will most often share
+     * cache lines (with a huge negative performance impact) without
+     * this precaution.
      *
      * In part because Cells are relatively large, we avoid creating
      * them until they are needed.  When there is no contention, all
--- a/jdk/test/java/util/concurrent/ConcurrentQueues/RemovePollRace.java	Tue Feb 16 09:49:14 2016 -0800
+++ b/jdk/test/java/util/concurrent/ConcurrentQueues/RemovePollRace.java	Tue Feb 16 09:52:49 2016 -0800
@@ -121,12 +121,16 @@
         final int SPINS = 5;
         final AtomicLong removes = new AtomicLong(0);
         final AtomicLong polls = new AtomicLong(0);
-        final int adderCount =
-            Math.max(1, Runtime.getRuntime().availableProcessors() / 4);
-        final int removerCount =
-            Math.max(1, Runtime.getRuntime().availableProcessors() / 4);
+
+        // We need at least 3 threads, but we don't want to use too
+        // many on massively multi-core systems.
+        final int cpus = Runtime.getRuntime().availableProcessors();
+        final int threadsToUse = Math.max(3, Math.min(cpus, 16));
+        final int adderCount = threadsToUse / 3;
+        final int removerCount = adderCount;
         final int pollerCount = removerCount;
         final int threadCount = adderCount + removerCount + pollerCount;
+
         final CountDownLatch startingGate = new CountDownLatch(1);
         final CountDownLatch addersDone = new CountDownLatch(adderCount);
         final Runnable remover = new Runnable() {
--- a/jdk/test/java/util/concurrent/ExecutorCompletionService/ExecutorCompletionServiceLoops.java	Tue Feb 16 09:49:14 2016 -0800
+++ b/jdk/test/java/util/concurrent/ExecutorCompletionService/ExecutorCompletionServiceLoops.java	Tue Feb 16 09:52:49 2016 -0800
@@ -61,13 +61,13 @@
             max = Integer.parseInt(args[0]);
 
         System.out.println("Warmup...");
-        oneTest( base );
+        oneTest(base);
         Thread.sleep(100);
         print = true;
 
         for (int i = 1; i <= max; i += (i+1) >>> 1) {
             System.out.print("n: " + i * base);
-            oneTest(i * base );
+            oneTest(i * base);
             Thread.sleep(100);
         }
         pool.shutdown();