44 |
|
* tasks that are never joined. |
45 |
|
* |
46 |
|
* <p>A static {@link #commonPool} is available and appropriate for |
47 |
< |
* most applications. The common pool is constructed upon first |
48 |
< |
* access, or upon usage by any ForkJoinTask that is not explictly |
49 |
< |
* submitted to a specified pool. Using the common pool normally |
50 |
< |
* reduces resource usage (its threads are slowly reclaimed during |
51 |
< |
* periods of non-use, and reinstated upon subsequent use). The |
52 |
< |
* common pool is by default constructed with default parameters, but |
53 |
< |
* these may be controlled by setting any or all of the three |
54 |
< |
* properties {@code |
47 |
> |
* most applications. The common pool is used by any ForkJoinTask that |
48 |
> |
* is not explicitly submitted to a specified pool. Using the common |
49 |
> |
* pool normally reduces resource usage (its threads are slowly |
50 |
> |
* reclaimed during periods of non-use, and reinstated upon subsequent |
51 |
> |
* use). The common pool is by default constructed with default |
52 |
> |
* parameters, but these may be controlled by setting any or all of |
53 |
> |
* the three properties {@code |
54 |
|
* java.util.concurrent.ForkJoinPool.common.{parallelism, |
55 |
|
* threadFactory, exceptionHandler}}. |
56 |
|
* |
236 |
|
* when locked remains available to check consistency. |
237 |
|
* |
238 |
|
* Recording WorkQueues. WorkQueues are recorded in the |
239 |
< |
* "workQueues" array that is created upon pool construction and |
240 |
< |
* expanded if necessary. Updates to the array while recording |
241 |
< |
* new workers and unrecording terminated ones are protected from |
242 |
< |
* each other by a lock but the array is otherwise concurrently |
243 |
< |
* readable, and accessed directly. To simplify index-based |
244 |
< |
* operations, the array size is always a power of two, and all |
245 |
< |
* readers must tolerate null slots. Shared (submission) queues |
246 |
< |
* are at even indices, worker queues at odd indices. Grouping |
247 |
< |
* them together in this way simplifies and speeds up task |
249 |
< |
* scanning. |
239 |
> |
* "workQueues" array that is created upon first use and expanded |
240 |
> |
* if necessary. Updates to the array while recording new workers |
241 |
> |
* and unrecording terminated ones are protected from each other |
242 |
> |
* by a lock but the array is otherwise concurrently readable, and |
243 |
> |
* accessed directly. To simplify index-based operations, the |
244 |
> |
* array size is always a power of two, and all readers must |
245 |
> |
* tolerate null slots. Shared (submission) queues are at even |
246 |
> |
* indices, worker queues at odd indices. Grouping them together |
247 |
> |
* in this way simplifies and speeds up task scanning. |
248 |
|
* |
249 |
|
* All worker thread creation is on-demand, triggered by task |
250 |
|
* submissions, replacement of terminated workers, and/or |
502 |
|
} |
503 |
|
|
504 |
|
/** |
507 |
– |
* A simple non-reentrant lock used for exclusion when managing |
508 |
– |
* queues and workers. We use a custom lock so that we can readily |
509 |
– |
* probe lock state in constructions that check among alternative |
510 |
– |
* actions. The lock is normally only very briefly held, and |
511 |
– |
* sometimes treated as a spinlock, but other usages block to |
512 |
– |
* reduce overall contention in those cases where locked code |
513 |
– |
* bodies perform allocation/resizing. |
514 |
– |
*/ |
515 |
– |
static final class Mutex extends AbstractQueuedSynchronizer { |
516 |
– |
public final boolean tryAcquire(int ignore) { |
517 |
– |
return compareAndSetState(0, 1); |
518 |
– |
} |
519 |
– |
public final boolean tryRelease(int ignore) { |
520 |
– |
setState(0); |
521 |
– |
return true; |
522 |
– |
} |
523 |
– |
public final void lock() { acquire(0); } |
524 |
– |
public final void unlock() { release(0); } |
525 |
– |
public final boolean isHeldExclusively() { return getState() == 1; } |
526 |
– |
public final Condition newCondition() { return new ConditionObject(); } |
527 |
– |
} |
528 |
– |
|
529 |
– |
/** |
505 |
|
* Class for artificial tasks that are used to replace the target |
506 |
|
* of local joins if they are removed from an interior queue slot |
507 |
|
* in WorkQueue.tryRemoveAndExec. We don't need the proxy to |
692 |
|
|
693 |
|
/** |
694 |
|
* Takes next task, if one exists, in LIFO order. Call only |
695 |
< |
* by owner in unshared queues. (We do not have a shared |
721 |
< |
* version of this method because it is never needed.) |
695 |
> |
* by owner in unshared queues. |
696 |
|
*/ |
697 |
|
final ForkJoinTask<?> pop() { |
698 |
|
ForkJoinTask<?>[] a; ForkJoinTask<?> t; int m; |
710 |
|
return null; |
711 |
|
} |
712 |
|
|
713 |
+ |
final ForkJoinTask<?> sharedPop() { |
714 |
+ |
ForkJoinTask<?> task = null; |
715 |
+ |
if (runState == 0 && U.compareAndSwapInt(this, RUNSTATE, 0, 1)) { |
716 |
+ |
try { |
717 |
+ |
ForkJoinTask<?>[] a; int m; |
718 |
+ |
if ((a = array) != null && (m = a.length - 1) >= 0) { |
719 |
+ |
for (int s; (s = top - 1) - base >= 0;) { |
720 |
+ |
long j = ((m & s) << ASHIFT) + ABASE; |
721 |
+ |
ForkJoinTask<?> t = |
722 |
+ |
(ForkJoinTask<?>)U.getObject(a, j); |
723 |
+ |
if (t == null) |
724 |
+ |
break; |
725 |
+ |
if (U.compareAndSwapObject(a, j, t, null)) { |
726 |
+ |
top = s; |
727 |
+ |
task = t; |
728 |
+ |
break; |
729 |
+ |
} |
730 |
+ |
} |
731 |
+ |
} |
732 |
+ |
} finally { |
733 |
+ |
runState = 0; |
734 |
+ |
} |
735 |
+ |
} |
736 |
+ |
return task; |
737 |
+ |
} |
738 |
+ |
|
739 |
+ |
/** |
740 |
+ |
* Version of pop that takes top element only if it |
741 |
+ |
* its root is the given CountedCompleter. |
742 |
+ |
*/ |
743 |
+ |
final ForkJoinTask<?> popCC(CountedCompleter<?> root) { |
744 |
+ |
ForkJoinTask<?>[] a; int m; |
745 |
+ |
if (root != null && (a = array) != null && (m = a.length - 1) >= 0) { |
746 |
+ |
for (int s; (s = top - 1) - base >= 0;) { |
747 |
+ |
long j = ((m & s) << ASHIFT) + ABASE; |
748 |
+ |
ForkJoinTask<?> t = |
749 |
+ |
(ForkJoinTask<?>)U.getObject(a, j); |
750 |
+ |
if (t == null || !(t instanceof CountedCompleter) || |
751 |
+ |
((CountedCompleter<?>)t).getRoot() != root) |
752 |
+ |
break; |
753 |
+ |
if (U.compareAndSwapObject(a, j, t, null)) { |
754 |
+ |
top = s; |
755 |
+ |
return t; |
756 |
+ |
} |
757 |
+ |
if (root.status < 0) |
758 |
+ |
break; |
759 |
+ |
} |
760 |
+ |
} |
761 |
+ |
return null; |
762 |
+ |
} |
763 |
+ |
|
764 |
+ |
/** |
765 |
+ |
* Shared version of popCC |
766 |
+ |
*/ |
767 |
+ |
final ForkJoinTask<?> sharedPopCC(CountedCompleter<?> root) { |
768 |
+ |
ForkJoinTask<?> task = null; |
769 |
+ |
if (root != null && |
770 |
+ |
runState == 0 && U.compareAndSwapInt(this, RUNSTATE, 0, 1)) { |
771 |
+ |
try { |
772 |
+ |
ForkJoinTask<?>[] a; int m; |
773 |
+ |
if ((a = array) != null && (m = a.length - 1) >= 0) { |
774 |
+ |
for (int s; (s = top - 1) - base >= 0;) { |
775 |
+ |
long j = ((m & s) << ASHIFT) + ABASE; |
776 |
+ |
ForkJoinTask<?> t = |
777 |
+ |
(ForkJoinTask<?>)U.getObject(a, j); |
778 |
+ |
if (t == null || !(t instanceof CountedCompleter) || |
779 |
+ |
((CountedCompleter<?>)t).getRoot() != root) |
780 |
+ |
break; |
781 |
+ |
if (U.compareAndSwapObject(a, j, t, null)) { |
782 |
+ |
top = s; |
783 |
+ |
task = t; |
784 |
+ |
break; |
785 |
+ |
} |
786 |
+ |
if (root.status < 0) |
787 |
+ |
break; |
788 |
+ |
} |
789 |
+ |
} |
790 |
+ |
} finally { |
791 |
+ |
runState = 0; |
792 |
+ |
} |
793 |
+ |
} |
794 |
+ |
return task; |
795 |
+ |
} |
796 |
+ |
|
797 |
|
/** |
798 |
|
* Takes a task in FIFO order if b is base of queue and a task |
799 |
|
* can be claimed without contention. Specialized versions |
872 |
|
|
873 |
|
/** |
874 |
|
* Version of tryUnpush for shared queues; called by non-FJ |
875 |
< |
* submitters. Conservatively fails to unpush if all workers |
818 |
< |
* are active unless there are multiple tasks in queue. |
875 |
> |
* submitters after prechecking that task probably exists. |
876 |
|
*/ |
877 |
< |
final boolean trySharedUnpush(ForkJoinTask<?> task, ForkJoinPool p) { |
877 |
> |
final boolean trySharedUnpush(ForkJoinTask<?> t) { |
878 |
|
boolean success = false; |
879 |
< |
if (task != null && top != base && runState == 0 && |
823 |
< |
U.compareAndSwapInt(this, RUNSTATE, 0, 1)) { |
879 |
> |
if (runState == 0 && U.compareAndSwapInt(this, RUNSTATE, 0, 1)) { |
880 |
|
try { |
881 |
< |
ForkJoinTask<?>[] a; int n, s; |
882 |
< |
if ((a = array) != null && (n = (s = top) - base) > 0 && |
883 |
< |
(n > 1 || p == null || (int)(p.ctl >> AC_SHIFT) < 0)) { |
884 |
< |
int j = (((a.length - 1) & --s) << ASHIFT) + ABASE; |
885 |
< |
if (U.getObjectVolatile(a, j) == task && |
886 |
< |
U.compareAndSwapObject(a, j, task, null)) { |
831 |
< |
top = s; |
832 |
< |
success = true; |
833 |
< |
} |
881 |
> |
ForkJoinTask<?>[] a; int s; |
882 |
> |
if ((a = array) != null && (s = top) != base && |
883 |
> |
U.compareAndSwapObject |
884 |
> |
(a, (((a.length - 1) & --s) << ASHIFT) + ABASE, t, null)) { |
885 |
> |
top = s; |
886 |
> |
success = true; |
887 |
|
} |
888 |
|
} finally { |
889 |
|
runState = 0; // unlock |
1166 |
|
public static final ForkJoinWorkerThreadFactory |
1167 |
|
defaultForkJoinWorkerThreadFactory; |
1168 |
|
|
1169 |
+ |
|
1170 |
+ |
/** Property prefix for constructing common pool */ |
1171 |
+ |
private static final String propPrefix = |
1172 |
+ |
"java.util.concurrent.ForkJoinPool.common."; |
1173 |
+ |
|
1174 |
+ |
/** |
1175 |
+ |
* Common (static) pool. Non-null for public use unless a static |
1176 |
+ |
* construction exception, but internal usages must null-check on |
1177 |
+ |
* use. |
1178 |
+ |
*/ |
1179 |
+ |
static final ForkJoinPool commonPool; |
1180 |
+ |
|
1181 |
+ |
/** |
1182 |
+ |
* Common pool parallelism. Must equal commonPool.parallelism. |
1183 |
+ |
*/ |
1184 |
+ |
static final int commonPoolParallelism; |
1185 |
+ |
|
1186 |
|
/** |
1187 |
|
* Generator for assigning sequence numbers as pool names. |
1188 |
|
*/ |
1207 |
|
*/ |
1208 |
|
private static final ThreadSubmitter submitters; |
1209 |
|
|
1140 |
– |
/** Common default pool */ |
1141 |
– |
static volatile ForkJoinPool commonPool; |
1142 |
– |
|
1143 |
– |
// commonPool construction parameters |
1144 |
– |
private static final String propPrefix = |
1145 |
– |
"java.util.concurrent.ForkJoinPool.common."; |
1146 |
– |
private static final Thread.UncaughtExceptionHandler commonPoolUEH; |
1147 |
– |
private static final ForkJoinWorkerThreadFactory commonPoolFactory; |
1148 |
– |
static final int commonPoolParallelism; |
1149 |
– |
|
1150 |
– |
/** Static initialization lock */ |
1151 |
– |
private static final Mutex initializationLock; |
1152 |
– |
|
1210 |
|
// static constants |
1211 |
|
|
1212 |
|
/** |
1213 |
< |
* Initial timeout value (in nanoseconds) for the tread triggering |
1213 |
> |
* Initial timeout value (in nanoseconds) for the thread triggering |
1214 |
|
* quiescence to park waiting for new work. On timeout, the thread |
1215 |
|
* will instead try to shrink the number of workers. |
1216 |
|
*/ |
1339 |
|
* empirically works OK on current JVMs. |
1340 |
|
*/ |
1341 |
|
|
1342 |
+ |
volatile long stealCount; // collects worker counts |
1343 |
|
volatile long ctl; // main pool control |
1344 |
|
final int parallelism; // parallelism level |
1345 |
|
final int localMode; // per-worker scheduling mode |
1346 |
+ |
volatile int nextWorkerNumber; // to create worker name string |
1347 |
|
final int submitMask; // submit queue index bound |
1348 |
|
int nextSeed; // for initializing worker seeds |
1349 |
+ |
volatile int mainLock; // spinlock for array updates |
1350 |
|
volatile int runState; // shutdown status and seq |
1351 |
|
WorkQueue[] workQueues; // main registry |
1292 |
– |
final Mutex lock; // for registration |
1293 |
– |
final Condition termination; // for awaitTermination |
1352 |
|
final ForkJoinWorkerThreadFactory factory; // factory for new workers |
1353 |
|
final Thread.UncaughtExceptionHandler ueh; // per-worker UEH |
1354 |
< |
final AtomicLong stealCount; // collect counts when terminated |
1355 |
< |
final AtomicInteger nextWorkerNumber; // to create worker name string |
1356 |
< |
String workerNamePrefix; // to create worker name string |
1354 |
> |
final String workerNamePrefix; // to create worker name string |
1355 |
> |
|
1356 |
> |
/* |
1357 |
> |
* Mechanics for main lock protecting worker array updates. Uses |
1358 |
> |
* the same strategy as ConcurrentHashMap bins -- a spinLock for |
1359 |
> |
* normal cases, but falling back to builtin lock when (rarely) |
1360 |
> |
* needed. See internal ConcurrentHashMap documentation for |
1361 |
> |
* explanation. |
1362 |
> |
*/ |
1363 |
> |
|
1364 |
> |
static final int LOCK_WAITING = 2; // bit to indicate need for signal |
1365 |
> |
static final int MAX_LOCK_SPINS = 1 << 8; |
1366 |
> |
|
1367 |
> |
private void tryAwaitMainLock() { |
1368 |
> |
int spins = MAX_LOCK_SPINS, r = 0, h; |
1369 |
> |
while (((h = mainLock) & 1) != 0) { |
1370 |
> |
if (r == 0) |
1371 |
> |
r = ThreadLocalRandom.current().nextInt(); // randomize spins |
1372 |
> |
else if (spins >= 0) { |
1373 |
> |
r ^= r << 1; r ^= r >>> 3; r ^= r << 10; // xorshift |
1374 |
> |
if (r >= 0) |
1375 |
> |
--spins; |
1376 |
> |
} |
1377 |
> |
else if (U.compareAndSwapInt(this, MAINLOCK, h, h | LOCK_WAITING)) { |
1378 |
> |
synchronized (this) { |
1379 |
> |
if ((mainLock & LOCK_WAITING) != 0) { |
1380 |
> |
try { |
1381 |
> |
wait(); |
1382 |
> |
} catch (InterruptedException ie) { |
1383 |
> |
Thread.currentThread().interrupt(); |
1384 |
> |
} |
1385 |
> |
} |
1386 |
> |
else |
1387 |
> |
notifyAll(); // possibly won race vs signaller |
1388 |
> |
} |
1389 |
> |
break; |
1390 |
> |
} |
1391 |
> |
} |
1392 |
> |
} |
1393 |
|
|
1394 |
|
// Creating, registering, and deregistering workers |
1395 |
|
|
1417 |
|
* ForkJoinWorkerThread. |
1418 |
|
*/ |
1419 |
|
final String nextWorkerName() { |
1420 |
< |
return workerNamePrefix.concat |
1421 |
< |
(Integer.toString(nextWorkerNumber.addAndGet(1))); |
1420 |
> |
int n; |
1421 |
> |
do {} while (!U.compareAndSwapInt(this, NEXTWORKERNUMBER, |
1422 |
> |
n = nextWorkerNumber, ++n)); |
1423 |
> |
return workerNamePrefix.concat(Integer.toString(n)); |
1424 |
|
} |
1425 |
|
|
1426 |
|
/** |
1433 |
|
* @param w the worker's queue |
1434 |
|
*/ |
1435 |
|
final void registerWorker(WorkQueue w) { |
1436 |
< |
Mutex lock = this.lock; |
1437 |
< |
lock.lock(); |
1436 |
> |
while (!U.compareAndSwapInt(this, MAINLOCK, 0, 1)) |
1437 |
> |
tryAwaitMainLock(); |
1438 |
|
try { |
1439 |
< |
WorkQueue[] ws = workQueues; |
1440 |
< |
if (w != null && ws != null) { // skip on shutdown/failure |
1439 |
> |
WorkQueue[] ws; |
1440 |
> |
if ((ws = workQueues) == null) |
1441 |
> |
ws = workQueues = new WorkQueue[submitMask + 1]; |
1442 |
> |
if (w != null) { |
1443 |
|
int rs, n = ws.length, m = n - 1; |
1444 |
|
int s = nextSeed += SEED_INCREMENT; // rarely-colliding sequence |
1445 |
|
w.seed = (s == 0) ? 1 : s; // ensure non-zero seed |
1460 |
|
runState = ((rs = runState) & SHUTDOWN) | ((rs + 2) & ~SHUTDOWN); |
1461 |
|
} |
1462 |
|
} finally { |
1463 |
< |
lock.unlock(); |
1463 |
> |
if (!U.compareAndSwapInt(this, MAINLOCK, 1, 0)) { |
1464 |
> |
mainLock = 0; |
1465 |
> |
synchronized (this) { notifyAll(); }; |
1466 |
> |
} |
1467 |
|
} |
1468 |
+ |
|
1469 |
|
} |
1470 |
|
|
1471 |
|
/** |
1478 |
|
* @param ex the exception causing failure, or null if none |
1479 |
|
*/ |
1480 |
|
final void deregisterWorker(ForkJoinWorkerThread wt, Throwable ex) { |
1379 |
– |
Mutex lock = this.lock; |
1481 |
|
WorkQueue w = null; |
1482 |
|
if (wt != null && (w = wt.workQueue) != null) { |
1483 |
|
w.runState = -1; // ensure runState is set |
1484 |
< |
stealCount.getAndAdd(w.totalSteals + w.nsteals); |
1484 |
> |
long steals = w.totalSteals + w.nsteals, sc; |
1485 |
> |
do {} while (!U.compareAndSwapLong(this, STEALCOUNT, |
1486 |
> |
sc = stealCount, sc + steals)); |
1487 |
|
int idx = w.poolIndex; |
1488 |
< |
lock.lock(); |
1489 |
< |
try { // remove record from array |
1488 |
> |
while (!U.compareAndSwapInt(this, MAINLOCK, 0, 1)) |
1489 |
> |
tryAwaitMainLock(); |
1490 |
> |
try { |
1491 |
|
WorkQueue[] ws = workQueues; |
1492 |
|
if (ws != null && idx >= 0 && idx < ws.length && ws[idx] == w) |
1493 |
|
ws[idx] = null; |
1494 |
|
} finally { |
1495 |
< |
lock.unlock(); |
1495 |
> |
if (!U.compareAndSwapInt(this, MAINLOCK, 1, 0)) { |
1496 |
> |
mainLock = 0; |
1497 |
> |
synchronized (this) { notifyAll(); }; |
1498 |
> |
} |
1499 |
|
} |
1500 |
|
} |
1501 |
|
|
1534 |
|
for (int r = s.seed, m = submitMask;;) { |
1535 |
|
WorkQueue[] ws; WorkQueue q; |
1536 |
|
int k = r & m & SQMASK; // use only even indices |
1537 |
< |
if (runState < 0 || (ws = workQueues) == null || ws.length <= k) |
1537 |
> |
if (runState < 0) |
1538 |
|
throw new RejectedExecutionException(); // shutting down |
1539 |
+ |
else if ((ws = workQueues) == null || ws.length <= k) { |
1540 |
+ |
while (!U.compareAndSwapInt(this, MAINLOCK, 0, 1)) |
1541 |
+ |
tryAwaitMainLock(); |
1542 |
+ |
try { |
1543 |
+ |
if (workQueues == null) |
1544 |
+ |
workQueues = new WorkQueue[submitMask + 1]; |
1545 |
+ |
} finally { |
1546 |
+ |
if (!U.compareAndSwapInt(this, MAINLOCK, 1, 0)) { |
1547 |
+ |
mainLock = 0; |
1548 |
+ |
synchronized (this) { notifyAll(); }; |
1549 |
+ |
} |
1550 |
+ |
} |
1551 |
+ |
} |
1552 |
|
else if ((q = ws[k]) == null) { // create new queue |
1553 |
|
WorkQueue nq = new WorkQueue(this, null, SHARED_QUEUE); |
1554 |
< |
Mutex lock = this.lock; // construct outside lock |
1555 |
< |
lock.lock(); |
1556 |
< |
try { // recheck under lock |
1554 |
> |
while (!U.compareAndSwapInt(this, MAINLOCK, 0, 1)) |
1555 |
> |
tryAwaitMainLock(); |
1556 |
> |
try { |
1557 |
|
int rs = runState; // to update seq |
1558 |
|
if (ws == workQueues && ws[k] == null) { |
1559 |
|
ws[k] = nq; |
1560 |
|
runState = ((rs & SHUTDOWN) | ((rs + 2) & ~SHUTDOWN)); |
1561 |
|
} |
1562 |
|
} finally { |
1563 |
< |
lock.unlock(); |
1563 |
> |
if (!U.compareAndSwapInt(this, MAINLOCK, 1, 0)) { |
1564 |
> |
mainLock = 0; |
1565 |
> |
synchronized (this) { notifyAll(); }; |
1566 |
> |
} |
1567 |
|
} |
1568 |
|
} |
1569 |
|
else if (q.trySharedPush(task)) { |
1586 |
|
static void submitToCommonPool(ForkJoinTask<?> task) { |
1587 |
|
ForkJoinPool p; |
1588 |
|
if ((p = commonPool) == null) |
1589 |
< |
p = ensureCommonPool(); |
1589 |
> |
throw new RejectedExecutionException("Common Pool Unavailable"); |
1590 |
|
p.doSubmit(task); |
1591 |
|
} |
1592 |
|
|
1593 |
|
/** |
1594 |
+ |
* Returns true if caller is (or may be) submitter to the common |
1595 |
+ |
* pool, and not all workers are active, and there appear to be |
1596 |
+ |
* tasks in the associated submission queue. |
1597 |
+ |
*/ |
1598 |
+ |
static boolean canHelpCommonPool() { |
1599 |
+ |
ForkJoinPool p; WorkQueue[] ws; WorkQueue q; |
1600 |
+ |
int k = submitters.get().seed & SQMASK; |
1601 |
+ |
return ((p = commonPool) != null && |
1602 |
+ |
(int)(p.ctl >> AC_SHIFT) < 0 && |
1603 |
+ |
(ws = p.workQueues) != null && |
1604 |
+ |
ws.length > (k &= p.submitMask) && |
1605 |
+ |
(q = ws[k]) != null && |
1606 |
+ |
q.top - q.base > 0); |
1607 |
+ |
} |
1608 |
+ |
|
1609 |
+ |
/** |
1610 |
|
* Returns true if the given task was submitted to common pool |
1611 |
|
* and has not yet commenced execution, and is available for |
1612 |
|
* removal according to execution policies; if so removing the |
1616 |
|
* @return true if successful |
1617 |
|
*/ |
1618 |
|
static boolean tryUnsubmitFromCommonPool(ForkJoinTask<?> task) { |
1619 |
+ |
// Peek, looking for task and eligibility before |
1620 |
+ |
// using trySharedUnpush to actually take it under lock |
1621 |
|
ForkJoinPool p; WorkQueue[] ws; WorkQueue q; |
1622 |
+ |
ForkJoinTask<?>[] a; int s; |
1623 |
|
int k = submitters.get().seed & SQMASK; |
1624 |
|
return ((p = commonPool) != null && |
1625 |
+ |
(int)(p.ctl >> AC_SHIFT) < 0 && |
1626 |
|
(ws = p.workQueues) != null && |
1627 |
|
ws.length > (k &= p.submitMask) && |
1628 |
|
(q = ws[k]) != null && |
1629 |
< |
q.trySharedUnpush(task, p)); |
1629 |
> |
(a = q.array) != null && |
1630 |
> |
(s = q.top - 1) - q.base >= 0 && |
1631 |
> |
s >= 0 && s < a.length && |
1632 |
> |
a[s] == task && |
1633 |
> |
q.trySharedUnpush(task)); |
1634 |
|
} |
1635 |
|
|
1636 |
+ |
/** |
1637 |
+ |
* Tries to pop a task from common pool with given root |
1638 |
+ |
*/ |
1639 |
+ |
static ForkJoinTask<?> popCCFromCommonPool(CountedCompleter<?> root) { |
1640 |
+ |
ForkJoinPool p; WorkQueue[] ws; WorkQueue q; |
1641 |
+ |
ForkJoinTask<?> t; |
1642 |
+ |
int k = submitters.get().seed & SQMASK; |
1643 |
+ |
if (root != null && |
1644 |
+ |
(p = commonPool) != null && |
1645 |
+ |
(int)(p.ctl >> AC_SHIFT) < 0 && |
1646 |
+ |
(ws = p.workQueues) != null && |
1647 |
+ |
ws.length > (k &= p.submitMask) && |
1648 |
+ |
(q = ws[k]) != null && q.top - q.base > 0 && |
1649 |
+ |
root.status < 0 && |
1650 |
+ |
(t = q.sharedPopCC(root)) != null) |
1651 |
+ |
return t; |
1652 |
+ |
return null; |
1653 |
+ |
} |
1654 |
+ |
|
1655 |
+ |
|
1656 |
|
// Maintaining ctl counts |
1657 |
|
|
1658 |
|
/** |
2124 |
|
*/ |
2125 |
|
private WorkQueue findNonEmptyStealQueue(WorkQueue w) { |
2126 |
|
// Similar to loop in scan(), but ignoring submissions |
2127 |
< |
int r; |
1961 |
< |
if (w == null) // allow external callers |
1962 |
< |
r = ThreadLocalRandom.current().nextInt(); |
1963 |
< |
else { |
1964 |
< |
r = w.seed; r ^= r << 13; r ^= r >>> 17; w.seed = r ^= r << 5; |
1965 |
< |
} |
2127 |
> |
int r = w.seed; r ^= r << 13; r ^= r >>> 17; w.seed = r ^= r << 5; |
2128 |
|
int step = (r >>> 16) | 1; |
2129 |
|
for (WorkQueue[] ws;;) { |
2130 |
|
int rs = runState, m; |
2188 |
|
* Restricted version of helpQuiescePool for non-FJ callers |
2189 |
|
*/ |
2190 |
|
static void externalHelpQuiescePool() { |
2191 |
< |
ForkJoinPool p; WorkQueue[] ws; WorkQueue w, q; |
2192 |
< |
ForkJoinTask<?> t; int b; |
2191 |
> |
ForkJoinPool p; WorkQueue[] ws; WorkQueue q, sq; |
2192 |
> |
ForkJoinTask<?>[] a; int b; |
2193 |
> |
ForkJoinTask<?> t = null; |
2194 |
|
int k = submitters.get().seed & SQMASK; |
2195 |
|
if ((p = commonPool) != null && |
2196 |
+ |
(int)(p.ctl >> AC_SHIFT) < 0 && |
2197 |
|
(ws = p.workQueues) != null && |
2198 |
|
ws.length > (k &= p.submitMask) && |
2199 |
< |
(w = ws[k]) != null && |
2200 |
< |
(q = p.findNonEmptyStealQueue(w)) != null && |
2201 |
< |
(b = q.base) - q.top < 0 && |
2202 |
< |
(t = q.pollAt(b)) != null) |
2203 |
< |
t.doExec(); |
2199 |
> |
(q = ws[k]) != null) { |
2200 |
> |
while (q.top - q.base > 0) { |
2201 |
> |
if ((t = q.sharedPop()) != null) |
2202 |
> |
break; |
2203 |
> |
} |
2204 |
> |
if (t == null && (sq = p.findNonEmptyStealQueue(q)) != null && |
2205 |
> |
(b = sq.base) - sq.top < 0) |
2206 |
> |
t = sq.pollAt(b); |
2207 |
> |
if (t != null) |
2208 |
> |
t.doExec(); |
2209 |
> |
} |
2210 |
|
} |
2211 |
|
|
2212 |
|
/** |
2273 |
|
* @return true if now terminating or terminated |
2274 |
|
*/ |
2275 |
|
private boolean tryTerminate(boolean now, boolean enable) { |
2106 |
– |
Mutex lock = this.lock; |
2276 |
|
for (long c;;) { |
2277 |
|
if (((c = ctl) & STOP_BIT) != 0) { // already terminating |
2278 |
|
if ((short)(c >>> TC_SHIFT) == -parallelism) { |
2279 |
< |
lock.lock(); // don't need try/finally |
2280 |
< |
termination.signalAll(); // signal when 0 workers |
2281 |
< |
lock.unlock(); |
2279 |
> |
synchronized (this) { |
2280 |
> |
notifyAll(); // signal when 0 workers |
2281 |
> |
} |
2282 |
|
} |
2283 |
|
return true; |
2284 |
|
} |
2285 |
|
if (runState >= 0) { // not yet enabled |
2286 |
|
if (!enable) |
2287 |
|
return false; |
2288 |
< |
lock.lock(); |
2289 |
< |
runState |= SHUTDOWN; |
2290 |
< |
lock.unlock(); |
2288 |
> |
while (!U.compareAndSwapInt(this, MAINLOCK, 0, 1)) |
2289 |
> |
tryAwaitMainLock(); |
2290 |
> |
try { |
2291 |
> |
runState |= SHUTDOWN; |
2292 |
> |
} finally { |
2293 |
> |
if (!U.compareAndSwapInt(this, MAINLOCK, 1, 0)) { |
2294 |
> |
mainLock = 0; |
2295 |
> |
synchronized (this) { notifyAll(); }; |
2296 |
> |
} |
2297 |
> |
} |
2298 |
|
} |
2299 |
|
if (!now) { // check if idle & no tasks |
2300 |
|
if ((int)(c >> AC_SHIFT) != -parallelism || |
2427 |
|
// Use nearest power 2 for workQueues size. See Hackers Delight sec 3.2. |
2428 |
|
int n = parallelism - 1; |
2429 |
|
n |= n >>> 1; n |= n >>> 2; n |= n >>> 4; n |= n >>> 8; n |= n >>> 16; |
2430 |
< |
int size = (n + 1) << 1; // #slots = 2*#workers |
2255 |
< |
this.submitMask = size - 1; // room for max # of submit queues |
2256 |
< |
this.workQueues = new WorkQueue[size]; |
2257 |
< |
this.termination = (this.lock = new Mutex()).newCondition(); |
2258 |
< |
this.stealCount = new AtomicLong(); |
2259 |
< |
this.nextWorkerNumber = new AtomicInteger(); |
2430 |
> |
this.submitMask = ((n + 1) << 1) - 1; |
2431 |
|
int pn = poolNumberGenerator.incrementAndGet(); |
2432 |
|
StringBuilder sb = new StringBuilder("ForkJoinPool-"); |
2433 |
|
sb.append(Integer.toString(pn)); |
2434 |
|
sb.append("-worker-"); |
2435 |
|
this.workerNamePrefix = sb.toString(); |
2265 |
– |
lock.lock(); |
2436 |
|
this.runState = 1; // set init flag |
2267 |
– |
lock.unlock(); |
2437 |
|
} |
2438 |
|
|
2439 |
|
/** |
2440 |
< |
* Returns the common pool instance |
2440 |
> |
* Constructor for common pool, suitable only for static initialization. |
2441 |
> |
* Basically the same as above, but uses smallest possible initial footprint. |
2442 |
> |
*/ |
2443 |
> |
ForkJoinPool(int parallelism, int submitMask, |
2444 |
> |
ForkJoinWorkerThreadFactory factory, |
2445 |
> |
Thread.UncaughtExceptionHandler handler) { |
2446 |
> |
this.factory = factory; |
2447 |
> |
this.ueh = handler; |
2448 |
> |
this.submitMask = submitMask; |
2449 |
> |
this.parallelism = parallelism; |
2450 |
> |
long np = (long)(-parallelism); |
2451 |
> |
this.ctl = ((np << AC_SHIFT) & AC_MASK) | ((np << TC_SHIFT) & TC_MASK); |
2452 |
> |
this.localMode = LIFO_QUEUE; |
2453 |
> |
this.workerNamePrefix = "ForkJoinPool.commonPool-worker-"; |
2454 |
> |
this.runState = 1; |
2455 |
> |
} |
2456 |
> |
|
2457 |
> |
/** |
2458 |
> |
* Returns the common pool instance. |
2459 |
|
* |
2460 |
|
* @return the common pool instance |
2461 |
|
*/ |
2462 |
|
public static ForkJoinPool commonPool() { |
2463 |
|
ForkJoinPool p; |
2464 |
< |
return (p = commonPool) != null? p : ensureCommonPool(); |
2465 |
< |
} |
2279 |
< |
|
2280 |
< |
private static ForkJoinPool ensureCommonPool() { |
2281 |
< |
ForkJoinPool p; |
2282 |
< |
if ((p = commonPool) == null) { |
2283 |
< |
final Mutex lock = initializationLock; |
2284 |
< |
lock.lock(); |
2285 |
< |
try { |
2286 |
< |
if ((p = commonPool) == null) { |
2287 |
< |
p = commonPool = new ForkJoinPool(commonPoolParallelism, |
2288 |
< |
commonPoolFactory, |
2289 |
< |
commonPoolUEH, false); |
2290 |
< |
// use a more informative name string for workers |
2291 |
< |
p.workerNamePrefix = "ForkJoinPool.commonPool-worker-"; |
2292 |
< |
} |
2293 |
< |
} finally { |
2294 |
< |
lock.unlock(); |
2295 |
< |
} |
2296 |
< |
} |
2464 |
> |
if ((p = commonPool) == null) |
2465 |
> |
throw new Error("Common Pool Unavailable"); |
2466 |
|
return p; |
2467 |
|
} |
2468 |
|
|
2728 |
|
* @return the number of steals |
2729 |
|
*/ |
2730 |
|
public long getStealCount() { |
2731 |
< |
long count = stealCount.get(); |
2731 |
> |
long count = stealCount; |
2732 |
|
WorkQueue[] ws; WorkQueue w; |
2733 |
|
if ((ws = workQueues) != null) { |
2734 |
|
for (int i = 1; i < ws.length; i += 2) { |
2858 |
|
public String toString() { |
2859 |
|
// Use a single pass through workQueues to collect counts |
2860 |
|
long qt = 0L, qs = 0L; int rc = 0; |
2861 |
< |
long st = stealCount.get(); |
2861 |
> |
long st = stealCount; |
2862 |
|
long c = ctl; |
2863 |
|
WorkQueue[] ws; WorkQueue w; |
2864 |
|
if ((ws = workQueues) != null) { |
2996 |
|
public boolean awaitTermination(long timeout, TimeUnit unit) |
2997 |
|
throws InterruptedException { |
2998 |
|
long nanos = unit.toNanos(timeout); |
2999 |
< |
final Mutex lock = this.lock; |
3000 |
< |
lock.lock(); |
3001 |
< |
try { |
3002 |
< |
for (;;) { |
3003 |
< |
if (isTerminated()) |
3004 |
< |
return true; |
3005 |
< |
if (nanos <= 0) |
3006 |
< |
return false; |
3007 |
< |
nanos = termination.awaitNanos(nanos); |
2999 |
> |
if (isTerminated()) |
3000 |
> |
return true; |
3001 |
> |
long startTime = System.nanoTime(); |
3002 |
> |
boolean terminated = false; |
3003 |
> |
synchronized (this) { |
3004 |
> |
for (long waitTime = nanos, millis = 0L;;) { |
3005 |
> |
if (terminated = isTerminated() || |
3006 |
> |
waitTime <= 0L || |
3007 |
> |
(millis = unit.toMillis(waitTime)) <= 0L) |
3008 |
> |
break; |
3009 |
> |
wait(millis); |
3010 |
> |
waitTime = nanos - (System.nanoTime() - startTime); |
3011 |
|
} |
2840 |
– |
} finally { |
2841 |
– |
lock.unlock(); |
3012 |
|
} |
3013 |
+ |
return terminated; |
3014 |
|
} |
3015 |
|
|
3016 |
|
/** |
3142 |
|
private static final long PARKBLOCKER; |
3143 |
|
private static final int ABASE; |
3144 |
|
private static final int ASHIFT; |
3145 |
+ |
private static final long NEXTWORKERNUMBER; |
3146 |
+ |
private static final long STEALCOUNT; |
3147 |
+ |
private static final long MAINLOCK; |
3148 |
|
|
3149 |
|
static { |
3150 |
|
poolNumberGenerator = new AtomicInteger(); |
3153 |
|
defaultForkJoinWorkerThreadFactory = |
3154 |
|
new DefaultForkJoinWorkerThreadFactory(); |
3155 |
|
submitters = new ThreadSubmitter(); |
2982 |
– |
initializationLock = new Mutex(); |
3156 |
|
int s; |
3157 |
|
try { |
3158 |
|
U = getUnsafe(); |
3160 |
|
Class<?> ak = ForkJoinTask[].class; |
3161 |
|
CTL = U.objectFieldOffset |
3162 |
|
(k.getDeclaredField("ctl")); |
3163 |
+ |
NEXTWORKERNUMBER = U.objectFieldOffset |
3164 |
+ |
(k.getDeclaredField("nextWorkerNumber")); |
3165 |
+ |
STEALCOUNT = U.objectFieldOffset |
3166 |
+ |
(k.getDeclaredField("stealCount")); |
3167 |
+ |
MAINLOCK = U.objectFieldOffset |
3168 |
+ |
(k.getDeclaredField("mainLock")); |
3169 |
|
Class<?> tk = Thread.class; |
3170 |
|
PARKBLOCKER = U.objectFieldOffset |
3171 |
|
(tk.getDeclaredField("parkBlocker")); |
3172 |
|
ABASE = U.arrayBaseOffset(ak); |
3173 |
|
s = U.arrayIndexScale(ak); |
3174 |
+ |
ASHIFT = 31 - Integer.numberOfLeadingZeros(s); |
3175 |
|
} catch (Exception e) { |
3176 |
|
throw new Error(e); |
3177 |
|
} |
3178 |
|
if ((s & (s-1)) != 0) |
3179 |
|
throw new Error("data type scale not a power of two"); |
3180 |
< |
ASHIFT = 31 - Integer.numberOfLeadingZeros(s); |
3001 |
< |
|
3002 |
< |
// Establish configuration for default pool |
3003 |
< |
try { |
3180 |
> |
try { // Establish common pool |
3181 |
|
String pp = System.getProperty(propPrefix + "parallelism"); |
3182 |
|
String fp = System.getProperty(propPrefix + "threadFactory"); |
3183 |
|
String up = System.getProperty(propPrefix + "exceptionHandler"); |
3184 |
+ |
ForkJoinWorkerThreadFactory fac = (fp == null) ? |
3185 |
+ |
defaultForkJoinWorkerThreadFactory : |
3186 |
+ |
((ForkJoinWorkerThreadFactory)ClassLoader. |
3187 |
+ |
getSystemClassLoader().loadClass(fp).newInstance()); |
3188 |
+ |
Thread.UncaughtExceptionHandler ueh = (up == null) ? null : |
3189 |
+ |
((Thread.UncaughtExceptionHandler)ClassLoader. |
3190 |
+ |
getSystemClassLoader().loadClass(up).newInstance()); |
3191 |
|
int par; |
3192 |
|
if ((pp == null || (par = Integer.parseInt(pp)) <= 0)) |
3193 |
|
par = Runtime.getRuntime().availableProcessors(); |
3194 |
+ |
if (par > MAX_CAP) |
3195 |
+ |
par = MAX_CAP; |
3196 |
|
commonPoolParallelism = par; |
3197 |
< |
if (fp != null) |
3198 |
< |
commonPoolFactory = (ForkJoinWorkerThreadFactory) |
3199 |
< |
ClassLoader.getSystemClassLoader().loadClass(fp).newInstance(); |
3200 |
< |
else |
3201 |
< |
commonPoolFactory = defaultForkJoinWorkerThreadFactory; |
3016 |
< |
if (up != null) |
3017 |
< |
commonPoolUEH = (Thread.UncaughtExceptionHandler) |
3018 |
< |
ClassLoader.getSystemClassLoader().loadClass(up).newInstance(); |
3019 |
< |
else |
3020 |
< |
commonPoolUEH = null; |
3197 |
> |
int n = par - 1; // precompute submit mask |
3198 |
> |
n |= n >>> 1; n |= n >>> 2; n |= n >>> 4; |
3199 |
> |
n |= n >>> 8; n |= n >>> 16; |
3200 |
> |
int mask = ((n + 1) << 1) - 1; |
3201 |
> |
commonPool = new ForkJoinPool(par, mask, fac, ueh); |
3202 |
|
} catch (Exception e) { |
3203 |
|
throw new Error(e); |
3204 |
|
} |