43 |
|
* tasks that are never joined. |
44 |
|
* |
45 |
|
* <p>A static {@link #commonPool} is available and appropriate for |
46 |
< |
* most applications. The common pool is constructed upon first |
47 |
< |
* access, or upon usage by any ForkJoinTask that is not explictly |
48 |
< |
* submitted to a specified pool. Using the common pool normally |
49 |
< |
* reduces resource usage (its threads are slowly reclaimed during |
50 |
< |
* periods of non-use, and reinstated upon subsequent use). The |
51 |
< |
* common pool is by default constructed with default parameters, but |
52 |
< |
* these may be controlled by setting any or all of the three |
53 |
< |
* properties {@code |
46 |
> |
* most applications. The common pool is used by any ForkJoinTask that |
47 |
> |
* is not explicitly submitted to a specified pool. Using the common |
48 |
> |
* pool normally reduces resource usage (its threads are slowly |
49 |
> |
* reclaimed during periods of non-use, and reinstated upon subsequent |
50 |
> |
* use). The common pool is by default constructed with default |
51 |
> |
* parameters, but these may be controlled by setting any or all of |
52 |
> |
* the three properties {@code |
53 |
|
* java.util.concurrent.ForkJoinPool.common.{parallelism, |
54 |
|
* threadFactory, exceptionHandler}}. |
55 |
|
* |
235 |
|
* when locked remains available to check consistency. |
236 |
|
* |
237 |
|
* Recording WorkQueues. WorkQueues are recorded in the |
238 |
< |
* "workQueues" array that is created upon pool construction and |
239 |
< |
* expanded if necessary. Updates to the array while recording |
240 |
< |
* new workers and unrecording terminated ones are protected from |
241 |
< |
* each other by a lock but the array is otherwise concurrently |
242 |
< |
* readable, and accessed directly. To simplify index-based |
243 |
< |
* operations, the array size is always a power of two, and all |
244 |
< |
* readers must tolerate null slots. Shared (submission) queues |
245 |
< |
* are at even indices, worker queues at odd indices. Grouping |
246 |
< |
* them together in this way simplifies and speeds up task |
248 |
< |
* scanning. |
238 |
> |
* "workQueues" array that is created upon first use and expanded |
239 |
> |
* if necessary. Updates to the array while recording new workers |
240 |
> |
* and unrecording terminated ones are protected from each other |
241 |
> |
* by a lock but the array is otherwise concurrently readable, and |
242 |
> |
* accessed directly. To simplify index-based operations, the |
243 |
> |
* array size is always a power of two, and all readers must |
244 |
> |
* tolerate null slots. Shared (submission) queues are at even |
245 |
> |
* indices, worker queues at odd indices. Grouping them together |
246 |
> |
* in this way simplifies and speeds up task scanning. |
247 |
|
* |
248 |
|
* All worker thread creation is on-demand, triggered by task |
249 |
|
* submissions, replacement of terminated workers, and/or |
501 |
|
} |
502 |
|
|
503 |
|
/** |
506 |
– |
* A simple non-reentrant lock used for exclusion when managing |
507 |
– |
* queues and workers. We use a custom lock so that we can readily |
508 |
– |
* probe lock state in constructions that check among alternative |
509 |
– |
* actions. The lock is normally only very briefly held, and |
510 |
– |
* sometimes treated as a spinlock, but other usages block to |
511 |
– |
* reduce overall contention in those cases where locked code |
512 |
– |
* bodies perform allocation/resizing. |
513 |
– |
*/ |
514 |
– |
static final class Mutex extends AbstractQueuedSynchronizer { |
515 |
– |
public final boolean tryAcquire(int ignore) { |
516 |
– |
return compareAndSetState(0, 1); |
517 |
– |
} |
518 |
– |
public final boolean tryRelease(int ignore) { |
519 |
– |
setState(0); |
520 |
– |
return true; |
521 |
– |
} |
522 |
– |
public final void lock() { acquire(0); } |
523 |
– |
public final void unlock() { release(0); } |
524 |
– |
public final boolean isHeldExclusively() { return getState() == 1; } |
525 |
– |
public final Condition newCondition() { return new ConditionObject(); } |
526 |
– |
} |
527 |
– |
|
528 |
– |
/** |
504 |
|
* Class for artificial tasks that are used to replace the target |
505 |
|
* of local joins if they are removed from an interior queue slot |
506 |
|
* in WorkQueue.tryRemoveAndExec. We don't need the proxy to |
691 |
|
|
692 |
|
/** |
693 |
|
* Takes next task, if one exists, in LIFO order. Call only |
694 |
< |
* by owner in unshared queues. (We do not have a shared |
720 |
< |
* version of this method because it is never needed.) |
694 |
> |
* by owner in unshared queues. |
695 |
|
*/ |
696 |
|
final ForkJoinTask<?> pop() { |
697 |
|
ForkJoinTask<?>[] a; ForkJoinTask<?> t; int m; |
709 |
|
return null; |
710 |
|
} |
711 |
|
|
712 |
+ |
final ForkJoinTask<?> sharedPop() { |
713 |
+ |
ForkJoinTask<?> task = null; |
714 |
+ |
if (runState == 0 && U.compareAndSwapInt(this, RUNSTATE, 0, 1)) { |
715 |
+ |
try { |
716 |
+ |
ForkJoinTask<?>[] a; int m; |
717 |
+ |
if ((a = array) != null && (m = a.length - 1) >= 0) { |
718 |
+ |
for (int s; (s = top - 1) - base >= 0;) { |
719 |
+ |
long j = ((m & s) << ASHIFT) + ABASE; |
720 |
+ |
ForkJoinTask<?> t = |
721 |
+ |
(ForkJoinTask<?>)U.getObject(a, j); |
722 |
+ |
if (t == null) |
723 |
+ |
break; |
724 |
+ |
if (U.compareAndSwapObject(a, j, t, null)) { |
725 |
+ |
top = s; |
726 |
+ |
task = t; |
727 |
+ |
break; |
728 |
+ |
} |
729 |
+ |
} |
730 |
+ |
} |
731 |
+ |
} finally { |
732 |
+ |
runState = 0; |
733 |
+ |
} |
734 |
+ |
} |
735 |
+ |
return task; |
736 |
+ |
} |
737 |
+ |
|
738 |
+ |
|
739 |
|
/** |
740 |
|
* Takes a task in FIFO order if b is base of queue and a task |
741 |
|
* can be claimed without contention. Specialized versions |
814 |
|
|
815 |
|
/** |
816 |
|
* Version of tryUnpush for shared queues; called by non-FJ |
817 |
< |
* submitters. Conservatively fails to unpush if all workers |
817 |
< |
* are active unless there are multiple tasks in queue. |
817 |
> |
* submitters after prechecking that task probably exists. |
818 |
|
*/ |
819 |
< |
final boolean trySharedUnpush(ForkJoinTask<?> task, ForkJoinPool p) { |
819 |
> |
final boolean trySharedUnpush(ForkJoinTask<?> t) { |
820 |
|
boolean success = false; |
821 |
< |
if (task != null && top != base && runState == 0 && |
822 |
< |
U.compareAndSwapInt(this, RUNSTATE, 0, 1)) { |
821 |
> |
if (runState == 0 && U.compareAndSwapInt(this, RUNSTATE, 0, 1)) { |
822 |
|
try { |
823 |
< |
ForkJoinTask<?>[] a; int n, s; |
824 |
< |
if ((a = array) != null && (n = (s = top) - base) > 0 && |
825 |
< |
(n > 1 || p == null || (int)(p.ctl >> AC_SHIFT) < 0)) { |
826 |
< |
int j = (((a.length - 1) & --s) << ASHIFT) + ABASE; |
827 |
< |
if (U.getObjectVolatile(a, j) == task && |
828 |
< |
U.compareAndSwapObject(a, j, task, null)) { |
830 |
< |
top = s; |
831 |
< |
success = true; |
832 |
< |
} |
823 |
> |
ForkJoinTask<?>[] a; int s; |
824 |
> |
if ((a = array) != null && (s = top) != base && |
825 |
> |
U.compareAndSwapObject |
826 |
> |
(a, (((a.length - 1) & --s) << ASHIFT) + ABASE, t, null)) { |
827 |
> |
top = s; |
828 |
> |
success = true; |
829 |
|
} |
830 |
|
} finally { |
831 |
|
runState = 0; // unlock |
909 |
|
return seed = r ^= r << 5; |
910 |
|
} |
911 |
|
|
912 |
< |
// Execution methods |
912 |
> |
// Specialized execution methods |
913 |
|
|
914 |
|
/** |
915 |
|
* Pops and runs tasks until empty. |
988 |
|
} |
989 |
|
|
990 |
|
/** |
991 |
+ |
* Version of shared pop that takes top element only if it |
992 |
+ |
* its root is the given CountedCompleter. |
993 |
+ |
*/ |
994 |
+ |
final CountedCompleter<?> sharedPopCC(CountedCompleter<?> root) { |
995 |
+ |
CountedCompleter<?> task = null; |
996 |
+ |
if (runState == 0 && U.compareAndSwapInt(this, RUNSTATE, 0, 1)) { |
997 |
+ |
try { |
998 |
+ |
ForkJoinTask<?>[] a; int m; |
999 |
+ |
if ((a = array) != null && (m = a.length - 1) >= 0) { |
1000 |
+ |
outer:for (int s; (s = top - 1) - base >= 0;) { |
1001 |
+ |
long j = ((m & s) << ASHIFT) + ABASE; |
1002 |
+ |
ForkJoinTask<?> t = |
1003 |
+ |
(ForkJoinTask<?>)U.getObject(a, j); |
1004 |
+ |
if (t == null || !(t instanceof CountedCompleter)) |
1005 |
+ |
break; |
1006 |
+ |
CountedCompleter<?> cc = (CountedCompleter<?>)t; |
1007 |
+ |
for (CountedCompleter<?> q = cc, p;;) { |
1008 |
+ |
if (q == root) { |
1009 |
+ |
if (U.compareAndSwapObject(a, j, cc, null)) { |
1010 |
+ |
top = s; |
1011 |
+ |
task = cc; |
1012 |
+ |
break outer; |
1013 |
+ |
} |
1014 |
+ |
break; |
1015 |
+ |
} |
1016 |
+ |
if ((p = q.completer) == null) |
1017 |
+ |
break outer; |
1018 |
+ |
q = p; |
1019 |
+ |
} |
1020 |
+ |
} |
1021 |
+ |
} |
1022 |
+ |
} finally { |
1023 |
+ |
runState = 0; |
1024 |
+ |
} |
1025 |
+ |
} |
1026 |
+ |
return task; |
1027 |
+ |
} |
1028 |
+ |
|
1029 |
+ |
/** |
1030 |
|
* Executes a top-level task and any local tasks remaining |
1031 |
|
* after execution. |
1032 |
|
*/ |
1147 |
|
public static final ForkJoinWorkerThreadFactory |
1148 |
|
defaultForkJoinWorkerThreadFactory; |
1149 |
|
|
1150 |
+ |
/** Property prefix for constructing common pool */ |
1151 |
+ |
private static final String propPrefix = |
1152 |
+ |
"java.util.concurrent.ForkJoinPool.common."; |
1153 |
+ |
|
1154 |
+ |
/** |
1155 |
+ |
* Common (static) pool. Non-null for public use unless a static |
1156 |
+ |
* construction exception, but internal usages must null-check on |
1157 |
+ |
* use. |
1158 |
+ |
*/ |
1159 |
+ |
static final ForkJoinPool commonPool; |
1160 |
+ |
|
1161 |
+ |
/** |
1162 |
+ |
* Common pool parallelism. Must equal commonPool.parallelism. |
1163 |
+ |
*/ |
1164 |
+ |
static final int commonPoolParallelism; |
1165 |
+ |
|
1166 |
|
/** |
1167 |
|
* Generator for assigning sequence numbers as pool names. |
1168 |
|
*/ |
1187 |
|
*/ |
1188 |
|
private static final ThreadSubmitter submitters; |
1189 |
|
|
1139 |
– |
/** Common default pool */ |
1140 |
– |
static volatile ForkJoinPool commonPool; |
1141 |
– |
|
1142 |
– |
// commonPool construction parameters |
1143 |
– |
private static final String propPrefix = |
1144 |
– |
"java.util.concurrent.ForkJoinPool.common."; |
1145 |
– |
private static final Thread.UncaughtExceptionHandler commonPoolUEH; |
1146 |
– |
private static final ForkJoinWorkerThreadFactory commonPoolFactory; |
1147 |
– |
static final int commonPoolParallelism; |
1148 |
– |
|
1149 |
– |
/** Static initialization lock */ |
1150 |
– |
private static final Mutex initializationLock; |
1151 |
– |
|
1190 |
|
// static constants |
1191 |
|
|
1192 |
|
/** |
1193 |
< |
* Initial timeout value (in nanoseconds) for the tread triggering |
1193 |
> |
* Initial timeout value (in nanoseconds) for the thread triggering |
1194 |
|
* quiescence to park waiting for new work. On timeout, the thread |
1195 |
|
* will instead try to shrink the number of workers. |
1196 |
|
*/ |
1319 |
|
* empirically works OK on current JVMs. |
1320 |
|
*/ |
1321 |
|
|
1322 |
+ |
volatile long stealCount; // collects worker counts |
1323 |
|
volatile long ctl; // main pool control |
1324 |
|
final int parallelism; // parallelism level |
1325 |
|
final int localMode; // per-worker scheduling mode |
1326 |
+ |
volatile int nextWorkerNumber; // to create worker name string |
1327 |
|
final int submitMask; // submit queue index bound |
1328 |
|
int nextSeed; // for initializing worker seeds |
1329 |
+ |
volatile int mainLock; // spinlock for array updates |
1330 |
|
volatile int runState; // shutdown status and seq |
1331 |
|
WorkQueue[] workQueues; // main registry |
1291 |
– |
final Mutex lock; // for registration |
1292 |
– |
final Condition termination; // for awaitTermination |
1332 |
|
final ForkJoinWorkerThreadFactory factory; // factory for new workers |
1333 |
|
final Thread.UncaughtExceptionHandler ueh; // per-worker UEH |
1334 |
< |
final AtomicLong stealCount; // collect counts when terminated |
1335 |
< |
final AtomicInteger nextWorkerNumber; // to create worker name string |
1336 |
< |
String workerNamePrefix; // to create worker name string |
1334 |
> |
final String workerNamePrefix; // to create worker name string |
1335 |
> |
|
1336 |
> |
/* |
1337 |
> |
* Mechanics for main lock protecting worker array updates. Uses |
1338 |
> |
* the same strategy as ConcurrentHashMap bins -- a spinLock for |
1339 |
> |
* normal cases, but falling back to builtin lock when (rarely) |
1340 |
> |
* needed. See internal ConcurrentHashMap documentation for |
1341 |
> |
* explanation. |
1342 |
> |
*/ |
1343 |
> |
|
1344 |
> |
static final int LOCK_WAITING = 2; // bit to indicate need for signal |
1345 |
> |
static final int MAX_LOCK_SPINS = 1 << 8; |
1346 |
> |
|
1347 |
> |
private void tryAwaitMainLock() { |
1348 |
> |
int spins = MAX_LOCK_SPINS, r = 0, h; |
1349 |
> |
while (((h = mainLock) & 1) != 0) { |
1350 |
> |
if (r == 0) |
1351 |
> |
r = ThreadLocalRandom.current().nextInt(); // randomize spins |
1352 |
> |
else if (spins >= 0) { |
1353 |
> |
r ^= r << 1; r ^= r >>> 3; r ^= r << 10; // xorshift |
1354 |
> |
if (r >= 0) |
1355 |
> |
--spins; |
1356 |
> |
} |
1357 |
> |
else if (U.compareAndSwapInt(this, MAINLOCK, h, h | LOCK_WAITING)) { |
1358 |
> |
synchronized (this) { |
1359 |
> |
if ((mainLock & LOCK_WAITING) != 0) { |
1360 |
> |
try { |
1361 |
> |
wait(); |
1362 |
> |
} catch (InterruptedException ie) { |
1363 |
> |
try { |
1364 |
> |
Thread.currentThread().interrupt(); |
1365 |
> |
} catch (SecurityException ignore) { |
1366 |
> |
} |
1367 |
> |
} |
1368 |
> |
} |
1369 |
> |
else |
1370 |
> |
notifyAll(); // possibly won race vs signaller |
1371 |
> |
} |
1372 |
> |
break; |
1373 |
> |
} |
1374 |
> |
} |
1375 |
> |
} |
1376 |
|
|
1377 |
|
// Creating, registering, and deregistering workers |
1378 |
|
|
1400 |
|
* ForkJoinWorkerThread. |
1401 |
|
*/ |
1402 |
|
final String nextWorkerName() { |
1403 |
< |
return workerNamePrefix.concat |
1404 |
< |
(Integer.toString(nextWorkerNumber.addAndGet(1))); |
1403 |
> |
int n; |
1404 |
> |
do {} while (!U.compareAndSwapInt(this, NEXTWORKERNUMBER, |
1405 |
> |
n = nextWorkerNumber, ++n)); |
1406 |
> |
return workerNamePrefix.concat(Integer.toString(n)); |
1407 |
|
} |
1408 |
|
|
1409 |
|
/** |
1416 |
|
* @param w the worker's queue |
1417 |
|
*/ |
1418 |
|
final void registerWorker(WorkQueue w) { |
1419 |
< |
Mutex lock = this.lock; |
1420 |
< |
lock.lock(); |
1419 |
> |
while (!U.compareAndSwapInt(this, MAINLOCK, 0, 1)) |
1420 |
> |
tryAwaitMainLock(); |
1421 |
|
try { |
1422 |
< |
WorkQueue[] ws = workQueues; |
1423 |
< |
if (w != null && ws != null) { // skip on shutdown/failure |
1424 |
< |
int rs, n = ws.length, m = n - 1; |
1422 |
> |
WorkQueue[] ws; |
1423 |
> |
if ((ws = workQueues) == null) |
1424 |
> |
ws = workQueues = new WorkQueue[submitMask + 1]; |
1425 |
> |
if (w != null) { |
1426 |
> |
int rs, n = ws.length, m = n - 1; |
1427 |
|
int s = nextSeed += SEED_INCREMENT; // rarely-colliding sequence |
1428 |
|
w.seed = (s == 0) ? 1 : s; // ensure non-zero seed |
1429 |
|
int r = (s << 1) | 1; // use odd-numbered indices |
1443 |
|
runState = ((rs = runState) & SHUTDOWN) | ((rs + 2) & ~SHUTDOWN); |
1444 |
|
} |
1445 |
|
} finally { |
1446 |
< |
lock.unlock(); |
1446 |
> |
if (!U.compareAndSwapInt(this, MAINLOCK, 1, 0)) { |
1447 |
> |
mainLock = 0; |
1448 |
> |
synchronized (this) { notifyAll(); }; |
1449 |
> |
} |
1450 |
|
} |
1451 |
|
} |
1452 |
|
|
1460 |
|
* @param ex the exception causing failure, or null if none |
1461 |
|
*/ |
1462 |
|
final void deregisterWorker(ForkJoinWorkerThread wt, Throwable ex) { |
1378 |
– |
Mutex lock = this.lock; |
1463 |
|
WorkQueue w = null; |
1464 |
|
if (wt != null && (w = wt.workQueue) != null) { |
1465 |
|
w.runState = -1; // ensure runState is set |
1466 |
< |
stealCount.getAndAdd(w.totalSteals + w.nsteals); |
1466 |
> |
long steals = w.totalSteals + w.nsteals, sc; |
1467 |
> |
do {} while (!U.compareAndSwapLong(this, STEALCOUNT, |
1468 |
> |
sc = stealCount, sc + steals)); |
1469 |
|
int idx = w.poolIndex; |
1470 |
< |
lock.lock(); |
1471 |
< |
try { // remove record from array |
1470 |
> |
while (!U.compareAndSwapInt(this, MAINLOCK, 0, 1)) |
1471 |
> |
tryAwaitMainLock(); |
1472 |
> |
try { |
1473 |
|
WorkQueue[] ws = workQueues; |
1474 |
|
if (ws != null && idx >= 0 && idx < ws.length && ws[idx] == w) |
1475 |
|
ws[idx] = null; |
1476 |
|
} finally { |
1477 |
< |
lock.unlock(); |
1477 |
> |
if (!U.compareAndSwapInt(this, MAINLOCK, 1, 0)) { |
1478 |
> |
mainLock = 0; |
1479 |
> |
synchronized (this) { notifyAll(); }; |
1480 |
> |
} |
1481 |
|
} |
1482 |
|
} |
1483 |
|
|
1496 |
|
} |
1497 |
|
|
1498 |
|
if (ex != null) // rethrow |
1499 |
< |
U.throwException(ex); |
1499 |
> |
ForkJoinTask.rethrow(ex); |
1500 |
|
} |
1501 |
|
|
1502 |
|
// Submissions |
1516 |
|
for (int r = s.seed, m = submitMask;;) { |
1517 |
|
WorkQueue[] ws; WorkQueue q; |
1518 |
|
int k = r & m & SQMASK; // use only even indices |
1519 |
< |
if (runState < 0 || (ws = workQueues) == null || ws.length <= k) |
1519 |
> |
if (runState < 0) |
1520 |
|
throw new RejectedExecutionException(); // shutting down |
1521 |
+ |
else if ((ws = workQueues) == null || ws.length <= k) { |
1522 |
+ |
while (!U.compareAndSwapInt(this, MAINLOCK, 0, 1)) |
1523 |
+ |
tryAwaitMainLock(); |
1524 |
+ |
try { |
1525 |
+ |
if (workQueues == null) |
1526 |
+ |
workQueues = new WorkQueue[submitMask + 1]; |
1527 |
+ |
} finally { |
1528 |
+ |
if (!U.compareAndSwapInt(this, MAINLOCK, 1, 0)) { |
1529 |
+ |
mainLock = 0; |
1530 |
+ |
synchronized (this) { notifyAll(); }; |
1531 |
+ |
} |
1532 |
+ |
} |
1533 |
+ |
} |
1534 |
|
else if ((q = ws[k]) == null) { // create new queue |
1535 |
|
WorkQueue nq = new WorkQueue(this, null, SHARED_QUEUE); |
1536 |
< |
Mutex lock = this.lock; // construct outside lock |
1537 |
< |
lock.lock(); |
1538 |
< |
try { // recheck under lock |
1536 |
> |
while (!U.compareAndSwapInt(this, MAINLOCK, 0, 1)) |
1537 |
> |
tryAwaitMainLock(); |
1538 |
> |
try { |
1539 |
|
int rs = runState; // to update seq |
1540 |
|
if (ws == workQueues && ws[k] == null) { |
1541 |
|
ws[k] = nq; |
1542 |
|
runState = ((rs & SHUTDOWN) | ((rs + 2) & ~SHUTDOWN)); |
1543 |
|
} |
1544 |
|
} finally { |
1545 |
< |
lock.unlock(); |
1545 |
> |
if (!U.compareAndSwapInt(this, MAINLOCK, 1, 0)) { |
1546 |
> |
mainLock = 0; |
1547 |
> |
synchronized (this) { notifyAll(); }; |
1548 |
> |
} |
1549 |
|
} |
1550 |
|
} |
1551 |
|
else if (q.trySharedPush(task)) { |
1568 |
|
static void submitToCommonPool(ForkJoinTask<?> task) { |
1569 |
|
ForkJoinPool p; |
1570 |
|
if ((p = commonPool) == null) |
1571 |
< |
p = ensureCommonPool(); |
1571 |
> |
throw new RejectedExecutionException("Common Pool Unavailable"); |
1572 |
|
p.doSubmit(task); |
1573 |
|
} |
1574 |
|
|
1582 |
|
* @return true if successful |
1583 |
|
*/ |
1584 |
|
static boolean tryUnsubmitFromCommonPool(ForkJoinTask<?> task) { |
1585 |
< |
ForkJoinPool p; WorkQueue[] ws; WorkQueue q; |
1586 |
< |
int k = submitters.get().seed & SQMASK; |
1587 |
< |
return ((p = commonPool) != null && |
1588 |
< |
(ws = p.workQueues) != null && |
1589 |
< |
ws.length > (k &= p.submitMask) && |
1590 |
< |
(q = ws[k]) != null && |
1591 |
< |
q.trySharedUnpush(task, p)); |
1585 |
> |
// If not oversaturating platform, peek, looking for task and |
1586 |
> |
// eligibility before using trySharedUnpush to actually take |
1587 |
> |
// it under lock |
1588 |
> |
ForkJoinPool p; WorkQueue[] ws; WorkQueue w, q; |
1589 |
> |
ForkJoinTask<?>[] a; int ac, s, m; |
1590 |
> |
if ((p = commonPool) != null && (ws = p.workQueues) != null) { |
1591 |
> |
int k = submitters.get().seed & p.submitMask & SQMASK; |
1592 |
> |
if ((m = ws.length - 1) >= k && (q = ws[k]) != null && |
1593 |
> |
(ac = (int)(p.ctl >> AC_SHIFT)) <= 0) { |
1594 |
> |
if (ac == 0) { // double check if all workers active |
1595 |
> |
for (int i = 1; i <= m; i += 2) { |
1596 |
> |
if ((w = ws[i]) != null && w.parker != null) { |
1597 |
> |
ac = -1; |
1598 |
> |
break; |
1599 |
> |
} |
1600 |
> |
} |
1601 |
> |
} |
1602 |
> |
return (ac < 0 && (a = q.array) != null && |
1603 |
> |
(s = q.top - 1) - q.base >= 0 && |
1604 |
> |
s >= 0 && s < a.length && |
1605 |
> |
a[s] == task && |
1606 |
> |
q.trySharedUnpush(task)); |
1607 |
> |
} |
1608 |
> |
} |
1609 |
> |
return false; |
1610 |
> |
} |
1611 |
> |
|
1612 |
> |
/** |
1613 |
> |
* Tries to pop and run a task within same computation from common pool |
1614 |
> |
*/ |
1615 |
> |
static void popAndExecCCFromCommonPool(CountedCompleter<?> cc) { |
1616 |
> |
ForkJoinPool p; WorkQueue[] ws; WorkQueue q, w; int m, ac; |
1617 |
> |
CountedCompleter<?> par, task; |
1618 |
> |
if ((p = commonPool) != null && (ws = p.workQueues) != null) { |
1619 |
> |
while ((par = cc.completer) != null) // find root |
1620 |
> |
cc = par; |
1621 |
> |
int k = submitters.get().seed & p.submitMask & SQMASK; |
1622 |
> |
if ((m = ws.length - 1) >= k && (q = ws[k]) != null && |
1623 |
> |
(ac = (int)(p.ctl >> AC_SHIFT)) <= 0) { |
1624 |
> |
if (ac == 0) { |
1625 |
> |
for (int i = 1; i <= m; i += 2) { |
1626 |
> |
if ((w = ws[i]) != null && w.parker != null) { |
1627 |
> |
ac = -1; |
1628 |
> |
break; |
1629 |
> |
} |
1630 |
> |
} |
1631 |
> |
} |
1632 |
> |
if (ac < 0 && q.top - q.base > 0 && |
1633 |
> |
(task = q.sharedPopCC(cc)) != null) |
1634 |
> |
task.exec(); |
1635 |
> |
} |
1636 |
> |
} |
1637 |
|
} |
1638 |
|
|
1639 |
|
// Maintaining ctl counts |
2107 |
|
*/ |
2108 |
|
private WorkQueue findNonEmptyStealQueue(WorkQueue w) { |
2109 |
|
// Similar to loop in scan(), but ignoring submissions |
2110 |
< |
int r; |
1960 |
< |
if (w == null) // allow external callers |
1961 |
< |
r = ThreadLocalRandom.current().nextInt(); |
1962 |
< |
else { |
1963 |
< |
r = w.seed; r ^= r << 13; r ^= r >>> 17; w.seed = r ^= r << 5; |
1964 |
< |
} |
2110 |
> |
int r = w.seed; r ^= r << 13; r ^= r >>> 17; w.seed = r ^= r << 5; |
2111 |
|
int step = (r >>> 16) | 1; |
2112 |
|
for (WorkQueue[] ws;;) { |
2113 |
|
int rs = runState, m; |
2171 |
|
* Restricted version of helpQuiescePool for non-FJ callers |
2172 |
|
*/ |
2173 |
|
static void externalHelpQuiescePool() { |
2174 |
< |
ForkJoinPool p; WorkQueue[] ws; WorkQueue w, q; |
2175 |
< |
ForkJoinTask<?> t; int b; |
2174 |
> |
ForkJoinPool p; WorkQueue[] ws; WorkQueue q, sq; |
2175 |
> |
ForkJoinTask<?>[] a; int b; |
2176 |
> |
ForkJoinTask<?> t = null; |
2177 |
|
int k = submitters.get().seed & SQMASK; |
2178 |
|
if ((p = commonPool) != null && |
2179 |
|
(ws = p.workQueues) != null && |
2180 |
|
ws.length > (k &= p.submitMask) && |
2181 |
< |
(w = ws[k]) != null && |
2182 |
< |
(q = p.findNonEmptyStealQueue(w)) != null && |
2183 |
< |
(b = q.base) - q.top < 0 && |
2184 |
< |
(t = q.pollAt(b)) != null) |
2185 |
< |
t.doExec(); |
2181 |
> |
(q = ws[k]) != null) { |
2182 |
> |
while (q.top - q.base > 0) { |
2183 |
> |
if ((t = q.sharedPop()) != null) |
2184 |
> |
break; |
2185 |
> |
} |
2186 |
> |
if (t == null && (sq = p.findNonEmptyStealQueue(q)) != null && |
2187 |
> |
(b = sq.base) - sq.top < 0) |
2188 |
> |
t = sq.pollAt(b); |
2189 |
> |
if (t != null) |
2190 |
> |
t.doExec(); |
2191 |
> |
} |
2192 |
|
} |
2193 |
|
|
2194 |
|
/** |
2230 |
|
static int getEstimatedSubmitterQueueLength() { |
2231 |
|
ForkJoinPool p; WorkQueue[] ws; WorkQueue q; |
2232 |
|
int k = submitters.get().seed & SQMASK; |
2233 |
< |
return ((p = commonPool) != null && |
2081 |
< |
p.runState >= 0 && |
2082 |
< |
(ws = p.workQueues) != null && |
2233 |
> |
return ((p = commonPool) != null && (ws = p.workQueues) != null && |
2234 |
|
ws.length > (k &= p.submitMask) && |
2235 |
|
(q = ws[k]) != null) ? |
2236 |
|
q.queueSize() : 0; |
2253 |
|
* @return true if now terminating or terminated |
2254 |
|
*/ |
2255 |
|
private boolean tryTerminate(boolean now, boolean enable) { |
2105 |
– |
Mutex lock = this.lock; |
2256 |
|
for (long c;;) { |
2257 |
|
if (((c = ctl) & STOP_BIT) != 0) { // already terminating |
2258 |
|
if ((short)(c >>> TC_SHIFT) == -parallelism) { |
2259 |
< |
lock.lock(); // don't need try/finally |
2260 |
< |
termination.signalAll(); // signal when 0 workers |
2261 |
< |
lock.unlock(); |
2259 |
> |
synchronized (this) { |
2260 |
> |
notifyAll(); // signal when 0 workers |
2261 |
> |
} |
2262 |
|
} |
2263 |
|
return true; |
2264 |
|
} |
2265 |
|
if (runState >= 0) { // not yet enabled |
2266 |
|
if (!enable) |
2267 |
|
return false; |
2268 |
< |
lock.lock(); |
2269 |
< |
runState |= SHUTDOWN; |
2270 |
< |
lock.unlock(); |
2268 |
> |
while (!U.compareAndSwapInt(this, MAINLOCK, 0, 1)) |
2269 |
> |
tryAwaitMainLock(); |
2270 |
> |
try { |
2271 |
> |
runState |= SHUTDOWN; |
2272 |
> |
} finally { |
2273 |
> |
if (!U.compareAndSwapInt(this, MAINLOCK, 1, 0)) { |
2274 |
> |
mainLock = 0; |
2275 |
> |
synchronized (this) { notifyAll(); }; |
2276 |
> |
} |
2277 |
> |
} |
2278 |
|
} |
2279 |
|
if (!now) { // check if idle & no tasks |
2280 |
|
if ((int)(c >> AC_SHIFT) != -parallelism || |
2407 |
|
// Use nearest power 2 for workQueues size. See Hackers Delight sec 3.2. |
2408 |
|
int n = parallelism - 1; |
2409 |
|
n |= n >>> 1; n |= n >>> 2; n |= n >>> 4; n |= n >>> 8; n |= n >>> 16; |
2410 |
< |
int size = (n + 1) << 1; // #slots = 2*#workers |
2254 |
< |
this.submitMask = size - 1; // room for max # of submit queues |
2255 |
< |
this.workQueues = new WorkQueue[size]; |
2256 |
< |
this.termination = (this.lock = new Mutex()).newCondition(); |
2257 |
< |
this.stealCount = new AtomicLong(); |
2258 |
< |
this.nextWorkerNumber = new AtomicInteger(); |
2410 |
> |
this.submitMask = ((n + 1) << 1) - 1; |
2411 |
|
int pn = poolNumberGenerator.incrementAndGet(); |
2412 |
|
StringBuilder sb = new StringBuilder("ForkJoinPool-"); |
2413 |
|
sb.append(Integer.toString(pn)); |
2414 |
|
sb.append("-worker-"); |
2415 |
|
this.workerNamePrefix = sb.toString(); |
2264 |
– |
lock.lock(); |
2416 |
|
this.runState = 1; // set init flag |
2266 |
– |
lock.unlock(); |
2417 |
|
} |
2418 |
|
|
2419 |
|
/** |
2420 |
< |
* Returns the common pool instance |
2420 |
> |
* Constructor for common pool, suitable only for static initialization. |
2421 |
> |
* Basically the same as above, but uses smallest possible initial footprint. |
2422 |
> |
*/ |
2423 |
> |
ForkJoinPool(int parallelism, int submitMask, |
2424 |
> |
ForkJoinWorkerThreadFactory factory, |
2425 |
> |
Thread.UncaughtExceptionHandler handler) { |
2426 |
> |
this.factory = factory; |
2427 |
> |
this.ueh = handler; |
2428 |
> |
this.submitMask = submitMask; |
2429 |
> |
this.parallelism = parallelism; |
2430 |
> |
long np = (long)(-parallelism); |
2431 |
> |
this.ctl = ((np << AC_SHIFT) & AC_MASK) | ((np << TC_SHIFT) & TC_MASK); |
2432 |
> |
this.localMode = LIFO_QUEUE; |
2433 |
> |
this.workerNamePrefix = "ForkJoinPool.commonPool-worker-"; |
2434 |
> |
this.runState = 1; |
2435 |
> |
} |
2436 |
> |
|
2437 |
> |
/** |
2438 |
> |
* Returns the common pool instance. |
2439 |
|
* |
2440 |
|
* @return the common pool instance |
2441 |
|
*/ |
2442 |
|
public static ForkJoinPool commonPool() { |
2443 |
|
ForkJoinPool p; |
2444 |
< |
return (p = commonPool) != null? p : ensureCommonPool(); |
2445 |
< |
} |
2278 |
< |
|
2279 |
< |
private static ForkJoinPool ensureCommonPool() { |
2280 |
< |
ForkJoinPool p; |
2281 |
< |
if ((p = commonPool) == null) { |
2282 |
< |
final Mutex lock = initializationLock; |
2283 |
< |
lock.lock(); |
2284 |
< |
try { |
2285 |
< |
if ((p = commonPool) == null) { |
2286 |
< |
p = commonPool = new ForkJoinPool(commonPoolParallelism, |
2287 |
< |
commonPoolFactory, |
2288 |
< |
commonPoolUEH, false); |
2289 |
< |
// use a more informative name string for workers |
2290 |
< |
p.workerNamePrefix = "ForkJoinPool.commonPool-worker-"; |
2291 |
< |
} |
2292 |
< |
} finally { |
2293 |
< |
lock.unlock(); |
2294 |
< |
} |
2295 |
< |
} |
2444 |
> |
if ((p = commonPool) == null) |
2445 |
> |
throw new Error("Common Pool Unavailable"); |
2446 |
|
return p; |
2447 |
|
} |
2448 |
|
|
2708 |
|
* @return the number of steals |
2709 |
|
*/ |
2710 |
|
public long getStealCount() { |
2711 |
< |
long count = stealCount.get(); |
2711 |
> |
long count = stealCount; |
2712 |
|
WorkQueue[] ws; WorkQueue w; |
2713 |
|
if ((ws = workQueues) != null) { |
2714 |
|
for (int i = 1; i < ws.length; i += 2) { |
2838 |
|
public String toString() { |
2839 |
|
// Use a single pass through workQueues to collect counts |
2840 |
|
long qt = 0L, qs = 0L; int rc = 0; |
2841 |
< |
long st = stealCount.get(); |
2841 |
> |
long st = stealCount; |
2842 |
|
long c = ctl; |
2843 |
|
WorkQueue[] ws; WorkQueue w; |
2844 |
|
if ((ws = workQueues) != null) { |
2976 |
|
public boolean awaitTermination(long timeout, TimeUnit unit) |
2977 |
|
throws InterruptedException { |
2978 |
|
long nanos = unit.toNanos(timeout); |
2979 |
< |
final Mutex lock = this.lock; |
2980 |
< |
lock.lock(); |
2981 |
< |
try { |
2982 |
< |
for (;;) { |
2983 |
< |
if (isTerminated()) |
2984 |
< |
return true; |
2985 |
< |
if (nanos <= 0) |
2986 |
< |
return false; |
2987 |
< |
nanos = termination.awaitNanos(nanos); |
2979 |
> |
if (isTerminated()) |
2980 |
> |
return true; |
2981 |
> |
long startTime = System.nanoTime(); |
2982 |
> |
boolean terminated = false; |
2983 |
> |
synchronized (this) { |
2984 |
> |
for (long waitTime = nanos, millis = 0L;;) { |
2985 |
> |
if (terminated = isTerminated() || |
2986 |
> |
waitTime <= 0L || |
2987 |
> |
(millis = unit.toMillis(waitTime)) <= 0L) |
2988 |
> |
break; |
2989 |
> |
wait(millis); |
2990 |
> |
waitTime = nanos - (System.nanoTime() - startTime); |
2991 |
|
} |
2839 |
– |
} finally { |
2840 |
– |
lock.unlock(); |
2992 |
|
} |
2993 |
+ |
return terminated; |
2994 |
|
} |
2995 |
|
|
2996 |
|
/** |
3122 |
|
private static final long PARKBLOCKER; |
3123 |
|
private static final int ABASE; |
3124 |
|
private static final int ASHIFT; |
3125 |
+ |
private static final long NEXTWORKERNUMBER; |
3126 |
+ |
private static final long STEALCOUNT; |
3127 |
+ |
private static final long MAINLOCK; |
3128 |
|
|
3129 |
|
static { |
3130 |
|
poolNumberGenerator = new AtomicInteger(); |
3133 |
|
defaultForkJoinWorkerThreadFactory = |
3134 |
|
new DefaultForkJoinWorkerThreadFactory(); |
3135 |
|
submitters = new ThreadSubmitter(); |
2981 |
– |
initializationLock = new Mutex(); |
3136 |
|
int s; |
3137 |
|
try { |
3138 |
|
U = getUnsafe(); |
3140 |
|
Class<?> ak = ForkJoinTask[].class; |
3141 |
|
CTL = U.objectFieldOffset |
3142 |
|
(k.getDeclaredField("ctl")); |
3143 |
+ |
NEXTWORKERNUMBER = U.objectFieldOffset |
3144 |
+ |
(k.getDeclaredField("nextWorkerNumber")); |
3145 |
+ |
STEALCOUNT = U.objectFieldOffset |
3146 |
+ |
(k.getDeclaredField("stealCount")); |
3147 |
+ |
MAINLOCK = U.objectFieldOffset |
3148 |
+ |
(k.getDeclaredField("mainLock")); |
3149 |
|
Class<?> tk = Thread.class; |
3150 |
|
PARKBLOCKER = U.objectFieldOffset |
3151 |
|
(tk.getDeclaredField("parkBlocker")); |
3152 |
|
ABASE = U.arrayBaseOffset(ak); |
3153 |
|
s = U.arrayIndexScale(ak); |
3154 |
+ |
ASHIFT = 31 - Integer.numberOfLeadingZeros(s); |
3155 |
|
} catch (Exception e) { |
3156 |
|
throw new Error(e); |
3157 |
|
} |
3158 |
|
if ((s & (s-1)) != 0) |
3159 |
|
throw new Error("data type scale not a power of two"); |
3160 |
< |
ASHIFT = 31 - Integer.numberOfLeadingZeros(s); |
3000 |
< |
|
3001 |
< |
// Establish configuration for default pool |
3002 |
< |
try { |
3160 |
> |
try { // Establish common pool |
3161 |
|
String pp = System.getProperty(propPrefix + "parallelism"); |
3162 |
|
String fp = System.getProperty(propPrefix + "threadFactory"); |
3163 |
|
String up = System.getProperty(propPrefix + "exceptionHandler"); |
3164 |
+ |
ForkJoinWorkerThreadFactory fac = (fp == null) ? |
3165 |
+ |
defaultForkJoinWorkerThreadFactory : |
3166 |
+ |
((ForkJoinWorkerThreadFactory)ClassLoader. |
3167 |
+ |
getSystemClassLoader().loadClass(fp).newInstance()); |
3168 |
+ |
Thread.UncaughtExceptionHandler ueh = (up == null) ? null : |
3169 |
+ |
((Thread.UncaughtExceptionHandler)ClassLoader. |
3170 |
+ |
getSystemClassLoader().loadClass(up).newInstance()); |
3171 |
|
int par; |
3172 |
|
if ((pp == null || (par = Integer.parseInt(pp)) <= 0)) |
3173 |
|
par = Runtime.getRuntime().availableProcessors(); |
3174 |
+ |
if (par > MAX_CAP) |
3175 |
+ |
par = MAX_CAP; |
3176 |
|
commonPoolParallelism = par; |
3177 |
< |
if (fp != null) |
3178 |
< |
commonPoolFactory = (ForkJoinWorkerThreadFactory) |
3179 |
< |
ClassLoader.getSystemClassLoader().loadClass(fp).newInstance(); |
3180 |
< |
else |
3181 |
< |
commonPoolFactory = defaultForkJoinWorkerThreadFactory; |
3015 |
< |
if (up != null) |
3016 |
< |
commonPoolUEH = (Thread.UncaughtExceptionHandler) |
3017 |
< |
ClassLoader.getSystemClassLoader().loadClass(up).newInstance(); |
3018 |
< |
else |
3019 |
< |
commonPoolUEH = null; |
3177 |
> |
int n = par - 1; // precompute submit mask |
3178 |
> |
n |= n >>> 1; n |= n >>> 2; n |= n >>> 4; |
3179 |
> |
n |= n >>> 8; n |= n >>> 16; |
3180 |
> |
int mask = ((n + 1) << 1) - 1; |
3181 |
> |
commonPool = new ForkJoinPool(par, mask, fac, ueh); |
3182 |
|
} catch (Exception e) { |
3183 |
|
throw new Error(e); |
3184 |
|
} |