103 |
|
* java.util.concurrent.ForkJoinPool.common}: {@code parallelism} -- |
104 |
|
* an integer greater than zero, {@code threadFactory} -- the class |
105 |
|
* name of a {@link ForkJoinWorkerThreadFactory}, and {@code |
106 |
– |
<<<<<<< ForkJoinPool.java |
107 |
– |
* exceptionHandler} -- the class name of a {@code |
108 |
– |
======= |
106 |
|
* exceptionHandler} -- the class name of a {@link |
107 |
|
* java.lang.Thread.UncaughtExceptionHandler |
111 |
– |
>>>>>>> 1.111 |
108 |
|
* Thread.UncaughtExceptionHandler}. Upon any error in establishing |
109 |
|
* these settings, default parameters are used. |
110 |
|
* |
316 |
|
* execute. However, many other threads may notice the same task |
317 |
|
* and each signal to wake up a thread that might take it. So in |
318 |
|
* general, pools will be over-signalled. When a submission is |
319 |
< |
* added or another worker adds a task to a queue that is |
320 |
< |
* apparently empty, they signal waiting workers (or trigger |
321 |
< |
* creation of new ones if fewer than the given parallelism |
322 |
< |
* level). These primary signals are buttressed by signals |
323 |
< |
* whenever other threads scan for work or do not have a task to |
324 |
< |
* process (including the case of leaving a hint to unparked |
325 |
< |
* threads to help signal others upon wakeup). On most platforms, |
326 |
< |
* signalling (unpark) overhead time is noticeably long, and the |
327 |
< |
* time between signalling a thread and it actually making |
328 |
< |
* progress can be very noticeably long, so it is worth offloading |
329 |
< |
* these delays from critical paths as much as possible. |
319 |
> |
* added or another worker adds a task to a queue that has fewer |
320 |
> |
* than two tasks, they signal waiting workers (or trigger |
321 |
> |
* creation of new ones if fewer than the given parallelism level |
322 |
> |
* -- signalWork), and may leave a hint to the unparked worker to |
323 |
> |
* help signal others upon wakeup). These primary signals are |
324 |
> |
* buttressed by others (see method helpSignal) whenever other |
325 |
> |
* threads scan for work or do not have a task to process. On |
326 |
> |
* most platforms, signalling (unpark) overhead time is noticeably |
327 |
> |
* long, and the time between signalling a thread and it actually |
328 |
> |
* making progress can be very noticeably long, so it is worth |
329 |
> |
* offloading these delays from critical paths as much as |
330 |
> |
* possible. |
331 |
|
* |
332 |
|
* Trimming workers. To release resources after periods of lack of |
333 |
|
* use, a worker starting to wait when the pool is quiescent will |
535 |
|
} |
536 |
|
|
537 |
|
/** |
538 |
+ |
* Per-thread records for threads that submit to pools. Currently |
539 |
+ |
* holds only pseudo-random seed / index that is used to choose |
540 |
+ |
* submission queues in method externalPush. In the future, this may |
541 |
+ |
* also incorporate a means to implement different task rejection |
542 |
+ |
* and resubmission policies. |
543 |
+ |
* |
544 |
+ |
* Seeds for submitters and workers/workQueues work in basically |
545 |
+ |
* the same way but are initialized and updated using slightly |
546 |
+ |
* different mechanics. Both are initialized using the same |
547 |
+ |
* approach as in class ThreadLocal, where successive values are |
548 |
+ |
* unlikely to collide with previous values. Seeds are then |
549 |
+ |
* randomly modified upon collisions using xorshifts, which |
550 |
+ |
* requires a non-zero seed. |
551 |
+ |
*/ |
552 |
+ |
static final class Submitter { |
553 |
+ |
int seed; |
554 |
+ |
Submitter(int s) { seed = s; } |
555 |
+ |
} |
556 |
+ |
|
557 |
+ |
/** |
558 |
|
* Class for artificial tasks that are used to replace the target |
559 |
|
* of local joins if they are removed from an interior queue slot |
560 |
|
* in WorkQueue.tryRemoveAndExec. We don't need the proxy to |
620 |
|
* trades off slightly slower average field access for the sake of |
621 |
|
* avoiding really bad worst-case access. (Until better JVM |
622 |
|
* support is in place, this padding is dependent on transient |
623 |
< |
* properties of JVM field layout rules.) |
623 |
> |
* properties of JVM field layout rules.) We also take care in |
624 |
> |
* allocating, sizing and resizing the array. Non-shared queue |
625 |
> |
* arrays are initialized by workers before use. Others are |
626 |
> |
* allocated on first use. |
627 |
|
*/ |
628 |
|
static final class WorkQueue { |
629 |
|
/** |
646 |
|
*/ |
647 |
|
static final int MAXIMUM_QUEUE_CAPACITY = 1 << 26; // 64M |
648 |
|
|
649 |
+ |
// Heuristic padding to ameliorate unfortunate memory placements |
650 |
+ |
volatile long pad00, pad01, pad02, pad03, pad04, pad05, pad06; |
651 |
+ |
|
652 |
|
int seed; // for random scanning; initialize nonzero |
653 |
|
volatile int eventCount; // encoded inactivation count; < 0 if inactive |
654 |
|
int nextWait; // encoded record of next event waiter |
666 |
|
volatile ForkJoinTask<?> currentJoin; // task being joined in awaitJoin |
667 |
|
ForkJoinTask<?> currentSteal; // current non-local task being executed |
668 |
|
|
669 |
< |
// Heuristic padding to ameliorate unfortunate memory placements |
670 |
< |
Object p00, p01, p02, p03, p04, p05, p06, p07; |
648 |
< |
Object p08, p09, p0a, p0b, p0c; |
669 |
> |
volatile Object pad10, pad11, pad12, pad13, pad14, pad15, pad16, pad17; |
670 |
> |
volatile Object pad18, pad19, pad1a, pad1b, pad1c, pad1d; |
671 |
|
|
672 |
|
WorkQueue(ForkJoinPool pool, ForkJoinWorkerThread owner, int mode, |
673 |
|
int seed) { |
652 |
– |
this.array = new ForkJoinTask<?>[WorkQueue.INITIAL_QUEUE_CAPACITY]; |
674 |
|
this.pool = pool; |
675 |
|
this.owner = owner; |
676 |
|
this.mode = mode; |
677 |
|
this.seed = seed; |
678 |
< |
// Place indices in the center of array |
678 |
> |
// Place indices in the center of array (that is not yet allocated) |
679 |
|
base = top = INITIAL_QUEUE_CAPACITY >>> 1; |
680 |
|
} |
681 |
|
|
682 |
|
/** |
683 |
< |
* Pushes a task. Call only by owner in unshared queues. |
684 |
< |
* Cases needing resizing or rejection are relayed to fullPush |
685 |
< |
* (that also handles shared queues). |
683 |
> |
* Returns the approximate number of tasks in the queue. |
684 |
> |
*/ |
685 |
> |
final int queueSize() { |
686 |
> |
int n = base - top; // non-owner callers must read base first |
687 |
> |
return (n >= 0) ? 0 : -n; // ignore transient negative |
688 |
> |
} |
689 |
> |
|
690 |
> |
/** |
691 |
> |
* Provides a more accurate estimate of whether this queue has |
692 |
> |
* any tasks than does queueSize, by checking whether a |
693 |
> |
* near-empty queue has at least one unclaimed task. |
694 |
> |
*/ |
695 |
> |
final boolean isEmpty() { |
696 |
> |
ForkJoinTask<?>[] a; int m, s; |
697 |
> |
int n = base - (s = top); |
698 |
> |
return (n >= 0 || |
699 |
> |
(n == -1 && |
700 |
> |
((a = array) == null || |
701 |
> |
(m = a.length - 1) < 0 || |
702 |
> |
U.getObject |
703 |
> |
(a, (long)((m & (s - 1)) << ASHIFT) + ABASE) == null))); |
704 |
> |
} |
705 |
> |
|
706 |
> |
/** |
707 |
> |
* Pushes a task. Call only by owner in unshared queues. (The |
708 |
> |
* shared-queue version is embedded in method externalPush.) |
709 |
|
* |
710 |
|
* @param task the task. Caller must ensure non-null. |
711 |
|
* @throw RejectedExecutionException if array cannot be resized |
714 |
|
ForkJoinTask<?>[] a; ForkJoinPool p; |
715 |
|
int s = top, m, n; |
716 |
|
if ((a = array) != null) { // ignore if queue removed |
717 |
< |
U.putOrderedObject |
718 |
< |
(a, (((m = a.length - 1) & s) << ASHIFT) + ABASE, task); |
719 |
< |
if ((n = (top = s + 1) - base) <= 1) { |
717 |
> |
int j = (((m = a.length - 1) & s) << ASHIFT) + ABASE; |
718 |
> |
U.putOrderedObject(a, j, task); |
719 |
> |
if ((n = (top = s + 1) - base) <= 2) { |
720 |
|
if ((p = pool) != null) |
721 |
< |
p.signalWork(this, 0); |
721 |
> |
p.signalWork(this); |
722 |
|
} |
723 |
|
else if (n >= m) |
724 |
|
growArray(); |
725 |
|
} |
726 |
|
} |
727 |
|
|
684 |
– |
/** |
685 |
– |
* Pushes a task if lock is free and array is either big |
686 |
– |
* enough or can be resized to be big enough. |
687 |
– |
* |
688 |
– |
* @param task the task. Caller must ensure non-null. |
689 |
– |
* @return true if submitted |
690 |
– |
*/ |
691 |
– |
final boolean trySharedPush(ForkJoinTask<?> task) { |
692 |
– |
boolean submitted = false; |
693 |
– |
if (qlock == 0 && U.compareAndSwapInt(this, QLOCK, 0, 1)) { |
694 |
– |
ForkJoinTask<?>[] a = array; ForkJoinPool p; |
695 |
– |
int s = top; |
696 |
– |
try { |
697 |
– |
if ((a != null && a.length > s + 1 - base) || |
698 |
– |
(a = growArray()) != null) { // must presize |
699 |
– |
int j = (((a.length - 1) & s) << ASHIFT) + ABASE; |
700 |
– |
U.putOrderedObject(a, j, task); |
701 |
– |
top = s + 1; |
702 |
– |
submitted = true; |
703 |
– |
} |
704 |
– |
} finally { |
705 |
– |
qlock = 0; // unlock |
706 |
– |
} |
707 |
– |
if (submitted && (p = pool) != null) |
708 |
– |
p.signalWork(this, 0); |
709 |
– |
} |
710 |
– |
return submitted; |
711 |
– |
} |
712 |
– |
|
728 |
|
/** |
729 |
|
* Initializes or doubles the capacity of array. Call either |
730 |
|
* by owner or with lock held -- it is OK for base, but not |
874 |
|
return seed = r ^= r << 5; |
875 |
|
} |
876 |
|
|
862 |
– |
/** |
863 |
– |
* Provides a more accurate estimate of size than (top - base) |
864 |
– |
* by ordering reads and checking whether a near-empty queue |
865 |
– |
* has at least one unclaimed task. |
866 |
– |
*/ |
867 |
– |
final int queueSize() { |
868 |
– |
ForkJoinTask<?>[] a; int k, s, n; |
869 |
– |
return ((n = base - (s = top)) < 0 && |
870 |
– |
(n != -1 || |
871 |
– |
((a = array) != null && (k = a.length) > 0 && |
872 |
– |
U.getObject |
873 |
– |
(a, (long)((((k - 1) & (s - 1)) << ASHIFT) + ABASE)) != null))) ? |
874 |
– |
-n : 0; |
875 |
– |
} |
876 |
– |
|
877 |
|
// Specialized execution methods |
878 |
|
|
879 |
|
/** |
987 |
|
if (t != null) { |
988 |
|
(currentSteal = t).doExec(); |
989 |
|
currentSteal = null; |
990 |
< |
++nsteals; |
991 |
< |
if (top != base) { // process remaining local tasks |
990 |
> |
if (base - top < 0) { // process remaining local tasks |
991 |
|
if (mode == 0) |
992 |
|
popAndExecAll(); |
993 |
|
else |
994 |
|
pollAndExecAll(); |
995 |
|
} |
996 |
+ |
++nsteals; |
997 |
+ |
hint = -1; |
998 |
|
} |
999 |
|
} |
1000 |
|
|
1071 |
|
defaultForkJoinWorkerThreadFactory; |
1072 |
|
|
1073 |
|
/** |
1073 |
– |
* Per-thread records for threads that submit to pools. Currently |
1074 |
– |
* holds only pseudo-random seed / index that is used to choose |
1075 |
– |
* submission queues in method externalPush. In the future, this may |
1076 |
– |
* also incorporate a means to implement different task rejection |
1077 |
– |
* and resubmission policies. |
1078 |
– |
* |
1079 |
– |
* Seeds for submitters and workers/workQueues work in basically |
1080 |
– |
* the same way but are initialized and updated using slightly |
1081 |
– |
* different mechanics. Both are initialized using the same |
1082 |
– |
* approach as in class ThreadLocal, where successive values are |
1083 |
– |
* unlikely to collide with previous values. Seeds are then |
1084 |
– |
* randomly modified upon collisions using xorshifts, which |
1085 |
– |
* requires a non-zero seed. |
1086 |
– |
*/ |
1087 |
– |
static final class Submitter { |
1088 |
– |
int seed; |
1089 |
– |
Submitter(int s) { seed = s; } |
1090 |
– |
} |
1091 |
– |
|
1092 |
– |
/** |
1074 |
|
* Per-thread submission bookkeeping. Shared across all pools |
1075 |
|
* to reduce ThreadLocal pollution and because random motion |
1076 |
|
* to avoid contention in one pool is likely to hold for others. |
1080 |
|
static final ThreadLocal<Submitter> submitters; |
1081 |
|
|
1082 |
|
/** |
1083 |
+ |
* Permission required for callers of methods that may start or |
1084 |
+ |
* kill threads. |
1085 |
+ |
*/ |
1086 |
+ |
private static final RuntimePermission modifyThreadPermission; |
1087 |
+ |
|
1088 |
+ |
/** |
1089 |
|
* Common (static) pool. Non-null for public use unless a static |
1090 |
|
* construction exception, but internal usages null-check on use |
1091 |
|
* to paranoically avoid potential initialization circularities |
1094 |
|
static final ForkJoinPool commonPool; |
1095 |
|
|
1096 |
|
/** |
1110 |
– |
* Permission required for callers of methods that may start or |
1111 |
– |
* kill threads. |
1112 |
– |
*/ |
1113 |
– |
private static final RuntimePermission modifyThreadPermission; |
1114 |
– |
|
1115 |
– |
/** |
1097 |
|
* Common pool parallelism. Must equal commonPool.parallelism. |
1098 |
|
*/ |
1099 |
|
static final int commonPoolParallelism; |
1240 |
|
* declaration order and may differ across JVMs, but the following |
1241 |
|
* empirically works OK on current JVMs. |
1242 |
|
*/ |
1243 |
+ |
|
1244 |
+ |
// Heuristic padding to ameliorate unfortunate memory placements |
1245 |
+ |
volatile long pad00, pad01, pad02, pad03, pad04, pad05, pad06; |
1246 |
+ |
|
1247 |
|
volatile long stealCount; // collects worker counts |
1248 |
|
volatile long ctl; // main pool control |
1249 |
|
volatile int plock; // shutdown status and seqLock |
1254 |
|
final Thread.UncaughtExceptionHandler ueh; // per-worker UEH |
1255 |
|
final String workerNamePrefix; // to create worker name string |
1256 |
|
|
1257 |
+ |
volatile Object pad10, pad11, pad12, pad13, pad14, pad15, pad16, pad17; |
1258 |
+ |
volatile Object pad18, pad19, pad1a, pad1b; |
1259 |
+ |
|
1260 |
|
/* |
1261 |
|
* Acquires the plock lock to protect worker array and related |
1262 |
|
* updates. This method is called only if an initial CAS on plock |
1317 |
|
} |
1318 |
|
|
1319 |
|
/** |
1332 |
– |
* Tries to create and start a worker; adjusts counts etc on failure |
1333 |
– |
*/ |
1334 |
– |
private void addWorker() { |
1335 |
– |
ForkJoinWorkerThread wt = null; |
1336 |
– |
try { |
1337 |
– |
(wt = factory.newThread(this)).start(); |
1338 |
– |
} catch (Throwable ex) { |
1339 |
– |
deregisterWorker(wt, ex); // adjust on failure |
1340 |
– |
} |
1341 |
– |
} |
1342 |
– |
|
1343 |
– |
/** |
1320 |
|
* Performs secondary initialization, called when plock is zero. |
1321 |
|
* Creates workQueue array and sets plock to a valid value. The |
1322 |
|
* lock body must be exception-free (so no try/finally) so we |
1325 |
|
* fullExternalPush.) Because the plock seq value can eventually |
1326 |
|
* wrap around zero, this method harmlessly fails to reinitialize |
1327 |
|
* if workQueues exists, while still advancing plock. |
1328 |
+ |
* |
1329 |
+ |
* Additonally tries to create the first worker. |
1330 |
|
*/ |
1331 |
< |
private void initWorkQueuesArray() { |
1332 |
< |
WorkQueue[] ws; int ps; |
1331 |
> |
private void initWorkers() { |
1332 |
> |
WorkQueue[] ws, nws; int ps; |
1333 |
|
int p = config & SMASK; // find power of two table size |
1334 |
|
int n = (p > 1) ? p - 1 : 1; // ensure at least 2 slots |
1335 |
|
n |= n >>> 1; n |= n >>> 2; n |= n >>> 4; n |= n >>> 8; n |= n >>> 16; |
1336 |
< |
WorkQueue[] nws = new WorkQueue[(n + 1) << 1]; |
1336 |
> |
n = (n + 1) << 1; |
1337 |
> |
if ((ws = workQueues) == null || ws.length == 0) |
1338 |
> |
nws = new WorkQueue[n]; |
1339 |
> |
else |
1340 |
> |
nws = null; |
1341 |
|
if (((ps = plock) & PL_LOCK) != 0 || |
1342 |
|
!U.compareAndSwapInt(this, PLOCK, ps, ps += PL_LOCK)) |
1343 |
|
ps = acquirePlock(); |
1344 |
< |
if ((ws = workQueues) == null || ws.length == 0) |
1344 |
> |
if (((ws = workQueues) == null || ws.length == 0) && nws != null) |
1345 |
|
workQueues = nws; |
1346 |
|
int nps = (ps & SHUTDOWN) | ((ps + PL_LOCK) & ~SHUTDOWN); |
1347 |
|
if (!U.compareAndSwapInt(this, PLOCK, ps, nps)) |
1348 |
|
releasePlock(nps); |
1349 |
+ |
tryAddWorker(); |
1350 |
+ |
} |
1351 |
+ |
|
1352 |
+ |
/** |
1353 |
+ |
* Tries to create and start one worker. Adjusts counts etc on |
1354 |
+ |
* failure. |
1355 |
+ |
*/ |
1356 |
+ |
private void tryAddWorker() { |
1357 |
|
long c; int u; |
1358 |
< |
if ((u = (int)((c = ctl) >>> 32)) < 0 && (int)c == 0) { |
1358 |
> |
while ((u = (int)((c = ctl) >>> 32)) < 0 && |
1359 |
> |
(u & SHORT_SIGN) != 0 && (int)c == 0) { |
1360 |
|
long nc = (long)(((u + UTC_UNIT) & UTC_MASK) | |
1361 |
|
((u + UAC_UNIT) & UAC_MASK)) << 32; |
1362 |
< |
if (U.compareAndSwapLong(this, CTL, c, nc)) |
1363 |
< |
addWorker(); |
1362 |
> |
if (U.compareAndSwapLong(this, CTL, c, nc)) { |
1363 |
> |
ForkJoinWorkerThreadFactory fac; |
1364 |
> |
Throwable ex = null; |
1365 |
> |
ForkJoinWorkerThread wt = null; |
1366 |
> |
try { |
1367 |
> |
if ((fac = factory) != null && |
1368 |
> |
(wt = fac.newThread(this)) != null) { |
1369 |
> |
wt.start(); |
1370 |
> |
break; |
1371 |
> |
} |
1372 |
> |
} catch (Throwable e) { |
1373 |
> |
ex = e; |
1374 |
> |
} |
1375 |
> |
deregisterWorker(wt, ex); |
1376 |
> |
break; |
1377 |
> |
} |
1378 |
|
} |
1374 |
– |
|
1379 |
|
} |
1380 |
|
|
1381 |
|
// Registering and deregistering workers |
1388 |
|
* expanding as needed. |
1389 |
|
* |
1390 |
|
* @param wt the worker thread |
1391 |
+ |
* @return the worker's queue |
1392 |
|
*/ |
1393 |
< |
final void registerWorker(ForkJoinWorkerThread wt) { |
1394 |
< |
if (wt != null && wt.workQueue == null) { |
1395 |
< |
int s, ps; // generate a rarely colliding candidate index seed |
1396 |
< |
do {} while (!U.compareAndSwapInt(this, INDEXSEED, s = indexSeed, |
1397 |
< |
s += SEED_INCREMENT) || |
1398 |
< |
s == 0); // skip 0 |
1399 |
< |
WorkQueue w = new WorkQueue(this, wt, config >>> 16, s); |
1400 |
< |
if (((ps = plock) & PL_LOCK) != 0 || |
1401 |
< |
!U.compareAndSwapInt(this, PLOCK, ps, ps += PL_LOCK)) |
1402 |
< |
ps = acquirePlock(); |
1403 |
< |
int nps = (ps & SHUTDOWN) | ((ps + PL_LOCK) & ~SHUTDOWN); |
1404 |
< |
try { |
1405 |
< |
WorkQueue[] ws; |
1406 |
< |
if ((ws = workQueues) != null && wt.workQueue == null) { |
1407 |
< |
int n = ws.length, m = n - 1; |
1408 |
< |
int r = (s << 1) | 1; // use odd-numbered indices |
1409 |
< |
if (ws[r &= m] != null) { // collision |
1410 |
< |
int probes = 0; // step by approx half size |
1411 |
< |
int step = (n <= 4) ? 2 : ((n >>> 1) & EVENMASK) + 2; |
1412 |
< |
while (ws[r = (r + step) & m] != null) { |
1413 |
< |
if (++probes >= n) { |
1414 |
< |
workQueues = ws = Arrays.copyOf(ws, n <<= 1); |
1415 |
< |
m = n - 1; |
1416 |
< |
probes = 0; |
1417 |
< |
} |
1393 |
> |
final WorkQueue registerWorker(ForkJoinWorkerThread wt) { |
1394 |
> |
Thread.UncaughtExceptionHandler handler; WorkQueue[] ws; int s, ps; |
1395 |
> |
wt.setDaemon(true); |
1396 |
> |
if ((handler = ueh) != null) |
1397 |
> |
wt.setUncaughtExceptionHandler(handler); |
1398 |
> |
do {} while (!U.compareAndSwapInt(this, INDEXSEED, s = indexSeed, |
1399 |
> |
s += SEED_INCREMENT) || |
1400 |
> |
s == 0); // skip 0 |
1401 |
> |
WorkQueue w = new WorkQueue(this, wt, config >>> 16, s); |
1402 |
> |
if (((ps = plock) & PL_LOCK) != 0 || |
1403 |
> |
!U.compareAndSwapInt(this, PLOCK, ps, ps += PL_LOCK)) |
1404 |
> |
ps = acquirePlock(); |
1405 |
> |
int nps = (ps & SHUTDOWN) | ((ps + PL_LOCK) & ~SHUTDOWN); |
1406 |
> |
try { |
1407 |
> |
if ((ws = workQueues) != null) { // skip if shutting down |
1408 |
> |
int n = ws.length, m = n - 1; |
1409 |
> |
int r = (s << 1) | 1; // use odd-numbered indices |
1410 |
> |
if (ws[r &= m] != null) { // collision |
1411 |
> |
int probes = 0; // step by approx half size |
1412 |
> |
int step = (n <= 4) ? 2 : ((n >>> 1) & EVENMASK) + 2; |
1413 |
> |
while (ws[r = (r + step) & m] != null) { |
1414 |
> |
if (++probes >= n) { |
1415 |
> |
workQueues = ws = Arrays.copyOf(ws, n <<= 1); |
1416 |
> |
m = n - 1; |
1417 |
> |
probes = 0; |
1418 |
|
} |
1419 |
|
} |
1415 |
– |
w.eventCount = w.poolIndex = r; // volatile write orders |
1416 |
– |
wt.workQueue = ws[r] = w; |
1420 |
|
} |
1421 |
< |
} finally { |
1422 |
< |
if (!U.compareAndSwapInt(this, PLOCK, ps, nps)) |
1420 |
< |
releasePlock(nps); |
1421 |
> |
w.eventCount = w.poolIndex = r; // volatile write orders |
1422 |
> |
ws[r] = w; |
1423 |
|
} |
1424 |
+ |
} finally { |
1425 |
+ |
if (!U.compareAndSwapInt(this, PLOCK, ps, nps)) |
1426 |
+ |
releasePlock(nps); |
1427 |
|
} |
1428 |
+ |
wt.setName(workerNamePrefix.concat(Integer.toString(w.poolIndex))); |
1429 |
+ |
return w; |
1430 |
|
} |
1431 |
|
|
1432 |
|
/** |
1470 |
|
if (!tryTerminate(false, false) && w != null) { |
1471 |
|
w.cancelAll(); // cancel remaining tasks |
1472 |
|
if (w.array != null) // suppress signal if never ran |
1473 |
< |
helpSignal(null, 0); // wake up or create replacement |
1473 |
> |
tryAddWorker(); // create replacement |
1474 |
|
if (ex == null) // help clean refs on way out |
1475 |
|
ForkJoinTask.helpExpungeStaleExceptions(); |
1476 |
|
} |
1497 |
|
U.compareAndSwapInt(q, QLOCK, 0, 1)) { // lock |
1498 |
|
int b = q.base, s = q.top, n, an; |
1499 |
|
if ((a = q.array) != null && (an = a.length) > (n = s + 1 - b)) { |
1500 |
< |
U.putObject(a, (long)(((an - 1) & s) << ASHIFT) + ABASE, task); |
1500 |
> |
int j = (((an - 1) & s) << ASHIFT) + ABASE; |
1501 |
> |
U.putOrderedObject(a, j, task); |
1502 |
|
q.top = s + 1; // push on to deque |
1503 |
|
q.qlock = 0; |
1504 |
|
if (n <= 2) |
1505 |
< |
signalWork(q, 0); |
1505 |
> |
signalWork(q); |
1506 |
|
return; |
1507 |
|
} |
1508 |
|
q.qlock = 0; |
1514 |
|
* Full version of externalPush. This method is called, among |
1515 |
|
* other times, upon the first submission of the first task to the |
1516 |
|
* pool, so must perform secondary initialization (via |
1517 |
< |
* initWorkQueuesArray). It also detects first submission by an |
1518 |
< |
* external thread by looking up its ThreadLocal, and creates a |
1519 |
< |
* new shared queue if the one at index if empty or contended. The |
1520 |
< |
* lock body must be exception-free (so no try/finally) so we |
1517 |
> |
* initWorkers). It also detects first submission by an external |
1518 |
> |
* thread by looking up its ThreadLocal, and creates a new shared |
1519 |
> |
* queue if the one at index if empty or contended. The plock lock |
1520 |
> |
* body must be exception-free (so no try/finally) so we |
1521 |
|
* optimistically allocate new queues outside the lock and throw |
1522 |
|
* them away if (very rarely) not needed. |
1523 |
|
*/ |
1524 |
|
private void fullExternalPush(ForkJoinTask<?> task) { |
1525 |
< |
int r = 0; |
1525 |
> |
int r = 0; // random index seed |
1526 |
|
for (Submitter z = submitters.get();;) { |
1527 |
|
WorkQueue[] ws; WorkQueue q; int ps, m, k; |
1528 |
|
if (z == null) { |
1540 |
|
throw new RejectedExecutionException(); |
1541 |
|
else if (ps == 0 || (ws = workQueues) == null || |
1542 |
|
(m = ws.length - 1) < 0) |
1543 |
< |
initWorkQueuesArray(); |
1543 |
> |
initWorkers(); |
1544 |
|
else if ((q = ws[k = r & m & SQMASK]) != null) { |
1545 |
< |
if (q.trySharedPush(task)) |
1546 |
< |
return; |
1547 |
< |
else |
1548 |
< |
r = 0; // move on contention |
1545 |
> |
if (q.qlock == 0 && U.compareAndSwapInt(q, QLOCK, 0, 1)) { |
1546 |
> |
ForkJoinTask<?>[] a = q.array; |
1547 |
> |
int s = q.top; |
1548 |
> |
boolean submitted = false; |
1549 |
> |
try { // locked version of push |
1550 |
> |
if ((a != null && a.length > s + 1 - q.base) || |
1551 |
> |
(a = q.growArray()) != null) { // must presize |
1552 |
> |
int j = (((a.length - 1) & s) << ASHIFT) + ABASE; |
1553 |
> |
U.putOrderedObject(a, j, task); |
1554 |
> |
q.top = s + 1; |
1555 |
> |
submitted = true; |
1556 |
> |
} |
1557 |
> |
} finally { |
1558 |
> |
q.qlock = 0; // unlock |
1559 |
> |
} |
1560 |
> |
if (submitted) { |
1561 |
> |
signalWork(q); |
1562 |
> |
return; |
1563 |
> |
} |
1564 |
> |
} |
1565 |
> |
r = 0; // move on failure |
1566 |
|
} |
1567 |
|
else if (((ps = plock) & PL_LOCK) == 0) { // create new queue |
1568 |
|
q = new WorkQueue(this, null, SHARED_QUEUE, r); |
1591 |
|
} |
1592 |
|
|
1593 |
|
/** |
1594 |
< |
* Tries to create (at most one) or activate (possibly several) |
1595 |
< |
* workers if too few are active. On contention failure, continues |
1596 |
< |
* until at least one worker is signalled or the given queue is |
1572 |
< |
* empty or all workers are active. |
1573 |
< |
* |
1574 |
< |
* @param q if non-null, the queue holding tasks to be signalled |
1575 |
< |
* @param signals the target number of signals (at least one -- |
1576 |
< |
* if argument is zero also sets signallee hint if parked). |
1594 |
> |
* Tries to create or activate a worker if too few are active. |
1595 |
> |
* |
1596 |
> |
* @param q the (non-null) queue holding tasks to be signalled |
1597 |
|
*/ |
1598 |
< |
final void signalWork(WorkQueue q, int signals) { |
1599 |
< |
long c; int e, u, i, s; WorkQueue[] ws; WorkQueue w; Thread p; |
1598 |
> |
final void signalWork(WorkQueue q) { |
1599 |
> |
int hint = q.poolIndex; |
1600 |
> |
long c; int e, u, i, n; WorkQueue[] ws; WorkQueue w; Thread p; |
1601 |
|
while ((u = (int)((c = ctl) >>> 32)) < 0) { |
1602 |
|
if ((e = (int)c) > 0) { |
1603 |
|
if ((ws = workQueues) != null && ws.length > (i = e & SMASK) && |
1605 |
|
long nc = (((long)(w.nextWait & E_MASK)) | |
1606 |
|
((long)(u + UAC_UNIT) << 32)); |
1607 |
|
if (U.compareAndSwapLong(this, CTL, c, nc)) { |
1608 |
+ |
w.hint = hint; |
1609 |
|
w.eventCount = (e + E_SEQ) & E_MASK; |
1610 |
< |
if ((p = w.parker) != null) { |
1589 |
< |
if (q != null && signals == 0) |
1590 |
< |
w.hint = q.poolIndex; |
1610 |
> |
if ((p = w.parker) != null) |
1611 |
|
U.unpark(p); |
1612 |
< |
} |
1593 |
< |
if (--signals <= 0) |
1594 |
< |
break; |
1612 |
> |
break; |
1613 |
|
} |
1614 |
< |
if (q != null && (s = q.queueSize()) <= signals && |
1597 |
< |
(signals = s) <= 0) |
1614 |
> |
if (q.top - q.base <= 0) |
1615 |
|
break; |
1616 |
|
} |
1617 |
|
else |
1618 |
|
break; |
1619 |
|
} |
1620 |
< |
else if (e == 0 && (u & SHORT_SIGN) != 0) { |
1621 |
< |
long nc = (long)(((u + UTC_UNIT) & UTC_MASK) | |
1622 |
< |
((u + UAC_UNIT) & UAC_MASK)) << 32; |
1606 |
< |
if (U.compareAndSwapLong(this, CTL, c, nc)) { |
1607 |
< |
addWorker(); |
1608 |
< |
break; |
1609 |
< |
} |
1610 |
< |
} |
1611 |
< |
else |
1620 |
> |
else { |
1621 |
> |
if ((short)u < 0) |
1622 |
> |
tryAddWorker(); |
1623 |
|
break; |
1624 |
+ |
} |
1625 |
|
} |
1626 |
|
} |
1627 |
|
|
1631 |
|
* Top-level runloop for workers, called by ForkJoinWorkerThread.run. |
1632 |
|
*/ |
1633 |
|
final void runWorker(WorkQueue w) { |
1634 |
< |
if (w != null) // skip on initialization failure |
1635 |
< |
do { w.runTask(scan(w)); } while (w.qlock >= 0); |
1634 |
> |
w.growArray(); // allocate queue |
1635 |
> |
do { w.runTask(scan(w)); } while (w.qlock >= 0); |
1636 |
|
} |
1637 |
|
|
1638 |
|
/** |
1671 |
|
* @return a task or null if none found |
1672 |
|
*/ |
1673 |
|
private final ForkJoinTask<?> scan(WorkQueue w) { |
1674 |
< |
WorkQueue[] ws; int m, hint; |
1674 |
> |
WorkQueue[] ws; int m; |
1675 |
|
int ps = plock; // read plock before ws |
1676 |
|
if (w != null && (ws = workQueues) != null && (m = ws.length - 1) >= 0) { |
1677 |
|
int ec = w.eventCount; // ec is negative if inactive |
1678 |
|
int r = w.seed; r ^= r << 13; r ^= r >>> 17; w.seed = r ^= r << 5; |
1679 |
< |
for (int j = ((m + m + 1) | MIN_SCAN) & MAX_SCAN; ; --j) { |
1679 |
> |
int j = ((m + m + 1) | MIN_SCAN) & MAX_SCAN; |
1680 |
> |
do { |
1681 |
|
WorkQueue q; ForkJoinTask<?>[] a; int b; |
1682 |
|
if ((q = ws[(r + j) & m]) != null && (b = q.base) - q.top < 0 && |
1683 |
|
(a = q.array) != null) { // probably nonempty |
1687 |
|
if (q.base == b && ec >= 0 && t != null && |
1688 |
|
U.compareAndSwapObject(a, i, t, null)) { |
1689 |
|
if ((q.base = b + 1) - q.top < 0) |
1690 |
< |
signalWork(q, 0); |
1690 |
> |
signalWork(q); |
1691 |
|
return t; // taken |
1692 |
|
} |
1693 |
< |
else if (ec < 0 || j < m) { // cannot take or cannot rescan |
1694 |
< |
w.hint = q.poolIndex; // use hint below |
1695 |
< |
break; // let caller retry after signal |
1696 |
< |
} |
1697 |
< |
} |
1698 |
< |
else if (j < 0) { // end of scan; in loop to simplify code |
1699 |
< |
long c, sc; int e, ns; |
1700 |
< |
if ((ns = w.nsteals) != 0) { |
1701 |
< |
if (U.compareAndSwapLong(this, STEALCOUNT, |
1702 |
< |
sc = stealCount, sc + ns)) |
1703 |
< |
w.nsteals = 0; // collect steals |
1704 |
< |
} |
1705 |
< |
else if (plock != ps) // ws may have changed |
1706 |
< |
break; |
1707 |
< |
else if ((e = (int)(c = ctl)) < 0) |
1708 |
< |
w.qlock = -1; // pool is terminating |
1709 |
< |
else 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.hint = -1; // use hint if set while parked |
1714 |
< |
w.eventCount = ec | INT_SIGN; |
1715 |
< |
if (ctl != c || |
1716 |
< |
!U.compareAndSwapLong(this, CTL, c, nc)) |
1717 |
< |
w.eventCount = ec; // unmark on CAS failure |
1718 |
< |
else if ((int)(c >> AC_SHIFT) == 1 - (config & SMASK)) |
1719 |
< |
idleAwaitWork(w, nc, c); |
1720 |
< |
} |
1721 |
< |
else if (w.eventCount < 0) { // block |
1722 |
< |
Thread wt = Thread.currentThread(); |
1723 |
< |
Thread.interrupted(); // clear status |
1724 |
< |
U.putObject(wt, PARKBLOCKER, this); |
1725 |
< |
w.parker = wt; // emulate LockSupport.park |
1726 |
< |
if (w.eventCount < 0) // recheck |
1727 |
< |
U.park(false, 0L); |
1728 |
< |
w.parker = null; |
1729 |
< |
U.putObject(wt, PARKBLOCKER, null); |
1730 |
< |
} |
1731 |
< |
break; |
1732 |
< |
} |
1733 |
< |
} |
1721 |
< |
if ((hint = w.hint) >= 0) { // help signal |
1722 |
< |
WorkQueue[] vs; WorkQueue v; int k; |
1723 |
< |
w.hint = -1; // suppress resignal |
1724 |
< |
if ((vs = workQueues) != null && hint < vs.length && |
1725 |
< |
(v = vs[hint]) != null && (k = v.base - v.top) < -1) |
1726 |
< |
signalWork(v, 1 - k); |
1693 |
> |
else if ((ec < 0 || j < m) && (int)(ctl >> AC_SHIFT) <= 0) { |
1694 |
> |
w.hint = (r + j) & m; // help signal below |
1695 |
> |
break; // cannot take |
1696 |
> |
} |
1697 |
> |
} |
1698 |
> |
} while (--j >= 0); |
1699 |
> |
|
1700 |
> |
long c, sc; int e, ns, h; |
1701 |
> |
if ((h = w.hint) < 0) { |
1702 |
> |
if ((ns = w.nsteals) != 0) { |
1703 |
> |
if (U.compareAndSwapLong(this, STEALCOUNT, |
1704 |
> |
sc = stealCount, sc + ns)) |
1705 |
> |
w.nsteals = 0; // collect steals |
1706 |
> |
} |
1707 |
> |
else if (plock != ps) // consistency check |
1708 |
> |
; // skip |
1709 |
> |
else if ((e = (int)(c = ctl)) < 0) |
1710 |
> |
w.qlock = -1; // pool is terminating |
1711 |
> |
else if (ec >= 0) { // try to enqueue/inactivate |
1712 |
> |
long nc = ((long)ec | ((c - AC_UNIT) & (AC_MASK|TC_MASK))); |
1713 |
> |
w.nextWait = e; // link and mark inactive |
1714 |
> |
w.eventCount = ec | INT_SIGN; |
1715 |
> |
if (ctl != c || !U.compareAndSwapLong(this, CTL, c, nc)) |
1716 |
> |
w.eventCount = ec; // unmark on CAS failure |
1717 |
> |
else if ((int)(c >> AC_SHIFT) == 1 - (config & SMASK)) |
1718 |
> |
idleAwaitWork(w, nc, c); |
1719 |
> |
} |
1720 |
> |
else if (w.eventCount < 0) { // block |
1721 |
> |
Thread wt = Thread.currentThread(); |
1722 |
> |
Thread.interrupted(); // clear status |
1723 |
> |
U.putObject(wt, PARKBLOCKER, this); |
1724 |
> |
w.parker = wt; // emulate LockSupport.park |
1725 |
> |
if (w.eventCount < 0) // recheck |
1726 |
> |
U.park(false, 0L); |
1727 |
> |
w.parker = null; |
1728 |
> |
U.putObject(wt, PARKBLOCKER, null); |
1729 |
> |
} |
1730 |
> |
} |
1731 |
> |
if (h >= 0 || (h = w.hint) >= 0) { // signal others before retry |
1732 |
> |
w.hint = -1; // reset |
1733 |
> |
helpSignal(null, h, true); |
1734 |
|
} |
1735 |
|
} |
1736 |
|
return null; |
1769 |
|
U.compareAndSwapLong(this, CTL, currentCtl, prevCtl)) { |
1770 |
|
w.eventCount = (w.eventCount + E_SEQ) | E_MASK; |
1771 |
|
w.qlock = -1; // shrink |
1765 |
– |
w.hint = -1; // suppress helping |
1772 |
|
break; |
1773 |
|
} |
1774 |
|
} |
1777 |
|
|
1778 |
|
/** |
1779 |
|
* Scans through queues looking for work (optionally, while |
1780 |
< |
* joining a task); if any are present, signals. May return early |
1781 |
< |
* if more signalling is detectably unneeded. |
1780 |
> |
* joining a task); if any present, signals. May return early if |
1781 |
> |
* more signalling is detectably unneeded. |
1782 |
|
* |
1783 |
|
* @param task if non-null, return early if done |
1784 |
|
* @param origin an index to start scan |
1785 |
+ |
* @param once if only the origin should be checked |
1786 |
|
*/ |
1787 |
< |
final int helpSignal(ForkJoinTask<?> task, int origin) { |
1788 |
< |
WorkQueue[] ws; WorkQueue q; int m, n, s, u; |
1789 |
< |
if ((ws = workQueues) != null && (m = ws.length - 1) >= 0) { |
1790 |
< |
for (int i = 0; i <= m; ++i) { |
1791 |
< |
if (task != null && (s = task.status) < 0) |
1792 |
< |
return s; |
1793 |
< |
if ((q = ws[(i + origin) & m]) != null && |
1794 |
< |
(n = q.queueSize()) > 0) { |
1795 |
< |
signalWork(q, n); |
1796 |
< |
if ((u = (int)(ctl >>> 32)) >= 0 || (u >> UAC_SHIFT) >= 0) |
1787 |
> |
private void helpSignal(ForkJoinTask<?> task, int origin, boolean once) { |
1788 |
> |
WorkQueue[] ws; WorkQueue w; Thread p; long c; int m, u, e, i, s; |
1789 |
> |
if ((u = (int)(ctl >>> 32)) < 0 && (u >> UAC_SHIFT) < 0 && |
1790 |
> |
(ws = workQueues) != null && (m = ws.length - 1) >= 0) { |
1791 |
> |
outer: for (int k = origin, j = once ? 0 : m; j >= 0; --j) { |
1792 |
> |
WorkQueue q = ws[k++ & m]; |
1793 |
> |
for (int n = m;;) { // limit to at most m signals |
1794 |
> |
if (task != null && task.status < 0) |
1795 |
> |
break outer; |
1796 |
> |
if (q == null || |
1797 |
> |
((s = (task == null ? -1 : 0) - q.base + q.top) <= n && |
1798 |
> |
(n = s) <= 0)) |
1799 |
|
break; |
1800 |
+ |
if ((u = (int)((c = ctl) >>> 32)) >= 0 || |
1801 |
+ |
(e = (int)c) <= 0 || m < (i = e & SMASK) || |
1802 |
+ |
(w = ws[i]) == null) |
1803 |
+ |
break outer; |
1804 |
+ |
long nc = (((long)(w.nextWait & E_MASK)) | |
1805 |
+ |
((long)(u + UAC_UNIT) << 32)); |
1806 |
+ |
if (w.eventCount == (e | INT_SIGN) && |
1807 |
+ |
U.compareAndSwapLong(this, CTL, c, nc)) { |
1808 |
+ |
w.eventCount = (e + E_SEQ) & E_MASK; |
1809 |
+ |
if ((p = w.parker) != null) |
1810 |
+ |
U.unpark(p); |
1811 |
+ |
if (--n <= 0) |
1812 |
+ |
break; |
1813 |
+ |
} |
1814 |
|
} |
1815 |
|
} |
1816 |
|
} |
1794 |
– |
return 0; |
1817 |
|
} |
1818 |
|
|
1819 |
|
/** |
1962 |
|
else if (tc + pc < MAX_CAP) { |
1963 |
|
long nc = ((c + TC_UNIT) & TC_MASK) | (c & ~TC_MASK); |
1964 |
|
if (U.compareAndSwapLong(this, CTL, c, nc)) { |
1965 |
< |
addWorker(); |
1966 |
< |
return true; |
1965 |
> |
ForkJoinWorkerThreadFactory fac; |
1966 |
> |
Throwable ex = null; |
1967 |
> |
ForkJoinWorkerThread wt = null; |
1968 |
> |
try { |
1969 |
> |
if ((fac = factory) != null && |
1970 |
> |
(wt = fac.newThread(this)) != null) { |
1971 |
> |
wt.start(); |
1972 |
> |
return true; |
1973 |
> |
} |
1974 |
> |
} catch (Throwable rex) { |
1975 |
> |
ex = rex; |
1976 |
> |
} |
1977 |
> |
deregisterWorker(wt, ex); // clean up and return false |
1978 |
|
} |
1979 |
|
} |
1980 |
|
} |
1993 |
|
if (joiner != null && task != null && (s = task.status) >= 0) { |
1994 |
|
ForkJoinTask<?> prevJoin = joiner.currentJoin; |
1995 |
|
joiner.currentJoin = task; |
1996 |
< |
do {} while ((s = task.status) >= 0 && |
1964 |
< |
joiner.queueSize() > 0 && |
1996 |
> |
do {} while ((s = task.status) >= 0 && !joiner.isEmpty() && |
1997 |
|
joiner.tryRemoveAndExec(task)); // process local tasks |
1998 |
< |
if (s >= 0 && (s = task.status) >= 0 && |
1999 |
< |
(s = helpSignal(task, joiner.poolIndex)) >= 0 && |
2000 |
< |
(task instanceof CountedCompleter)) |
2001 |
< |
s = helpComplete(task, LIFO_QUEUE); |
2002 |
< |
int k = 0; // to perform pre-block yield for politeness |
1998 |
> |
if (s >= 0 && (s = task.status) >= 0) { |
1999 |
> |
helpSignal(task, joiner.poolIndex, false); |
2000 |
> |
if ((s = task.status) >= 0 && |
2001 |
> |
(task instanceof CountedCompleter)) |
2002 |
> |
s = helpComplete(task, LIFO_QUEUE); |
2003 |
> |
} |
2004 |
|
while (s >= 0 && (s = task.status) >= 0) { |
2005 |
< |
if ((joiner.queueSize() > 0 || // try helping |
2005 |
> |
if ((!joiner.isEmpty() || // try helping |
2006 |
|
(s = tryHelpStealer(joiner, task)) == 0) && |
2007 |
|
(s = task.status) >= 0) { |
2008 |
< |
if (k < 3) { |
2009 |
< |
if (++k < 3) |
1977 |
< |
s = helpSignal(task, joiner.poolIndex); |
1978 |
< |
else |
1979 |
< |
Thread.yield(); |
1980 |
< |
} |
1981 |
< |
else if (!tryCompensate()) |
1982 |
< |
k = 0; |
1983 |
< |
else { |
2008 |
> |
helpSignal(task, joiner.poolIndex, false); |
2009 |
> |
if ((s = task.status) >= 0 && tryCompensate()) { |
2010 |
|
if (task.trySetSignal() && (s = task.status) >= 0) { |
2011 |
|
synchronized (task) { |
2012 |
|
if (task.status >= 0) { |
2043 |
|
if (joiner != null && task != null && (s = task.status) >= 0) { |
2044 |
|
ForkJoinTask<?> prevJoin = joiner.currentJoin; |
2045 |
|
joiner.currentJoin = task; |
2046 |
< |
do {} while ((s = task.status) >= 0 && |
2021 |
< |
joiner.queueSize() > 0 && |
2046 |
> |
do {} while ((s = task.status) >= 0 && !joiner.isEmpty() && |
2047 |
|
joiner.tryRemoveAndExec(task)); |
2048 |
< |
if (s >= 0 && (s = task.status) >= 0 && |
2049 |
< |
(s = helpSignal(task, joiner.poolIndex)) >= 0 && |
2050 |
< |
(task instanceof CountedCompleter)) |
2051 |
< |
s = helpComplete(task, LIFO_QUEUE); |
2052 |
< |
if (s >= 0 && joiner.queueSize() == 0) { |
2048 |
> |
if (s >= 0 && (s = task.status) >= 0) { |
2049 |
> |
helpSignal(task, joiner.poolIndex, false); |
2050 |
> |
if ((s = task.status) >= 0 && |
2051 |
> |
(task instanceof CountedCompleter)) |
2052 |
> |
s = helpComplete(task, LIFO_QUEUE); |
2053 |
> |
} |
2054 |
> |
if (s >= 0 && joiner.isEmpty()) { |
2055 |
|
do {} while (task.status >= 0 && |
2056 |
|
tryHelpStealer(joiner, task) > 0); |
2057 |
|
} |
2073 |
|
return null; |
2074 |
|
for (int j = (m + 1) << 2; ;) { |
2075 |
|
WorkQueue q = ws[(((r + j) << 1) | 1) & m]; |
2076 |
< |
if (q != null && (n = q.queueSize()) > 0) { |
2077 |
< |
if (n > 1) |
2078 |
< |
signalWork(q, 0); |
2076 |
> |
if (q != null && (n = q.base - q.top) < 0) { |
2077 |
> |
if (n < -1) |
2078 |
> |
signalWork(q); |
2079 |
|
return q; |
2080 |
|
} |
2081 |
|
else if (--j < 0) { |
2317 |
|
*/ |
2318 |
|
static boolean tryExternalUnpush(ForkJoinTask<?> t) { |
2319 |
|
ForkJoinPool p; WorkQueue[] ws; WorkQueue q; Submitter z; |
2320 |
< |
ForkJoinTask<?>[] a; int m, s; long j; |
2321 |
< |
if ((z = submitters.get()) != null && |
2320 |
> |
ForkJoinTask<?>[] a; int m, s; |
2321 |
> |
if (t != null && |
2322 |
> |
(z = submitters.get()) != null && |
2323 |
|
(p = commonPool) != null && |
2324 |
|
(ws = p.workQueues) != null && |
2325 |
|
(m = ws.length - 1) >= 0 && |
2326 |
|
(q = ws[m & z.seed & SQMASK]) != null && |
2327 |
|
(s = q.top) != q.base && |
2328 |
< |
(a = q.array) != null && |
2329 |
< |
U.getObjectVolatile |
2330 |
< |
(a, j = (((a.length - 1) & (s - 1)) << ASHIFT) + ABASE) == t && |
2331 |
< |
U.compareAndSwapInt(q, QLOCK, 0, 1)) { |
2332 |
< |
if (q.array == a && q.top == s && // recheck |
2333 |
< |
U.compareAndSwapObject(a, j, t, null)) { |
2334 |
< |
q.top = s - 1; |
2328 |
> |
(a = q.array) != null) { |
2329 |
> |
long j = (((a.length - 1) & (s - 1)) << ASHIFT) + ABASE; |
2330 |
> |
if (U.getObject(a, j) == t && |
2331 |
> |
U.compareAndSwapInt(q, QLOCK, 0, 1)) { |
2332 |
> |
if (q.array == a && q.top == s && // recheck |
2333 |
> |
U.compareAndSwapObject(a, j, t, null)) { |
2334 |
> |
q.top = s - 1; |
2335 |
> |
q.qlock = 0; |
2336 |
> |
return true; |
2337 |
> |
} |
2338 |
|
q.qlock = 0; |
2308 |
– |
return true; |
2339 |
|
} |
2310 |
– |
q.qlock = 0; |
2340 |
|
} |
2341 |
|
return false; |
2342 |
|
} |
2378 |
|
(u = (int)(ctl >>> 32)) >= 0 || (u >> UAC_SHIFT) >= 0) |
2379 |
|
break; |
2380 |
|
if (task == null) { |
2381 |
< |
if (helpSignal(root, q.poolIndex) >= 0) |
2381 |
> |
helpSignal(root, q.poolIndex, false); |
2382 |
> |
if (root.status >= 0) |
2383 |
|
helpComplete(root, SHARED_QUEUE); |
2384 |
|
break; |
2385 |
|
} |
2394 |
|
static void externalHelpJoin(ForkJoinTask<?> t) { |
2395 |
|
// Some hard-to-avoid overlap with tryExternalUnpush |
2396 |
|
ForkJoinPool p; WorkQueue[] ws; WorkQueue q, w; Submitter z; |
2397 |
< |
ForkJoinTask<?>[] a; int m, s, n; long j; |
2397 |
> |
ForkJoinTask<?>[] a; int m, s, n; |
2398 |
|
if (t != null && |
2399 |
|
(z = submitters.get()) != null && |
2400 |
|
(p = commonPool) != null && |
2401 |
|
(ws = p.workQueues) != null && |
2402 |
|
(m = ws.length - 1) >= 0 && |
2403 |
|
(q = ws[m & z.seed & SQMASK]) != null && |
2404 |
< |
(a = q.array) != null && |
2405 |
< |
t.status >= 0) { |
2406 |
< |
if ((s = q.top) != q.base && |
2407 |
< |
U.getObjectVolatile |
2408 |
< |
(a, j = (((a.length - 1) & (s - 1)) << ASHIFT) + ABASE) == t && |
2409 |
< |
U.compareAndSwapInt(q, QLOCK, 0, 1)) { |
2410 |
< |
if (q.array == a && q.top == s && |
2411 |
< |
U.compareAndSwapObject(a, j, t, null)) { |
2412 |
< |
q.top = s - 1; |
2413 |
< |
q.qlock = 0; |
2414 |
< |
t.doExec(); |
2404 |
> |
(a = q.array) != null) { |
2405 |
> |
int am = a.length - 1; |
2406 |
> |
if ((s = q.top) != q.base) { |
2407 |
> |
long j = ((am & (s - 1)) << ASHIFT) + ABASE; |
2408 |
> |
if (U.getObject(a, j) == t && |
2409 |
> |
U.compareAndSwapInt(q, QLOCK, 0, 1)) { |
2410 |
> |
if (q.array == a && q.top == s && |
2411 |
> |
U.compareAndSwapObject(a, j, t, null)) { |
2412 |
> |
q.top = s - 1; |
2413 |
> |
q.qlock = 0; |
2414 |
> |
t.doExec(); |
2415 |
> |
} |
2416 |
> |
else |
2417 |
> |
q.qlock = 0; |
2418 |
|
} |
2386 |
– |
else |
2387 |
– |
q.qlock = 0; |
2419 |
|
} |
2420 |
|
if (t.status >= 0) { |
2421 |
|
if (t instanceof CountedCompleter) |
2422 |
|
p.externalHelpComplete(q, t); |
2423 |
|
else |
2424 |
< |
p.helpSignal(t, q.poolIndex); |
2424 |
> |
p.helpSignal(t, q.poolIndex, false); |
2425 |
|
} |
2426 |
|
} |
2427 |
|
} |
2869 |
|
WorkQueue[] ws; WorkQueue w; |
2870 |
|
if ((ws = workQueues) != null) { |
2871 |
|
for (int i = 0; i < ws.length; i += 2) { |
2872 |
< |
if ((w = ws[i]) != null && w.queueSize() != 0) |
2872 |
> |
if ((w = ws[i]) != null && !w.isEmpty()) |
2873 |
|
return true; |
2874 |
|
} |
2875 |
|
} |
3191 |
|
if (t instanceof ForkJoinWorkerThread) { |
3192 |
|
ForkJoinPool p = ((ForkJoinWorkerThread)t).pool; |
3193 |
|
while (!blocker.isReleasable()) { // variant of helpSignal |
3194 |
< |
WorkQueue[] ws; WorkQueue q; int m, n, u; |
3194 |
> |
WorkQueue[] ws; WorkQueue q; int m, u; |
3195 |
|
if ((ws = p.workQueues) != null && (m = ws.length - 1) >= 0) { |
3196 |
|
for (int i = 0; i <= m; ++i) { |
3197 |
|
if (blocker.isReleasable()) |
3198 |
|
return; |
3199 |
< |
if ((q = ws[i]) != null && (n = q.queueSize()) > 0) { |
3200 |
< |
p.signalWork(q, n); |
3199 |
> |
if ((q = ws[i]) != null && q.base - q.top < 0) { |
3200 |
> |
p.signalWork(q); |
3201 |
|
if ((u = (int)(p.ctl >>> 32)) >= 0 || |
3202 |
|
(u >> UAC_SHIFT) >= 0) |
3203 |
|
break; |
3276 |
|
submitters = new ThreadLocal<Submitter>(); |
3277 |
|
ForkJoinWorkerThreadFactory fac = defaultForkJoinWorkerThreadFactory = |
3278 |
|
new DefaultForkJoinWorkerThreadFactory(); |
3279 |
+ |
modifyThreadPermission = new RuntimePermission("modifyThread"); |
3280 |
+ |
|
3281 |
|
/* |
3282 |
|
* Establish common pool parameters. For extra caution, |
3283 |
|
* computations to set up common pool state are here; the |
3313 |
|
long ct = ((np << AC_SHIFT) & AC_MASK) | ((np << TC_SHIFT) & TC_MASK); |
3314 |
|
|
3315 |
|
commonPool = new ForkJoinPool(par, ct, fac, handler); |
3283 |
– |
modifyThreadPermission = new RuntimePermission("modifyThread"); |
3316 |
|
} |
3317 |
|
|
3318 |
|
/** |