7 |
|
package jsr166y; |
8 |
|
|
9 |
|
import java.util.concurrent.*; |
10 |
– |
|
10 |
|
import java.util.ArrayList; |
11 |
|
import java.util.Arrays; |
12 |
|
import java.util.Collection; |
156 |
|
* links to try to find such a task. |
157 |
|
* |
158 |
|
* Compensating: Unless there are already enough live threads, |
159 |
< |
* method helpMaintainParallelism() may create or or |
159 |
> |
* method helpMaintainParallelism() may create or |
160 |
|
* re-activate a spare thread to compensate for blocked |
161 |
|
* joiners until they unblock. |
162 |
|
* |
317 |
|
* exactly #parallelism threads running, which is an impossible |
318 |
|
* task. We always need to create one when the number of running |
319 |
|
* threads would become zero and all workers are busy. Beyond |
320 |
< |
* this, we must rely on heuristics that work well in the the |
321 |
< |
* presence of transients phenomena such as GC stalls, dynamic |
320 |
> |
* this, we must rely on heuristics that work well in the |
321 |
> |
* presence of transient phenomena such as GC stalls, dynamic |
322 |
|
* compilation, and wake-up lags. These transients are extremely |
323 |
|
* common -- we are normally trying to fully saturate the CPUs on |
324 |
|
* a machine, so almost any activity other than running tasks |
345 |
|
* "while ((local = field) != 0)") which are usually the simplest |
346 |
|
* way to ensure the required read orderings (which are sometimes |
347 |
|
* critical). Also several occurrences of the unusual "do {} |
348 |
< |
* while(!cas...)" which is the simplest way to force an update of |
348 |
> |
* while (!cas...)" which is the simplest way to force an update of |
349 |
|
* a CAS'ed variable. There are also other coding oddities that |
350 |
|
* help some methods perform reasonably even when interpreted (not |
351 |
|
* compiled), at the expense of some messy constructions that |
673 |
|
} |
674 |
|
|
675 |
|
/** |
676 |
< |
* Nulls out record of worker in workers array |
676 |
> |
* Nulls out record of worker in workers array. |
677 |
|
*/ |
678 |
|
private void forgetWorker(ForkJoinWorkerThread w) { |
679 |
|
int idx = w.poolIndex; |
831 |
|
UNSAFE.compareAndSwapInt(this, spareWaitersOffset, |
832 |
|
sw, w.nextSpare)) { |
833 |
|
int c; // increment running count before resume |
834 |
< |
do {} while(!UNSAFE.compareAndSwapInt |
835 |
< |
(this, workerCountsOffset, |
836 |
< |
c = workerCounts, c + ONE_RUNNING)); |
834 |
> |
do {} while (!UNSAFE.compareAndSwapInt |
835 |
> |
(this, workerCountsOffset, |
836 |
> |
c = workerCounts, c + ONE_RUNNING)); |
837 |
|
if (w.tryUnsuspend()) |
838 |
|
LockSupport.unpark(w); |
839 |
|
else // back out if w was shutdown |
984 |
|
w.lastEventCount = ec; // no need to wait |
985 |
|
break; |
986 |
|
} |
987 |
< |
else if (!(inactivate |= active)) |
987 |
> |
else if (!(inactivate |= active)) |
988 |
|
eventSync(w, wec); // must inactivate before sync |
989 |
|
} |
990 |
|
else |
1106 |
|
c = eventCount, c+1); |
1107 |
|
eventWaiters = 0L; // clobber lists |
1108 |
|
spareWaiters = 0; |
1109 |
< |
ForkJoinWorkerThread[] ws = workers; |
1111 |
< |
int n = ws.length; |
1112 |
< |
for (int i = 0; i < n; ++i) { |
1113 |
< |
ForkJoinWorkerThread w = ws[i]; |
1109 |
> |
for (ForkJoinWorkerThread w : workers) { |
1110 |
|
if (w != null) { |
1111 |
|
w.shutdown(); |
1112 |
|
if (passes > 0 && !w.isTerminated()) { |
1523 |
|
*/ |
1524 |
|
public long getQueuedTaskCount() { |
1525 |
|
long count = 0; |
1526 |
< |
ForkJoinWorkerThread[] ws = workers; |
1531 |
< |
int n = ws.length; |
1532 |
< |
for (int i = 0; i < n; ++i) { |
1533 |
< |
ForkJoinWorkerThread w = ws[i]; |
1526 |
> |
for (ForkJoinWorkerThread w : workers) |
1527 |
|
if (w != null) |
1528 |
|
count += w.getQueueSize(); |
1536 |
– |
} |
1529 |
|
return count; |
1530 |
|
} |
1531 |
|
|
1580 |
|
*/ |
1581 |
|
protected int drainTasksTo(Collection<? super ForkJoinTask<?>> c) { |
1582 |
|
int count = submissionQueue.drainTo(c); |
1583 |
< |
ForkJoinWorkerThread[] ws = workers; |
1592 |
< |
int n = ws.length; |
1593 |
< |
for (int i = 0; i < n; ++i) { |
1594 |
< |
ForkJoinWorkerThread w = ws[i]; |
1583 |
> |
for (ForkJoinWorkerThread w : workers) |
1584 |
|
if (w != null) |
1585 |
|
count += w.drainTasksTo(c); |
1597 |
– |
} |
1586 |
|
return count; |
1587 |
|
} |
1588 |
|
|
1709 |
|
throws InterruptedException { |
1710 |
|
try { |
1711 |
|
return termination.awaitAdvanceInterruptibly(0, timeout, unit) > 0; |
1712 |
< |
} catch(TimeoutException ex) { |
1712 |
> |
} catch (TimeoutException ex) { |
1713 |
|
return false; |
1714 |
|
} |
1715 |
|
} |