6 |
|
|
7 |
|
package jsr166e; |
8 |
|
|
9 |
+ |
import java.lang.Thread.UncaughtExceptionHandler; |
10 |
|
import java.util.ArrayList; |
11 |
|
import java.util.Arrays; |
12 |
|
import java.util.Collection; |
18 |
|
import java.util.concurrent.Future; |
19 |
|
import java.util.concurrent.RejectedExecutionException; |
20 |
|
import java.util.concurrent.RunnableFuture; |
21 |
+ |
import java.util.concurrent.ThreadLocalRandom; |
22 |
|
import java.util.concurrent.TimeUnit; |
23 |
|
|
24 |
|
/** |
51 |
|
* level; by default, equal to the number of available processors. The |
52 |
|
* pool attempts to maintain enough active (or available) threads by |
53 |
|
* dynamically adding, suspending, or resuming internal worker |
54 |
< |
* threads, even if some tasks are stalled waiting to join |
55 |
< |
* others. However, no such adjustments are guaranteed in the face of |
56 |
< |
* blocked I/O or other unmanaged synchronization. The nested {@link |
54 |
> |
* threads, even if some tasks are stalled waiting to join others. |
55 |
> |
* However, no such adjustments are guaranteed in the face of blocked |
56 |
> |
* I/O or other unmanaged synchronization. The nested {@link |
57 |
|
* ManagedBlocker} interface enables extension of the kinds of |
58 |
|
* synchronization accommodated. |
59 |
|
* |
77 |
|
* there is little difference among choice of methods. |
78 |
|
* |
79 |
|
* <table BORDER CELLPADDING=3 CELLSPACING=1> |
80 |
+ |
* <caption>Summary of task execution methods</caption> |
81 |
|
* <tr> |
82 |
|
* <td></td> |
83 |
|
* <td ALIGN=CENTER> <b>Call from non-fork/join clients</b></td> |
84 |
|
* <td ALIGN=CENTER> <b>Call from within fork/join computations</b></td> |
85 |
|
* </tr> |
86 |
|
* <tr> |
87 |
< |
* <td> <b>Arrange async execution</td> |
87 |
> |
* <td> <b>Arrange async execution</b></td> |
88 |
|
* <td> {@link #execute(ForkJoinTask)}</td> |
89 |
|
* <td> {@link ForkJoinTask#fork}</td> |
90 |
|
* </tr> |
91 |
|
* <tr> |
92 |
< |
* <td> <b>Await and obtain result</td> |
92 |
> |
* <td> <b>Await and obtain result</b></td> |
93 |
|
* <td> {@link #invoke(ForkJoinTask)}</td> |
94 |
|
* <td> {@link ForkJoinTask#invoke}</td> |
95 |
|
* </tr> |
96 |
|
* <tr> |
97 |
< |
* <td> <b>Arrange exec and obtain Future</td> |
97 |
> |
* <td> <b>Arrange exec and obtain Future</b></td> |
98 |
|
* <td> {@link #submit(ForkJoinTask)}</td> |
99 |
|
* <td> {@link ForkJoinTask#fork} (ForkJoinTasks <em>are</em> Futures)</td> |
100 |
|
* </tr> |
101 |
|
* </table> |
102 |
|
* |
103 |
|
* <p>The common pool is by default constructed with default |
104 |
< |
* parameters, but these may be controlled by setting three {@link |
105 |
< |
* System#getProperty system properties} with prefix {@code |
106 |
< |
* java.util.concurrent.ForkJoinPool.common}: {@code parallelism} -- |
107 |
< |
* an integer greater than zero, {@code threadFactory} -- the class |
108 |
< |
* name of a {@link ForkJoinWorkerThreadFactory}, and {@code |
109 |
< |
* exceptionHandler} -- the class name of a {@link |
110 |
< |
* java.lang.Thread.UncaughtExceptionHandler |
111 |
< |
* Thread.UncaughtExceptionHandler}. Upon any error in establishing |
112 |
< |
* these settings, default parameters are used. |
104 |
> |
* parameters, but these may be controlled by setting three |
105 |
> |
* {@linkplain System#getProperty system properties}: |
106 |
> |
* <ul> |
107 |
> |
* <li>{@code java.util.concurrent.ForkJoinPool.common.parallelism} |
108 |
> |
* - the parallelism level, a non-negative integer |
109 |
> |
* <li>{@code java.util.concurrent.ForkJoinPool.common.threadFactory} |
110 |
> |
* - the class name of a {@link ForkJoinWorkerThreadFactory} |
111 |
> |
* <li>{@code java.util.concurrent.ForkJoinPool.common.exceptionHandler} |
112 |
> |
* - the class name of a {@link UncaughtExceptionHandler} |
113 |
> |
* </ul> |
114 |
> |
* The system class loader is used to load these classes. |
115 |
> |
* Upon any error in establishing these settings, default parameters |
116 |
> |
* are used. It is possible to disable or limit the use of threads in |
117 |
> |
* the common pool by setting the parallelism property to zero, and/or |
118 |
> |
* using a factory that may return {@code null}. |
119 |
|
* |
120 |
|
* <p><b>Implementation notes</b>: This implementation restricts the |
121 |
|
* maximum number of running threads to 32767. Attempts to create |
161 |
|
* (http://research.sun.com/scalable/pubs/index.html) and |
162 |
|
* "Idempotent work stealing" by Michael, Saraswat, and Vechev, |
163 |
|
* PPoPP 2009 (http://portal.acm.org/citation.cfm?id=1504186). |
164 |
< |
* The main differences ultimately stem from GC requirements that |
165 |
< |
* we null out taken slots as soon as we can, to maintain as small |
166 |
< |
* a footprint as possible even in programs generating huge |
167 |
< |
* numbers of tasks. To accomplish this, we shift the CAS |
168 |
< |
* arbitrating pop vs poll (steal) from being on the indices |
169 |
< |
* ("base" and "top") to the slots themselves. So, both a |
170 |
< |
* successful pop and poll mainly entail a CAS of a slot from |
171 |
< |
* non-null to null. Because we rely on CASes of references, we |
172 |
< |
* do not need tag bits on base or top. They are simple ints as |
173 |
< |
* used in any circular array-based queue (see for example |
174 |
< |
* ArrayDeque). Updates to the indices must still be ordered in a |
175 |
< |
* way that guarantees that top == base means the queue is empty, |
176 |
< |
* but otherwise may err on the side of possibly making the queue |
177 |
< |
* appear nonempty when a push, pop, or poll have not fully |
178 |
< |
* committed. Note that this means that the poll operation, |
179 |
< |
* considered individually, is not wait-free. One thief cannot |
180 |
< |
* successfully continue until another in-progress one (or, if |
181 |
< |
* previously empty, a push) completes. However, in the |
182 |
< |
* aggregate, we ensure at least probabilistic non-blockingness. |
183 |
< |
* If an attempted steal fails, a thief always chooses a different |
184 |
< |
* random victim target to try next. So, in order for one thief to |
185 |
< |
* progress, it suffices for any in-progress poll or new push on |
186 |
< |
* any empty queue to complete. (This is why we normally use |
187 |
< |
* method pollAt and its variants that try once at the apparent |
188 |
< |
* base index, else consider alternative actions, rather than |
189 |
< |
* method poll.) |
164 |
> |
* See also "Correct and Efficient Work-Stealing for Weak Memory |
165 |
> |
* Models" by Le, Pop, Cohen, and Nardelli, PPoPP 2013 |
166 |
> |
* (http://www.di.ens.fr/~zappa/readings/ppopp13.pdf) for an |
167 |
> |
* analysis of memory ordering (atomic, volatile etc) issues. The |
168 |
> |
* main differences ultimately stem from GC requirements that we |
169 |
> |
* null out taken slots as soon as we can, to maintain as small a |
170 |
> |
* footprint as possible even in programs generating huge numbers |
171 |
> |
* of tasks. To accomplish this, we shift the CAS arbitrating pop |
172 |
> |
* vs poll (steal) from being on the indices ("base" and "top") to |
173 |
> |
* the slots themselves. So, both a successful pop and poll |
174 |
> |
* mainly entail a CAS of a slot from non-null to null. Because |
175 |
> |
* we rely on CASes of references, we do not need tag bits on base |
176 |
> |
* or top. They are simple ints as used in any circular |
177 |
> |
* array-based queue (see for example ArrayDeque). Updates to the |
178 |
> |
* indices must still be ordered in a way that guarantees that top |
179 |
> |
* == base means the queue is empty, but otherwise may err on the |
180 |
> |
* side of possibly making the queue appear nonempty when a push, |
181 |
> |
* pop, or poll have not fully committed. Note that this means |
182 |
> |
* that the poll operation, considered individually, is not |
183 |
> |
* wait-free. One thief cannot successfully continue until another |
184 |
> |
* in-progress one (or, if previously empty, a push) completes. |
185 |
> |
* However, in the aggregate, we ensure at least probabilistic |
186 |
> |
* non-blockingness. If an attempted steal fails, a thief always |
187 |
> |
* chooses a different random victim target to try next. So, in |
188 |
> |
* order for one thief to progress, it suffices for any |
189 |
> |
* in-progress poll or new push on any empty queue to |
190 |
> |
* complete. (This is why we normally use method pollAt and its |
191 |
> |
* variants that try once at the apparent base index, else |
192 |
> |
* consider alternative actions, rather than method poll.) |
193 |
|
* |
194 |
|
* This approach also enables support of a user mode in which local |
195 |
|
* task processing is in FIFO, not LIFO order, simply by using |
208 |
|
* for work-stealing (this would contaminate lifo/fifo |
209 |
|
* processing). Instead, we randomly associate submission queues |
210 |
|
* with submitting threads, using a form of hashing. The |
211 |
< |
* ThreadLocal Submitter class contains a value initially used as |
212 |
< |
* a hash code for choosing existing queues, but may be randomly |
213 |
< |
* repositioned upon contention with other submitters. In |
214 |
< |
* essence, submitters act like workers except that they are |
215 |
< |
* restricted to executing local tasks that they submitted (or in |
216 |
< |
* the case of CountedCompleters, others with the same root task). |
217 |
< |
* However, because most shared/external queue operations are more |
218 |
< |
* expensive than internal, and because, at steady state, external |
219 |
< |
* submitters will compete for CPU with workers, ForkJoinTask.join |
220 |
< |
* and related methods disable them from repeatedly helping to |
221 |
< |
* process tasks if all workers are active. Insertion of tasks in |
222 |
< |
* shared mode requires a lock (mainly to protect in the case of |
211 |
> |
* Submitter probe value serves as a hash code for |
212 |
> |
* choosing existing queues, and may be randomly repositioned upon |
213 |
> |
* contention with other submitters. In essence, submitters act |
214 |
> |
* like workers except that they are restricted to executing local |
215 |
> |
* tasks that they submitted (or in the case of CountedCompleters, |
216 |
> |
* others with the same root task). However, because most |
217 |
> |
* shared/external queue operations are more expensive than |
218 |
> |
* internal, and because, at steady state, external submitters |
219 |
> |
* will compete for CPU with workers, ForkJoinTask.join and |
220 |
> |
* related methods disable them from repeatedly helping to process |
221 |
> |
* tasks if all workers are active. Insertion of tasks in shared |
222 |
> |
* mode requires a lock (mainly to protect in the case of |
223 |
|
* resizing) but we use only a simple spinlock (using bits in |
224 |
|
* field qlock), because submitters encountering a busy queue move |
225 |
|
* on to try or create other queues -- they block only when |
309 |
|
* has not yet entered the wait queue. We solve this by requiring |
310 |
|
* a full sweep of all workers (via repeated calls to method |
311 |
|
* scan()) both before and after a newly waiting worker is added |
312 |
< |
* to the wait queue. During a rescan, the worker might release |
313 |
< |
* some other queued worker rather than itself, which has the same |
314 |
< |
* net effect. Because enqueued workers may actually be rescanning |
315 |
< |
* rather than waiting, we set and clear the "parker" field of |
316 |
< |
* WorkQueues to reduce unnecessary calls to unpark. (This |
317 |
< |
* requires a secondary recheck to avoid missed signals.) Note |
318 |
< |
* the unusual conventions about Thread.interrupts surrounding |
319 |
< |
* parking and other blocking: Because interrupts are used solely |
320 |
< |
* to alert threads to check termination, which is checked anyway |
321 |
< |
* upon blocking, we clear status (using Thread.interrupted) |
322 |
< |
* before any call to park, so that park does not immediately |
311 |
< |
* return due to status being set via some other unrelated call to |
312 |
< |
* interrupt in user code. |
312 |
> |
* to the wait queue. Because enqueued workers may actually be |
313 |
> |
* rescanning rather than waiting, we set and clear the "parker" |
314 |
> |
* field of WorkQueues to reduce unnecessary calls to unpark. |
315 |
> |
* (This requires a secondary recheck to avoid missed signals.) |
316 |
> |
* Note the unusual conventions about Thread.interrupts |
317 |
> |
* surrounding parking and other blocking: Because interrupts are |
318 |
> |
* used solely to alert threads to check termination, which is |
319 |
> |
* checked anyway upon blocking, we clear status (using |
320 |
> |
* Thread.interrupted) before any call to park, so that park does |
321 |
> |
* not immediately return due to status being set via some other |
322 |
> |
* unrelated call to interrupt in user code. |
323 |
|
* |
324 |
|
* Signalling. We create or wake up workers only when there |
325 |
|
* appears to be at least one task they might be able to find and |
326 |
< |
* execute. However, many other threads may notice the same task |
327 |
< |
* and each signal to wake up a thread that might take it. So in |
328 |
< |
* general, pools will be over-signalled. When a submission is |
329 |
< |
* added or another worker adds a task to a queue that has fewer |
330 |
< |
* than two tasks, they signal waiting workers (or trigger |
331 |
< |
* creation of new ones if fewer than the given parallelism level |
332 |
< |
* -- signalWork), and may leave a hint to the unparked worker to |
333 |
< |
* 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 |
326 |
> |
* execute. When a submission is added or another worker adds a |
327 |
> |
* task to a queue that has fewer than two tasks, they signal |
328 |
> |
* waiting workers (or trigger creation of new ones if fewer than |
329 |
> |
* the given parallelism level -- signalWork). These primary |
330 |
> |
* signals are buttressed by others whenever other threads remove |
331 |
> |
* a task from a queue and notice that there are other tasks there |
332 |
> |
* as well. So in general, pools will be over-signalled. On most |
333 |
> |
* platforms, signalling (unpark) overhead time is noticeably |
334 |
|
* long, and the time between signalling a thread and it actually |
335 |
|
* making progress can be very noticeably long, so it is worth |
336 |
|
* offloading these delays from critical paths as much as |
337 |
< |
* possible. |
337 |
> |
* possible. Additionally, workers spin-down gradually, by staying |
338 |
> |
* alive so long as they see the ctl state changing. Similar |
339 |
> |
* stability-sensing techniques are also used before blocking in |
340 |
> |
* awaitJoin and helpComplete. |
341 |
|
* |
342 |
|
* Trimming workers. To release resources after periods of lack of |
343 |
|
* use, a worker starting to wait when the pool is quiescent will |
450 |
|
* Common Pool |
451 |
|
* =========== |
452 |
|
* |
453 |
< |
* The static common Pool always exists after static |
453 |
> |
* The static common pool always exists after static |
454 |
|
* initialization. Since it (or any other created pool) need |
455 |
|
* never be used, we minimize initial construction overhead and |
456 |
|
* footprint to the setup of about a dozen fields, with no nested |
458 |
|
* fullExternalPush during the first submission to the pool. |
459 |
|
* |
460 |
|
* When external threads submit to the common pool, they can |
461 |
< |
* perform some subtask processing (see externalHelpJoin and |
462 |
< |
* related methods). We do not need to record whether these |
461 |
> |
* perform subtask processing (see externalHelpJoin and related |
462 |
> |
* methods). This caller-helps policy makes it sensible to set |
463 |
> |
* common pool parallelism level to one (or more) less than the |
464 |
> |
* total number of available cores, or even zero for pure |
465 |
> |
* caller-runs. We do not need to record whether external |
466 |
|
* submissions are to the common pool -- if not, externalHelpJoin |
467 |
|
* returns quickly (at the most helping to signal some common pool |
468 |
|
* workers). These submitters would otherwise be blocked waiting |
532 |
|
* |
533 |
|
* @param pool the pool this thread works in |
534 |
|
* @throws NullPointerException if the pool is null |
535 |
+ |
* @return the new worker thread |
536 |
|
*/ |
537 |
|
public ForkJoinWorkerThread newThread(ForkJoinPool pool); |
538 |
|
} |
549 |
|
} |
550 |
|
|
551 |
|
/** |
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 |
– |
/** |
552 |
|
* Class for artificial tasks that are used to replace the target |
553 |
|
* of local joins if they are removed from an interior queue slot |
554 |
|
* in WorkQueue.tryRemoveAndExec. We don't need the proxy to |
607 |
|
* do not want multiple WorkQueue instances or multiple queue |
608 |
|
* arrays sharing cache lines. (It would be best for queue objects |
609 |
|
* and their arrays to share, but there is nothing available to |
610 |
< |
* help arrange that). Unfortunately, because they are recorded |
611 |
< |
* in a common array, WorkQueue instances are often moved to be |
618 |
< |
* adjacent by garbage collectors. To reduce impact, we use field |
619 |
< |
* padding that works OK on common platforms; this effectively |
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.) 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. |
610 |
> |
* help arrange that). The @Contended annotation alerts JVMs to |
611 |
> |
* try to keep instances apart. |
612 |
|
*/ |
613 |
|
static final class WorkQueue { |
614 |
|
/** |
634 |
|
// Heuristic padding to ameliorate unfortunate memory placements |
635 |
|
volatile long pad00, pad01, pad02, pad03, pad04, pad05, pad06; |
636 |
|
|
652 |
– |
int seed; // for random scanning; initialize nonzero |
637 |
|
volatile int eventCount; // encoded inactivation count; < 0 if inactive |
638 |
|
int nextWait; // encoded record of next event waiter |
655 |
– |
int hint; // steal or signal hint (index) |
656 |
– |
int poolIndex; // index of this queue in pool (or 0) |
657 |
– |
final int mode; // 0: lifo, > 0: fifo, < 0: shared |
639 |
|
int nsteals; // number of steals |
640 |
+ |
int hint; // steal index hint |
641 |
+ |
short poolIndex; // index of this queue in pool |
642 |
+ |
final short mode; // 0: lifo, > 0: fifo, < 0: shared |
643 |
|
volatile int qlock; // 1: locked, -1: terminate; else 0 |
644 |
|
volatile int base; // index of next slot for poll |
645 |
|
int top; // index of next slot for push |
657 |
|
int seed) { |
658 |
|
this.pool = pool; |
659 |
|
this.owner = owner; |
660 |
< |
this.mode = mode; |
661 |
< |
this.seed = seed; |
660 |
> |
this.mode = (short)mode; |
661 |
> |
this.hint = seed; // store initial seed for runWorker |
662 |
|
// Place indices in the center of array (that is not yet allocated) |
663 |
|
base = top = INITIAL_QUEUE_CAPACITY >>> 1; |
664 |
|
} |
671 |
|
return (n >= 0) ? 0 : -n; // ignore transient negative |
672 |
|
} |
673 |
|
|
674 |
< |
/** |
674 |
> |
/** |
675 |
|
* Provides a more accurate estimate of whether this queue has |
676 |
|
* any tasks than does queueSize, by checking whether a |
677 |
|
* near-empty queue has at least one unclaimed task. |
696 |
|
*/ |
697 |
|
final void push(ForkJoinTask<?> task) { |
698 |
|
ForkJoinTask<?>[] a; ForkJoinPool p; |
699 |
< |
int s = top, m, n; |
699 |
> |
int s = top, n; |
700 |
|
if ((a = array) != null) { // ignore if queue removed |
701 |
< |
int j = (((m = a.length - 1) & s) << ASHIFT) + ABASE; |
702 |
< |
U.putOrderedObject(a, j, task); |
703 |
< |
if ((n = (top = s + 1) - base) <= 2) { |
704 |
< |
if ((p = pool) != null) |
721 |
< |
p.signalWork(this); |
722 |
< |
} |
701 |
> |
int m = a.length - 1; |
702 |
> |
U.putOrderedObject(a, ((m & s) << ASHIFT) + ABASE, task); |
703 |
> |
if ((n = (top = s + 1) - base) <= 2) |
704 |
> |
(p = pool).signalWork(p.workQueues, this); |
705 |
|
else if (n >= m) |
706 |
|
growArray(); |
707 |
|
} |
708 |
|
} |
709 |
|
|
710 |
< |
/** |
710 |
> |
/** |
711 |
|
* Initializes or doubles the capacity of array. Call either |
712 |
|
* by owner or with lock held -- it is OK for base, but not |
713 |
|
* top, to move while resizings are in progress. |
765 |
|
if ((a = array) != null) { |
766 |
|
int j = (((a.length - 1) & b) << ASHIFT) + ABASE; |
767 |
|
if ((t = (ForkJoinTask<?>)U.getObjectVolatile(a, j)) != null && |
768 |
< |
base == b && |
769 |
< |
U.compareAndSwapObject(a, j, t, null)) { |
788 |
< |
base = b + 1; |
768 |
> |
base == b && U.compareAndSwapObject(a, j, t, null)) { |
769 |
> |
U.putOrderedInt(this, QBASE, b + 1); |
770 |
|
return t; |
771 |
|
} |
772 |
|
} |
782 |
|
int j = (((a.length - 1) & b) << ASHIFT) + ABASE; |
783 |
|
t = (ForkJoinTask<?>)U.getObjectVolatile(a, j); |
784 |
|
if (t != null) { |
785 |
< |
if (base == b && |
786 |
< |
U.compareAndSwapObject(a, j, t, null)) { |
806 |
< |
base = b + 1; |
785 |
> |
if (U.compareAndSwapObject(a, j, t, null)) { |
786 |
> |
U.putOrderedInt(this, QBASE, b + 1); |
787 |
|
return t; |
788 |
|
} |
789 |
|
} |
840 |
|
ForkJoinTask.cancelIgnoringExceptions(t); |
841 |
|
} |
842 |
|
|
863 |
– |
/** |
864 |
– |
* Computes next value for random probes. Scans don't require |
865 |
– |
* a very high quality generator, but also not a crummy one. |
866 |
– |
* Marsaglia xor-shift is cheap and works well enough. Note: |
867 |
– |
* This is manually inlined in its usages in ForkJoinPool to |
868 |
– |
* avoid writes inside busy scan loops. |
869 |
– |
*/ |
870 |
– |
final int nextSeed() { |
871 |
– |
int r = seed; |
872 |
– |
r ^= r << 13; |
873 |
– |
r ^= r >>> 17; |
874 |
– |
return seed = r ^= r << 5; |
875 |
– |
} |
876 |
– |
|
843 |
|
// Specialized execution methods |
844 |
|
|
845 |
|
/** |
846 |
< |
* Pops and runs tasks until empty. |
846 |
> |
* Polls and runs tasks until empty. |
847 |
|
*/ |
848 |
< |
private void popAndExecAll() { |
849 |
< |
// A bit faster than repeated pop calls |
850 |
< |
ForkJoinTask<?>[] a; int m, s; long j; ForkJoinTask<?> t; |
885 |
< |
while ((a = array) != null && (m = a.length - 1) >= 0 && |
886 |
< |
(s = top - 1) - base >= 0 && |
887 |
< |
(t = ((ForkJoinTask<?>) |
888 |
< |
U.getObject(a, j = ((m & s) << ASHIFT) + ABASE))) |
889 |
< |
!= null) { |
890 |
< |
if (U.compareAndSwapObject(a, j, t, null)) { |
891 |
< |
top = s; |
892 |
< |
t.doExec(); |
893 |
< |
} |
894 |
< |
} |
848 |
> |
final void pollAndExecAll() { |
849 |
> |
for (ForkJoinTask<?> t; (t = poll()) != null;) |
850 |
> |
t.doExec(); |
851 |
|
} |
852 |
|
|
853 |
|
/** |
854 |
< |
* Polls and runs tasks until empty. |
854 |
> |
* Executes a top-level task and any local tasks remaining |
855 |
> |
* after execution. |
856 |
|
*/ |
857 |
< |
private void pollAndExecAll() { |
858 |
< |
for (ForkJoinTask<?> t; (t = poll()) != null;) |
859 |
< |
t.doExec(); |
857 |
> |
final void runTask(ForkJoinTask<?> task) { |
858 |
> |
if ((currentSteal = task) != null) { |
859 |
> |
task.doExec(); |
860 |
> |
ForkJoinTask<?>[] a = array; |
861 |
> |
int md = mode; |
862 |
> |
++nsteals; |
863 |
> |
currentSteal = null; |
864 |
> |
if (md != 0) |
865 |
> |
pollAndExecAll(); |
866 |
> |
else if (a != null) { |
867 |
> |
int s, m = a.length - 1; |
868 |
> |
while ((s = top - 1) - base >= 0) { |
869 |
> |
long i = ((m & s) << ASHIFT) + ABASE; |
870 |
> |
ForkJoinTask<?> t = (ForkJoinTask<?>)U.getObject(a, i); |
871 |
> |
if (t == null) |
872 |
> |
break; |
873 |
> |
if (U.compareAndSwapObject(a, i, t, null)) { |
874 |
> |
top = s; |
875 |
> |
t.doExec(); |
876 |
> |
} |
877 |
> |
} |
878 |
> |
} |
879 |
> |
} |
880 |
|
} |
881 |
|
|
882 |
|
/** |
884 |
|
* or any other cancelled task. Returns (true) on any CAS |
885 |
|
* or consistency check failure so caller can retry. |
886 |
|
* |
887 |
< |
* @return false if no progress can be made, else true; |
887 |
> |
* @return false if no progress can be made, else true |
888 |
|
*/ |
889 |
|
final boolean tryRemoveAndExec(ForkJoinTask<?> task) { |
890 |
< |
boolean stat = true, removed = false, empty = true; |
890 |
> |
boolean stat; |
891 |
|
ForkJoinTask<?>[] a; int m, s, b, n; |
892 |
< |
if ((a = array) != null && (m = a.length - 1) >= 0 && |
892 |
> |
if (task != null && (a = array) != null && (m = a.length - 1) >= 0 && |
893 |
|
(n = (s = top) - (b = base)) > 0) { |
894 |
+ |
boolean removed = false, empty = true; |
895 |
+ |
stat = true; |
896 |
|
for (ForkJoinTask<?> t;;) { // traverse from s to b |
897 |
< |
int j = ((--s & m) << ASHIFT) + ABASE; |
898 |
< |
t = (ForkJoinTask<?>)U.getObjectVolatile(a, j); |
897 |
> |
long j = ((--s & m) << ASHIFT) + ABASE; |
898 |
> |
t = (ForkJoinTask<?>)U.getObject(a, j); |
899 |
|
if (t == null) // inconsistent length |
900 |
|
break; |
901 |
|
else if (t == task) { |
923 |
|
break; |
924 |
|
} |
925 |
|
} |
926 |
+ |
if (removed) |
927 |
+ |
task.doExec(); |
928 |
|
} |
929 |
< |
if (removed) |
930 |
< |
task.doExec(); |
929 |
> |
else |
930 |
> |
stat = false; |
931 |
|
return stat; |
932 |
|
} |
933 |
|
|
934 |
|
/** |
935 |
< |
* Polls for and executes the given task or any other task in |
936 |
< |
* its CountedCompleter computation |
935 |
> |
* Tries to poll for and execute the given task or any other |
936 |
> |
* task in its CountedCompleter computation. |
937 |
|
*/ |
938 |
< |
final boolean pollAndExecCC(ForkJoinTask<?> root) { |
939 |
< |
ForkJoinTask<?>[] a; int b; Object o; |
940 |
< |
outer: while ((b = base) - top < 0 && (a = array) != null) { |
938 |
> |
final boolean pollAndExecCC(CountedCompleter<?> root) { |
939 |
> |
ForkJoinTask<?>[] a; int b; Object o; CountedCompleter<?> t, r; |
940 |
> |
if ((b = base) - top < 0 && (a = array) != null) { |
941 |
|
long j = (((a.length - 1) & b) << ASHIFT) + ABASE; |
942 |
< |
if ((o = U.getObject(a, j)) == null || |
943 |
< |
!(o instanceof CountedCompleter)) |
944 |
< |
break; |
945 |
< |
for (CountedCompleter<?> t = (CountedCompleter<?>)o, r = t;;) { |
946 |
< |
if (r == root) { |
947 |
< |
if (base == b && |
948 |
< |
U.compareAndSwapObject(a, j, t, null)) { |
949 |
< |
base = b + 1; |
950 |
< |
t.doExec(); |
942 |
> |
if ((o = U.getObjectVolatile(a, j)) == null) |
943 |
> |
return true; // retry |
944 |
> |
if (o instanceof CountedCompleter) { |
945 |
> |
for (t = (CountedCompleter<?>)o, r = t;;) { |
946 |
> |
if (r == root) { |
947 |
> |
if (base == b && |
948 |
> |
U.compareAndSwapObject(a, j, t, null)) { |
949 |
> |
U.putOrderedInt(this, QBASE, b + 1); |
950 |
> |
t.doExec(); |
951 |
> |
} |
952 |
|
return true; |
953 |
|
} |
954 |
< |
else |
955 |
< |
break; // restart |
954 |
> |
else if ((r = r.completer) == null) |
955 |
> |
break; // not part of root computation |
956 |
|
} |
975 |
– |
if ((r = r.completer) == null) |
976 |
– |
break outer; // not part of root computation |
957 |
|
} |
958 |
|
} |
959 |
|
return false; |
960 |
|
} |
961 |
|
|
962 |
|
/** |
963 |
< |
* Executes a top-level task and any local tasks remaining |
964 |
< |
* after execution. |
963 |
> |
* Tries to pop and execute the given task or any other task |
964 |
> |
* in its CountedCompleter computation. |
965 |
|
*/ |
966 |
< |
final void runTask(ForkJoinTask<?> t) { |
967 |
< |
if (t != null) { |
968 |
< |
(currentSteal = t).doExec(); |
969 |
< |
currentSteal = null; |
970 |
< |
++nsteals; |
971 |
< |
if (base - top < 0) { // process remaining local tasks |
972 |
< |
if (mode == 0) |
973 |
< |
popAndExecAll(); |
974 |
< |
else |
975 |
< |
pollAndExecAll(); |
966 |
> |
final boolean externalPopAndExecCC(CountedCompleter<?> root) { |
967 |
> |
ForkJoinTask<?>[] a; int s; Object o; CountedCompleter<?> t, r; |
968 |
> |
if (base - (s = top) < 0 && (a = array) != null) { |
969 |
> |
long j = (((a.length - 1) & (s - 1)) << ASHIFT) + ABASE; |
970 |
> |
if ((o = U.getObject(a, j)) instanceof CountedCompleter) { |
971 |
> |
for (t = (CountedCompleter<?>)o, r = t;;) { |
972 |
> |
if (r == root) { |
973 |
> |
if (U.compareAndSwapInt(this, QLOCK, 0, 1)) { |
974 |
> |
if (top == s && array == a && |
975 |
> |
U.compareAndSwapObject(a, j, t, null)) { |
976 |
> |
top = s - 1; |
977 |
> |
qlock = 0; |
978 |
> |
t.doExec(); |
979 |
> |
} |
980 |
> |
else |
981 |
> |
qlock = 0; |
982 |
> |
} |
983 |
> |
return true; |
984 |
> |
} |
985 |
> |
else if ((r = r.completer) == null) |
986 |
> |
break; |
987 |
> |
} |
988 |
|
} |
989 |
|
} |
990 |
+ |
return false; |
991 |
|
} |
992 |
|
|
993 |
|
/** |
994 |
< |
* Executes a non-top-level (stolen) task. |
994 |
> |
* Internal version |
995 |
|
*/ |
996 |
< |
final void runSubtask(ForkJoinTask<?> t) { |
997 |
< |
if (t != null) { |
998 |
< |
ForkJoinTask<?> ps = currentSteal; |
999 |
< |
(currentSteal = t).doExec(); |
1000 |
< |
currentSteal = ps; |
996 |
> |
final boolean internalPopAndExecCC(CountedCompleter<?> root) { |
997 |
> |
ForkJoinTask<?>[] a; int s; Object o; CountedCompleter<?> t, r; |
998 |
> |
if (base - (s = top) < 0 && (a = array) != null) { |
999 |
> |
long j = (((a.length - 1) & (s - 1)) << ASHIFT) + ABASE; |
1000 |
> |
if ((o = U.getObject(a, j)) instanceof CountedCompleter) { |
1001 |
> |
for (t = (CountedCompleter<?>)o, r = t;;) { |
1002 |
> |
if (r == root) { |
1003 |
> |
if (U.compareAndSwapObject(a, j, t, null)) { |
1004 |
> |
top = s - 1; |
1005 |
> |
t.doExec(); |
1006 |
> |
} |
1007 |
> |
return true; |
1008 |
> |
} |
1009 |
> |
else if ((r = r.completer) == null) |
1010 |
> |
break; |
1011 |
> |
} |
1012 |
> |
} |
1013 |
|
} |
1014 |
+ |
return false; |
1015 |
|
} |
1016 |
|
|
1017 |
|
/** |
1028 |
|
|
1029 |
|
// Unsafe mechanics |
1030 |
|
private static final sun.misc.Unsafe U; |
1031 |
+ |
private static final long QBASE; |
1032 |
|
private static final long QLOCK; |
1033 |
|
private static final int ABASE; |
1034 |
|
private static final int ASHIFT; |
1037 |
|
U = getUnsafe(); |
1038 |
|
Class<?> k = WorkQueue.class; |
1039 |
|
Class<?> ak = ForkJoinTask[].class; |
1040 |
+ |
QBASE = U.objectFieldOffset |
1041 |
+ |
(k.getDeclaredField("base")); |
1042 |
|
QLOCK = U.objectFieldOffset |
1043 |
|
(k.getDeclaredField("qlock")); |
1044 |
|
ABASE = U.arrayBaseOffset(ak); |
1055 |
|
// static fields (initialized in static initializer below) |
1056 |
|
|
1057 |
|
/** |
1049 |
– |
* Creates a new ForkJoinWorkerThread. This factory is used unless |
1050 |
– |
* overridden in ForkJoinPool constructors. |
1051 |
– |
*/ |
1052 |
– |
public static final ForkJoinWorkerThreadFactory |
1053 |
– |
defaultForkJoinWorkerThreadFactory; |
1054 |
– |
|
1055 |
– |
/** |
1058 |
|
* Per-thread submission bookkeeping. Shared across all pools |
1059 |
|
* to reduce ThreadLocal pollution and because random motion |
1060 |
|
* to avoid contention in one pool is likely to hold for others. |
1064 |
|
static final ThreadLocal<Submitter> submitters; |
1065 |
|
|
1066 |
|
/** |
1067 |
+ |
* Creates a new ForkJoinWorkerThread. This factory is used unless |
1068 |
+ |
* overridden in ForkJoinPool constructors. |
1069 |
+ |
*/ |
1070 |
+ |
public static final ForkJoinWorkerThreadFactory |
1071 |
+ |
defaultForkJoinWorkerThreadFactory; |
1072 |
+ |
|
1073 |
+ |
/** |
1074 |
|
* Permission required for callers of methods that may start or |
1075 |
|
* kill threads. |
1076 |
|
*/ |
1085 |
|
static final ForkJoinPool common; |
1086 |
|
|
1087 |
|
/** |
1088 |
< |
* Common pool parallelism. Must equal common.parallelism. |
1088 |
> |
* Common pool parallelism. To allow simpler use and management |
1089 |
> |
* when common pool threads are disabled, we allow the underlying |
1090 |
> |
* common.parallelism field to be zero, but in that case still report |
1091 |
> |
* parallelism as 1 to reflect resulting caller-runs mechanics. |
1092 |
|
*/ |
1093 |
|
static final int commonParallelism; |
1094 |
|
|
1143 |
|
*/ |
1144 |
|
private static final int SEED_INCREMENT = 0x61c88647; |
1145 |
|
|
1146 |
< |
/** |
1146 |
> |
/* |
1147 |
|
* Bits and masks for control variables |
1148 |
|
* |
1149 |
|
* Field ctl is a long packed with: |
1227 |
|
static final int FIFO_QUEUE = 1; |
1228 |
|
static final int SHARED_QUEUE = -1; |
1229 |
|
|
1218 |
– |
// bounds for #steps in scan loop -- must be power 2 minus 1 |
1219 |
– |
private static final int MIN_SCAN = 0x1ff; // cover estimation slop |
1220 |
– |
private static final int MAX_SCAN = 0x1ffff; // 4 * max workers |
1221 |
– |
|
1222 |
– |
// Instance fields |
1223 |
– |
|
1224 |
– |
/* |
1225 |
– |
* Field layout of this class tends to matter more than one would |
1226 |
– |
* like. Runtime layout order is only loosely related to |
1227 |
– |
* declaration order and may differ across JVMs, but the following |
1228 |
– |
* empirically works OK on current JVMs. |
1229 |
– |
*/ |
1230 |
– |
|
1230 |
|
// Heuristic padding to ameliorate unfortunate memory placements |
1231 |
|
volatile long pad00, pad01, pad02, pad03, pad04, pad05, pad06; |
1232 |
|
|
1233 |
+ |
// Instance fields |
1234 |
|
volatile long stealCount; // collects worker counts |
1235 |
|
volatile long ctl; // main pool control |
1236 |
|
volatile int plock; // shutdown status and seqLock |
1237 |
|
volatile int indexSeed; // worker/submitter index seed |
1238 |
< |
final int config; // mode and parallelism level |
1238 |
> |
final short parallelism; // parallelism level |
1239 |
> |
final short mode; // LIFO/FIFO |
1240 |
|
WorkQueue[] workQueues; // main registry |
1241 |
|
final ForkJoinWorkerThreadFactory factory; |
1242 |
< |
final Thread.UncaughtExceptionHandler ueh; // per-worker UEH |
1242 |
> |
final UncaughtExceptionHandler ueh; // per-worker UEH |
1243 |
|
final String workerNamePrefix; // to create worker name string |
1244 |
|
|
1245 |
|
volatile Object pad10, pad11, pad12, pad13, pad14, pad15, pad16, pad17; |
1254 |
|
* a more conservative alternative to a pure spinlock. |
1255 |
|
*/ |
1256 |
|
private int acquirePlock() { |
1257 |
< |
int spins = PL_SPINS, r = 0, ps, nps; |
1257 |
> |
int spins = PL_SPINS, ps, nps; |
1258 |
|
for (;;) { |
1259 |
|
if (((ps = plock) & PL_LOCK) == 0 && |
1260 |
|
U.compareAndSwapInt(this, PLOCK, ps, nps = ps + PL_LOCK)) |
1261 |
|
return nps; |
1261 |
– |
else if (r == 0) { // randomize spins if possible |
1262 |
– |
Thread t = Thread.currentThread(); WorkQueue w; Submitter z; |
1263 |
– |
if ((t instanceof ForkJoinWorkerThread) && |
1264 |
– |
(w = ((ForkJoinWorkerThread)t).workQueue) != null) |
1265 |
– |
r = w.seed; |
1266 |
– |
else if ((z = submitters.get()) != null) |
1267 |
– |
r = z.seed; |
1268 |
– |
else |
1269 |
– |
r = 1; |
1270 |
– |
} |
1262 |
|
else if (spins >= 0) { |
1263 |
< |
r ^= r << 1; r ^= r >>> 3; r ^= r << 10; // xorshift |
1273 |
< |
if (r >= 0) |
1263 |
> |
if (ThreadLocalRandom.current().nextInt() >= 0) |
1264 |
|
--spins; |
1265 |
|
} |
1266 |
|
else if (U.compareAndSwapInt(this, PLOCK, ps, ps | PL_SIGNAL)) { |
1296 |
|
* parallelism level exist. Adjusts counts etc on failure. |
1297 |
|
*/ |
1298 |
|
private void tryAddWorker() { |
1299 |
< |
long c; int u; |
1299 |
> |
long c; int u, e; |
1300 |
|
while ((u = (int)((c = ctl) >>> 32)) < 0 && |
1301 |
< |
(u & SHORT_SIGN) != 0 && (int)c == 0) { |
1302 |
< |
long nc = (long)(((u + UTC_UNIT) & UTC_MASK) | |
1303 |
< |
((u + UAC_UNIT) & UAC_MASK)) << 32; |
1301 |
> |
(u & SHORT_SIGN) != 0 && (e = (int)c) >= 0) { |
1302 |
> |
long nc = ((long)(((u + UTC_UNIT) & UTC_MASK) | |
1303 |
> |
((u + UAC_UNIT) & UAC_MASK)) << 32) | (long)e; |
1304 |
|
if (U.compareAndSwapLong(this, CTL, c, nc)) { |
1305 |
|
ForkJoinWorkerThreadFactory fac; |
1306 |
|
Throwable ex = null; |
1311 |
|
wt.start(); |
1312 |
|
break; |
1313 |
|
} |
1314 |
< |
} catch (Throwable e) { |
1315 |
< |
ex = e; |
1314 |
> |
} catch (Throwable rex) { |
1315 |
> |
ex = rex; |
1316 |
|
} |
1317 |
|
deregisterWorker(wt, ex); |
1318 |
|
break; |
1333 |
|
* @return the worker's queue |
1334 |
|
*/ |
1335 |
|
final WorkQueue registerWorker(ForkJoinWorkerThread wt) { |
1336 |
< |
Thread.UncaughtExceptionHandler handler; WorkQueue[] ws; int s, ps; |
1336 |
> |
UncaughtExceptionHandler handler; WorkQueue[] ws; int s, ps; |
1337 |
|
wt.setDaemon(true); |
1338 |
|
if ((handler = ueh) != null) |
1339 |
|
wt.setUncaughtExceptionHandler(handler); |
1340 |
|
do {} while (!U.compareAndSwapInt(this, INDEXSEED, s = indexSeed, |
1341 |
|
s += SEED_INCREMENT) || |
1342 |
|
s == 0); // skip 0 |
1343 |
< |
WorkQueue w = new WorkQueue(this, wt, config >>> 16, s); |
1343 |
> |
WorkQueue w = new WorkQueue(this, wt, mode, s); |
1344 |
|
if (((ps = plock) & PL_LOCK) != 0 || |
1345 |
|
!U.compareAndSwapInt(this, PLOCK, ps, ps += PL_LOCK)) |
1346 |
|
ps = acquirePlock(); |
1360 |
|
} |
1361 |
|
} |
1362 |
|
} |
1363 |
< |
w.eventCount = w.poolIndex = r; // volatile write orders |
1363 |
> |
w.poolIndex = (short)r; |
1364 |
> |
w.eventCount = r; // volatile write orders |
1365 |
|
ws[r] = w; |
1366 |
|
} |
1367 |
|
} finally { |
1368 |
|
if (!U.compareAndSwapInt(this, PLOCK, ps, nps)) |
1369 |
|
releasePlock(nps); |
1370 |
|
} |
1371 |
< |
wt.setName(workerNamePrefix.concat(Integer.toString(w.poolIndex))); |
1371 |
> |
wt.setName(workerNamePrefix.concat(Integer.toString(w.poolIndex >>> 1))); |
1372 |
|
return w; |
1373 |
|
} |
1374 |
|
|
1378 |
|
* array, and adjusts counts. If pool is shutting down, tries to |
1379 |
|
* complete termination. |
1380 |
|
* |
1381 |
< |
* @param wt the worker thread or null if construction failed |
1381 |
> |
* @param wt the worker thread, or null if construction failed |
1382 |
|
* @param ex the exception causing failure, or null if none |
1383 |
|
*/ |
1384 |
|
final void deregisterWorker(ForkJoinWorkerThread wt, Throwable ex) { |
1385 |
|
WorkQueue w = null; |
1386 |
|
if (wt != null && (w = wt.workQueue) != null) { |
1387 |
< |
int ps; |
1387 |
> |
int ps; long sc; |
1388 |
|
w.qlock = -1; // ensure set |
1398 |
– |
long ns = w.nsteals, sc; // collect steal count |
1389 |
|
do {} while (!U.compareAndSwapLong(this, STEALCOUNT, |
1390 |
< |
sc = stealCount, sc + ns)); |
1390 |
> |
sc = stealCount, |
1391 |
> |
sc + w.nsteals)); |
1392 |
|
if (((ps = plock) & PL_LOCK) != 0 || |
1393 |
|
!U.compareAndSwapInt(this, PLOCK, ps, ps += PL_LOCK)) |
1394 |
|
ps = acquirePlock(); |
1446 |
|
// Submissions |
1447 |
|
|
1448 |
|
/** |
1449 |
+ |
* Per-thread records for threads that submit to pools. Currently |
1450 |
+ |
* holds only pseudo-random seed / index that is used to choose |
1451 |
+ |
* submission queues in method externalPush. In the future, this may |
1452 |
+ |
* also incorporate a means to implement different task rejection |
1453 |
+ |
* and resubmission policies. |
1454 |
+ |
* |
1455 |
+ |
* Seeds for submitters and workers/workQueues work in basically |
1456 |
+ |
* the same way but are initialized and updated using slightly |
1457 |
+ |
* different mechanics. Both are initialized using the same |
1458 |
+ |
* approach as in class ThreadLocal, where successive values are |
1459 |
+ |
* unlikely to collide with previous values. Seeds are then |
1460 |
+ |
* randomly modified upon collisions using xorshifts, which |
1461 |
+ |
* requires a non-zero seed. |
1462 |
+ |
*/ |
1463 |
+ |
static final class Submitter { |
1464 |
+ |
int seed; |
1465 |
+ |
Submitter(int s) { seed = s; } |
1466 |
+ |
} |
1467 |
+ |
|
1468 |
+ |
/** |
1469 |
|
* Unless shutting down, adds the given task to a submission queue |
1470 |
|
* at submitter's current queue index (modulo submission |
1471 |
|
* range). Only the most common path is directly handled in this |
1474 |
|
* @param task the task. Caller must ensure non-null. |
1475 |
|
*/ |
1476 |
|
final void externalPush(ForkJoinTask<?> task) { |
1477 |
< |
WorkQueue[] ws; WorkQueue q; Submitter z; int m; ForkJoinTask<?>[] a; |
1478 |
< |
if ((z = submitters.get()) != null && plock > 0 && |
1479 |
< |
(ws = workQueues) != null && (m = (ws.length - 1)) >= 0 && |
1480 |
< |
(q = ws[m & z.seed & SQMASK]) != null && |
1477 |
> |
Submitter z = submitters.get(); |
1478 |
> |
WorkQueue q; int r, m, s, n, am; ForkJoinTask<?>[] a; |
1479 |
> |
int ps = plock; |
1480 |
> |
WorkQueue[] ws = workQueues; |
1481 |
> |
if (z != null && ps > 0 && ws != null && (m = (ws.length - 1)) >= 0 && |
1482 |
> |
(q = ws[m & (r = z.seed) & SQMASK]) != null && r != 0 && |
1483 |
|
U.compareAndSwapInt(q, QLOCK, 0, 1)) { // lock |
1484 |
< |
int b = q.base, s = q.top, n, an; |
1485 |
< |
if ((a = q.array) != null && (an = a.length) > (n = s + 1 - b)) { |
1486 |
< |
int j = (((an - 1) & s) << ASHIFT) + ABASE; |
1484 |
> |
if ((a = q.array) != null && |
1485 |
> |
(am = a.length - 1) > (n = (s = q.top) - q.base)) { |
1486 |
> |
int j = ((am & s) << ASHIFT) + ABASE; |
1487 |
|
U.putOrderedObject(a, j, task); |
1488 |
|
q.top = s + 1; // push on to deque |
1489 |
|
q.qlock = 0; |
1490 |
< |
if (n <= 2) |
1491 |
< |
signalWork(q); |
1490 |
> |
if (n <= 1) |
1491 |
> |
signalWork(ws, q); |
1492 |
|
return; |
1493 |
|
} |
1494 |
|
q.qlock = 0; |
1526 |
|
r = z.seed; |
1527 |
|
r ^= r << 13; // same xorshift as WorkQueues |
1528 |
|
r ^= r >>> 17; |
1529 |
< |
z.seed = r ^ (r << 5); |
1529 |
> |
z.seed = r ^= (r << 5); |
1530 |
|
} |
1531 |
< |
else if ((ps = plock) < 0) |
1531 |
> |
if ((ps = plock) < 0) |
1532 |
|
throw new RejectedExecutionException(); |
1533 |
|
else if (ps == 0 || (ws = workQueues) == null || |
1534 |
|
(m = ws.length - 1) < 0) { // initialize workQueues |
1535 |
< |
int p = config & SMASK; // find power of two table size |
1535 |
> |
int p = parallelism; // find power of two table size |
1536 |
|
int n = (p > 1) ? p - 1 : 1; // ensure at least 2 slots |
1537 |
|
n |= n >>> 1; n |= n >>> 2; n |= n >>> 4; |
1538 |
|
n |= n >>> 8; n |= n >>> 16; n = (n + 1) << 1; |
1564 |
|
q.qlock = 0; // unlock |
1565 |
|
} |
1566 |
|
if (submitted) { |
1567 |
< |
signalWork(q); |
1567 |
> |
signalWork(ws, q); |
1568 |
|
return; |
1569 |
|
} |
1570 |
|
} |
1572 |
|
} |
1573 |
|
else if (((ps = plock) & PL_LOCK) == 0) { // create new queue |
1574 |
|
q = new WorkQueue(this, null, SHARED_QUEUE, r); |
1575 |
+ |
q.poolIndex = (short)k; |
1576 |
|
if (((ps = plock) & PL_LOCK) != 0 || |
1577 |
|
!U.compareAndSwapInt(this, PLOCK, ps, ps += PL_LOCK)) |
1578 |
|
ps = acquirePlock(); |
1583 |
|
releasePlock(nps); |
1584 |
|
} |
1585 |
|
else |
1586 |
< |
r = 0; // try elsewhere while lock held |
1586 |
> |
r = 0; |
1587 |
|
} |
1588 |
|
} |
1589 |
|
|
1594 |
|
*/ |
1595 |
|
final void incrementActiveCount() { |
1596 |
|
long c; |
1597 |
< |
do {} while (!U.compareAndSwapLong(this, CTL, c = ctl, c + AC_UNIT)); |
1597 |
> |
do {} while (!U.compareAndSwapLong |
1598 |
> |
(this, CTL, c = ctl, ((c & ~AC_MASK) | |
1599 |
> |
((c & AC_MASK) + AC_UNIT)))); |
1600 |
|
} |
1601 |
|
|
1602 |
|
/** |
1603 |
|
* Tries to create or activate a worker if too few are active. |
1604 |
|
* |
1605 |
< |
* @param q the (non-null) queue holding tasks to be signalled |
1605 |
> |
* @param ws the worker array to use to find signallees |
1606 |
> |
* @param q if non-null, the queue holding tasks to be processed |
1607 |
|
*/ |
1608 |
< |
final void signalWork(WorkQueue q) { |
1609 |
< |
int hint = q.poolIndex; |
1610 |
< |
long c; int e, u, i, n; WorkQueue[] ws; WorkQueue w; Thread p; |
1611 |
< |
while ((u = (int)((c = ctl) >>> 32)) < 0) { |
1612 |
< |
if ((e = (int)c) > 0) { |
1613 |
< |
if ((ws = workQueues) != null && ws.length > (i = e & SMASK) && |
1597 |
< |
(w = ws[i]) != null && w.eventCount == (e | INT_SIGN)) { |
1598 |
< |
long nc = (((long)(w.nextWait & E_MASK)) | |
1599 |
< |
((long)(u + UAC_UNIT) << 32)); |
1600 |
< |
if (U.compareAndSwapLong(this, CTL, c, nc)) { |
1601 |
< |
w.hint = hint; |
1602 |
< |
w.eventCount = (e + E_SEQ) & E_MASK; |
1603 |
< |
if ((p = w.parker) != null) |
1604 |
< |
U.unpark(p); |
1605 |
< |
break; |
1606 |
< |
} |
1607 |
< |
if (q.top - q.base <= 0) |
1608 |
< |
break; |
1609 |
< |
} |
1610 |
< |
else |
1611 |
< |
break; |
1612 |
< |
} |
1613 |
< |
else { |
1608 |
> |
final void signalWork(WorkQueue[] ws, WorkQueue q) { |
1609 |
> |
for (;;) { |
1610 |
> |
long c; int e, u, i; WorkQueue w; Thread p; |
1611 |
> |
if ((u = (int)((c = ctl) >>> 32)) >= 0) |
1612 |
> |
break; |
1613 |
> |
if ((e = (int)c) <= 0) { |
1614 |
|
if ((short)u < 0) |
1615 |
|
tryAddWorker(); |
1616 |
|
break; |
1617 |
|
} |
1618 |
+ |
if (ws == null || ws.length <= (i = e & SMASK) || |
1619 |
+ |
(w = ws[i]) == null) |
1620 |
+ |
break; |
1621 |
+ |
long nc = (((long)(w.nextWait & E_MASK)) | |
1622 |
+ |
((long)(u + UAC_UNIT)) << 32); |
1623 |
+ |
int ne = (e + E_SEQ) & E_MASK; |
1624 |
+ |
if (w.eventCount == (e | INT_SIGN) && |
1625 |
+ |
U.compareAndSwapLong(this, CTL, c, nc)) { |
1626 |
+ |
w.eventCount = ne; |
1627 |
+ |
if ((p = w.parker) != null) |
1628 |
+ |
U.unpark(p); |
1629 |
+ |
break; |
1630 |
+ |
} |
1631 |
+ |
if (q != null && q.base >= q.top) |
1632 |
+ |
break; |
1633 |
|
} |
1634 |
|
} |
1635 |
|
|
1640 |
|
*/ |
1641 |
|
final void runWorker(WorkQueue w) { |
1642 |
|
w.growArray(); // allocate queue |
1643 |
< |
do { w.runTask(scan(w)); } while (w.qlock >= 0); |
1643 |
> |
for (int r = w.hint; scan(w, r) == 0; ) { |
1644 |
> |
r ^= r << 13; r ^= r >>> 17; r ^= r << 5; // xorshift |
1645 |
> |
} |
1646 |
|
} |
1647 |
|
|
1648 |
|
/** |
1649 |
< |
* Scans for and, if found, returns one task, else possibly |
1649 |
> |
* Scans for and, if found, runs one task, else possibly |
1650 |
|
* inactivates the worker. This method operates on single reads of |
1651 |
|
* volatile state and is designed to be re-invoked continuously, |
1652 |
|
* in part because it returns upon detecting inconsistencies, |
1653 |
|
* contention, or state changes that indicate possible success on |
1654 |
|
* re-invocation. |
1655 |
|
* |
1656 |
< |
* The scan searches for tasks across queues (starting at a random |
1657 |
< |
* index, and relying on registerWorker to irregularly scatter |
1658 |
< |
* them within array to avoid bias), checking each at least twice. |
1659 |
< |
* The scan terminates upon either finding a non-empty queue, or |
1660 |
< |
* completing the sweep. If the worker is not inactivated, it |
1661 |
< |
* takes and returns a task from this queue. Otherwise, if not |
1662 |
< |
* activated, it signals workers (that may include itself) and |
1663 |
< |
* returns so caller can retry. Also returns for true if the |
1664 |
< |
* worker array may have changed during an empty scan. On failure |
1648 |
< |
* to find a task, we take one of the following actions, after |
1649 |
< |
* which the caller will retry calling this method unless |
1650 |
< |
* terminated. |
1651 |
< |
* |
1652 |
< |
* * If pool is terminating, terminate the worker. |
1653 |
< |
* |
1654 |
< |
* * If not already enqueued, try to inactivate and enqueue the |
1655 |
< |
* worker on wait queue. Or, if inactivating has caused the pool |
1656 |
< |
* to be quiescent, relay to idleAwaitWork to possibly shrink |
1657 |
< |
* pool. |
1658 |
< |
* |
1659 |
< |
* * If already enqueued and none of the above apply, possibly |
1660 |
< |
* park awaiting signal, else lingering to help scan and signal. |
1661 |
< |
* |
1662 |
< |
* * If a non-empty queue discovered or left as a hint, |
1663 |
< |
* help wake up other workers before return. |
1656 |
> |
* The scan searches for tasks across queues starting at a random |
1657 |
> |
* index, checking each at least twice. The scan terminates upon |
1658 |
> |
* either finding a non-empty queue, or completing the sweep. If |
1659 |
> |
* the worker is not inactivated, it takes and runs a task from |
1660 |
> |
* this queue. Otherwise, if not activated, it tries to activate |
1661 |
> |
* itself or some other worker by signalling. On failure to find a |
1662 |
> |
* task, returns (for retry) if pool state may have changed during |
1663 |
> |
* an empty scan, or tries to inactivate if active, else possibly |
1664 |
> |
* blocks or terminates via method awaitWork. |
1665 |
|
* |
1666 |
|
* @param w the worker (via its WorkQueue) |
1667 |
< |
* @return a task or null if none found |
1667 |
> |
* @param r a random seed |
1668 |
> |
* @return worker qlock status if would have waited, else 0 |
1669 |
|
*/ |
1670 |
< |
private final ForkJoinTask<?> scan(WorkQueue w) { |
1670 |
> |
private final int scan(WorkQueue w, int r) { |
1671 |
|
WorkQueue[] ws; int m; |
1672 |
< |
int ps = plock; // read plock before ws |
1673 |
< |
if (w != null && (ws = workQueues) != null && (m = ws.length - 1) >= 0) { |
1674 |
< |
int ec = w.eventCount; // ec is negative if inactive |
1675 |
< |
int r = w.seed; r ^= r << 13; r ^= r >>> 17; w.seed = r ^= r << 5; |
1676 |
< |
w.hint = -1; // update seed and clear hint |
1677 |
< |
int j = ((m + m + 1) | MIN_SCAN) & MAX_SCAN; |
1678 |
< |
do { |
1679 |
< |
WorkQueue q; ForkJoinTask<?>[] a; int b; |
1680 |
< |
if ((q = ws[(r + j) & m]) != null && (b = q.base) - q.top < 0 && |
1681 |
< |
(a = q.array) != null) { // probably nonempty |
1682 |
< |
int i = (((a.length - 1) & b) << ASHIFT) + ABASE; |
1683 |
< |
ForkJoinTask<?> t = (ForkJoinTask<?>) |
1684 |
< |
U.getObjectVolatile(a, i); |
1685 |
< |
if (q.base == b && ec >= 0 && t != null && |
1686 |
< |
U.compareAndSwapObject(a, i, t, null)) { |
1687 |
< |
if ((q.base = b + 1) - q.top < 0) |
1688 |
< |
signalWork(q); |
1689 |
< |
return t; // taken |
1690 |
< |
} |
1691 |
< |
else if ((ec < 0 || j < m) && (int)(ctl >> AC_SHIFT) <= 0) { |
1692 |
< |
w.hint = (r + j) & m; // help signal below |
1693 |
< |
break; // cannot take |
1694 |
< |
} |
1695 |
< |
} |
1696 |
< |
} while (--j >= 0); |
1697 |
< |
|
1698 |
< |
int h, e, ns; long c, sc; WorkQueue q; |
1697 |
< |
if ((ns = w.nsteals) != 0) { |
1698 |
< |
if (U.compareAndSwapLong(this, STEALCOUNT, |
1699 |
< |
sc = stealCount, sc + ns)) |
1700 |
< |
w.nsteals = 0; // collect steals and rescan |
1701 |
< |
} |
1702 |
< |
else if (plock != ps) // consistency check |
1703 |
< |
; // skip |
1704 |
< |
else if ((e = (int)(c = ctl)) < 0) |
1705 |
< |
w.qlock = -1; // pool is terminating |
1706 |
< |
else { |
1707 |
< |
if ((h = w.hint) < 0) { |
1708 |
< |
if (ec >= 0) { // try to enqueue/inactivate |
1709 |
< |
long nc = (((long)ec | |
1710 |
< |
((c - AC_UNIT) & (AC_MASK|TC_MASK)))); |
1711 |
< |
w.nextWait = e; // link and mark inactive |
1672 |
> |
long c = ctl; // for consistency check |
1673 |
> |
if ((ws = workQueues) != null && (m = ws.length - 1) >= 0 && w != null) { |
1674 |
> |
for (int j = m + m + 1, ec = w.eventCount;;) { |
1675 |
> |
WorkQueue q; int b, e; ForkJoinTask<?>[] a; ForkJoinTask<?> t; |
1676 |
> |
if ((q = ws[(r - j) & m]) != null && |
1677 |
> |
(b = q.base) - q.top < 0 && (a = q.array) != null) { |
1678 |
> |
long i = (((a.length - 1) & b) << ASHIFT) + ABASE; |
1679 |
> |
if ((t = ((ForkJoinTask<?>) |
1680 |
> |
U.getObjectVolatile(a, i))) != null) { |
1681 |
> |
if (ec < 0) |
1682 |
> |
helpRelease(c, ws, w, q, b); |
1683 |
> |
else if (q.base == b && |
1684 |
> |
U.compareAndSwapObject(a, i, t, null)) { |
1685 |
> |
U.putOrderedInt(q, QBASE, b + 1); |
1686 |
> |
if ((b + 1) - q.top < 0) |
1687 |
> |
signalWork(ws, q); |
1688 |
> |
w.runTask(t); |
1689 |
> |
} |
1690 |
> |
} |
1691 |
> |
break; |
1692 |
> |
} |
1693 |
> |
else if (--j < 0) { |
1694 |
> |
if ((ec | (e = (int)c)) < 0) // inactive or terminating |
1695 |
> |
return awaitWork(w, c, ec); |
1696 |
> |
else if (ctl == c) { // try to inactivate and enqueue |
1697 |
> |
long nc = (long)ec | ((c - AC_UNIT) & (AC_MASK|TC_MASK)); |
1698 |
> |
w.nextWait = e; |
1699 |
|
w.eventCount = ec | INT_SIGN; |
1700 |
< |
if (ctl != c || !U.compareAndSwapLong(this, CTL, c, nc)) |
1701 |
< |
w.eventCount = ec; // unmark on CAS failure |
1715 |
< |
else if ((int)(c >> AC_SHIFT) == 1 - (config & SMASK)) |
1716 |
< |
idleAwaitWork(w, nc, c); |
1717 |
< |
} |
1718 |
< |
else if (w.eventCount < 0 && ctl == c) { |
1719 |
< |
Thread wt = Thread.currentThread(); |
1720 |
< |
Thread.interrupted(); // clear status |
1721 |
< |
U.putObject(wt, PARKBLOCKER, this); |
1722 |
< |
w.parker = wt; // emulate LockSupport.park |
1723 |
< |
if (w.eventCount < 0) // recheck |
1724 |
< |
U.park(false, 0L); // block |
1725 |
< |
w.parker = null; |
1726 |
< |
U.putObject(wt, PARKBLOCKER, null); |
1727 |
< |
} |
1728 |
< |
} |
1729 |
< |
if ((h >= 0 || (h = w.hint) >= 0) && |
1730 |
< |
(ws = workQueues) != null && h < ws.length && |
1731 |
< |
(q = ws[h]) != null) { // signal others before retry |
1732 |
< |
WorkQueue v; Thread p; int u, i, s; |
1733 |
< |
for (int n = (config & SMASK) - 1;;) { |
1734 |
< |
int idleCount = (w.eventCount < 0) ? 0 : -1; |
1735 |
< |
if (((s = idleCount - q.base + q.top) <= n && |
1736 |
< |
(n = s) <= 0) || |
1737 |
< |
(u = (int)((c = ctl) >>> 32)) >= 0 || |
1738 |
< |
(e = (int)c) <= 0 || m < (i = e & SMASK) || |
1739 |
< |
(v = ws[i]) == null) |
1740 |
< |
break; |
1741 |
< |
long nc = (((long)(v.nextWait & E_MASK)) | |
1742 |
< |
((long)(u + UAC_UNIT) << 32)); |
1743 |
< |
if (v.eventCount != (e | INT_SIGN) || |
1744 |
< |
!U.compareAndSwapLong(this, CTL, c, nc)) |
1745 |
< |
break; |
1746 |
< |
v.hint = h; |
1747 |
< |
v.eventCount = (e + E_SEQ) & E_MASK; |
1748 |
< |
if ((p = v.parker) != null) |
1749 |
< |
U.unpark(p); |
1750 |
< |
if (--n <= 0) |
1751 |
< |
break; |
1700 |
> |
if (!U.compareAndSwapLong(this, CTL, c, nc)) |
1701 |
> |
w.eventCount = ec; // back out |
1702 |
|
} |
1703 |
+ |
break; |
1704 |
|
} |
1705 |
|
} |
1706 |
|
} |
1707 |
< |
return null; |
1707 |
> |
return 0; |
1708 |
|
} |
1709 |
|
|
1710 |
|
/** |
1711 |
< |
* If inactivating worker w has caused the pool to become |
1712 |
< |
* quiescent, checks for pool termination, and, so long as this is |
1713 |
< |
* not the only worker, waits for event for up to a given |
1714 |
< |
* duration. On timeout, if ctl has not changed, terminates the |
1715 |
< |
* worker, which will in turn wake up another worker to possibly |
1716 |
< |
* repeat this process. |
1711 |
> |
* A continuation of scan(), possibly blocking or terminating |
1712 |
> |
* worker w. Returns without blocking if pool state has apparently |
1713 |
> |
* changed since last invocation. Also, if inactivating w has |
1714 |
> |
* caused the pool to become quiescent, checks for pool |
1715 |
> |
* termination, and, so long as this is not the only worker, waits |
1716 |
> |
* for event for up to a given duration. On timeout, if ctl has |
1717 |
> |
* not changed, terminates the worker, which will in turn wake up |
1718 |
> |
* another worker to possibly repeat this process. |
1719 |
|
* |
1720 |
|
* @param w the calling worker |
1721 |
< |
* @param currentCtl the ctl value triggering possible quiescence |
1722 |
< |
* @param prevCtl the ctl value to restore if thread is terminated |
1721 |
> |
* @param c the ctl value on entry to scan |
1722 |
> |
* @param ec the worker's eventCount on entry to scan |
1723 |
|
*/ |
1724 |
< |
private void idleAwaitWork(WorkQueue w, long currentCtl, long prevCtl) { |
1725 |
< |
if (w != null && w.eventCount < 0 && |
1726 |
< |
!tryTerminate(false, false) && (int)prevCtl != 0 && |
1727 |
< |
ctl == currentCtl) { |
1728 |
< |
int dc = -(short)(currentCtl >>> TC_SHIFT); |
1729 |
< |
long parkTime = dc < 0 ? FAST_IDLE_TIMEOUT: (dc + 1) * IDLE_TIMEOUT; |
1730 |
< |
long deadline = System.nanoTime() + parkTime - TIMEOUT_SLOP; |
1731 |
< |
Thread wt = Thread.currentThread(); |
1732 |
< |
while (ctl == currentCtl) { |
1733 |
< |
Thread.interrupted(); // timed variant of version in scan() |
1734 |
< |
U.putObject(wt, PARKBLOCKER, this); |
1735 |
< |
w.parker = wt; |
1736 |
< |
if (ctl == currentCtl) |
1737 |
< |
U.park(false, parkTime); |
1738 |
< |
w.parker = null; |
1739 |
< |
U.putObject(wt, PARKBLOCKER, null); |
1740 |
< |
if (ctl != currentCtl) |
1741 |
< |
break; |
1742 |
< |
if (deadline - System.nanoTime() <= 0L && |
1743 |
< |
U.compareAndSwapLong(this, CTL, currentCtl, prevCtl)) { |
1744 |
< |
w.eventCount = (w.eventCount + E_SEQ) | E_MASK; |
1745 |
< |
w.hint = -1; |
1746 |
< |
w.qlock = -1; // shrink |
1747 |
< |
break; |
1724 |
> |
private final int awaitWork(WorkQueue w, long c, int ec) { |
1725 |
> |
int stat, ns; long parkTime, deadline; |
1726 |
> |
if ((stat = w.qlock) >= 0 && w.eventCount == ec && ctl == c && |
1727 |
> |
!Thread.interrupted()) { |
1728 |
> |
int e = (int)c; |
1729 |
> |
int u = (int)(c >>> 32); |
1730 |
> |
int d = (u >> UAC_SHIFT) + parallelism; // active count |
1731 |
> |
|
1732 |
> |
if (e < 0 || (d <= 0 && tryTerminate(false, false))) |
1733 |
> |
stat = w.qlock = -1; // pool is terminating |
1734 |
> |
else if ((ns = w.nsteals) != 0) { // collect steals and retry |
1735 |
> |
long sc; |
1736 |
> |
w.nsteals = 0; |
1737 |
> |
do {} while (!U.compareAndSwapLong(this, STEALCOUNT, |
1738 |
> |
sc = stealCount, sc + ns)); |
1739 |
> |
} |
1740 |
> |
else { |
1741 |
> |
long pc = ((d > 0 || ec != (e | INT_SIGN)) ? 0L : |
1742 |
> |
((long)(w.nextWait & E_MASK)) | // ctl to restore |
1743 |
> |
((long)(u + UAC_UNIT)) << 32); |
1744 |
> |
if (pc != 0L) { // timed wait if last waiter |
1745 |
> |
int dc = -(short)(c >>> TC_SHIFT); |
1746 |
> |
parkTime = (dc < 0 ? FAST_IDLE_TIMEOUT: |
1747 |
> |
(dc + 1) * IDLE_TIMEOUT); |
1748 |
> |
deadline = System.nanoTime() + parkTime - TIMEOUT_SLOP; |
1749 |
> |
} |
1750 |
> |
else |
1751 |
> |
parkTime = deadline = 0L; |
1752 |
> |
if (w.eventCount == ec && ctl == c) { |
1753 |
> |
Thread wt = Thread.currentThread(); |
1754 |
> |
U.putObject(wt, PARKBLOCKER, this); |
1755 |
> |
w.parker = wt; // emulate LockSupport.park |
1756 |
> |
if (w.eventCount == ec && ctl == c) |
1757 |
> |
U.park(false, parkTime); // must recheck before park |
1758 |
> |
w.parker = null; |
1759 |
> |
U.putObject(wt, PARKBLOCKER, null); |
1760 |
> |
if (parkTime != 0L && ctl == c && |
1761 |
> |
deadline - System.nanoTime() <= 0L && |
1762 |
> |
U.compareAndSwapLong(this, CTL, c, pc)) |
1763 |
> |
stat = w.qlock = -1; // shrink pool |
1764 |
|
} |
1765 |
|
} |
1766 |
|
} |
1767 |
+ |
return stat; |
1768 |
|
} |
1769 |
|
|
1770 |
|
/** |
1771 |
< |
* Scans through queues looking for work while joining a task; if |
1772 |
< |
* any present, signals. May return early if more signalling is |
1773 |
< |
* detectably unneeded. |
1774 |
< |
* |
1775 |
< |
* @param task return early if done |
1776 |
< |
* @param origin an index to start scan |
1777 |
< |
*/ |
1778 |
< |
private void helpSignal(ForkJoinTask<?> task, int origin) { |
1779 |
< |
WorkQueue[] ws; WorkQueue w; Thread p; long c; int m, u, e, i, s; |
1780 |
< |
if (task != null && task.status >= 0 && |
1781 |
< |
(u = (int)(ctl >>> 32)) < 0 && (u >> UAC_SHIFT) < 0 && |
1782 |
< |
(ws = workQueues) != null && (m = ws.length - 1) >= 0) { |
1783 |
< |
outer: for (int k = origin, j = m; j >= 0; --j) { |
1784 |
< |
WorkQueue q = ws[k++ & m]; |
1785 |
< |
for (int n = m;;) { // limit to at most m signals |
1786 |
< |
if (task.status < 0) |
1787 |
< |
break outer; |
1788 |
< |
if (q == null || |
1789 |
< |
((s = -q.base + q.top) <= n && (n = s) <= 0)) |
1790 |
< |
break; |
1821 |
< |
if ((u = (int)((c = ctl) >>> 32)) >= 0 || |
1822 |
< |
(e = (int)c) <= 0 || m < (i = e & SMASK) || |
1823 |
< |
(w = ws[i]) == null) |
1824 |
< |
break outer; |
1825 |
< |
long nc = (((long)(w.nextWait & E_MASK)) | |
1826 |
< |
((long)(u + UAC_UNIT) << 32)); |
1827 |
< |
if (w.eventCount != (e | INT_SIGN)) |
1828 |
< |
break outer; |
1829 |
< |
if (U.compareAndSwapLong(this, CTL, c, nc)) { |
1830 |
< |
w.eventCount = (e + E_SEQ) & E_MASK; |
1831 |
< |
if ((p = w.parker) != null) |
1832 |
< |
U.unpark(p); |
1833 |
< |
if (--n <= 0) |
1834 |
< |
break; |
1835 |
< |
} |
1836 |
< |
} |
1771 |
> |
* Possibly releases (signals) a worker. Called only from scan() |
1772 |
> |
* when a worker with apparently inactive status finds a non-empty |
1773 |
> |
* queue. This requires revalidating all of the associated state |
1774 |
> |
* from caller. |
1775 |
> |
*/ |
1776 |
> |
private final void helpRelease(long c, WorkQueue[] ws, WorkQueue w, |
1777 |
> |
WorkQueue q, int b) { |
1778 |
> |
WorkQueue v; int e, i; Thread p; |
1779 |
> |
if (w != null && w.eventCount < 0 && (e = (int)c) > 0 && |
1780 |
> |
ws != null && ws.length > (i = e & SMASK) && |
1781 |
> |
(v = ws[i]) != null && ctl == c) { |
1782 |
> |
long nc = (((long)(v.nextWait & E_MASK)) | |
1783 |
> |
((long)((int)(c >>> 32) + UAC_UNIT)) << 32); |
1784 |
> |
int ne = (e + E_SEQ) & E_MASK; |
1785 |
> |
if (q != null && q.base == b && w.eventCount < 0 && |
1786 |
> |
v.eventCount == (e | INT_SIGN) && |
1787 |
> |
U.compareAndSwapLong(this, CTL, c, nc)) { |
1788 |
> |
v.eventCount = ne; |
1789 |
> |
if ((p = v.parker) != null) |
1790 |
> |
U.unpark(p); |
1791 |
|
} |
1792 |
|
} |
1793 |
|
} |
1812 |
|
*/ |
1813 |
|
private int tryHelpStealer(WorkQueue joiner, ForkJoinTask<?> task) { |
1814 |
|
int stat = 0, steps = 0; // bound to avoid cycles |
1815 |
< |
if (joiner != null && task != null) { // hoist null checks |
1815 |
> |
if (task != null && joiner != null && |
1816 |
> |
joiner.base - joiner.top >= 0) { // hoist checks |
1817 |
|
restart: for (;;) { |
1818 |
|
ForkJoinTask<?> subtask = task; // current target |
1819 |
|
for (WorkQueue j = joiner, v;;) { // v is stealer of subtask |
1840 |
|
} |
1841 |
|
} |
1842 |
|
for (;;) { // help stealer or descend to its stealer |
1843 |
< |
ForkJoinTask[] a; int b; |
1843 |
> |
ForkJoinTask[] a; int b; |
1844 |
|
if (subtask.status < 0) // surround probes with |
1845 |
|
continue restart; // consistency checks |
1846 |
|
if ((b = v.base) - v.top < 0 && (a = v.array) != null) { |
1851 |
|
v.currentSteal != subtask) |
1852 |
|
continue restart; // stale |
1853 |
|
stat = 1; // apparent progress |
1854 |
< |
if (t != null && v.base == b && |
1855 |
< |
U.compareAndSwapObject(a, i, t, null)) { |
1856 |
< |
v.base = b + 1; // help stealer |
1857 |
< |
joiner.runSubtask(t); |
1854 |
> |
if (v.base == b) { |
1855 |
> |
if (t == null) |
1856 |
> |
break restart; |
1857 |
> |
if (U.compareAndSwapObject(a, i, t, null)) { |
1858 |
> |
U.putOrderedInt(v, QBASE, b + 1); |
1859 |
> |
ForkJoinTask<?> ps = joiner.currentSteal; |
1860 |
> |
int jt = joiner.top; |
1861 |
> |
do { |
1862 |
> |
joiner.currentSteal = t; |
1863 |
> |
t.doExec(); // clear local tasks too |
1864 |
> |
} while (task.status >= 0 && |
1865 |
> |
joiner.top != jt && |
1866 |
> |
(t = joiner.pop()) != null); |
1867 |
> |
joiner.currentSteal = ps; |
1868 |
> |
break restart; |
1869 |
> |
} |
1870 |
|
} |
1904 |
– |
else if (v.base == b && ++steps == MAX_HELP) |
1905 |
– |
break restart; // v apparently stalled |
1871 |
|
} |
1872 |
|
else { // empty -- try to descend |
1873 |
|
ForkJoinTask<?> next = v.currentJoin; |
1894 |
|
* and run tasks within the target's computation. |
1895 |
|
* |
1896 |
|
* @param task the task to join |
1932 |
– |
* @param mode if shared, exit upon completing any task |
1933 |
– |
* if all workers are active |
1897 |
|
*/ |
1898 |
< |
private int helpComplete(ForkJoinTask<?> task, int mode) { |
1899 |
< |
WorkQueue[] ws; WorkQueue q; int m, n, s, u; |
1900 |
< |
if (task != null && (ws = workQueues) != null && |
1901 |
< |
(m = ws.length - 1) >= 0) { |
1902 |
< |
for (int j = 1, origin = j;;) { |
1898 |
> |
private int helpComplete(WorkQueue joiner, CountedCompleter<?> task) { |
1899 |
> |
WorkQueue[] ws; int m; |
1900 |
> |
int s = 0; |
1901 |
> |
if ((ws = workQueues) != null && (m = ws.length - 1) >= 0 && |
1902 |
> |
joiner != null && task != null) { |
1903 |
> |
int j = joiner.poolIndex; |
1904 |
> |
int scans = m + m + 1; |
1905 |
> |
long c = 0L; // for stability check |
1906 |
> |
for (int k = scans; ; j += 2) { |
1907 |
> |
WorkQueue q; |
1908 |
|
if ((s = task.status) < 0) |
1909 |
< |
return s; |
1910 |
< |
if ((q = ws[j & m]) != null && q.pollAndExecCC(task)) { |
1911 |
< |
origin = j; |
1912 |
< |
if (mode == SHARED_QUEUE && |
1913 |
< |
((u = (int)(ctl >>> 32)) >= 0 || (u >> UAC_SHIFT) >= 0)) |
1909 |
> |
break; |
1910 |
> |
else if (joiner.internalPopAndExecCC(task)) |
1911 |
> |
k = scans; |
1912 |
> |
else if ((s = task.status) < 0) |
1913 |
> |
break; |
1914 |
> |
else if ((q = ws[j & m]) != null && q.pollAndExecCC(task)) |
1915 |
> |
k = scans; |
1916 |
> |
else if (--k < 0) { |
1917 |
> |
if (c == (c = ctl)) |
1918 |
|
break; |
1919 |
+ |
k = scans; |
1920 |
|
} |
1948 |
– |
else if ((j = (j + 2) & m) == origin) |
1949 |
– |
break; |
1921 |
|
} |
1922 |
|
} |
1923 |
< |
return 0; |
1923 |
> |
return s; |
1924 |
|
} |
1925 |
|
|
1926 |
|
/** |
1929 |
|
* for blocking. Fails on contention or termination. Otherwise, |
1930 |
|
* adds a new thread if no idle workers are available and pool |
1931 |
|
* may become starved. |
1932 |
+ |
* |
1933 |
+ |
* @param c the assumed ctl value |
1934 |
|
*/ |
1935 |
< |
final boolean tryCompensate() { |
1936 |
< |
int pc = config & SMASK, e, i, tc; long c; |
1937 |
< |
WorkQueue[] ws; WorkQueue w; Thread p; |
1938 |
< |
if ((ws = workQueues) != null && (e = (int)(c = ctl)) >= 0) { |
1939 |
< |
if (e != 0 && (i = e & SMASK) < ws.length && |
1940 |
< |
(w = ws[i]) != null && w.eventCount == (e | INT_SIGN)) { |
1935 |
> |
final boolean tryCompensate(long c) { |
1936 |
> |
WorkQueue[] ws = workQueues; |
1937 |
> |
int pc = parallelism, e = (int)c, m, tc; |
1938 |
> |
if (ws != null && (m = ws.length - 1) >= 0 && e >= 0 && ctl == c) { |
1939 |
> |
WorkQueue w = ws[e & m]; |
1940 |
> |
if (e != 0 && w != null) { |
1941 |
> |
Thread p; |
1942 |
|
long nc = ((long)(w.nextWait & E_MASK) | |
1943 |
|
(c & (AC_MASK|TC_MASK))); |
1944 |
< |
if (U.compareAndSwapLong(this, CTL, c, nc)) { |
1945 |
< |
w.eventCount = (e + E_SEQ) & E_MASK; |
1944 |
> |
int ne = (e + E_SEQ) & E_MASK; |
1945 |
> |
if (w.eventCount == (e | INT_SIGN) && |
1946 |
> |
U.compareAndSwapLong(this, CTL, c, nc)) { |
1947 |
> |
w.eventCount = ne; |
1948 |
|
if ((p = w.parker) != null) |
1949 |
|
U.unpark(p); |
1950 |
|
return true; // replace with idle worker |
1987 |
|
*/ |
1988 |
|
final int awaitJoin(WorkQueue joiner, ForkJoinTask<?> task) { |
1989 |
|
int s = 0; |
1990 |
< |
if (joiner != null && task != null && (s = task.status) >= 0) { |
1990 |
> |
if (task != null && (s = task.status) >= 0 && joiner != null) { |
1991 |
|
ForkJoinTask<?> prevJoin = joiner.currentJoin; |
1992 |
|
joiner.currentJoin = task; |
1993 |
< |
do {} while ((s = task.status) >= 0 && !joiner.isEmpty() && |
1994 |
< |
joiner.tryRemoveAndExec(task)); // process local tasks |
1995 |
< |
if (s >= 0 && (s = task.status) >= 0) { |
1996 |
< |
helpSignal(task, joiner.poolIndex); |
1997 |
< |
if ((s = task.status) >= 0 && |
2022 |
< |
(task instanceof CountedCompleter)) |
2023 |
< |
s = helpComplete(task, LIFO_QUEUE); |
2024 |
< |
} |
1993 |
> |
do {} while (joiner.tryRemoveAndExec(task) && // process local tasks |
1994 |
> |
(s = task.status) >= 0); |
1995 |
> |
if (s >= 0 && (task instanceof CountedCompleter)) |
1996 |
> |
s = helpComplete(joiner, (CountedCompleter<?>)task); |
1997 |
> |
long cc = 0; // for stability checks |
1998 |
|
while (s >= 0 && (s = task.status) >= 0) { |
1999 |
< |
if ((!joiner.isEmpty() || // try helping |
2027 |
< |
(s = tryHelpStealer(joiner, task)) == 0) && |
1999 |
> |
if ((s = tryHelpStealer(joiner, task)) == 0 && |
2000 |
|
(s = task.status) >= 0) { |
2001 |
< |
helpSignal(task, joiner.poolIndex); |
2002 |
< |
if ((s = task.status) >= 0 && tryCompensate()) { |
2001 |
> |
if (!tryCompensate(cc)) |
2002 |
> |
cc = ctl; |
2003 |
> |
else { |
2004 |
|
if (task.trySetSignal() && (s = task.status) >= 0) { |
2005 |
|
synchronized (task) { |
2006 |
|
if (task.status >= 0) { |
2013 |
|
task.notifyAll(); |
2014 |
|
} |
2015 |
|
} |
2016 |
< |
long c; // re-activate |
2016 |
> |
long c; // reactivate |
2017 |
|
do {} while (!U.compareAndSwapLong |
2018 |
< |
(this, CTL, c = ctl, c + AC_UNIT)); |
2018 |
> |
(this, CTL, c = ctl, |
2019 |
> |
((c & ~AC_MASK) | |
2020 |
> |
((c & AC_MASK) + AC_UNIT)))); |
2021 |
|
} |
2022 |
|
} |
2023 |
|
} |
2039 |
|
if (joiner != null && task != null && (s = task.status) >= 0) { |
2040 |
|
ForkJoinTask<?> prevJoin = joiner.currentJoin; |
2041 |
|
joiner.currentJoin = task; |
2042 |
< |
do {} while ((s = task.status) >= 0 && !joiner.isEmpty() && |
2043 |
< |
joiner.tryRemoveAndExec(task)); |
2044 |
< |
if (s >= 0 && (s = task.status) >= 0) { |
2045 |
< |
helpSignal(task, joiner.poolIndex); |
2046 |
< |
if ((s = task.status) >= 0 && |
2072 |
< |
(task instanceof CountedCompleter)) |
2073 |
< |
s = helpComplete(task, LIFO_QUEUE); |
2074 |
< |
} |
2075 |
< |
if (s >= 0 && joiner.isEmpty()) { |
2042 |
> |
do {} while (joiner.tryRemoveAndExec(task) && // process local tasks |
2043 |
> |
(s = task.status) >= 0); |
2044 |
> |
if (s >= 0) { |
2045 |
> |
if (task instanceof CountedCompleter) |
2046 |
> |
helpComplete(joiner, (CountedCompleter<?>)task); |
2047 |
|
do {} while (task.status >= 0 && |
2048 |
|
tryHelpStealer(joiner, task) > 0); |
2049 |
|
} |
2055 |
|
* Returns a (probably) non-empty steal queue, if one is found |
2056 |
|
* during a scan, else null. This method must be retried by |
2057 |
|
* caller if, by the time it tries to use the queue, it is empty. |
2087 |
– |
* @param r a (random) seed for scanning |
2058 |
|
*/ |
2059 |
< |
private WorkQueue findNonEmptyStealQueue(int r) { |
2059 |
> |
private WorkQueue findNonEmptyStealQueue() { |
2060 |
> |
int r = ThreadLocalRandom.current().nextInt(); |
2061 |
|
for (;;) { |
2062 |
|
int ps = plock, m; WorkQueue[] ws; WorkQueue q; |
2063 |
|
if ((ws = workQueues) != null && (m = ws.length - 1) >= 0) { |
2064 |
|
for (int j = (m + 1) << 2; j >= 0; --j) { |
2065 |
< |
if ((q = ws[(((r + j) << 1) | 1) & m]) != null && |
2065 |
> |
if ((q = ws[(((r - j) << 1) | 1) & m]) != null && |
2066 |
|
q.base - q.top < 0) |
2067 |
|
return q; |
2068 |
|
} |
2079 |
|
* find tasks either. |
2080 |
|
*/ |
2081 |
|
final void helpQuiescePool(WorkQueue w) { |
2082 |
+ |
ForkJoinTask<?> ps = w.currentSteal; |
2083 |
|
for (boolean active = true;;) { |
2084 |
|
long c; WorkQueue q; ForkJoinTask<?> t; int b; |
2085 |
< |
while ((t = w.nextLocalTask()) != null) { |
2114 |
< |
if (w.base - w.top < 0) |
2115 |
< |
signalWork(w); |
2085 |
> |
while ((t = w.nextLocalTask()) != null) |
2086 |
|
t.doExec(); |
2087 |
< |
} |
2118 |
< |
if ((q = findNonEmptyStealQueue(w.nextSeed())) != null) { |
2087 |
> |
if ((q = findNonEmptyStealQueue()) != null) { |
2088 |
|
if (!active) { // re-establish active count |
2089 |
|
active = true; |
2090 |
|
do {} while (!U.compareAndSwapLong |
2091 |
< |
(this, CTL, c = ctl, c + AC_UNIT)); |
2091 |
> |
(this, CTL, c = ctl, |
2092 |
> |
((c & ~AC_MASK) | |
2093 |
> |
((c & AC_MASK) + AC_UNIT)))); |
2094 |
|
} |
2095 |
|
if ((b = q.base) - q.top < 0 && (t = q.pollAt(b)) != null) { |
2096 |
< |
if (q.base - q.top < 0) |
2097 |
< |
signalWork(q); |
2127 |
< |
w.runSubtask(t); |
2096 |
> |
(w.currentSteal = t).doExec(); |
2097 |
> |
w.currentSteal = ps; |
2098 |
|
} |
2099 |
|
} |
2100 |
|
else if (active) { // decrement active count without queuing |
2101 |
< |
long nc = (c = ctl) - AC_UNIT; |
2102 |
< |
if ((int)(nc >> AC_SHIFT) + (config & SMASK) == 0) |
2103 |
< |
return; // bypass decrement-then-increment |
2101 |
> |
long nc = ((c = ctl) & ~AC_MASK) | ((c & AC_MASK) - AC_UNIT); |
2102 |
> |
if ((int)(nc >> AC_SHIFT) + parallelism == 0) |
2103 |
> |
break; // bypass decrement-then-increment |
2104 |
|
if (U.compareAndSwapLong(this, CTL, c, nc)) |
2105 |
|
active = false; |
2106 |
|
} |
2107 |
< |
else if ((int)((c = ctl) >> AC_SHIFT) + (config & SMASK) == 0 && |
2108 |
< |
U.compareAndSwapLong(this, CTL, c, c + AC_UNIT)) |
2109 |
< |
return; |
2107 |
> |
else if ((int)((c = ctl) >> AC_SHIFT) + parallelism <= 0 && |
2108 |
> |
U.compareAndSwapLong |
2109 |
> |
(this, CTL, c, ((c & ~AC_MASK) | |
2110 |
> |
((c & AC_MASK) + AC_UNIT)))) |
2111 |
> |
break; |
2112 |
|
} |
2113 |
|
} |
2114 |
|
|
2122 |
|
WorkQueue q; int b; |
2123 |
|
if ((t = w.nextLocalTask()) != null) |
2124 |
|
return t; |
2125 |
< |
if ((q = findNonEmptyStealQueue(w.nextSeed())) == null) |
2125 |
> |
if ((q = findNonEmptyStealQueue()) == null) |
2126 |
|
return null; |
2127 |
< |
if ((b = q.base) - q.top < 0 && (t = q.pollAt(b)) != null) { |
2156 |
< |
if (q.base - q.top < 0) |
2157 |
< |
signalWork(q); |
2127 |
> |
if ((b = q.base) - q.top < 0 && (t = q.pollAt(b)) != null) |
2128 |
|
return t; |
2159 |
– |
} |
2129 |
|
} |
2130 |
|
} |
2131 |
|
|
2154 |
|
* producing extra tasks amortizes the uncertainty of progress and |
2155 |
|
* diffusion assumptions. |
2156 |
|
* |
2157 |
< |
* So, users will want to use values larger, but not much larger |
2157 |
> |
* So, users will want to use values larger (but not much larger) |
2158 |
|
* than 1 to both smooth over transient shortages and hedge |
2159 |
|
* against uneven progress; as traded off against the cost of |
2160 |
|
* extra task overhead. We leave the user to pick a threshold |
2178 |
|
static int getSurplusQueuedTaskCount() { |
2179 |
|
Thread t; ForkJoinWorkerThread wt; ForkJoinPool pool; WorkQueue q; |
2180 |
|
if (((t = Thread.currentThread()) instanceof ForkJoinWorkerThread)) { |
2181 |
< |
int p = (pool = (wt = (ForkJoinWorkerThread)t).pool).config & SMASK; |
2181 |
> |
int p = (pool = (wt = (ForkJoinWorkerThread)t).pool).parallelism; |
2182 |
|
int n = (q = wt.workQueue).top - q.base; |
2183 |
|
int a = (int)(pool.ctl >> AC_SHIFT) + p; |
2184 |
|
return n - (a > (p >>>= 1) ? 0 : |
2208 |
|
*/ |
2209 |
|
private boolean tryTerminate(boolean now, boolean enable) { |
2210 |
|
int ps; |
2211 |
< |
if (this == common) // cannot shut down |
2211 |
> |
if (this == common) // cannot shut down |
2212 |
|
return false; |
2213 |
|
if ((ps = plock) >= 0) { // enable by setting plock |
2214 |
|
if (!enable) |
2222 |
|
} |
2223 |
|
for (long c;;) { |
2224 |
|
if (((c = ctl) & STOP_BIT) != 0) { // already terminating |
2225 |
< |
if ((short)(c >>> TC_SHIFT) == -(config & SMASK)) { |
2225 |
> |
if ((short)(c >>> TC_SHIFT) + parallelism <= 0) { |
2226 |
|
synchronized (this) { |
2227 |
|
notifyAll(); // signal when 0 workers |
2228 |
|
} |
2231 |
|
} |
2232 |
|
if (!now) { // check if idle & no tasks |
2233 |
|
WorkQueue[] ws; WorkQueue w; |
2234 |
< |
if ((int)(c >> AC_SHIFT) != -(config & SMASK)) |
2234 |
> |
if ((int)(c >> AC_SHIFT) + parallelism > 0) |
2235 |
|
return false; |
2236 |
|
if ((ws = workQueues) != null) { |
2237 |
|
for (int i = 0; i < ws.length; ++i) { |
2238 |
< |
if ((w = ws[i]) != null) { |
2239 |
< |
if (!w.isEmpty()) { // signal unprocessed tasks |
2240 |
< |
signalWork(w); |
2241 |
< |
return false; |
2242 |
< |
} |
2274 |
< |
if ((i & 1) != 0 && w.eventCount >= 0) |
2275 |
< |
return false; // unqueued inactive worker |
2238 |
> |
if ((w = ws[i]) != null && |
2239 |
> |
(!w.isEmpty() || |
2240 |
> |
((i & 1) != 0 && w.eventCount >= 0))) { |
2241 |
> |
signalWork(ws, w); |
2242 |
> |
return false; |
2243 |
|
} |
2244 |
|
} |
2245 |
|
} |
2295 |
|
* least one task. |
2296 |
|
*/ |
2297 |
|
static WorkQueue commonSubmitterQueue() { |
2298 |
< |
ForkJoinPool p; WorkQueue[] ws; int m; Submitter z; |
2298 |
> |
Submitter z; ForkJoinPool p; WorkQueue[] ws; int m, r; |
2299 |
|
return ((z = submitters.get()) != null && |
2300 |
|
(p = common) != null && |
2301 |
|
(ws = p.workQueues) != null && |
2306 |
|
/** |
2307 |
|
* Tries to pop the given task from submitter's queue in common pool. |
2308 |
|
*/ |
2309 |
< |
static boolean tryExternalUnpush(ForkJoinTask<?> t) { |
2310 |
< |
ForkJoinPool p; WorkQueue[] ws; WorkQueue q; Submitter z; |
2311 |
< |
ForkJoinTask<?>[] a; int m, s; |
2312 |
< |
if (t != null && |
2313 |
< |
(z = submitters.get()) != null && |
2314 |
< |
(p = common) != null && |
2315 |
< |
(ws = p.workQueues) != null && |
2316 |
< |
(m = ws.length - 1) >= 0 && |
2317 |
< |
(q = ws[m & z.seed & SQMASK]) != null && |
2351 |
< |
(s = q.top) != q.base && |
2352 |
< |
(a = q.array) != null) { |
2309 |
> |
final boolean tryExternalUnpush(ForkJoinTask<?> task) { |
2310 |
> |
WorkQueue joiner; ForkJoinTask<?>[] a; int m, s; |
2311 |
> |
Submitter z = submitters.get(); |
2312 |
> |
WorkQueue[] ws = workQueues; |
2313 |
> |
boolean popped = false; |
2314 |
> |
if (z != null && ws != null && (m = ws.length - 1) >= 0 && |
2315 |
> |
(joiner = ws[z.seed & m & SQMASK]) != null && |
2316 |
> |
joiner.base != (s = joiner.top) && |
2317 |
> |
(a = joiner.array) != null) { |
2318 |
|
long j = (((a.length - 1) & (s - 1)) << ASHIFT) + ABASE; |
2319 |
< |
if (U.getObject(a, j) == t && |
2320 |
< |
U.compareAndSwapInt(q, QLOCK, 0, 1)) { |
2321 |
< |
if (q.array == a && q.top == s && // recheck |
2322 |
< |
U.compareAndSwapObject(a, j, t, null)) { |
2323 |
< |
q.top = s - 1; |
2324 |
< |
q.qlock = 0; |
2360 |
< |
return true; |
2319 |
> |
if (U.getObject(a, j) == task && |
2320 |
> |
U.compareAndSwapInt(joiner, QLOCK, 0, 1)) { |
2321 |
> |
if (joiner.top == s && joiner.array == a && |
2322 |
> |
U.compareAndSwapObject(a, j, task, null)) { |
2323 |
> |
joiner.top = s - 1; |
2324 |
> |
popped = true; |
2325 |
|
} |
2326 |
< |
q.qlock = 0; |
2326 |
> |
joiner.qlock = 0; |
2327 |
|
} |
2328 |
|
} |
2329 |
< |
return false; |
2329 |
> |
return popped; |
2330 |
|
} |
2331 |
|
|
2332 |
< |
/** |
2333 |
< |
* Tries to pop and run local tasks within the same computation |
2334 |
< |
* as the given root. On failure, tries to help complete from |
2335 |
< |
* other queues via helpComplete. |
2336 |
< |
*/ |
2337 |
< |
private void externalHelpComplete(WorkQueue q, ForkJoinTask<?> root) { |
2338 |
< |
ForkJoinTask<?>[] a; int m; |
2339 |
< |
if (q != null && (a = q.array) != null && (m = (a.length - 1)) >= 0 && |
2340 |
< |
root != null && root.status >= 0) { |
2341 |
< |
for (;;) { |
2342 |
< |
int s, u; Object o; CountedCompleter<?> task = null; |
2343 |
< |
if ((s = q.top) - q.base > 0) { |
2344 |
< |
long j = ((m & (s - 1)) << ASHIFT) + ABASE; |
2381 |
< |
if ((o = U.getObject(a, j)) != null && |
2382 |
< |
(o instanceof CountedCompleter)) { |
2383 |
< |
CountedCompleter<?> t = (CountedCompleter<?>)o, r = t; |
2384 |
< |
do { |
2385 |
< |
if (r == root) { |
2386 |
< |
if (U.compareAndSwapInt(q, QLOCK, 0, 1)) { |
2387 |
< |
if (q.array == a && q.top == s && |
2388 |
< |
U.compareAndSwapObject(a, j, t, null)) { |
2389 |
< |
q.top = s - 1; |
2390 |
< |
task = t; |
2391 |
< |
} |
2392 |
< |
q.qlock = 0; |
2393 |
< |
} |
2394 |
< |
break; |
2395 |
< |
} |
2396 |
< |
} while ((r = r.completer) != null); |
2397 |
< |
} |
2398 |
< |
} |
2399 |
< |
if (task != null) |
2400 |
< |
task.doExec(); |
2401 |
< |
if (root.status < 0 || |
2402 |
< |
(u = (int)(ctl >>> 32)) >= 0 || (u >> UAC_SHIFT) >= 0) |
2332 |
> |
final int externalHelpComplete(CountedCompleter<?> task) { |
2333 |
> |
WorkQueue joiner; int m, j; |
2334 |
> |
Submitter z = submitters.get(); |
2335 |
> |
WorkQueue[] ws = workQueues; |
2336 |
> |
int s = 0; |
2337 |
> |
if (z != null && ws != null && (m = ws.length - 1) >= 0 && |
2338 |
> |
(joiner = ws[(j = z.seed) & m & SQMASK]) != null && task != null) { |
2339 |
> |
int scans = m + m + 1; |
2340 |
> |
long c = 0L; // for stability check |
2341 |
> |
j |= 1; // poll odd queues |
2342 |
> |
for (int k = scans; ; j += 2) { |
2343 |
> |
WorkQueue q; |
2344 |
> |
if ((s = task.status) < 0) |
2345 |
|
break; |
2346 |
< |
if (task == null) { |
2347 |
< |
helpSignal(root, q.poolIndex); |
2348 |
< |
if (root.status >= 0) |
2407 |
< |
helpComplete(root, SHARED_QUEUE); |
2346 |
> |
else if (joiner.externalPopAndExecCC(task)) |
2347 |
> |
k = scans; |
2348 |
> |
else if ((s = task.status) < 0) |
2349 |
|
break; |
2350 |
+ |
else if ((q = ws[j & m]) != null && q.pollAndExecCC(task)) |
2351 |
+ |
k = scans; |
2352 |
+ |
else if (--k < 0) { |
2353 |
+ |
if (c == (c = ctl)) |
2354 |
+ |
break; |
2355 |
+ |
k = scans; |
2356 |
|
} |
2357 |
|
} |
2358 |
|
} |
2359 |
< |
} |
2413 |
< |
|
2414 |
< |
/** |
2415 |
< |
* Tries to help execute or signal availability of the given task |
2416 |
< |
* from submitter's queue in common pool. |
2417 |
< |
*/ |
2418 |
< |
static void externalHelpJoin(ForkJoinTask<?> t) { |
2419 |
< |
// Some hard-to-avoid overlap with tryExternalUnpush |
2420 |
< |
ForkJoinPool p; WorkQueue[] ws; WorkQueue q, w; Submitter z; |
2421 |
< |
ForkJoinTask<?>[] a; int m, s, n; |
2422 |
< |
if (t != null && |
2423 |
< |
(z = submitters.get()) != null && |
2424 |
< |
(p = common) != null && |
2425 |
< |
(ws = p.workQueues) != null && |
2426 |
< |
(m = ws.length - 1) >= 0 && |
2427 |
< |
(q = ws[m & z.seed & SQMASK]) != null && |
2428 |
< |
(a = q.array) != null) { |
2429 |
< |
int am = a.length - 1; |
2430 |
< |
if ((s = q.top) != q.base) { |
2431 |
< |
long j = ((am & (s - 1)) << ASHIFT) + ABASE; |
2432 |
< |
if (U.getObject(a, j) == t && |
2433 |
< |
U.compareAndSwapInt(q, QLOCK, 0, 1)) { |
2434 |
< |
if (q.array == a && q.top == s && |
2435 |
< |
U.compareAndSwapObject(a, j, t, null)) { |
2436 |
< |
q.top = s - 1; |
2437 |
< |
q.qlock = 0; |
2438 |
< |
t.doExec(); |
2439 |
< |
} |
2440 |
< |
else |
2441 |
< |
q.qlock = 0; |
2442 |
< |
} |
2443 |
< |
} |
2444 |
< |
if (t.status >= 0) { |
2445 |
< |
if (t instanceof CountedCompleter) |
2446 |
< |
p.externalHelpComplete(q, t); |
2447 |
< |
else |
2448 |
< |
p.helpSignal(t, q.poolIndex); |
2449 |
< |
} |
2450 |
< |
} |
2359 |
> |
return s; |
2360 |
|
} |
2361 |
|
|
2362 |
|
// Exported methods |
2375 |
|
* java.lang.RuntimePermission}{@code ("modifyThread")} |
2376 |
|
*/ |
2377 |
|
public ForkJoinPool() { |
2378 |
< |
this(Runtime.getRuntime().availableProcessors(), |
2378 |
> |
this(Math.min(MAX_CAP, Runtime.getRuntime().availableProcessors()), |
2379 |
|
defaultForkJoinWorkerThreadFactory, null, false); |
2380 |
|
} |
2381 |
|
|
2423 |
|
*/ |
2424 |
|
public ForkJoinPool(int parallelism, |
2425 |
|
ForkJoinWorkerThreadFactory factory, |
2426 |
< |
Thread.UncaughtExceptionHandler handler, |
2426 |
> |
UncaughtExceptionHandler handler, |
2427 |
|
boolean asyncMode) { |
2428 |
+ |
this(checkParallelism(parallelism), |
2429 |
+ |
checkFactory(factory), |
2430 |
+ |
handler, |
2431 |
+ |
(asyncMode ? FIFO_QUEUE : LIFO_QUEUE), |
2432 |
+ |
"ForkJoinPool-" + nextPoolId() + "-worker-"); |
2433 |
|
checkPermission(); |
2434 |
< |
if (factory == null) |
2435 |
< |
throw new NullPointerException(); |
2434 |
> |
} |
2435 |
> |
|
2436 |
> |
private static int checkParallelism(int parallelism) { |
2437 |
|
if (parallelism <= 0 || parallelism > MAX_CAP) |
2438 |
|
throw new IllegalArgumentException(); |
2439 |
< |
this.factory = factory; |
2440 |
< |
this.ueh = handler; |
2441 |
< |
this.config = parallelism | (asyncMode ? (FIFO_QUEUE << 16) : 0); |
2442 |
< |
long np = (long)(-parallelism); // offset ctl counts |
2443 |
< |
this.ctl = ((np << AC_SHIFT) & AC_MASK) | ((np << TC_SHIFT) & TC_MASK); |
2444 |
< |
int pn = nextPoolId(); |
2445 |
< |
StringBuilder sb = new StringBuilder("ForkJoinPool-"); |
2446 |
< |
sb.append(Integer.toString(pn)); |
2532 |
< |
sb.append("-worker-"); |
2533 |
< |
this.workerNamePrefix = sb.toString(); |
2439 |
> |
return parallelism; |
2440 |
> |
} |
2441 |
> |
|
2442 |
> |
private static ForkJoinWorkerThreadFactory checkFactory |
2443 |
> |
(ForkJoinWorkerThreadFactory factory) { |
2444 |
> |
if (factory == null) |
2445 |
> |
throw new NullPointerException(); |
2446 |
> |
return factory; |
2447 |
|
} |
2448 |
|
|
2449 |
|
/** |
2450 |
< |
* Constructor for common pool, suitable only for static initialization. |
2451 |
< |
* Basically the same as above, but uses smallest possible initial footprint. |
2452 |
< |
*/ |
2453 |
< |
ForkJoinPool(int parallelism, long ctl, |
2454 |
< |
ForkJoinWorkerThreadFactory factory, |
2455 |
< |
Thread.UncaughtExceptionHandler handler) { |
2456 |
< |
this.config = parallelism; |
2457 |
< |
this.ctl = ctl; |
2450 |
> |
* Creates a {@code ForkJoinPool} with the given parameters, without |
2451 |
> |
* any security checks or parameter validation. Invoked directly by |
2452 |
> |
* makeCommonPool. |
2453 |
> |
*/ |
2454 |
> |
private ForkJoinPool(int parallelism, |
2455 |
> |
ForkJoinWorkerThreadFactory factory, |
2456 |
> |
UncaughtExceptionHandler handler, |
2457 |
> |
int mode, |
2458 |
> |
String workerNamePrefix) { |
2459 |
> |
this.workerNamePrefix = workerNamePrefix; |
2460 |
|
this.factory = factory; |
2461 |
|
this.ueh = handler; |
2462 |
< |
this.workerNamePrefix = "ForkJoinPool.commonPool-worker-"; |
2462 |
> |
this.mode = (short)mode; |
2463 |
> |
this.parallelism = (short)parallelism; |
2464 |
> |
long np = (long)(-parallelism); // offset ctl counts |
2465 |
> |
this.ctl = ((np << AC_SHIFT) & AC_MASK) | ((np << TC_SHIFT) & TC_MASK); |
2466 |
|
} |
2467 |
|
|
2468 |
|
/** |
2472 |
|
* ongoing processing are automatically terminated upon program |
2473 |
|
* {@link System#exit}. Any program that relies on asynchronous |
2474 |
|
* task processing to complete before program termination should |
2475 |
< |
* invoke {@code commonPool().}{@link #awaitQuiescence}, before |
2476 |
< |
* exit. |
2475 |
> |
* invoke {@code commonPool().}{@link #awaitQuiescence awaitQuiescence}, |
2476 |
> |
* before exit. |
2477 |
|
* |
2478 |
|
* @return the common pool instance |
2479 |
|
* @since 1.8 |
2536 |
|
if (task instanceof ForkJoinTask<?>) // avoid re-wrap |
2537 |
|
job = (ForkJoinTask<?>) task; |
2538 |
|
else |
2539 |
< |
job = new ForkJoinTask.AdaptedRunnableAction(task); |
2539 |
> |
job = new ForkJoinTask.RunnableExecuteAction(task); |
2540 |
|
externalPush(job); |
2541 |
|
} |
2542 |
|
|
2638 |
|
* |
2639 |
|
* @return the handler, or {@code null} if none |
2640 |
|
*/ |
2641 |
< |
public Thread.UncaughtExceptionHandler getUncaughtExceptionHandler() { |
2641 |
> |
public UncaughtExceptionHandler getUncaughtExceptionHandler() { |
2642 |
|
return ueh; |
2643 |
|
} |
2644 |
|
|
2648 |
|
* @return the targeted parallelism level of this pool |
2649 |
|
*/ |
2650 |
|
public int getParallelism() { |
2651 |
< |
return config & SMASK; |
2651 |
> |
int par; |
2652 |
> |
return ((par = parallelism) > 0) ? par : 1; |
2653 |
|
} |
2654 |
|
|
2655 |
|
/** |
2671 |
|
* @return the number of worker threads |
2672 |
|
*/ |
2673 |
|
public int getPoolSize() { |
2674 |
< |
return (config & SMASK) + (short)(ctl >>> TC_SHIFT); |
2674 |
> |
return parallelism + (short)(ctl >>> TC_SHIFT); |
2675 |
|
} |
2676 |
|
|
2677 |
|
/** |
2681 |
|
* @return {@code true} if this pool uses async mode |
2682 |
|
*/ |
2683 |
|
public boolean getAsyncMode() { |
2684 |
< |
return (config >>> 16) == FIFO_QUEUE; |
2684 |
> |
return mode == FIFO_QUEUE; |
2685 |
|
} |
2686 |
|
|
2687 |
|
/** |
2712 |
|
* @return the number of active threads |
2713 |
|
*/ |
2714 |
|
public int getActiveThreadCount() { |
2715 |
< |
int r = (config & SMASK) + (int)(ctl >> AC_SHIFT); |
2715 |
> |
int r = parallelism + (int)(ctl >> AC_SHIFT); |
2716 |
|
return (r <= 0) ? 0 : r; // suppress momentarily negative values |
2717 |
|
} |
2718 |
|
|
2728 |
|
* @return {@code true} if all threads are currently idle |
2729 |
|
*/ |
2730 |
|
public boolean isQuiescent() { |
2731 |
< |
return (int)(ctl >> AC_SHIFT) + (config & SMASK) == 0; |
2731 |
> |
return parallelism + (int)(ctl >> AC_SHIFT) <= 0; |
2732 |
|
} |
2733 |
|
|
2734 |
|
/** |
2891 |
|
} |
2892 |
|
} |
2893 |
|
} |
2894 |
< |
int pc = (config & SMASK); |
2894 |
> |
int pc = parallelism; |
2895 |
|
int tc = pc + (short)(c >>> TC_SHIFT); |
2896 |
|
int ac = pc + (int)(c >> AC_SHIFT); |
2897 |
|
if (ac < 0) // ignore transient negative |
2964 |
|
public boolean isTerminated() { |
2965 |
|
long c = ctl; |
2966 |
|
return ((c & STOP_BIT) != 0L && |
2967 |
< |
(short)(c >>> TC_SHIFT) == -(config & SMASK)); |
2967 |
> |
(short)(c >>> TC_SHIFT) + parallelism <= 0); |
2968 |
|
} |
2969 |
|
|
2970 |
|
/** |
2983 |
|
public boolean isTerminating() { |
2984 |
|
long c = ctl; |
2985 |
|
return ((c & STOP_BIT) != 0L && |
2986 |
< |
(short)(c >>> TC_SHIFT) != -(config & SMASK)); |
2986 |
> |
(short)(c >>> TC_SHIFT) + parallelism > 0); |
2987 |
|
} |
2988 |
|
|
2989 |
|
/** |
3001 |
|
* is interrupted, whichever happens first. Because the {@link |
3002 |
|
* #commonPool()} never terminates until program shutdown, when |
3003 |
|
* applied to the common pool, this method is equivalent to {@link |
3004 |
< |
* #awaitQuiescence} but always returns {@code false}. |
3004 |
> |
* #awaitQuiescence(long, TimeUnit)} but always returns {@code false}. |
3005 |
|
* |
3006 |
|
* @param timeout the maximum time to wait |
3007 |
|
* @param unit the time unit of the timeout argument |
3020 |
|
long nanos = unit.toNanos(timeout); |
3021 |
|
if (isTerminated()) |
3022 |
|
return true; |
3023 |
< |
long startTime = System.nanoTime(); |
3024 |
< |
boolean terminated = false; |
3023 |
> |
if (nanos <= 0L) |
3024 |
> |
return false; |
3025 |
> |
long deadline = System.nanoTime() + nanos; |
3026 |
|
synchronized (this) { |
3027 |
< |
for (long waitTime = nanos, millis = 0L;;) { |
3028 |
< |
if (terminated = isTerminated() || |
3029 |
< |
waitTime <= 0L || |
3030 |
< |
(millis = unit.toMillis(waitTime)) <= 0L) |
3031 |
< |
break; |
3032 |
< |
wait(millis); |
3033 |
< |
waitTime = nanos - (System.nanoTime() - startTime); |
3027 |
> |
for (;;) { |
3028 |
> |
if (isTerminated()) |
3029 |
> |
return true; |
3030 |
> |
if (nanos <= 0L) |
3031 |
> |
return false; |
3032 |
> |
long millis = TimeUnit.NANOSECONDS.toMillis(nanos); |
3033 |
> |
wait(millis > 0L ? millis : 1L); |
3034 |
> |
nanos = deadline - System.nanoTime(); |
3035 |
|
} |
3036 |
|
} |
3116 |
– |
return terminated; |
3037 |
|
} |
3038 |
|
|
3039 |
|
/** |
3072 |
|
ForkJoinTask<?> t; WorkQueue q; int b; |
3073 |
|
if ((q = ws[r++ & m]) != null && (b = q.base) - q.top < 0) { |
3074 |
|
found = true; |
3075 |
< |
if ((t = q.pollAt(b)) != null) { |
3156 |
< |
if (q.base - q.top < 0) |
3157 |
< |
signalWork(q); |
3075 |
> |
if ((t = q.pollAt(b)) != null) |
3076 |
|
t.doExec(); |
3159 |
– |
} |
3077 |
|
break; |
3078 |
|
} |
3079 |
|
} |
3083 |
|
|
3084 |
|
/** |
3085 |
|
* Waits and/or attempts to assist performing tasks indefinitely |
3086 |
< |
* until the {@link #commonPool()} {@link #isQuiescent} |
3086 |
> |
* until the {@link #commonPool()} {@link #isQuiescent}. |
3087 |
|
*/ |
3088 |
|
static void quiesceCommonPool() { |
3089 |
|
common.awaitQuiescence(Long.MAX_VALUE, TimeUnit.NANOSECONDS); |
3098 |
|
* not necessary. Method {@code block} blocks the current thread |
3099 |
|
* if necessary (perhaps internally invoking {@code isReleasable} |
3100 |
|
* before actually blocking). These actions are performed by any |
3101 |
< |
* thread invoking {@link ForkJoinPool#managedBlock}. The |
3102 |
< |
* unusual methods in this API accommodate synchronizers that may, |
3103 |
< |
* but don't usually, block for long periods. Similarly, they |
3101 |
> |
* thread invoking {@link ForkJoinPool#managedBlock(ManagedBlocker)}. |
3102 |
> |
* The unusual methods in this API accommodate synchronizers that |
3103 |
> |
* may, but don't usually, block for long periods. Similarly, they |
3104 |
|
* allow more efficient internal handling of cases in which |
3105 |
|
* additional workers may be, but usually are not, needed to |
3106 |
|
* ensure sufficient parallelism. Toward this end, |
3158 |
|
|
3159 |
|
/** |
3160 |
|
* Returns {@code true} if blocking is unnecessary. |
3161 |
+ |
* @return {@code true} if blocking is unnecessary |
3162 |
|
*/ |
3163 |
|
boolean isReleasable(); |
3164 |
|
} |
3188 |
|
Thread t = Thread.currentThread(); |
3189 |
|
if (t instanceof ForkJoinWorkerThread) { |
3190 |
|
ForkJoinPool p = ((ForkJoinWorkerThread)t).pool; |
3191 |
< |
while (!blocker.isReleasable()) { // variant of helpSignal |
3192 |
< |
WorkQueue[] ws; WorkQueue q; int m, u; |
3275 |
< |
if ((ws = p.workQueues) != null && (m = ws.length - 1) >= 0) { |
3276 |
< |
for (int i = 0; i <= m; ++i) { |
3277 |
< |
if (blocker.isReleasable()) |
3278 |
< |
return; |
3279 |
< |
if ((q = ws[i]) != null && q.base - q.top < 0) { |
3280 |
< |
p.signalWork(q); |
3281 |
< |
if ((u = (int)(p.ctl >>> 32)) >= 0 || |
3282 |
< |
(u >> UAC_SHIFT) >= 0) |
3283 |
< |
break; |
3284 |
< |
} |
3285 |
< |
} |
3286 |
< |
} |
3287 |
< |
if (p.tryCompensate()) { |
3191 |
> |
while (!blocker.isReleasable()) { |
3192 |
> |
if (p.tryCompensate(p.ctl)) { |
3193 |
|
try { |
3194 |
|
do {} while (!blocker.isReleasable() && |
3195 |
|
!blocker.block()); |
3227 |
|
private static final long STEALCOUNT; |
3228 |
|
private static final long PLOCK; |
3229 |
|
private static final long INDEXSEED; |
3230 |
+ |
private static final long QBASE; |
3231 |
|
private static final long QLOCK; |
3232 |
|
|
3233 |
|
static { |
3247 |
|
PARKBLOCKER = U.objectFieldOffset |
3248 |
|
(tk.getDeclaredField("parkBlocker")); |
3249 |
|
Class<?> wk = WorkQueue.class; |
3250 |
+ |
QBASE = U.objectFieldOffset |
3251 |
+ |
(wk.getDeclaredField("base")); |
3252 |
|
QLOCK = U.objectFieldOffset |
3253 |
|
(wk.getDeclaredField("qlock")); |
3254 |
|
Class<?> ak = ForkJoinTask[].class; |
3262 |
|
} |
3263 |
|
|
3264 |
|
submitters = new ThreadLocal<Submitter>(); |
3265 |
< |
ForkJoinWorkerThreadFactory fac = defaultForkJoinWorkerThreadFactory = |
3265 |
> |
defaultForkJoinWorkerThreadFactory = |
3266 |
|
new DefaultForkJoinWorkerThreadFactory(); |
3267 |
|
modifyThreadPermission = new RuntimePermission("modifyThread"); |
3268 |
|
|
3269 |
< |
/* |
3270 |
< |
* Establish common pool parameters. For extra caution, |
3271 |
< |
* computations to set up common pool state are here; the |
3272 |
< |
* constructor just assigns these values to fields. |
3273 |
< |
*/ |
3269 |
> |
common = java.security.AccessController.doPrivileged |
3270 |
> |
(new java.security.PrivilegedAction<ForkJoinPool>() { |
3271 |
> |
public ForkJoinPool run() { return makeCommonPool(); }}); |
3272 |
> |
int par = common.parallelism; // report 1 even if threads disabled |
3273 |
> |
commonParallelism = par > 0 ? par : 1; |
3274 |
> |
} |
3275 |
|
|
3276 |
< |
int par = 0; |
3277 |
< |
Thread.UncaughtExceptionHandler handler = null; |
3278 |
< |
try { // TBD: limit or report ignored exceptions? |
3276 |
> |
/** |
3277 |
> |
* Creates and returns the common pool, respecting user settings |
3278 |
> |
* specified via system properties. |
3279 |
> |
*/ |
3280 |
> |
private static ForkJoinPool makeCommonPool() { |
3281 |
> |
int parallelism = -1; |
3282 |
> |
ForkJoinWorkerThreadFactory factory |
3283 |
> |
= defaultForkJoinWorkerThreadFactory; |
3284 |
> |
UncaughtExceptionHandler handler = null; |
3285 |
> |
try { // ignore exceptions in accessing/parsing properties |
3286 |
|
String pp = System.getProperty |
3287 |
|
("java.util.concurrent.ForkJoinPool.common.parallelism"); |
3372 |
– |
String hp = System.getProperty |
3373 |
– |
("java.util.concurrent.ForkJoinPool.common.exceptionHandler"); |
3288 |
|
String fp = System.getProperty |
3289 |
|
("java.util.concurrent.ForkJoinPool.common.threadFactory"); |
3290 |
+ |
String hp = System.getProperty |
3291 |
+ |
("java.util.concurrent.ForkJoinPool.common.exceptionHandler"); |
3292 |
+ |
if (pp != null) |
3293 |
+ |
parallelism = Integer.parseInt(pp); |
3294 |
|
if (fp != null) |
3295 |
< |
fac = ((ForkJoinWorkerThreadFactory)ClassLoader. |
3296 |
< |
getSystemClassLoader().loadClass(fp).newInstance()); |
3295 |
> |
factory = ((ForkJoinWorkerThreadFactory)ClassLoader. |
3296 |
> |
getSystemClassLoader().loadClass(fp).newInstance()); |
3297 |
|
if (hp != null) |
3298 |
< |
handler = ((Thread.UncaughtExceptionHandler)ClassLoader. |
3298 |
> |
handler = ((UncaughtExceptionHandler)ClassLoader. |
3299 |
|
getSystemClassLoader().loadClass(hp).newInstance()); |
3382 |
– |
if (pp != null) |
3383 |
– |
par = Integer.parseInt(pp); |
3300 |
|
} catch (Exception ignore) { |
3301 |
|
} |
3302 |
|
|
3303 |
< |
if (par <= 0) |
3304 |
< |
par = Runtime.getRuntime().availableProcessors(); |
3305 |
< |
if (par > MAX_CAP) |
3306 |
< |
par = MAX_CAP; |
3307 |
< |
commonParallelism = par; |
3308 |
< |
long np = (long)(-par); // precompute initial ctl value |
3309 |
< |
long ct = ((np << AC_SHIFT) & AC_MASK) | ((np << TC_SHIFT) & TC_MASK); |
3394 |
< |
|
3395 |
< |
common = new ForkJoinPool(par, ct, fac, handler); |
3303 |
> |
if (parallelism < 0 && // default 1 less than #cores |
3304 |
> |
(parallelism = Runtime.getRuntime().availableProcessors() - 1) < 0) |
3305 |
> |
parallelism = 0; |
3306 |
> |
if (parallelism > MAX_CAP) |
3307 |
> |
parallelism = MAX_CAP; |
3308 |
> |
return new ForkJoinPool(parallelism, factory, handler, LIFO_QUEUE, |
3309 |
> |
"ForkJoinPool.commonPool-worker-"); |
3310 |
|
} |
3311 |
|
|
3312 |
|
/** |