987 |
|
if (t != null) { |
988 |
|
(currentSteal = t).doExec(); |
989 |
|
currentSteal = null; |
990 |
+ |
++nsteals; |
991 |
|
if (base - top < 0) { // process remaining local tasks |
992 |
|
if (mode == 0) |
993 |
|
popAndExecAll(); |
994 |
|
else |
995 |
|
pollAndExecAll(); |
996 |
|
} |
996 |
– |
++nsteals; |
997 |
– |
hint = -1; |
997 |
|
} |
998 |
|
} |
999 |
|
|
1020 |
|
s != Thread.State.TIMED_WAITING); |
1021 |
|
} |
1022 |
|
|
1024 |
– |
/** |
1025 |
– |
* If this owned and is not already interrupted, try to |
1026 |
– |
* interrupt and/or unpark, ignoring exceptions. |
1027 |
– |
*/ |
1028 |
– |
final void interruptOwner() { |
1029 |
– |
Thread wt, p; |
1030 |
– |
if ((wt = owner) != null && !wt.isInterrupted()) { |
1031 |
– |
try { |
1032 |
– |
wt.interrupt(); |
1033 |
– |
} catch (SecurityException ignore) { |
1034 |
– |
} |
1035 |
– |
} |
1036 |
– |
if ((p = parker) != null) |
1037 |
– |
U.unpark(p); |
1038 |
– |
} |
1039 |
– |
|
1023 |
|
// Unsafe mechanics |
1024 |
|
private static final sun.misc.Unsafe U; |
1025 |
|
private static final long QLOCK; |
1087 |
|
private static int poolNumberSequence; |
1088 |
|
|
1089 |
|
/** |
1090 |
< |
* Return the next sequence number. We don't expect this to |
1091 |
< |
* ever contend so use simple builtin sync. |
1090 |
> |
* Returns the next sequence number. We don't expect this to |
1091 |
> |
* ever contend, so use simple builtin sync. |
1092 |
|
*/ |
1093 |
|
private static final synchronized int nextPoolId() { |
1094 |
|
return ++poolNumberSequence; |
1114 |
|
/** |
1115 |
|
* Tolerance for idle timeouts, to cope with timer undershoots |
1116 |
|
*/ |
1117 |
< |
private static final long TIMEOUT_SLOP = 2000000L; // 20ms |
1117 |
> |
private static final long TIMEOUT_SLOP = 2000000L; |
1118 |
|
|
1119 |
|
/** |
1120 |
|
* The maximum stolen->joining link depth allowed in method |
1251 |
|
* fails. This acts as a spinLock for normal cases, but falls back |
1252 |
|
* to builtin monitor to block when (rarely) needed. This would be |
1253 |
|
* a terrible idea for a highly contended lock, but works fine as |
1254 |
< |
* a more conservative alternative to a pure spinlock. See |
1272 |
< |
* internal ConcurrentHashMap documentation for further |
1273 |
< |
* explanation of nearly the same construction. |
1254 |
> |
* a more conservative alternative to a pure spinlock. |
1255 |
|
*/ |
1256 |
|
private int acquirePlock() { |
1257 |
|
int spins = PL_SPINS, r = 0, ps, nps; |
1303 |
|
} |
1304 |
|
|
1305 |
|
/** |
1325 |
– |
* Performs secondary initialization, called when plock is zero. |
1326 |
– |
* Creates workQueue array and sets plock to a valid value. The |
1327 |
– |
* lock body must be exception-free (so no try/finally) so we |
1328 |
– |
* optimistically allocate new array outside the lock and throw |
1329 |
– |
* away if (very rarely) not needed. (A similar tactic is used in |
1330 |
– |
* fullExternalPush.) Because the plock seq value can eventually |
1331 |
– |
* wrap around zero, this method harmlessly fails to reinitialize |
1332 |
– |
* if workQueues exists, while still advancing plock. |
1333 |
– |
* |
1334 |
– |
* Additionally tries to create the first worker. |
1335 |
– |
*/ |
1336 |
– |
private void initWorkers() { |
1337 |
– |
WorkQueue[] ws, nws; int ps; |
1338 |
– |
int p = config & SMASK; // find power of two table size |
1339 |
– |
int n = (p > 1) ? p - 1 : 1; // ensure at least 2 slots |
1340 |
– |
n |= n >>> 1; n |= n >>> 2; n |= n >>> 4; n |= n >>> 8; n |= n >>> 16; |
1341 |
– |
n = (n + 1) << 1; |
1342 |
– |
if ((ws = workQueues) == null || ws.length == 0) |
1343 |
– |
nws = new WorkQueue[n]; |
1344 |
– |
else |
1345 |
– |
nws = null; |
1346 |
– |
if (((ps = plock) & PL_LOCK) != 0 || |
1347 |
– |
!U.compareAndSwapInt(this, PLOCK, ps, ps += PL_LOCK)) |
1348 |
– |
ps = acquirePlock(); |
1349 |
– |
if (((ws = workQueues) == null || ws.length == 0) && nws != null) |
1350 |
– |
workQueues = nws; |
1351 |
– |
int nps = (ps & SHUTDOWN) | ((ps + PL_LOCK) & ~SHUTDOWN); |
1352 |
– |
if (!U.compareAndSwapInt(this, PLOCK, ps, nps)) |
1353 |
– |
releasePlock(nps); |
1354 |
– |
tryAddWorker(); |
1355 |
– |
} |
1356 |
– |
|
1357 |
– |
/** |
1306 |
|
* Tries to create and start one worker if fewer than target |
1307 |
|
* parallelism level exist. Adjusts counts etc on failure. |
1308 |
|
*/ |
1414 |
|
} |
1415 |
|
} |
1416 |
|
|
1417 |
< |
long c; // adjust ctl counts |
1417 |
> |
long c; // adjust ctl counts |
1418 |
|
do {} while (!U.compareAndSwapLong |
1419 |
|
(this, CTL, c = ctl, (((c - AC_UNIT) & AC_MASK) | |
1420 |
|
((c - TC_UNIT) & TC_MASK) | |
1421 |
|
(c & ~(AC_MASK|TC_MASK))))); |
1422 |
|
|
1423 |
|
if (!tryTerminate(false, false) && w != null && w.array != null) { |
1424 |
< |
w.cancelAll(); // cancel remaining tasks |
1425 |
< |
int e, u, i, n; WorkQueue[] ws; WorkQueue v; Thread p; |
1426 |
< |
while ((u = (int)((c = ctl) >>> 32)) < 0) { |
1427 |
< |
if ((e = (int)c) > 0) { // activate or create replacement |
1428 |
< |
if ((ws = workQueues) != null && |
1429 |
< |
ws.length > (i = e & SMASK) && |
1430 |
< |
(v = ws[i]) != null && v.eventCount == (e | INT_SIGN)) { |
1431 |
< |
long nc = (((long)(v.nextWait & E_MASK)) | |
1432 |
< |
((long)(u + UAC_UNIT) << 32)); |
1433 |
< |
if (U.compareAndSwapLong(this, CTL, c, nc)) { |
1434 |
< |
v.eventCount = (e + E_SEQ) & E_MASK; |
1487 |
< |
if ((p = v.parker) != null) |
1488 |
< |
U.unpark(p); |
1489 |
< |
break; |
1490 |
< |
} |
1491 |
< |
} |
1492 |
< |
else |
1424 |
> |
w.cancelAll(); // cancel remaining tasks |
1425 |
> |
WorkQueue[] ws; WorkQueue v; Thread p; int u, i, e; |
1426 |
> |
while ((u = (int)((c = ctl) >>> 32)) < 0 && (e = (int)c) >= 0) { |
1427 |
> |
if (e > 0) { // activate or create replacement |
1428 |
> |
if ((ws = workQueues) == null || |
1429 |
> |
(i = e & SMASK) >= ws.length || |
1430 |
> |
(v = ws[i]) == null) |
1431 |
> |
break; |
1432 |
> |
long nc = (((long)(v.nextWait & E_MASK)) | |
1433 |
> |
((long)(u + UAC_UNIT) << 32)); |
1434 |
> |
if (v.eventCount != (e | INT_SIGN)) |
1435 |
|
break; |
1436 |
+ |
if (U.compareAndSwapLong(this, CTL, c, nc)) { |
1437 |
+ |
v.eventCount = (e + E_SEQ) & E_MASK; |
1438 |
+ |
if ((p = v.parker) != null) |
1439 |
+ |
U.unpark(p); |
1440 |
+ |
break; |
1441 |
+ |
} |
1442 |
|
} |
1443 |
|
else { |
1444 |
|
if ((short)u < 0) |
1487 |
|
/** |
1488 |
|
* Full version of externalPush. This method is called, among |
1489 |
|
* other times, upon the first submission of the first task to the |
1490 |
< |
* pool, so must perform secondary initialization (via |
1491 |
< |
* initWorkers). It also detects first submission by an external |
1492 |
< |
* thread by looking up its ThreadLocal, and creates a new shared |
1493 |
< |
* queue if the one at index if empty or contended. The plock lock |
1494 |
< |
* body must be exception-free (so no try/finally) so we |
1495 |
< |
* optimistically allocate new queues outside the lock and throw |
1496 |
< |
* them away if (very rarely) not needed. |
1490 |
> |
* pool, so must perform secondary initialization. It also |
1491 |
> |
* detects first submission by an external thread by looking up |
1492 |
> |
* its ThreadLocal, and creates a new shared queue if the one at |
1493 |
> |
* index if empty or contended. The plock lock body must be |
1494 |
> |
* exception-free (so no try/finally) so we optimistically |
1495 |
> |
* allocate new queues outside the lock and throw them away if |
1496 |
> |
* (very rarely) not needed. |
1497 |
> |
* |
1498 |
> |
* Secondary initialization occurs when plock is zero, to create |
1499 |
> |
* workQueue array and set plock to a valid value. This lock body |
1500 |
> |
* must also be exception-free. Because the plock seq value can |
1501 |
> |
* eventually wrap around zero, this method harmlessly fails to |
1502 |
> |
* reinitialize if workQueues exists, while still advancing plock. |
1503 |
|
*/ |
1504 |
|
private void fullExternalPush(ForkJoinTask<?> task) { |
1505 |
|
int r = 0; // random index seed |
1510 |
|
r += SEED_INCREMENT) && r != 0) |
1511 |
|
submitters.set(z = new Submitter(r)); |
1512 |
|
} |
1513 |
< |
else if (r == 0) { // move to a different index |
1513 |
> |
else if (r == 0) { // move to a different index |
1514 |
|
r = z.seed; |
1515 |
< |
r ^= r << 13; // same xorshift as WorkQueues |
1515 |
> |
r ^= r << 13; // same xorshift as WorkQueues |
1516 |
|
r ^= r >>> 17; |
1517 |
|
z.seed = r ^ (r << 5); |
1518 |
|
} |
1519 |
|
else if ((ps = plock) < 0) |
1520 |
|
throw new RejectedExecutionException(); |
1521 |
|
else if (ps == 0 || (ws = workQueues) == null || |
1522 |
< |
(m = ws.length - 1) < 0) |
1523 |
< |
initWorkers(); |
1522 |
> |
(m = ws.length - 1) < 0) { // initialize workQueues |
1523 |
> |
int p = config & SMASK; // find power of two table size |
1524 |
> |
int n = (p > 1) ? p - 1 : 1; // ensure at least 2 slots |
1525 |
> |
n |= n >>> 1; n |= n >>> 2; n |= n >>> 4; |
1526 |
> |
n |= n >>> 8; n |= n >>> 16; n = (n + 1) << 1; |
1527 |
> |
WorkQueue[] nws = ((ws = workQueues) == null || ws.length == 0 ? |
1528 |
> |
new WorkQueue[n] : null); |
1529 |
> |
if (((ps = plock) & PL_LOCK) != 0 || |
1530 |
> |
!U.compareAndSwapInt(this, PLOCK, ps, ps += PL_LOCK)) |
1531 |
> |
ps = acquirePlock(); |
1532 |
> |
if (((ws = workQueues) == null || ws.length == 0) && nws != null) |
1533 |
> |
workQueues = nws; |
1534 |
> |
int nps = (ps & SHUTDOWN) | ((ps + PL_LOCK) & ~SHUTDOWN); |
1535 |
> |
if (!U.compareAndSwapInt(this, PLOCK, ps, nps)) |
1536 |
> |
releasePlock(nps); |
1537 |
> |
} |
1538 |
|
else if ((q = ws[k = r & m & SQMASK]) != null) { |
1539 |
|
if (q.qlock == 0 && U.compareAndSwapInt(q, QLOCK, 0, 1)) { |
1540 |
|
ForkJoinTask<?>[] a = q.array; |
1654 |
|
* |
1655 |
|
* * If not already enqueued, try to inactivate and enqueue the |
1656 |
|
* worker on wait queue. Or, if inactivating has caused the pool |
1657 |
< |
* to be quiescent, relay to idleAwaitWork to check for |
1658 |
< |
* termination and possibly shrink pool. |
1657 |
> |
* to be quiescent, relay to idleAwaitWork to possibly shrink |
1658 |
> |
* pool. |
1659 |
|
* |
1660 |
|
* * If already enqueued and none of the above apply, possibly |
1661 |
< |
* (with 1/2 probability) park awaiting signal, else lingering to |
1662 |
< |
* help scan and signal. |
1661 |
> |
* park awaiting signal, else lingering to help scan and signal. |
1662 |
> |
* |
1663 |
> |
* * If a non-empty queue discovered or left as a hint, |
1664 |
> |
* help wake up other workers before return. |
1665 |
|
* |
1666 |
|
* @param w the worker (via its WorkQueue) |
1667 |
|
* @return a task or null if none found |
1672 |
|
if (w != null && (ws = workQueues) != null && (m = ws.length - 1) >= 0) { |
1673 |
|
int ec = w.eventCount; // ec is negative if inactive |
1674 |
|
int r = w.seed; r ^= r << 13; r ^= r >>> 17; w.seed = r ^= r << 5; |
1675 |
+ |
w.hint = -1; // update seed and clear hint |
1676 |
|
int j = ((m + m + 1) | MIN_SCAN) & MAX_SCAN; |
1677 |
|
do { |
1678 |
|
WorkQueue q; ForkJoinTask<?>[] a; int b; |
1694 |
|
} |
1695 |
|
} while (--j >= 0); |
1696 |
|
|
1697 |
< |
long c, sc; int e, ns, h; |
1698 |
< |
if ((h = w.hint) < 0) { |
1699 |
< |
if ((ns = w.nsteals) != 0) { |
1700 |
< |
if (U.compareAndSwapLong(this, STEALCOUNT, |
1701 |
< |
sc = stealCount, sc + ns)) |
1702 |
< |
w.nsteals = 0; // collect steals |
1703 |
< |
} |
1704 |
< |
else if (plock != ps) // consistency check |
1705 |
< |
; // skip |
1706 |
< |
else if ((e = (int)(c = ctl)) < 0) |
1707 |
< |
w.qlock = -1; // pool is terminating |
1708 |
< |
else if (ec >= 0) { // try to enqueue/inactivate |
1709 |
< |
long nc = ((long)ec | ((c - AC_UNIT) & (AC_MASK|TC_MASK))); |
1710 |
< |
w.nextWait = e; // link and mark inactive |
1711 |
< |
w.eventCount = ec | INT_SIGN; |
1712 |
< |
if (ctl != c || !U.compareAndSwapLong(this, CTL, c, nc)) |
1713 |
< |
w.eventCount = ec; // unmark on CAS failure |
1714 |
< |
else if ((int)(c >> AC_SHIFT) == 1 - (config & SMASK)) |
1715 |
< |
idleAwaitWork(w, nc, c); |
1716 |
< |
} |
1717 |
< |
else if (w.eventCount < 0) { // block |
1718 |
< |
Thread wt = Thread.currentThread(); |
1719 |
< |
Thread.interrupted(); // clear status |
1720 |
< |
U.putObject(wt, PARKBLOCKER, this); |
1721 |
< |
w.parker = wt; // emulate LockSupport.park |
1722 |
< |
if (w.eventCount < 0) // recheck |
1723 |
< |
U.park(false, 0L); |
1724 |
< |
w.parker = null; |
1725 |
< |
U.putObject(wt, PARKBLOCKER, null); |
1697 |
> |
int h, e, ns; long c, sc; WorkQueue q; |
1698 |
> |
if ((ns = w.nsteals) != 0) { |
1699 |
> |
if (U.compareAndSwapLong(this, STEALCOUNT, |
1700 |
> |
sc = stealCount, sc + ns)) |
1701 |
> |
w.nsteals = 0; // collect steals and rescan |
1702 |
> |
} |
1703 |
> |
else if (plock != ps) // consistency check |
1704 |
> |
; // skip |
1705 |
> |
else if ((e = (int)(c = ctl)) < 0) |
1706 |
> |
w.qlock = -1; // pool is terminating |
1707 |
> |
else { |
1708 |
> |
if ((h = w.hint) < 0) { |
1709 |
> |
if (ec >= 0) { // try to enqueue/inactivate |
1710 |
> |
long nc = (((long)ec | |
1711 |
> |
((c - AC_UNIT) & (AC_MASK|TC_MASK)))); |
1712 |
> |
w.nextWait = e; // link and mark inactive |
1713 |
> |
w.eventCount = ec | INT_SIGN; |
1714 |
> |
if (ctl != c || !U.compareAndSwapLong(this, CTL, c, nc)) |
1715 |
> |
w.eventCount = ec; // unmark on CAS failure |
1716 |
> |
else if ((int)(c >> AC_SHIFT) == 1 - (config & SMASK)) |
1717 |
> |
idleAwaitWork(w, nc, c); |
1718 |
> |
} |
1719 |
> |
else if (w.eventCount < 0 && ctl == c) { |
1720 |
> |
Thread wt = Thread.currentThread(); |
1721 |
> |
Thread.interrupted(); // clear status |
1722 |
> |
U.putObject(wt, PARKBLOCKER, this); |
1723 |
> |
w.parker = wt; // emulate LockSupport.park |
1724 |
> |
if (w.eventCount < 0) // recheck |
1725 |
> |
U.park(false, 0L); // block |
1726 |
> |
w.parker = null; |
1727 |
> |
U.putObject(wt, PARKBLOCKER, null); |
1728 |
> |
} |
1729 |
> |
} |
1730 |
> |
if ((h >= 0 || (h = w.hint) >= 0) && |
1731 |
> |
(ws = workQueues) != null && h < ws.length && |
1732 |
> |
(q = ws[h]) != null) { // signal others before retry |
1733 |
> |
WorkQueue v; Thread p; int u, i, s; |
1734 |
> |
for (int n = (config & SMASK) - 1;;) { |
1735 |
> |
int idleCount = (w.eventCount < 0) ? 0 : -1; |
1736 |
> |
if (((s = idleCount - q.base + q.top) <= n && |
1737 |
> |
(n = s) <= 0) || |
1738 |
> |
(u = (int)((c = ctl) >>> 32)) >= 0 || |
1739 |
> |
(e = (int)c) <= 0 || m < (i = e & SMASK) || |
1740 |
> |
(v = ws[i]) == null) |
1741 |
> |
break; |
1742 |
> |
long nc = (((long)(v.nextWait & E_MASK)) | |
1743 |
> |
((long)(u + UAC_UNIT) << 32)); |
1744 |
> |
if (v.eventCount != (e | INT_SIGN) || |
1745 |
> |
!U.compareAndSwapLong(this, CTL, c, nc)) |
1746 |
> |
break; |
1747 |
> |
v.hint = h; |
1748 |
> |
v.eventCount = (e + E_SEQ) & E_MASK; |
1749 |
> |
if ((p = v.parker) != null) |
1750 |
> |
U.unpark(p); |
1751 |
> |
if (--n <= 0) |
1752 |
> |
break; |
1753 |
> |
} |
1754 |
|
} |
1755 |
|
} |
1757 |
– |
if (h >= 0 || w.hint >= 0) // signal others before retry |
1758 |
– |
helpSignalHint(w); |
1756 |
|
} |
1757 |
|
return null; |
1758 |
|
} |
1771 |
|
*/ |
1772 |
|
private void idleAwaitWork(WorkQueue w, long currentCtl, long prevCtl) { |
1773 |
|
if (w != null && w.eventCount < 0 && |
1774 |
< |
!tryTerminate(false, false) && (int)prevCtl != 0) { |
1774 |
> |
!tryTerminate(false, false) && (int)prevCtl != 0 && |
1775 |
> |
ctl == currentCtl) { |
1776 |
|
int dc = -(short)(currentCtl >>> TC_SHIFT); |
1777 |
|
long parkTime = dc < 0 ? FAST_IDLE_TIMEOUT: (dc + 1) * IDLE_TIMEOUT; |
1778 |
|
long deadline = System.nanoTime() + parkTime - TIMEOUT_SLOP; |
1790 |
|
if (deadline - System.nanoTime() <= 0L && |
1791 |
|
U.compareAndSwapLong(this, CTL, currentCtl, prevCtl)) { |
1792 |
|
w.eventCount = (w.eventCount + E_SEQ) | E_MASK; |
1793 |
+ |
w.hint = -1; |
1794 |
|
w.qlock = -1; // shrink |
1795 |
|
break; |
1796 |
|
} |
1825 |
|
break outer; |
1826 |
|
long nc = (((long)(w.nextWait & E_MASK)) | |
1827 |
|
((long)(u + UAC_UNIT) << 32)); |
1828 |
< |
if (w.eventCount == (e | INT_SIGN) && |
1829 |
< |
U.compareAndSwapLong(this, CTL, c, nc)) { |
1830 |
< |
w.eventCount = (e + E_SEQ) & E_MASK; |
1832 |
< |
if ((p = w.parker) != null) |
1833 |
< |
U.unpark(p); |
1834 |
< |
if (--n <= 0) |
1835 |
< |
break; |
1836 |
< |
} |
1837 |
< |
} |
1838 |
< |
} |
1839 |
< |
} |
1840 |
< |
} |
1841 |
< |
|
1842 |
< |
/** |
1843 |
< |
* Signals other workers if tasks are present in hinted queue. |
1844 |
< |
* |
1845 |
< |
* @param caller the worker with the hint |
1846 |
< |
*/ |
1847 |
< |
private void helpSignalHint(WorkQueue caller) { |
1848 |
< |
WorkQueue[] ws; WorkQueue q, w; Thread p; long c; int h, m, u, e, i, s; |
1849 |
< |
if (caller != null && (h = caller.hint) >= 0) { |
1850 |
< |
caller.hint = -1; |
1851 |
< |
if ((u = (int)(ctl >>> 32)) < 0 && (u >> UAC_SHIFT) < 0 && |
1852 |
< |
(ws = workQueues) != null && (m = ws.length - 1) >= 0 && |
1853 |
< |
(q = ws[h & m]) != null) { |
1854 |
< |
for (int n = 2;;) { // limit to at most 2 signals |
1855 |
< |
int idleCount = (caller.eventCount < 0) ? 0 : -1; |
1856 |
< |
if (((s = idleCount - q.base + q.top) <= n && |
1857 |
< |
(n = s) <= 0) || |
1858 |
< |
(u = (int)((c = ctl) >>> 32)) >= 0 || |
1859 |
< |
(e = (int)c) <= 0 || m < (i = e & SMASK) || |
1860 |
< |
(w = ws[i]) == null) |
1861 |
< |
break; |
1862 |
< |
long nc = (((long)(w.nextWait & E_MASK)) | |
1863 |
< |
((long)(u + UAC_UNIT) << 32)); |
1864 |
< |
if (w.eventCount == (e | INT_SIGN) && |
1865 |
< |
U.compareAndSwapLong(this, CTL, c, nc)) { |
1866 |
< |
w.hint = h; |
1828 |
> |
if (w.eventCount != (e | INT_SIGN)) |
1829 |
> |
break outer; |
1830 |
> |
if (U.compareAndSwapLong(this, CTL, c, nc)) { |
1831 |
|
w.eventCount = (e + E_SEQ) & E_MASK; |
1832 |
|
if ((p = w.parker) != null) |
1833 |
|
U.unpark(p); |
2084 |
|
|
2085 |
|
/** |
2086 |
|
* Returns a (probably) non-empty steal queue, if one is found |
2087 |
< |
* during a random, then cyclic scan, else null. This method must |
2088 |
< |
* be retried by caller if, by the time it tries to use the queue, |
2125 |
< |
* it is empty. |
2087 |
> |
* during a scan, else null. This method must be retried by |
2088 |
> |
* caller if, by the time it tries to use the queue, it is empty. |
2089 |
|
* @param r a (random) seed for scanning |
2090 |
|
*/ |
2091 |
|
private WorkQueue findNonEmptyStealQueue(int r) { |
2092 |
< |
for (WorkQueue[] ws;;) { |
2093 |
< |
int ps = plock, m, n; |
2094 |
< |
if ((ws = workQueues) == null || (m = ws.length - 1) < 1) |
2095 |
< |
return null; |
2096 |
< |
for (int j = (m + 1) << 2; ;) { |
2097 |
< |
WorkQueue q = ws[(((r + j) << 1) | 1) & m]; |
2098 |
< |
if (q != null && (n = q.base - q.top) < 0) { |
2136 |
< |
if (n < -1) |
2137 |
< |
signalWork(q); |
2138 |
< |
return q; |
2139 |
< |
} |
2140 |
< |
else if (--j < 0) { |
2141 |
< |
if (plock == ps) |
2142 |
< |
return null; |
2143 |
< |
break; |
2092 |
> |
for (;;) { |
2093 |
> |
int ps = plock, m; WorkQueue[] ws; WorkQueue q; |
2094 |
> |
if ((ws = workQueues) != null && (m = ws.length - 1) >= 0) { |
2095 |
> |
for (int j = (m + 1) << 2; j >= 0; --j) { |
2096 |
> |
if ((q = ws[(((r + j) << 1) | 1) & m]) != null && |
2097 |
> |
q.base - q.top < 0) |
2098 |
> |
return q; |
2099 |
|
} |
2100 |
|
} |
2101 |
+ |
if (plock == ps) |
2102 |
+ |
return null; |
2103 |
|
} |
2104 |
|
} |
2105 |
|
|
2111 |
|
*/ |
2112 |
|
final void helpQuiescePool(WorkQueue w) { |
2113 |
|
for (boolean active = true;;) { |
2114 |
< |
ForkJoinTask<?> localTask; // exhaust local queue |
2115 |
< |
while ((localTask = w.nextLocalTask()) != null) |
2116 |
< |
localTask.doExec(); |
2117 |
< |
// Similar to loop in scan(), but ignoring submissions |
2118 |
< |
WorkQueue q = findNonEmptyStealQueue(w.nextSeed()); |
2119 |
< |
if (q != null) { |
2120 |
< |
ForkJoinTask<?> t; int b; |
2114 |
> |
long c; WorkQueue q; ForkJoinTask<?> t; int b; |
2115 |
> |
while ((t = w.nextLocalTask()) != null) { |
2116 |
> |
if (w.base - w.top < 0) |
2117 |
> |
signalWork(w); |
2118 |
> |
t.doExec(); |
2119 |
> |
} |
2120 |
> |
if ((q = findNonEmptyStealQueue(w.nextSeed())) != null) { |
2121 |
|
if (!active) { // re-establish active count |
2165 |
– |
long c; |
2122 |
|
active = true; |
2123 |
|
do {} while (!U.compareAndSwapLong |
2124 |
|
(this, CTL, c = ctl, c + AC_UNIT)); |
2125 |
|
} |
2126 |
< |
if ((b = q.base) - q.top < 0 && (t = q.pollAt(b)) != null) |
2126 |
> |
if ((b = q.base) - q.top < 0 && (t = q.pollAt(b)) != null) { |
2127 |
> |
if (q.base - q.top < 0) |
2128 |
> |
signalWork(q); |
2129 |
|
w.runSubtask(t); |
2130 |
+ |
} |
2131 |
|
} |
2132 |
< |
else { |
2133 |
< |
long c; |
2134 |
< |
if (active) { // decrement active count without queuing |
2132 |
> |
else if (active) { // decrement active count without queuing |
2133 |
> |
long nc = (c = ctl) - AC_UNIT; |
2134 |
> |
if ((int)(nc >> AC_SHIFT) + (config & SMASK) == 0) |
2135 |
> |
return; // bypass decrement-then-increment |
2136 |
> |
if (U.compareAndSwapLong(this, CTL, c, nc)) |
2137 |
|
active = false; |
2177 |
– |
do {} while (!U.compareAndSwapLong |
2178 |
– |
(this, CTL, c = ctl, c -= AC_UNIT)); |
2179 |
– |
} |
2180 |
– |
else |
2181 |
– |
c = ctl; // re-increment on exit |
2182 |
– |
if ((int)(c >> AC_SHIFT) + (config & SMASK) == 0) { |
2183 |
– |
do {} while (!U.compareAndSwapLong |
2184 |
– |
(this, CTL, c = ctl, c + AC_UNIT)); |
2185 |
– |
break; |
2186 |
– |
} |
2138 |
|
} |
2139 |
+ |
else if ((int)((c = ctl) >> AC_SHIFT) + (config & SMASK) == 0 && |
2140 |
+ |
U.compareAndSwapLong(this, CTL, c, c + AC_UNIT)) |
2141 |
+ |
return; |
2142 |
|
} |
2143 |
|
} |
2144 |
|
|
2154 |
|
return t; |
2155 |
|
if ((q = findNonEmptyStealQueue(w.nextSeed())) == null) |
2156 |
|
return null; |
2157 |
< |
if ((b = q.base) - q.top < 0 && (t = q.pollAt(b)) != null) |
2157 |
> |
if ((b = q.base) - q.top < 0 && (t = q.pollAt(b)) != null) { |
2158 |
> |
if (q.base - q.top < 0) |
2159 |
> |
signalWork(q); |
2160 |
|
return t; |
2161 |
+ |
} |
2162 |
|
} |
2163 |
|
} |
2164 |
|
|
2240 |
|
* @return true if now terminating or terminated |
2241 |
|
*/ |
2242 |
|
private boolean tryTerminate(boolean now, boolean enable) { |
2243 |
< |
if (this == commonPool) // cannot shut down |
2243 |
> |
int ps; |
2244 |
> |
if (this == commonPool) // cannot shut down |
2245 |
|
return false; |
2246 |
+ |
if ((ps = plock) >= 0) { // enable by setting plock |
2247 |
+ |
if (!enable) |
2248 |
+ |
return false; |
2249 |
+ |
if ((ps & PL_LOCK) != 0 || |
2250 |
+ |
!U.compareAndSwapInt(this, PLOCK, ps, ps += PL_LOCK)) |
2251 |
+ |
ps = acquirePlock(); |
2252 |
+ |
int nps = ((ps + PL_LOCK) & ~SHUTDOWN) | SHUTDOWN; |
2253 |
+ |
if (!U.compareAndSwapInt(this, PLOCK, ps, nps)) |
2254 |
+ |
releasePlock(nps); |
2255 |
+ |
} |
2256 |
|
for (long c;;) { |
2257 |
< |
if (((c = ctl) & STOP_BIT) != 0) { // already terminating |
2257 |
> |
if (((c = ctl) & STOP_BIT) != 0) { // already terminating |
2258 |
|
if ((short)(c >>> TC_SHIFT) == -(config & SMASK)) { |
2259 |
|
synchronized (this) { |
2260 |
< |
notifyAll(); // signal when 0 workers |
2260 |
> |
notifyAll(); // signal when 0 workers |
2261 |
|
} |
2262 |
|
} |
2263 |
|
return true; |
2264 |
|
} |
2265 |
< |
if (plock >= 0) { // not yet enabled |
2266 |
< |
int ps; |
2267 |
< |
if (!enable) |
2265 |
> |
if (!now) { // check if idle & no tasks |
2266 |
> |
WorkQueue[] ws; WorkQueue w; |
2267 |
> |
if ((int)(c >> AC_SHIFT) != -(config & SMASK)) |
2268 |
|
return false; |
2269 |
< |
if (((ps = plock) & PL_LOCK) != 0 || |
2270 |
< |
!U.compareAndSwapInt(this, PLOCK, ps, ps += PL_LOCK)) |
2271 |
< |
ps = acquirePlock(); |
2272 |
< |
int nps = SHUTDOWN; |
2273 |
< |
if (!U.compareAndSwapInt(this, PLOCK, ps, nps)) |
2274 |
< |
releasePlock(nps); |
2275 |
< |
} |
2276 |
< |
if (!now) { // check if idle & no tasks |
2277 |
< |
if ((int)(c >> AC_SHIFT) != -(config & SMASK) || |
2278 |
< |
hasQueuedSubmissions()) |
2311 |
< |
return false; |
2312 |
< |
// Check for unqueued inactive workers. One pass suffices. |
2313 |
< |
WorkQueue[] ws = workQueues; WorkQueue w; |
2314 |
< |
if (ws != null) { |
2315 |
< |
for (int i = 1; i < ws.length; i += 2) { |
2316 |
< |
if ((w = ws[i]) != null && w.eventCount >= 0) |
2317 |
< |
return false; |
2269 |
> |
if ((ws = workQueues) != null) { |
2270 |
> |
for (int i = 0; i < ws.length; ++i) { |
2271 |
> |
if ((w = ws[i]) != null) { |
2272 |
> |
if (!w.isEmpty()) { // signal unprocessed tasks |
2273 |
> |
signalWork(w); |
2274 |
> |
return false; |
2275 |
> |
} |
2276 |
> |
if ((i & 1) != 0 && w.eventCount >= 0) |
2277 |
> |
return false; // unqueued inactive worker |
2278 |
> |
} |
2279 |
|
} |
2280 |
|
} |
2281 |
|
} |
2282 |
|
if (U.compareAndSwapLong(this, CTL, c, c | STOP_BIT)) { |
2283 |
|
for (int pass = 0; pass < 3; ++pass) { |
2284 |
< |
WorkQueue[] ws = workQueues; |
2285 |
< |
if (ws != null) { |
2325 |
< |
WorkQueue w; |
2284 |
> |
WorkQueue[] ws; WorkQueue w; Thread wt; |
2285 |
> |
if ((ws = workQueues) != null) { |
2286 |
|
int n = ws.length; |
2287 |
|
for (int i = 0; i < n; ++i) { |
2288 |
|
if ((w = ws[i]) != null) { |
2289 |
|
w.qlock = -1; |
2290 |
|
if (pass > 0) { |
2291 |
|
w.cancelAll(); |
2292 |
< |
if (pass > 1) |
2293 |
< |
w.interruptOwner(); |
2292 |
> |
if (pass > 1 && (wt = w.owner) != null) { |
2293 |
> |
if (!wt.isInterrupted()) { |
2294 |
> |
try { |
2295 |
> |
wt.interrupt(); |
2296 |
> |
} catch (Throwable ignore) { |
2297 |
> |
} |
2298 |
> |
} |
2299 |
> |
U.unpark(wt); |
2300 |
> |
} |
2301 |
|
} |
2302 |
|
} |
2303 |
|
} |
2304 |
|
// Wake up workers parked on event queue |
2305 |
|
int i, e; long cc; Thread p; |
2306 |
|
while ((e = (int)(cc = ctl) & E_MASK) != 0 && |
2307 |
< |
(i = e & SMASK) < n && |
2307 |
> |
(i = e & SMASK) < n && i >= 0 && |
2308 |
|
(w = ws[i]) != null) { |
2309 |
|
long nc = ((long)(w.nextWait & E_MASK) | |
2310 |
|
((cc + AC_UNIT) & AC_MASK) | |
2460 |
|
if ((p = commonPool) != null && |
2461 |
|
(q = p.findNonEmptyStealQueue(1)) != null && |
2462 |
|
(b = q.base) - q.top < 0 && |
2463 |
< |
(t = q.pollAt(b)) != null) |
2463 |
> |
(t = q.pollAt(b)) != null) { |
2464 |
> |
if (q.base - q.top < 0) |
2465 |
> |
p.signalWork(q); |
2466 |
|
t.doExec(); |
2467 |
+ |
} |
2468 |
|
} |
2469 |
|
|
2470 |
|
// Exported methods |
2565 |
|
} |
2566 |
|
|
2567 |
|
/** |
2568 |
< |
* Returns the common pool instance. |
2568 |
> |
* Returns the common pool instance. This pool is statically |
2569 |
> |
* constructed; its run state is unaffected by attempts to |
2570 |
> |
* {@link #shutdown} or {@link #shutdownNow}. |
2571 |
|
* |
2572 |
|
* @return the common pool instance |
2573 |
|
*/ |
3346 |
|
commonPool = new ForkJoinPool(par, ct, fac, handler); |
3347 |
|
} |
3348 |
|
|
3349 |
+ |
|
3350 |
|
/** |
3351 |
|
* Returns a sun.misc.Unsafe. Suitable for use in a 3rd party package. |
3352 |
|
* Replace with a simple call to Unsafe.getUnsafe when integrating |