440 |
|
* Common Pool |
441 |
|
* =========== |
442 |
|
* |
443 |
< |
* The static commonPool always exists after static |
443 |
> |
* The static common Pool always exists after static |
444 |
|
* initialization. Since it (or any other created pool) need |
445 |
|
* never be used, we minimize initial construction overhead and |
446 |
|
* footprint to the setup of about a dozen fields, with no nested |
1074 |
|
* to paranoically avoid potential initialization circularities |
1075 |
|
* as well as to simplify generated code. |
1076 |
|
*/ |
1077 |
< |
static final ForkJoinPool commonPool; |
1077 |
> |
static final ForkJoinPool common; |
1078 |
|
|
1079 |
|
/** |
1080 |
< |
* Common pool parallelism. Must equal commonPool.parallelism. |
1080 |
> |
* Common pool parallelism. Must equal common.parallelism. |
1081 |
|
*/ |
1082 |
< |
static final int commonPoolParallelism; |
1082 |
> |
static final int commonParallelism; |
1083 |
|
|
1084 |
|
/** |
1085 |
|
* Sequence number for creating workerNamePrefix. |
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; |
1303 |
|
} |
1304 |
|
|
1305 |
|
/** |
1306 |
– |
* Performs secondary initialization, called when plock is zero. |
1307 |
– |
* Creates workQueue array and sets plock to a valid value. The |
1308 |
– |
* lock body must be exception-free (so no try/finally) so we |
1309 |
– |
* optimistically allocate new array outside the lock and throw |
1310 |
– |
* away if (very rarely) not needed. (A similar tactic is used in |
1311 |
– |
* fullExternalPush.) Because the plock seq value can eventually |
1312 |
– |
* wrap around zero, this method harmlessly fails to reinitialize |
1313 |
– |
* if workQueues exists, while still advancing plock. |
1314 |
– |
* |
1315 |
– |
* Additionally tries to create the first worker. |
1316 |
– |
*/ |
1317 |
– |
private void initWorkers() { |
1318 |
– |
WorkQueue[] ws, nws; int ps; |
1319 |
– |
int p = config & SMASK; // find power of two table size |
1320 |
– |
int n = (p > 1) ? p - 1 : 1; // ensure at least 2 slots |
1321 |
– |
n |= n >>> 1; n |= n >>> 2; n |= n >>> 4; n |= n >>> 8; n |= n >>> 16; |
1322 |
– |
n = (n + 1) << 1; |
1323 |
– |
if ((ws = workQueues) == null || ws.length == 0) |
1324 |
– |
nws = new WorkQueue[n]; |
1325 |
– |
else |
1326 |
– |
nws = null; |
1327 |
– |
if (((ps = plock) & PL_LOCK) != 0 || |
1328 |
– |
!U.compareAndSwapInt(this, PLOCK, ps, ps += PL_LOCK)) |
1329 |
– |
ps = acquirePlock(); |
1330 |
– |
if (((ws = workQueues) == null || ws.length == 0) && nws != null) |
1331 |
– |
workQueues = nws; |
1332 |
– |
int nps = (ps & SHUTDOWN) | ((ps + PL_LOCK) & ~SHUTDOWN); |
1333 |
– |
if (!U.compareAndSwapInt(this, PLOCK, ps, nps)) |
1334 |
– |
releasePlock(nps); |
1335 |
– |
tryAddWorker(); |
1336 |
– |
} |
1337 |
– |
|
1338 |
– |
/** |
1306 |
|
* Tries to create and start one worker if fewer than target |
1307 |
|
* parallelism level exist. Adjusts counts etc on failure. |
1308 |
|
*/ |
1427 |
|
if (e > 0) { // activate or create replacement |
1428 |
|
if ((ws = workQueues) == null || |
1429 |
|
(i = e & SMASK) >= ws.length || |
1430 |
< |
(v = ws[i]) != null) |
1430 |
> |
(v = ws[i]) == null) |
1431 |
|
break; |
1432 |
|
long nc = (((long)(v.nextWait & E_MASK)) | |
1433 |
|
((long)(u + UAC_UNIT) << 32)); |
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; |
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 |
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 |
1716 |
|
else if ((int)(c >> AC_SHIFT) == 1 - (config & SMASK)) |
1717 |
|
idleAwaitWork(w, nc, c); |
1718 |
|
} |
1719 |
< |
else if (w.eventCount < 0 && !tryTerminate(false, false) && |
1733 |
< |
ctl == c) { // block |
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); |
1725 |
> |
U.park(false, 0L); // block |
1726 |
|
w.parker = null; |
1727 |
|
U.putObject(wt, PARKBLOCKER, null); |
1728 |
|
} |
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;;) { |
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) || |
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 |
|
} |
1932 |
|
* @param task the task to join |
1933 |
|
* @param mode if shared, exit upon completing any task |
1934 |
|
* if all workers are active |
1947 |
– |
* |
1935 |
|
*/ |
1936 |
|
private int helpComplete(ForkJoinTask<?> task, int mode) { |
1937 |
|
WorkQueue[] ws; WorkQueue q; int m, n, s, u; |
2083 |
|
|
2084 |
|
/** |
2085 |
|
* Returns a (probably) non-empty steal queue, if one is found |
2086 |
< |
* during a random, then cyclic scan, else null. This method must |
2087 |
< |
* be retried by caller if, by the time it tries to use the queue, |
2101 |
< |
* it is empty. |
2086 |
> |
* during a scan, else null. This method must be retried by |
2087 |
> |
* caller if, by the time it tries to use the queue, it is empty. |
2088 |
|
* @param r a (random) seed for scanning |
2089 |
|
*/ |
2090 |
|
private WorkQueue findNonEmptyStealQueue(int r) { |
2091 |
< |
for (WorkQueue[] ws;;) { |
2092 |
< |
int ps = plock, m, n; |
2093 |
< |
if ((ws = workQueues) == null || (m = ws.length - 1) < 1) |
2094 |
< |
return null; |
2095 |
< |
for (int j = (m + 1) << 2; ;) { |
2096 |
< |
WorkQueue q = ws[(((r + j) << 1) | 1) & m]; |
2097 |
< |
if (q != null && (n = q.base - q.top) < 0) { |
2112 |
< |
if (n < -1) |
2113 |
< |
signalWork(q); |
2114 |
< |
return q; |
2115 |
< |
} |
2116 |
< |
else if (--j < 0) { |
2117 |
< |
if (plock == ps) |
2118 |
< |
return null; |
2119 |
< |
break; |
2091 |
> |
for (;;) { |
2092 |
> |
int ps = plock, m; WorkQueue[] ws; WorkQueue q; |
2093 |
> |
if ((ws = workQueues) != null && (m = ws.length - 1) >= 0) { |
2094 |
> |
for (int j = (m + 1) << 2; j >= 0; --j) { |
2095 |
> |
if ((q = ws[(((r + j) << 1) | 1) & m]) != null && |
2096 |
> |
q.base - q.top < 0) |
2097 |
> |
return q; |
2098 |
|
} |
2099 |
|
} |
2100 |
+ |
if (plock == ps) |
2101 |
+ |
return null; |
2102 |
|
} |
2103 |
|
} |
2104 |
|
|
2110 |
|
*/ |
2111 |
|
final void helpQuiescePool(WorkQueue w) { |
2112 |
|
for (boolean active = true;;) { |
2113 |
< |
ForkJoinTask<?> localTask; // exhaust local queue |
2114 |
< |
while ((localTask = w.nextLocalTask()) != null) |
2115 |
< |
localTask.doExec(); |
2116 |
< |
// Similar to loop in scan(), but ignoring submissions |
2117 |
< |
WorkQueue q = findNonEmptyStealQueue(w.nextSeed()); |
2118 |
< |
if (q != null) { |
2119 |
< |
ForkJoinTask<?> t; int b; |
2113 |
> |
long c; WorkQueue q; ForkJoinTask<?> t; int b; |
2114 |
> |
while ((t = w.nextLocalTask()) != null) { |
2115 |
> |
if (w.base - w.top < 0) |
2116 |
> |
signalWork(w); |
2117 |
> |
t.doExec(); |
2118 |
> |
} |
2119 |
> |
if ((q = findNonEmptyStealQueue(w.nextSeed())) != null) { |
2120 |
|
if (!active) { // re-establish active count |
2141 |
– |
long c; |
2121 |
|
active = true; |
2122 |
|
do {} while (!U.compareAndSwapLong |
2123 |
|
(this, CTL, c = ctl, c + AC_UNIT)); |
2124 |
|
} |
2125 |
< |
if ((b = q.base) - q.top < 0 && (t = q.pollAt(b)) != null) |
2125 |
> |
if ((b = q.base) - q.top < 0 && (t = q.pollAt(b)) != null) { |
2126 |
> |
if (q.base - q.top < 0) |
2127 |
> |
signalWork(q); |
2128 |
|
w.runSubtask(t); |
2129 |
+ |
} |
2130 |
|
} |
2131 |
< |
else { |
2132 |
< |
long c; |
2133 |
< |
if (active) { // decrement active count without queuing |
2131 |
> |
else if (active) { // decrement active count without queuing |
2132 |
> |
long nc = (c = ctl) - AC_UNIT; |
2133 |
> |
if ((int)(nc >> AC_SHIFT) + (config & SMASK) == 0) |
2134 |
> |
return; // bypass decrement-then-increment |
2135 |
> |
if (U.compareAndSwapLong(this, CTL, c, nc)) |
2136 |
|
active = false; |
2153 |
– |
do {} while (!U.compareAndSwapLong |
2154 |
– |
(this, CTL, c = ctl, c -= AC_UNIT)); |
2155 |
– |
} |
2156 |
– |
else |
2157 |
– |
c = ctl; // re-increment on exit |
2158 |
– |
if ((int)(c >> AC_SHIFT) + (config & SMASK) == 0) { |
2159 |
– |
do {} while (!U.compareAndSwapLong |
2160 |
– |
(this, CTL, c = ctl, c + AC_UNIT)); |
2161 |
– |
break; |
2162 |
– |
} |
2137 |
|
} |
2138 |
+ |
else if ((int)((c = ctl) >> AC_SHIFT) + (config & SMASK) == 0 && |
2139 |
+ |
U.compareAndSwapLong(this, CTL, c, c + AC_UNIT)) |
2140 |
+ |
return; |
2141 |
|
} |
2142 |
|
} |
2143 |
|
|
2153 |
|
return t; |
2154 |
|
if ((q = findNonEmptyStealQueue(w.nextSeed())) == null) |
2155 |
|
return null; |
2156 |
< |
if ((b = q.base) - q.top < 0 && (t = q.pollAt(b)) != null) |
2156 |
> |
if ((b = q.base) - q.top < 0 && (t = q.pollAt(b)) != null) { |
2157 |
> |
if (q.base - q.top < 0) |
2158 |
> |
signalWork(q); |
2159 |
|
return t; |
2160 |
+ |
} |
2161 |
|
} |
2162 |
|
} |
2163 |
|
|
2239 |
|
* @return true if now terminating or terminated |
2240 |
|
*/ |
2241 |
|
private boolean tryTerminate(boolean now, boolean enable) { |
2242 |
< |
if (this == commonPool) // cannot shut down |
2242 |
> |
int ps; |
2243 |
> |
if (this == common) // cannot shut down |
2244 |
|
return false; |
2245 |
+ |
if ((ps = plock) >= 0) { // enable by setting plock |
2246 |
+ |
if (!enable) |
2247 |
+ |
return false; |
2248 |
+ |
if ((ps & PL_LOCK) != 0 || |
2249 |
+ |
!U.compareAndSwapInt(this, PLOCK, ps, ps += PL_LOCK)) |
2250 |
+ |
ps = acquirePlock(); |
2251 |
+ |
int nps = ((ps + PL_LOCK) & ~SHUTDOWN) | SHUTDOWN; |
2252 |
+ |
if (!U.compareAndSwapInt(this, PLOCK, ps, nps)) |
2253 |
+ |
releasePlock(nps); |
2254 |
+ |
} |
2255 |
|
for (long c;;) { |
2256 |
< |
if (((c = ctl) & STOP_BIT) != 0) { // already terminating |
2256 |
> |
if (((c = ctl) & STOP_BIT) != 0) { // already terminating |
2257 |
|
if ((short)(c >>> TC_SHIFT) == -(config & SMASK)) { |
2258 |
|
synchronized (this) { |
2259 |
< |
notifyAll(); // signal when 0 workers |
2259 |
> |
notifyAll(); // signal when 0 workers |
2260 |
|
} |
2261 |
|
} |
2262 |
|
return true; |
2263 |
|
} |
2264 |
< |
if (plock >= 0) { // not yet enabled |
2265 |
< |
int ps; |
2266 |
< |
if (!enable) |
2276 |
< |
return false; |
2277 |
< |
if (((ps = plock) & PL_LOCK) != 0 || |
2278 |
< |
!U.compareAndSwapInt(this, PLOCK, ps, ps += PL_LOCK)) |
2279 |
< |
ps = acquirePlock(); |
2280 |
< |
if (!U.compareAndSwapInt(this, PLOCK, ps, SHUTDOWN)) |
2281 |
< |
releasePlock(SHUTDOWN); |
2282 |
< |
} |
2283 |
< |
if (!now) { // check if idle & no tasks |
2284 |
< |
if ((int)(c >> AC_SHIFT) != -(config & SMASK) || |
2285 |
< |
hasQueuedSubmissions()) |
2264 |
> |
if (!now) { // check if idle & no tasks |
2265 |
> |
WorkQueue[] ws; WorkQueue w; |
2266 |
> |
if ((int)(c >> AC_SHIFT) != -(config & SMASK)) |
2267 |
|
return false; |
2268 |
< |
// Check for unqueued inactive workers. One pass suffices. |
2269 |
< |
WorkQueue[] ws = workQueues; WorkQueue w; |
2270 |
< |
if (ws != null) { |
2271 |
< |
for (int i = 1; i < ws.length; i += 2) { |
2272 |
< |
if ((w = ws[i]) != null && w.eventCount >= 0) |
2273 |
< |
return false; |
2268 |
> |
if ((ws = workQueues) != null) { |
2269 |
> |
for (int i = 0; i < ws.length; ++i) { |
2270 |
> |
if ((w = ws[i]) != null) { |
2271 |
> |
if (!w.isEmpty()) { // signal unprocessed tasks |
2272 |
> |
signalWork(w); |
2273 |
> |
return false; |
2274 |
> |
} |
2275 |
> |
if ((i & 1) != 0 && w.eventCount >= 0) |
2276 |
> |
return false; // unqueued inactive worker |
2277 |
> |
} |
2278 |
|
} |
2279 |
|
} |
2280 |
|
} |
2281 |
|
if (U.compareAndSwapLong(this, CTL, c, c | STOP_BIT)) { |
2282 |
|
for (int pass = 0; pass < 3; ++pass) { |
2283 |
< |
WorkQueue[] ws = workQueues; |
2284 |
< |
if (ws != null) { |
2300 |
< |
WorkQueue w; Thread wt; |
2283 |
> |
WorkQueue[] ws; WorkQueue w; Thread wt; |
2284 |
> |
if ((ws = workQueues) != null) { |
2285 |
|
int n = ws.length; |
2286 |
|
for (int i = 0; i < n; ++i) { |
2287 |
|
if ((w = ws[i]) != null) { |
2292 |
|
if (!wt.isInterrupted()) { |
2293 |
|
try { |
2294 |
|
wt.interrupt(); |
2295 |
< |
} catch (SecurityException ignore) { |
2295 |
> |
} catch (Throwable ignore) { |
2296 |
|
} |
2297 |
|
} |
2298 |
|
U.unpark(wt); |
2303 |
|
// Wake up workers parked on event queue |
2304 |
|
int i, e; long cc; Thread p; |
2305 |
|
while ((e = (int)(cc = ctl) & E_MASK) != 0 && |
2306 |
< |
(i = e & SMASK) < n && |
2306 |
> |
(i = e & SMASK) < n && i >= 0 && |
2307 |
|
(w = ws[i]) != null) { |
2308 |
|
long nc = ((long)(w.nextWait & E_MASK) | |
2309 |
|
((cc + AC_UNIT) & AC_MASK) | |
2331 |
|
static WorkQueue commonSubmitterQueue() { |
2332 |
|
ForkJoinPool p; WorkQueue[] ws; int m; Submitter z; |
2333 |
|
return ((z = submitters.get()) != null && |
2334 |
< |
(p = commonPool) != null && |
2334 |
> |
(p = common) != null && |
2335 |
|
(ws = p.workQueues) != null && |
2336 |
|
(m = ws.length - 1) >= 0) ? |
2337 |
|
ws[m & z.seed & SQMASK] : null; |
2345 |
|
ForkJoinTask<?>[] a; int m, s; |
2346 |
|
if (t != null && |
2347 |
|
(z = submitters.get()) != null && |
2348 |
< |
(p = commonPool) != null && |
2348 |
> |
(p = common) != null && |
2349 |
|
(ws = p.workQueues) != null && |
2350 |
|
(m = ws.length - 1) >= 0 && |
2351 |
|
(q = ws[m & z.seed & SQMASK]) != null && |
2422 |
|
ForkJoinTask<?>[] a; int m, s, n; |
2423 |
|
if (t != null && |
2424 |
|
(z = submitters.get()) != null && |
2425 |
< |
(p = commonPool) != null && |
2425 |
> |
(p = common) != null && |
2426 |
|
(ws = p.workQueues) != null && |
2427 |
|
(m = ws.length - 1) >= 0 && |
2428 |
|
(q = ws[m & z.seed & SQMASK]) != null && |
2451 |
|
} |
2452 |
|
} |
2453 |
|
|
2470 |
– |
/** |
2471 |
– |
* Restricted version of helpQuiescePool for external callers |
2472 |
– |
*/ |
2473 |
– |
static void externalHelpQuiescePool() { |
2474 |
– |
ForkJoinPool p; ForkJoinTask<?> t; WorkQueue q; int b; |
2475 |
– |
if ((p = commonPool) != null && |
2476 |
– |
(q = p.findNonEmptyStealQueue(1)) != null && |
2477 |
– |
(b = q.base) - q.top < 0 && |
2478 |
– |
(t = q.pollAt(b)) != null) |
2479 |
– |
t.doExec(); |
2480 |
– |
} |
2481 |
– |
|
2454 |
|
// Exported methods |
2455 |
|
|
2456 |
|
// Constructors |
2549 |
|
} |
2550 |
|
|
2551 |
|
/** |
2552 |
< |
* Returns the common pool instance. |
2552 |
> |
* Returns the common pool instance. This pool is statically |
2553 |
> |
* constructed; its run state is unaffected by attempts to {@link |
2554 |
> |
* #shutdown} or {@link #shutdownNow}. However this pool and any |
2555 |
> |
* ongoing processing are automatically terminated upon program |
2556 |
> |
* {@link System#exit}. Any program that relies on asynchronous |
2557 |
> |
* task processing to complete before program termination should |
2558 |
> |
* invoke {@link #quiesceCommonPool}, or the timeout-based {@code |
2559 |
> |
* commonPool().}{@link #awaitQuiescence}, before exit. |
2560 |
|
* |
2561 |
|
* @return the common pool instance |
2562 |
|
*/ |
2563 |
|
public static ForkJoinPool commonPool() { |
2564 |
< |
// assert commonPool != null : "static init error"; |
2565 |
< |
return commonPool; |
2564 |
> |
// assert common != null : "static init error"; |
2565 |
> |
return common; |
2566 |
|
} |
2567 |
|
|
2568 |
|
// Execution methods |
2743 |
|
* @return the targeted parallelism level of the common pool |
2744 |
|
*/ |
2745 |
|
public static int getCommonPoolParallelism() { |
2746 |
< |
return commonPoolParallelism; |
2746 |
> |
return commonParallelism; |
2747 |
|
} |
2748 |
|
|
2749 |
|
/** |
3082 |
|
/** |
3083 |
|
* Blocks until all tasks have completed execution after a |
3084 |
|
* shutdown request, or the timeout occurs, or the current thread |
3085 |
< |
* is interrupted, whichever happens first. Note that the {@link |
3086 |
< |
* #commonPool()} never terminates until program shutdown so |
3087 |
< |
* this method will always time out. |
3085 |
> |
* is interrupted, whichever happens first. Because the {@link |
3086 |
> |
* #commonPool()} never terminates until program shutdown, when |
3087 |
> |
* applied to the common pool, this method is equivalent to {@link |
3088 |
> |
* #awaitQuiescence} but always returns {@code false}. |
3089 |
|
* |
3090 |
|
* @param timeout the maximum time to wait |
3091 |
|
* @param unit the time unit of the timeout argument |
3095 |
|
*/ |
3096 |
|
public boolean awaitTermination(long timeout, TimeUnit unit) |
3097 |
|
throws InterruptedException { |
3098 |
+ |
if (Thread.interrupted()) |
3099 |
+ |
throw new InterruptedException(); |
3100 |
+ |
if (this == common) { |
3101 |
+ |
awaitQuiescence(timeout, unit); |
3102 |
+ |
return false; |
3103 |
+ |
} |
3104 |
|
long nanos = unit.toNanos(timeout); |
3105 |
|
if (isTerminated()) |
3106 |
|
return true; |
3120 |
|
} |
3121 |
|
|
3122 |
|
/** |
3123 |
+ |
* If called by a ForkJoinTask operating in this pool, equivalent |
3124 |
+ |
* in effect to {@link ForkJoinTask#helpQuiesce}. Otherwise, |
3125 |
+ |
* waits and/or attempts to assist performing tasks until this |
3126 |
+ |
* pool {@link #isQuiescent} or the indicated timeout elapses. |
3127 |
+ |
* |
3128 |
+ |
* @param timeout the maximum time to wait |
3129 |
+ |
* @param unit the time unit of the timeout argument |
3130 |
+ |
* @return {@code true} if quiescent; {@code false} if the |
3131 |
+ |
* timeout elapsed. |
3132 |
+ |
*/ |
3133 |
+ |
public boolean awaitQuiescence(long timeout, TimeUnit unit) { |
3134 |
+ |
long nanos = unit.toNanos(timeout); |
3135 |
+ |
ForkJoinWorkerThread wt; |
3136 |
+ |
Thread thread = Thread.currentThread(); |
3137 |
+ |
if ((thread instanceof ForkJoinWorkerThread) && |
3138 |
+ |
(wt = (ForkJoinWorkerThread)thread).pool == this) { |
3139 |
+ |
helpQuiescePool(wt.workQueue); |
3140 |
+ |
return true; |
3141 |
+ |
} |
3142 |
+ |
long startTime = System.nanoTime(); |
3143 |
+ |
WorkQueue[] ws; |
3144 |
+ |
int r = 0, m; |
3145 |
+ |
boolean found = true; |
3146 |
+ |
while (!isQuiescent() && (ws = workQueues) != null && |
3147 |
+ |
(m = ws.length - 1) >= 0) { |
3148 |
+ |
if (!found) { |
3149 |
+ |
if ((System.nanoTime() - startTime) > nanos) |
3150 |
+ |
return false; |
3151 |
+ |
Thread.yield(); // cannot block |
3152 |
+ |
} |
3153 |
+ |
found = false; |
3154 |
+ |
for (int j = (m + 1) << 2; j >= 0; --j) { |
3155 |
+ |
ForkJoinTask<?> t; WorkQueue q; int b; |
3156 |
+ |
if ((q = ws[r++ & m]) != null && (b = q.base) - q.top < 0) { |
3157 |
+ |
found = true; |
3158 |
+ |
if ((t = q.pollAt(b)) != null) { |
3159 |
+ |
if (q.base - q.top < 0) |
3160 |
+ |
signalWork(q); |
3161 |
+ |
t.doExec(); |
3162 |
+ |
} |
3163 |
+ |
break; |
3164 |
+ |
} |
3165 |
+ |
} |
3166 |
+ |
} |
3167 |
+ |
return true; |
3168 |
+ |
} |
3169 |
+ |
|
3170 |
+ |
/** |
3171 |
+ |
* Waits and/or attempts to assist performing tasks indefinitely |
3172 |
+ |
* until the {@link #commonPool()} {@link #isQuiescent} |
3173 |
+ |
*/ |
3174 |
+ |
public static void quiesceCommonPool() { |
3175 |
+ |
common.awaitQuiescence(Long.MAX_VALUE, TimeUnit.NANOSECONDS); |
3176 |
+ |
} |
3177 |
+ |
|
3178 |
+ |
/** |
3179 |
|
* Interface for extending managed parallelism for tasks running |
3180 |
|
* in {@link ForkJoinPool}s. |
3181 |
|
* |
3391 |
|
par = Runtime.getRuntime().availableProcessors(); |
3392 |
|
if (par > MAX_CAP) |
3393 |
|
par = MAX_CAP; |
3394 |
< |
commonPoolParallelism = par; |
3394 |
> |
commonParallelism = par; |
3395 |
|
long np = (long)(-par); // precompute initial ctl value |
3396 |
|
long ct = ((np << AC_SHIFT) & AC_MASK) | ((np << TC_SHIFT) & TC_MASK); |
3397 |
|
|
3398 |
< |
commonPool = new ForkJoinPool(par, ct, fac, handler); |
3398 |
> |
common = new ForkJoinPool(par, ct, fac, handler); |
3399 |
|
} |
3400 |
|
|
3401 |
|
/** |