changeset 13129:64e1f7283f2b

8143086: Document that ForkJoinWorkerThreadFactory.newThread can return null to reject request Reviewed-by: martin, psandoz, chegar, shade, plevart
author dl
date Wed, 25 Nov 2015 18:24:21 -0800
parents e2896d563f7b
children 9494284852a5
files src/java.base/share/classes/java/util/concurrent/ForkJoinPool.java
diffstat 1 files changed, 10 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/share/classes/java/util/concurrent/ForkJoinPool.java	Wed Nov 25 18:18:28 2015 -0800
+++ b/src/java.base/share/classes/java/util/concurrent/ForkJoinPool.java	Wed Nov 25 18:24:21 2015 -0800
@@ -703,7 +703,8 @@
          * Returns a new worker thread operating in the given pool.
          *
          * @param pool the pool this thread works in
-         * @return the new worker thread
+         * @return the new worker thread, or {@code null} if the request
+         *         to create a thread is rejected
          * @throws NullPointerException if the pool is null
          */
         public ForkJoinWorkerThread newThread(ForkJoinPool pool);
@@ -1053,7 +1054,7 @@
         }
 
         /**
-         * Shared version of pop.
+         * Shared version of tryUnpush.
          */
         final boolean trySharedUnpush(ForkJoinTask<?> task) {
             boolean popped = false;
@@ -1064,7 +1065,8 @@
                 ForkJoinTask<?> t = (ForkJoinTask<?>) U.getObject(a, offset);
                 if (t == task &&
                     U.compareAndSwapInt(this, QLOCK, 0, 1)) {
-                    if (U.compareAndSwapObject(a, offset, task, null)) {
+                    if (top == s + 1 && array == a &&
+                        U.compareAndSwapObject(a, offset, task, null)) {
                         popped = true;
                         top = s;
                     }
@@ -1250,12 +1252,14 @@
                     for (CountedCompleter<?> r = t;;) {
                         if (r == task) {
                             if ((mode & IS_OWNED) == 0) {
-                                boolean popped;
+                                boolean popped = false;
                                 if (U.compareAndSwapInt(this, QLOCK, 0, 1)) {
-                                    if (popped =
+                                    if (top == s && array == a &&
                                         U.compareAndSwapObject(a, offset,
-                                                               t, null))
+                                                               t, null)) {
+                                        popped = true;
                                         top = s - 1;
+                                    }
                                     U.putOrderedInt(this, QLOCK, 0);
                                     if (popped)
                                         return t;