1 |
jsr166 |
1.1 |
/* |
2 |
|
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
|
* Expert Group and released to the public domain, as explained at |
4 |
jsr166 |
1.58 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
jsr166 |
1.1 |
*/ |
6 |
jsr166 |
1.301 |
|
7 |
jsr166 |
1.1 |
package java.util.concurrent; |
8 |
|
|
|
9 |
jsr166 |
1.156 |
import java.lang.Thread.UncaughtExceptionHandler; |
10 |
dl |
1.314 |
import java.lang.invoke.MethodHandles; |
11 |
|
|
import java.lang.invoke.VarHandle; |
12 |
jsr166 |
1.228 |
import java.security.AccessControlContext; |
13 |
|
|
import java.security.Permissions; |
14 |
|
|
import java.security.ProtectionDomain; |
15 |
jsr166 |
1.1 |
import java.util.ArrayList; |
16 |
|
|
import java.util.Collection; |
17 |
|
|
import java.util.Collections; |
18 |
|
|
import java.util.List; |
19 |
dl |
1.307 |
import java.util.function.Predicate; |
20 |
dl |
1.243 |
import java.util.concurrent.locks.LockSupport; |
21 |
jsr166 |
1.1 |
|
22 |
|
|
/** |
23 |
jsr166 |
1.4 |
* An {@link ExecutorService} for running {@link ForkJoinTask}s. |
24 |
jsr166 |
1.8 |
* A {@code ForkJoinPool} provides the entry point for submissions |
25 |
dl |
1.18 |
* from non-{@code ForkJoinTask} clients, as well as management and |
26 |
jsr166 |
1.11 |
* monitoring operations. |
27 |
jsr166 |
1.1 |
* |
28 |
jsr166 |
1.9 |
* <p>A {@code ForkJoinPool} differs from other kinds of {@link |
29 |
|
|
* ExecutorService} mainly by virtue of employing |
30 |
|
|
* <em>work-stealing</em>: all threads in the pool attempt to find and |
31 |
dl |
1.78 |
* execute tasks submitted to the pool and/or created by other active |
32 |
|
|
* tasks (eventually blocking waiting for work if none exist). This |
33 |
|
|
* enables efficient processing when most tasks spawn other subtasks |
34 |
|
|
* (as do most {@code ForkJoinTask}s), as well as when many small |
35 |
|
|
* tasks are submitted to the pool from external clients. Especially |
36 |
|
|
* when setting <em>asyncMode</em> to true in constructors, {@code |
37 |
|
|
* ForkJoinPool}s may also be appropriate for use with event-style |
38 |
|
|
* tasks that are never joined. |
39 |
jsr166 |
1.1 |
* |
40 |
dl |
1.112 |
* <p>A static {@link #commonPool()} is available and appropriate for |
41 |
dl |
1.101 |
* most applications. The common pool is used by any ForkJoinTask that |
42 |
|
|
* is not explicitly submitted to a specified pool. Using the common |
43 |
|
|
* pool normally reduces resource usage (its threads are slowly |
44 |
|
|
* reclaimed during periods of non-use, and reinstated upon subsequent |
45 |
dl |
1.105 |
* use). |
46 |
dl |
1.100 |
* |
47 |
|
|
* <p>For applications that require separate or custom pools, a {@code |
48 |
|
|
* ForkJoinPool} may be constructed with a given target parallelism |
49 |
jsr166 |
1.214 |
* level; by default, equal to the number of available processors. |
50 |
|
|
* The pool attempts to maintain enough active (or available) threads |
51 |
|
|
* by dynamically adding, suspending, or resuming internal worker |
52 |
jsr166 |
1.187 |
* threads, even if some tasks are stalled waiting to join others. |
53 |
|
|
* However, no such adjustments are guaranteed in the face of blocked |
54 |
|
|
* I/O or other unmanaged synchronization. The nested {@link |
55 |
dl |
1.100 |
* ManagedBlocker} interface enables extension of the kinds of |
56 |
dl |
1.300 |
* synchronization accommodated. The default policies may be |
57 |
|
|
* overridden using a constructor with parameters corresponding to |
58 |
|
|
* those documented in class {@link ThreadPoolExecutor}. |
59 |
jsr166 |
1.1 |
* |
60 |
|
|
* <p>In addition to execution and lifecycle control methods, this |
61 |
|
|
* class provides status check methods (for example |
62 |
jsr166 |
1.4 |
* {@link #getStealCount}) that are intended to aid in developing, |
63 |
jsr166 |
1.1 |
* tuning, and monitoring fork/join applications. Also, method |
64 |
jsr166 |
1.4 |
* {@link #toString} returns indications of pool state in a |
65 |
jsr166 |
1.1 |
* convenient form for informal monitoring. |
66 |
|
|
* |
67 |
jsr166 |
1.109 |
* <p>As is the case with other ExecutorServices, there are three |
68 |
jsr166 |
1.84 |
* main task execution methods summarized in the following table. |
69 |
|
|
* These are designed to be used primarily by clients not already |
70 |
|
|
* engaged in fork/join computations in the current pool. The main |
71 |
|
|
* forms of these methods accept instances of {@code ForkJoinTask}, |
72 |
|
|
* but overloaded forms also allow mixed execution of plain {@code |
73 |
|
|
* Runnable}- or {@code Callable}- based activities as well. However, |
74 |
|
|
* tasks that are already executing in a pool should normally instead |
75 |
|
|
* use the within-computation forms listed in the table unless using |
76 |
|
|
* async event-style tasks that are not usually joined, in which case |
77 |
|
|
* there is little difference among choice of methods. |
78 |
dl |
1.18 |
* |
79 |
|
|
* <table BORDER CELLPADDING=3 CELLSPACING=1> |
80 |
jsr166 |
1.159 |
* <caption>Summary of task execution methods</caption> |
81 |
dl |
1.18 |
* <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 |
jsr166 |
1.153 |
* <td> <b>Arrange async execution</b></td> |
88 |
dl |
1.18 |
* <td> {@link #execute(ForkJoinTask)}</td> |
89 |
|
|
* <td> {@link ForkJoinTask#fork}</td> |
90 |
|
|
* </tr> |
91 |
|
|
* <tr> |
92 |
jsr166 |
1.153 |
* <td> <b>Await and obtain result</b></td> |
93 |
dl |
1.18 |
* <td> {@link #invoke(ForkJoinTask)}</td> |
94 |
|
|
* <td> {@link ForkJoinTask#invoke}</td> |
95 |
|
|
* </tr> |
96 |
|
|
* <tr> |
97 |
jsr166 |
1.153 |
* <td> <b>Arrange exec and obtain Future</b></td> |
98 |
dl |
1.18 |
* <td> {@link #submit(ForkJoinTask)}</td> |
99 |
|
|
* <td> {@link ForkJoinTask#fork} (ForkJoinTasks <em>are</em> Futures)</td> |
100 |
|
|
* </tr> |
101 |
|
|
* </table> |
102 |
dl |
1.19 |
* |
103 |
dl |
1.105 |
* <p>The common pool is by default constructed with default |
104 |
jsr166 |
1.321 |
* parameters, but these may be controlled by setting the following |
105 |
jsr166 |
1.162 |
* {@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 |
dl |
1.208 |
* <li>{@code java.util.concurrent.ForkJoinPool.common.maximumSpares} |
114 |
dl |
1.223 |
* - the maximum number of allowed extra threads to maintain target |
115 |
dl |
1.208 |
* parallelism (default 256). |
116 |
jsr166 |
1.162 |
* </ul> |
117 |
dl |
1.197 |
* If a {@link SecurityManager} is present and no factory is |
118 |
|
|
* specified, then the default pool uses a factory supplying |
119 |
|
|
* threads that have no {@link Permissions} enabled. |
120 |
jsr166 |
1.165 |
* The system class loader is used to load these classes. |
121 |
jsr166 |
1.156 |
* Upon any error in establishing these settings, default parameters |
122 |
dl |
1.160 |
* are used. It is possible to disable or limit the use of threads in |
123 |
|
|
* the common pool by setting the parallelism property to zero, and/or |
124 |
dl |
1.193 |
* using a factory that may return {@code null}. However doing so may |
125 |
|
|
* cause unjoined tasks to never be executed. |
126 |
dl |
1.105 |
* |
127 |
jsr166 |
1.1 |
* <p><b>Implementation notes</b>: This implementation restricts the |
128 |
|
|
* maximum number of running threads to 32767. Attempts to create |
129 |
jsr166 |
1.11 |
* pools with greater than the maximum number result in |
130 |
jsr166 |
1.8 |
* {@code IllegalArgumentException}. |
131 |
jsr166 |
1.1 |
* |
132 |
jsr166 |
1.11 |
* <p>This implementation rejects submitted tasks (that is, by throwing |
133 |
dl |
1.19 |
* {@link RejectedExecutionException}) only when the pool is shut down |
134 |
dl |
1.20 |
* or internal resources have been exhausted. |
135 |
jsr166 |
1.11 |
* |
136 |
jsr166 |
1.1 |
* @since 1.7 |
137 |
|
|
* @author Doug Lea |
138 |
|
|
*/ |
139 |
|
|
public class ForkJoinPool extends AbstractExecutorService { |
140 |
|
|
|
141 |
|
|
/* |
142 |
dl |
1.14 |
* Implementation Overview |
143 |
|
|
* |
144 |
dl |
1.78 |
* This class and its nested classes provide the main |
145 |
|
|
* functionality and control for a set of worker threads: |
146 |
jsr166 |
1.84 |
* Submissions from non-FJ threads enter into submission queues. |
147 |
|
|
* Workers take these tasks and typically split them into subtasks |
148 |
|
|
* that may be stolen by other workers. Preference rules give |
149 |
|
|
* first priority to processing tasks from their own queues (LIFO |
150 |
|
|
* or FIFO, depending on mode), then to randomized FIFO steals of |
151 |
dl |
1.200 |
* tasks in other queues. This framework began as vehicle for |
152 |
|
|
* supporting tree-structured parallelism using work-stealing. |
153 |
|
|
* Over time, its scalability advantages led to extensions and |
154 |
dl |
1.208 |
* changes to better support more diverse usage contexts. Because |
155 |
|
|
* most internal methods and nested classes are interrelated, |
156 |
|
|
* their main rationale and descriptions are presented here; |
157 |
|
|
* individual methods and nested classes contain only brief |
158 |
|
|
* comments about details. |
159 |
dl |
1.78 |
* |
160 |
jsr166 |
1.84 |
* WorkQueues |
161 |
dl |
1.78 |
* ========== |
162 |
|
|
* |
163 |
|
|
* Most operations occur within work-stealing queues (in nested |
164 |
|
|
* class WorkQueue). These are special forms of Deques that |
165 |
|
|
* support only three of the four possible end-operations -- push, |
166 |
|
|
* pop, and poll (aka steal), under the further constraints that |
167 |
|
|
* push and pop are called only from the owning thread (or, as |
168 |
|
|
* extended here, under a lock), while poll may be called from |
169 |
|
|
* other threads. (If you are unfamiliar with them, you probably |
170 |
|
|
* want to read Herlihy and Shavit's book "The Art of |
171 |
|
|
* Multiprocessor programming", chapter 16 describing these in |
172 |
|
|
* more detail before proceeding.) The main work-stealing queue |
173 |
|
|
* design is roughly similar to those in the papers "Dynamic |
174 |
|
|
* Circular Work-Stealing Deque" by Chase and Lev, SPAA 2005 |
175 |
|
|
* (http://research.sun.com/scalable/pubs/index.html) and |
176 |
|
|
* "Idempotent work stealing" by Michael, Saraswat, and Vechev, |
177 |
|
|
* PPoPP 2009 (http://portal.acm.org/citation.cfm?id=1504186). |
178 |
dl |
1.200 |
* The main differences ultimately stem from GC requirements that |
179 |
|
|
* we null out taken slots as soon as we can, to maintain as small |
180 |
|
|
* a footprint as possible even in programs generating huge |
181 |
|
|
* numbers of tasks. To accomplish this, we shift the CAS |
182 |
|
|
* arbitrating pop vs poll (steal) from being on the indices |
183 |
|
|
* ("base" and "top") to the slots themselves. |
184 |
|
|
* |
185 |
dl |
1.243 |
* Adding tasks then takes the form of a classic array push(task) |
186 |
|
|
* in a circular buffer: |
187 |
|
|
* q.array[q.top++ % length] = task; |
188 |
dl |
1.200 |
* |
189 |
|
|
* (The actual code needs to null-check and size-check the array, |
190 |
jsr166 |
1.247 |
* uses masking, not mod, for indexing a power-of-two-sized array, |
191 |
dl |
1.243 |
* properly fences accesses, and possibly signals waiting workers |
192 |
|
|
* to start scanning -- see below.) Both a successful pop and |
193 |
|
|
* poll mainly entail a CAS of a slot from non-null to null. |
194 |
dl |
1.200 |
* |
195 |
jsr166 |
1.202 |
* The pop operation (always performed by owner) is: |
196 |
dl |
1.243 |
* if ((the task at top slot is not null) and |
197 |
dl |
1.200 |
* (CAS slot to null)) |
198 |
|
|
* decrement top and return task; |
199 |
|
|
* |
200 |
|
|
* And the poll operation (usually by a stealer) is |
201 |
dl |
1.243 |
* if ((the task at base slot is not null) and |
202 |
dl |
1.200 |
* (CAS slot to null)) |
203 |
|
|
* increment base and return task; |
204 |
|
|
* |
205 |
dl |
1.300 |
* There are several variants of each of these. In particular, |
206 |
|
|
* almost all uses of poll occur within scan operations that also |
207 |
|
|
* interleave contention tracking (with associated code sprawl.) |
208 |
dl |
1.243 |
* |
209 |
|
|
* Memory ordering. See "Correct and Efficient Work-Stealing for |
210 |
|
|
* Weak Memory Models" by Le, Pop, Cohen, and Nardelli, PPoPP 2013 |
211 |
|
|
* (http://www.di.ens.fr/~zappa/readings/ppopp13.pdf) for an |
212 |
|
|
* analysis of memory ordering requirements in work-stealing |
213 |
|
|
* algorithms similar to (but different than) the one used here. |
214 |
|
|
* Extracting tasks in array slots via (fully fenced) CAS provides |
215 |
|
|
* primary synchronization. The base and top indices imprecisely |
216 |
|
|
* guide where to extract from. We do not always require strict |
217 |
|
|
* orderings of array and index updates, so sometimes let them be |
218 |
|
|
* subject to compiler and processor reorderings. However, the |
219 |
|
|
* volatile "base" index also serves as a basis for memory |
220 |
|
|
* ordering: Slot accesses are preceded by a read of base, |
221 |
jsr166 |
1.247 |
* ensuring happens-before ordering with respect to stealers (so |
222 |
dl |
1.243 |
* the slots themselves can be read via plain array reads.) The |
223 |
|
|
* only other memory orderings relied on are maintained in the |
224 |
|
|
* course of signalling and activation (see below). A check that |
225 |
|
|
* base == top indicates (momentary) emptiness, but otherwise may |
226 |
|
|
* err on the side of possibly making the queue appear nonempty |
227 |
|
|
* when a push, pop, or poll have not fully committed, or making |
228 |
|
|
* it appear empty when an update of top has not yet been visibly |
229 |
|
|
* written. (Method isEmpty() checks the case of a partially |
230 |
dl |
1.211 |
* completed removal of the last element.) Because of this, the |
231 |
|
|
* poll operation, considered individually, is not wait-free. One |
232 |
|
|
* thief cannot successfully continue until another in-progress |
233 |
dl |
1.243 |
* one (or, if previously empty, a push) visibly completes. |
234 |
|
|
* However, in the aggregate, we ensure at least probabilistic |
235 |
|
|
* non-blockingness. If an attempted steal fails, a scanning |
236 |
|
|
* thief chooses a different random victim target to try next. So, |
237 |
|
|
* in order for one thief to progress, it suffices for any |
238 |
dl |
1.205 |
* in-progress poll or new push on any empty queue to |
239 |
dl |
1.300 |
* complete. |
240 |
dl |
1.200 |
* |
241 |
|
|
* This approach also enables support of a user mode in which |
242 |
|
|
* local task processing is in FIFO, not LIFO order, simply by |
243 |
|
|
* using poll rather than pop. This can be useful in |
244 |
|
|
* message-passing frameworks in which tasks are never joined. |
245 |
dl |
1.78 |
* |
246 |
|
|
* WorkQueues are also used in a similar way for tasks submitted |
247 |
|
|
* to the pool. We cannot mix these tasks in the same queues used |
248 |
dl |
1.200 |
* by workers. Instead, we randomly associate submission queues |
249 |
dl |
1.83 |
* with submitting threads, using a form of hashing. The |
250 |
dl |
1.139 |
* ThreadLocalRandom probe value serves as a hash code for |
251 |
|
|
* choosing existing queues, and may be randomly repositioned upon |
252 |
|
|
* contention with other submitters. In essence, submitters act |
253 |
|
|
* like workers except that they are restricted to executing local |
254 |
dl |
1.300 |
* tasks that they submitted. Insertion of tasks in shared mode |
255 |
|
|
* requires a lock but we use only a simple spinlock (using field |
256 |
jsr166 |
1.304 |
* phase), because submitters encountering a busy queue move to a |
257 |
|
|
* different position to use or create other queues -- they block |
258 |
|
|
* only when creating and registering new queues. Because it is |
259 |
|
|
* used only as a spinlock, unlocking requires only a "releasing" |
260 |
dl |
1.314 |
* store (using setRelease). |
261 |
dl |
1.78 |
* |
262 |
jsr166 |
1.84 |
* Management |
263 |
dl |
1.78 |
* ========== |
264 |
dl |
1.52 |
* |
265 |
|
|
* The main throughput advantages of work-stealing stem from |
266 |
|
|
* decentralized control -- workers mostly take tasks from |
267 |
dl |
1.200 |
* themselves or each other, at rates that can exceed a billion |
268 |
|
|
* per second. The pool itself creates, activates (enables |
269 |
|
|
* scanning for and running tasks), deactivates, blocks, and |
270 |
|
|
* terminates threads, all with minimal central information. |
271 |
|
|
* There are only a few properties that we can globally track or |
272 |
|
|
* maintain, so we pack them into a small number of variables, |
273 |
|
|
* often maintaining atomicity without blocking or locking. |
274 |
dl |
1.300 |
* Nearly all essentially atomic control state is held in a few |
275 |
dl |
1.200 |
* volatile variables that are by far most often read (not |
276 |
dl |
1.300 |
* written) as status and consistency checks. We pack as much |
277 |
|
|
* information into them as we can. |
278 |
dl |
1.78 |
* |
279 |
dl |
1.200 |
* Field "ctl" contains 64 bits holding information needed to |
280 |
dl |
1.300 |
* atomically decide to add, enqueue (on an event queue), and |
281 |
|
|
* dequeue (and release)-activate workers. To enable this |
282 |
dl |
1.78 |
* packing, we restrict maximum parallelism to (1<<15)-1 (which is |
283 |
|
|
* far in excess of normal operating range) to allow ids, counts, |
284 |
|
|
* and their negations (used for thresholding) to fit into 16bit |
285 |
dl |
1.215 |
* subfields. |
286 |
|
|
* |
287 |
dl |
1.300 |
* Field "mode" holds configuration parameters as well as lifetime |
288 |
|
|
* status, atomically and monotonically setting SHUTDOWN, STOP, |
289 |
|
|
* and finally TERMINATED bits. |
290 |
dl |
1.258 |
* |
291 |
|
|
* Field "workQueues" holds references to WorkQueues. It is |
292 |
dl |
1.300 |
* updated (only during worker creation and termination) under |
293 |
|
|
* lock (using field workerNamePrefix as lock), but is otherwise |
294 |
|
|
* concurrently readable, and accessed directly. We also ensure |
295 |
|
|
* that uses of the array reference itself never become too stale |
296 |
|
|
* in case of resizing. To simplify index-based operations, the |
297 |
|
|
* array size is always a power of two, and all readers must |
298 |
|
|
* tolerate null slots. Worker queues are at odd indices. Shared |
299 |
|
|
* (submission) queues are at even indices, up to a maximum of 64 |
300 |
|
|
* slots, to limit growth even if array needs to expand to add |
301 |
|
|
* more workers. Grouping them together in this way simplifies and |
302 |
dl |
1.262 |
* speeds up task scanning. |
303 |
dl |
1.86 |
* |
304 |
|
|
* All worker thread creation is on-demand, triggered by task |
305 |
|
|
* submissions, replacement of terminated workers, and/or |
306 |
dl |
1.78 |
* compensation for blocked workers. However, all other support |
307 |
|
|
* code is set up to work with other policies. To ensure that we |
308 |
jsr166 |
1.264 |
* do not hold on to worker references that would prevent GC, all |
309 |
dl |
1.78 |
* accesses to workQueues are via indices into the workQueues |
310 |
|
|
* array (which is one source of some of the messy code |
311 |
|
|
* constructions here). In essence, the workQueues array serves as |
312 |
dl |
1.200 |
* a weak reference mechanism. Thus for example the stack top |
313 |
|
|
* subfield of ctl stores indices, not references. |
314 |
|
|
* |
315 |
|
|
* Queuing Idle Workers. Unlike HPC work-stealing frameworks, we |
316 |
|
|
* cannot let workers spin indefinitely scanning for tasks when |
317 |
|
|
* none can be found immediately, and we cannot start/resume |
318 |
|
|
* workers unless there appear to be tasks available. On the |
319 |
|
|
* other hand, we must quickly prod them into action when new |
320 |
|
|
* tasks are submitted or generated. In many usages, ramp-up time |
321 |
dl |
1.300 |
* is the main limiting factor in overall performance, which is |
322 |
|
|
* compounded at program start-up by JIT compilation and |
323 |
|
|
* allocation. So we streamline this as much as possible. |
324 |
|
|
* |
325 |
|
|
* The "ctl" field atomically maintains total worker and |
326 |
|
|
* "released" worker counts, plus the head of the available worker |
327 |
|
|
* queue (actually stack, represented by the lower 32bit subfield |
328 |
|
|
* of ctl). Released workers are those known to be scanning for |
329 |
|
|
* and/or running tasks. Unreleased ("available") workers are |
330 |
|
|
* recorded in the ctl stack. These workers are made available for |
331 |
|
|
* signalling by enqueuing in ctl (see method runWorker). The |
332 |
|
|
* "queue" is a form of Treiber stack. This is ideal for |
333 |
|
|
* activating threads in most-recently used order, and improves |
334 |
dl |
1.200 |
* performance and locality, outweighing the disadvantages of |
335 |
|
|
* being prone to contention and inability to release a worker |
336 |
dl |
1.300 |
* unless it is topmost on stack. To avoid missed signal problems |
337 |
|
|
* inherent in any wait/signal design, available workers rescan |
338 |
|
|
* for (and if found run) tasks after enqueuing. Normally their |
339 |
|
|
* release status will be updated while doing so, but the released |
340 |
|
|
* worker ctl count may underestimate the number of active |
341 |
|
|
* threads. (However, it is still possible to determine quiescence |
342 |
|
|
* via a validation traversal -- see isQuiescent). After an |
343 |
|
|
* unsuccessful rescan, available workers are blocked until |
344 |
|
|
* signalled (see signalWork). The top stack state holds the |
345 |
|
|
* value of the "phase" field of the worker: its index and status, |
346 |
|
|
* plus a version counter that, in addition to the count subfields |
347 |
|
|
* (also serving as version stamps) provide protection against |
348 |
|
|
* Treiber stack ABA effects. |
349 |
dl |
1.200 |
* |
350 |
dl |
1.300 |
* Creating workers. To create a worker, we pre-increment counts |
351 |
|
|
* (serving as a reservation), and attempt to construct a |
352 |
dl |
1.200 |
* ForkJoinWorkerThread via its factory. Upon construction, the |
353 |
|
|
* new thread invokes registerWorker, where it constructs a |
354 |
|
|
* WorkQueue and is assigned an index in the workQueues array |
355 |
jsr166 |
1.266 |
* (expanding the array if necessary). The thread is then started. |
356 |
|
|
* Upon any exception across these steps, or null return from |
357 |
|
|
* factory, deregisterWorker adjusts counts and records |
358 |
dl |
1.200 |
* accordingly. If a null return, the pool continues running with |
359 |
|
|
* fewer than the target number workers. If exceptional, the |
360 |
|
|
* exception is propagated, generally to some external caller. |
361 |
|
|
* Worker index assignment avoids the bias in scanning that would |
362 |
|
|
* occur if entries were sequentially packed starting at the front |
363 |
|
|
* of the workQueues array. We treat the array as a simple |
364 |
|
|
* power-of-two hash table, expanding as needed. The seedIndex |
365 |
|
|
* increment ensures no collisions until a resize is needed or a |
366 |
|
|
* worker is deregistered and replaced, and thereafter keeps |
367 |
jsr166 |
1.202 |
* probability of collision low. We cannot use |
368 |
dl |
1.200 |
* ThreadLocalRandom.getProbe() for similar purposes here because |
369 |
|
|
* the thread has not started yet, but do so for creating |
370 |
dl |
1.243 |
* submission queues for existing external threads (see |
371 |
|
|
* externalPush). |
372 |
|
|
* |
373 |
dl |
1.300 |
* WorkQueue field "phase" is used by both workers and the pool to |
374 |
|
|
* manage and track whether a worker is UNSIGNALLED (possibly |
375 |
|
|
* blocked waiting for a signal). When a worker is enqueued its |
376 |
|
|
* phase field is set. Note that phase field updates lag queue CAS |
377 |
|
|
* releases so usage requires care -- seeing a negative phase does |
378 |
|
|
* not guarantee that the worker is available. When queued, the |
379 |
|
|
* lower 16 bits of scanState must hold its pool index. So we |
380 |
|
|
* place the index there upon initialization (see registerWorker) |
381 |
|
|
* and otherwise keep it there or restore it when necessary. |
382 |
dl |
1.243 |
* |
383 |
|
|
* The ctl field also serves as the basis for memory |
384 |
|
|
* synchronization surrounding activation. This uses a more |
385 |
|
|
* efficient version of a Dekker-like rule that task producers and |
386 |
|
|
* consumers sync with each other by both writing/CASing ctl (even |
387 |
dl |
1.253 |
* if to its current value). This would be extremely costly. So |
388 |
|
|
* we relax it in several ways: (1) Producers only signal when |
389 |
|
|
* their queue is empty. Other workers propagate this signal (in |
390 |
dl |
1.300 |
* method scan) when they find tasks; to further reduce flailing, |
391 |
|
|
* each worker signals only one other per activation. (2) Workers |
392 |
|
|
* only enqueue after scanning (see below) and not finding any |
393 |
|
|
* tasks. (3) Rather than CASing ctl to its current value in the |
394 |
|
|
* common case where no action is required, we reduce write |
395 |
|
|
* contention by equivalently prefacing signalWork when called by |
396 |
|
|
* an external task producer using a memory access with |
397 |
|
|
* full-volatile semantics or a "fullFence". |
398 |
dl |
1.243 |
* |
399 |
jsr166 |
1.249 |
* Almost always, too many signals are issued. A task producer |
400 |
dl |
1.243 |
* cannot in general tell if some existing worker is in the midst |
401 |
|
|
* of finishing one task (or already scanning) and ready to take |
402 |
|
|
* another without being signalled. So the producer might instead |
403 |
|
|
* activate a different worker that does not find any work, and |
404 |
|
|
* then inactivates. This scarcely matters in steady-state |
405 |
|
|
* computations involving all workers, but can create contention |
406 |
jsr166 |
1.247 |
* and bookkeeping bottlenecks during ramp-up, ramp-down, and small |
407 |
dl |
1.243 |
* computations involving only a few workers. |
408 |
|
|
* |
409 |
dl |
1.300 |
* Scanning. Method runWorker performs top-level scanning for |
410 |
|
|
* tasks. Each scan traverses and tries to poll from each queue |
411 |
|
|
* starting at a random index and circularly stepping. Scans are |
412 |
|
|
* not performed in ideal random permutation order, to reduce |
413 |
|
|
* cacheline contention. The pseudorandom generator need not have |
414 |
|
|
* high-quality statistical properties in the long term, but just |
415 |
|
|
* within computations; We use Marsaglia XorShifts (often via |
416 |
|
|
* ThreadLocalRandom.nextSecondarySeed), which are cheap and |
417 |
|
|
* suffice. Scanning also employs contention reduction: When |
418 |
|
|
* scanning workers fail to extract an apparently existing task, |
419 |
|
|
* they soon restart at a different pseudorandom index. This |
420 |
|
|
* improves throughput when many threads are trying to take tasks |
421 |
|
|
* from few queues, which can be common in some usages. Scans do |
422 |
|
|
* not otherwise explicitly take into account core affinities, |
423 |
|
|
* loads, cache localities, etc, However, they do exploit temporal |
424 |
|
|
* locality (which usually approximates these) by preferring to |
425 |
|
|
* re-poll (at most #workers times) from the same queue after a |
426 |
|
|
* successful poll before trying others. |
427 |
dl |
1.52 |
* |
428 |
|
|
* Trimming workers. To release resources after periods of lack of |
429 |
|
|
* use, a worker starting to wait when the pool is quiescent will |
430 |
dl |
1.300 |
* time out and terminate (see method scan) if the pool has |
431 |
|
|
* remained quiescent for period given by field keepAlive. |
432 |
dl |
1.52 |
* |
433 |
dl |
1.210 |
* Shutdown and Termination. A call to shutdownNow invokes |
434 |
|
|
* tryTerminate to atomically set a runState bit. The calling |
435 |
|
|
* thread, as well as every other worker thereafter terminating, |
436 |
dl |
1.300 |
* helps terminate others by cancelling their unprocessed tasks, |
437 |
|
|
* and waking them up, doing so repeatedly until stable. Calls to |
438 |
|
|
* non-abrupt shutdown() preface this by checking whether |
439 |
|
|
* termination should commence by sweeping through queues (until |
440 |
|
|
* stable) to ensure lack of in-flight submissions and workers |
441 |
|
|
* about to process them before triggering the "STOP" phase of |
442 |
|
|
* termination. |
443 |
dl |
1.211 |
* |
444 |
jsr166 |
1.84 |
* Joining Tasks |
445 |
|
|
* ============= |
446 |
dl |
1.78 |
* |
447 |
|
|
* Any of several actions may be taken when one worker is waiting |
448 |
jsr166 |
1.84 |
* to join a task stolen (or always held) by another. Because we |
449 |
dl |
1.78 |
* are multiplexing many tasks on to a pool of workers, we can't |
450 |
dl |
1.300 |
* always just let them block (as in Thread.join). We also cannot |
451 |
|
|
* just reassign the joiner's run-time stack with another and |
452 |
|
|
* replace it later, which would be a form of "continuation", that |
453 |
|
|
* even if possible is not necessarily a good idea since we may |
454 |
|
|
* need both an unblocked task and its continuation to progress. |
455 |
|
|
* Instead we combine two tactics: |
456 |
dl |
1.19 |
* |
457 |
|
|
* Helping: Arranging for the joiner to execute some task that it |
458 |
dl |
1.78 |
* would be running if the steal had not occurred. |
459 |
dl |
1.19 |
* |
460 |
|
|
* Compensating: Unless there are already enough live threads, |
461 |
dl |
1.78 |
* method tryCompensate() may create or re-activate a spare |
462 |
|
|
* thread to compensate for blocked joiners until they unblock. |
463 |
|
|
* |
464 |
dl |
1.105 |
* A third form (implemented in tryRemoveAndExec) amounts to |
465 |
|
|
* helping a hypothetical compensator: If we can readily tell that |
466 |
|
|
* a possible action of a compensator is to steal and execute the |
467 |
|
|
* task being joined, the joining thread can do so directly, |
468 |
dl |
1.300 |
* without the need for a compensation thread. |
469 |
dl |
1.52 |
* |
470 |
|
|
* The ManagedBlocker extension API can't use helping so relies |
471 |
|
|
* only on compensation in method awaitBlocker. |
472 |
dl |
1.19 |
* |
473 |
dl |
1.300 |
* The algorithm in awaitJoin entails a form of "linear helping". |
474 |
|
|
* Each worker records (in field source) the id of the queue from |
475 |
|
|
* which it last stole a task. The scan in method awaitJoin uses |
476 |
|
|
* these markers to try to find a worker to help (i.e., steal back |
477 |
|
|
* a task from and execute it) that could hasten completion of the |
478 |
|
|
* actively joined task. Thus, the joiner executes a task that |
479 |
|
|
* would be on its own local deque if the to-be-joined task had |
480 |
|
|
* not been stolen. This is a conservative variant of the approach |
481 |
|
|
* described in Wagner & Calder "Leapfrogging: a portable |
482 |
|
|
* technique for implementing efficient futures" SIGPLAN Notices, |
483 |
|
|
* 1993 (http://portal.acm.org/citation.cfm?id=155354). It differs |
484 |
|
|
* mainly in that we only record queue ids, not full dependency |
485 |
|
|
* links. This requires a linear scan of the workQueues array to |
486 |
|
|
* locate stealers, but isolates cost to when it is needed, rather |
487 |
|
|
* than adding to per-task overhead. Searches can fail to locate |
488 |
|
|
* stealers GC stalls and the like delay recording sources. |
489 |
|
|
* Further, even when accurately identified, stealers might not |
490 |
|
|
* ever produce a task that the joiner can in turn help with. So, |
491 |
|
|
* compensation is tried upon failure to find tasks to run. |
492 |
dl |
1.105 |
* |
493 |
dl |
1.300 |
* Compensation does not by default aim to keep exactly the target |
494 |
dl |
1.200 |
* parallelism number of unblocked threads running at any given |
495 |
|
|
* time. Some previous versions of this class employed immediate |
496 |
|
|
* compensations for any blocked join. However, in practice, the |
497 |
|
|
* vast majority of blockages are transient byproducts of GC and |
498 |
|
|
* other JVM or OS activities that are made worse by replacement. |
499 |
dl |
1.300 |
* Rather than impose arbitrary policies, we allow users to |
500 |
|
|
* override the default of only adding threads upon apparent |
501 |
|
|
* starvation. The compensation mechanism may also be bounded. |
502 |
|
|
* Bounds for the commonPool (see COMMON_MAX_SPARES) better enable |
503 |
|
|
* JVMs to cope with programming errors and abuse before running |
504 |
|
|
* out of resources to do so. |
505 |
jsr166 |
1.301 |
* |
506 |
dl |
1.105 |
* Common Pool |
507 |
|
|
* =========== |
508 |
|
|
* |
509 |
jsr166 |
1.175 |
* The static common pool always exists after static |
510 |
dl |
1.105 |
* initialization. Since it (or any other created pool) need |
511 |
|
|
* never be used, we minimize initial construction overhead and |
512 |
dl |
1.300 |
* footprint to the setup of about a dozen fields. |
513 |
dl |
1.105 |
* |
514 |
|
|
* When external threads submit to the common pool, they can |
515 |
dl |
1.200 |
* perform subtask processing (see externalHelpComplete and |
516 |
|
|
* related methods) upon joins. This caller-helps policy makes it |
517 |
|
|
* sensible to set common pool parallelism level to one (or more) |
518 |
|
|
* less than the total number of available cores, or even zero for |
519 |
|
|
* pure caller-runs. We do not need to record whether external |
520 |
|
|
* submissions are to the common pool -- if not, external help |
521 |
|
|
* methods return quickly. These submitters would otherwise be |
522 |
|
|
* blocked waiting for completion, so the extra effort (with |
523 |
|
|
* liberally sprinkled task status checks) in inapplicable cases |
524 |
|
|
* amounts to an odd form of limited spin-wait before blocking in |
525 |
|
|
* ForkJoinTask.join. |
526 |
dl |
1.105 |
* |
527 |
dl |
1.197 |
* As a more appropriate default in managed environments, unless |
528 |
|
|
* overridden by system properties, we use workers of subclass |
529 |
|
|
* InnocuousForkJoinWorkerThread when there is a SecurityManager |
530 |
|
|
* present. These workers have no permissions set, do not belong |
531 |
|
|
* to any user-defined ThreadGroup, and erase all ThreadLocals |
532 |
dl |
1.300 |
* after executing any top-level task (see |
533 |
|
|
* WorkQueue.afterTopLevelExec). The associated mechanics (mainly |
534 |
|
|
* in ForkJoinWorkerThread) may be JVM-dependent and must access |
535 |
|
|
* particular Thread class fields to achieve this effect. |
536 |
jsr166 |
1.198 |
* |
537 |
dl |
1.105 |
* Style notes |
538 |
|
|
* =========== |
539 |
|
|
* |
540 |
jsr166 |
1.315 |
* Memory ordering relies mainly on VarHandles. This can be |
541 |
|
|
* awkward and ugly, but also reflects the need to control |
542 |
|
|
* outcomes across the unusual cases that arise in very racy code |
543 |
dl |
1.319 |
* with very few invariants. All fields are read into locals |
544 |
|
|
* before use, and null-checked if they are references. This is |
545 |
|
|
* usually done in a "C"-like style of listing declarations at the |
546 |
|
|
* heads of methods or blocks, and using inline assignments on |
547 |
|
|
* first encounter. Nearly all explicit checks lead to |
548 |
|
|
* bypass/return, not exception throws, because they may |
549 |
|
|
* legitimately arise due to cancellation/revocation during |
550 |
|
|
* shutdown. |
551 |
dl |
1.200 |
* |
552 |
dl |
1.105 |
* There is a lot of representation-level coupling among classes |
553 |
|
|
* ForkJoinPool, ForkJoinWorkerThread, and ForkJoinTask. The |
554 |
|
|
* fields of WorkQueue maintain data structures managed by |
555 |
|
|
* ForkJoinPool, so are directly accessed. There is little point |
556 |
|
|
* trying to reduce this, since any associated future changes in |
557 |
|
|
* representations will need to be accompanied by algorithmic |
558 |
|
|
* changes anyway. Several methods intrinsically sprawl because |
559 |
dl |
1.200 |
* they must accumulate sets of consistent reads of fields held in |
560 |
|
|
* local variables. There are also other coding oddities |
561 |
|
|
* (including several unnecessary-looking hoisted null checks) |
562 |
|
|
* that help some methods perform reasonably even when interpreted |
563 |
|
|
* (not compiled). |
564 |
dl |
1.52 |
* |
565 |
dl |
1.208 |
* The order of declarations in this file is (with a few exceptions): |
566 |
dl |
1.86 |
* (1) Static utility functions |
567 |
|
|
* (2) Nested (static) classes |
568 |
|
|
* (3) Static fields |
569 |
|
|
* (4) Fields, along with constants used when unpacking some of them |
570 |
|
|
* (5) Internal control methods |
571 |
|
|
* (6) Callbacks and other support for ForkJoinTask methods |
572 |
|
|
* (7) Exported methods |
573 |
|
|
* (8) Static block initializing statics in minimally dependent order |
574 |
|
|
*/ |
575 |
|
|
|
576 |
|
|
// Static utilities |
577 |
|
|
|
578 |
|
|
/** |
579 |
|
|
* If there is a security manager, makes sure caller has |
580 |
|
|
* permission to modify threads. |
581 |
jsr166 |
1.1 |
*/ |
582 |
dl |
1.86 |
private static void checkPermission() { |
583 |
|
|
SecurityManager security = System.getSecurityManager(); |
584 |
|
|
if (security != null) |
585 |
|
|
security.checkPermission(modifyThreadPermission); |
586 |
|
|
} |
587 |
|
|
|
588 |
|
|
// Nested classes |
589 |
jsr166 |
1.1 |
|
590 |
|
|
/** |
591 |
jsr166 |
1.8 |
* Factory for creating new {@link ForkJoinWorkerThread}s. |
592 |
|
|
* A {@code ForkJoinWorkerThreadFactory} must be defined and used |
593 |
|
|
* for {@code ForkJoinWorkerThread} subclasses that extend base |
594 |
|
|
* functionality or initialize threads with different contexts. |
595 |
jsr166 |
1.1 |
*/ |
596 |
|
|
public static interface ForkJoinWorkerThreadFactory { |
597 |
|
|
/** |
598 |
|
|
* Returns a new worker thread operating in the given pool. |
599 |
dl |
1.300 |
* Returning null or throwing an exception may result in tasks |
600 |
|
|
* never being executed. If this method throws an exception, |
601 |
|
|
* it is relayed to the caller of the method (for example |
602 |
|
|
* {@code execute}) causing attempted thread creation. If this |
603 |
|
|
* method returns null or throws an exception, it is not |
604 |
|
|
* retried until the next attempted creation (for example |
605 |
|
|
* another call to {@code execute}). |
606 |
jsr166 |
1.1 |
* |
607 |
|
|
* @param pool the pool this thread works in |
608 |
jsr166 |
1.296 |
* @return the new worker thread, or {@code null} if the request |
609 |
dl |
1.300 |
* to create a thread is rejected. |
610 |
jsr166 |
1.11 |
* @throws NullPointerException if the pool is null |
611 |
jsr166 |
1.1 |
*/ |
612 |
|
|
public ForkJoinWorkerThread newThread(ForkJoinPool pool); |
613 |
|
|
} |
614 |
|
|
|
615 |
|
|
/** |
616 |
|
|
* Default ForkJoinWorkerThreadFactory implementation; creates a |
617 |
|
|
* new ForkJoinWorkerThread. |
618 |
|
|
*/ |
619 |
jsr166 |
1.278 |
private static final class DefaultForkJoinWorkerThreadFactory |
620 |
jsr166 |
1.1 |
implements ForkJoinWorkerThreadFactory { |
621 |
dl |
1.112 |
public final ForkJoinWorkerThread newThread(ForkJoinPool pool) { |
622 |
dl |
1.14 |
return new ForkJoinWorkerThread(pool); |
623 |
jsr166 |
1.1 |
} |
624 |
|
|
} |
625 |
|
|
|
626 |
dl |
1.200 |
// Constants shared across ForkJoinPool and WorkQueue |
627 |
|
|
|
628 |
|
|
// Bounds |
629 |
dl |
1.300 |
static final int SWIDTH = 16; // width of short |
630 |
dl |
1.200 |
static final int SMASK = 0xffff; // short bits == max index |
631 |
|
|
static final int MAX_CAP = 0x7fff; // max #workers - 1 |
632 |
|
|
static final int SQMASK = 0x007e; // max 64 (even) slots |
633 |
|
|
|
634 |
dl |
1.300 |
// Masks and units for WorkQueue.phase and ctl sp subfield |
635 |
dl |
1.243 |
static final int UNSIGNALLED = 1 << 31; // must be negative |
636 |
dl |
1.211 |
static final int SS_SEQ = 1 << 16; // version count |
637 |
dl |
1.300 |
static final int QLOCK = 1; // must be 1 |
638 |
dl |
1.200 |
|
639 |
dl |
1.300 |
// Mode bits and sentinels, some also used in WorkQueue id and.source fields |
640 |
|
|
static final int OWNED = 1; // queue has owner thread |
641 |
|
|
static final int FIFO = 1 << 16; // fifo queue or access mode |
642 |
|
|
static final int SHUTDOWN = 1 << 18; |
643 |
|
|
static final int TERMINATED = 1 << 19; |
644 |
|
|
static final int STOP = 1 << 31; // must be negative |
645 |
|
|
static final int QUIET = 1 << 30; // not scanning or working |
646 |
|
|
static final int DORMANT = QUIET | UNSIGNALLED; |
647 |
|
|
|
648 |
|
|
/** |
649 |
|
|
* The maximum number of local polls from the same queue before |
650 |
|
|
* checking others. This is a safeguard against infinitely unfair |
651 |
|
|
* looping under unbounded user task recursion, and must be larger |
652 |
|
|
* than plausible cases of intentional bounded task recursion. |
653 |
dl |
1.253 |
*/ |
654 |
dl |
1.300 |
static final int POLL_LIMIT = 1 << 10; |
655 |
dl |
1.253 |
|
656 |
|
|
/** |
657 |
dl |
1.78 |
* Queues supporting work-stealing as well as external task |
658 |
jsr166 |
1.202 |
* submission. See above for descriptions and algorithms. |
659 |
dl |
1.78 |
* Performance on most platforms is very sensitive to placement of |
660 |
|
|
* instances of both WorkQueues and their arrays -- we absolutely |
661 |
|
|
* do not want multiple WorkQueue instances or multiple queue |
662 |
dl |
1.200 |
* arrays sharing cache lines. The @Contended annotation alerts |
663 |
|
|
* JVMs to try to keep instances apart. |
664 |
dl |
1.78 |
*/ |
665 |
dl |
1.308 |
@jdk.internal.vm.annotation.Contended |
666 |
dl |
1.78 |
static final class WorkQueue { |
667 |
dl |
1.200 |
|
668 |
dl |
1.78 |
/** |
669 |
|
|
* Capacity of work-stealing queue array upon initialization. |
670 |
dl |
1.90 |
* Must be a power of two; at least 4, but should be larger to |
671 |
|
|
* reduce or eliminate cacheline sharing among queues. |
672 |
|
|
* Currently, it is much larger, as a partial workaround for |
673 |
|
|
* the fact that JVMs often place arrays in locations that |
674 |
|
|
* share GC bookkeeping (especially cardmarks) such that |
675 |
|
|
* per-write accesses encounter serious memory contention. |
676 |
dl |
1.78 |
*/ |
677 |
dl |
1.90 |
static final int INITIAL_QUEUE_CAPACITY = 1 << 13; |
678 |
dl |
1.78 |
|
679 |
|
|
/** |
680 |
|
|
* Maximum size for queue arrays. Must be a power of two less |
681 |
|
|
* than or equal to 1 << (31 - width of array entry) to ensure |
682 |
|
|
* lack of wraparound of index calculations, but defined to a |
683 |
|
|
* value a bit less than this to help users trap runaway |
684 |
|
|
* programs before saturating systems. |
685 |
|
|
*/ |
686 |
|
|
static final int MAXIMUM_QUEUE_CAPACITY = 1 << 26; // 64M |
687 |
|
|
|
688 |
dl |
1.200 |
// Instance fields |
689 |
dl |
1.300 |
volatile int phase; // versioned, negative: queued, 1: locked |
690 |
|
|
int stackPred; // pool stack (ctl) predecessor link |
691 |
dl |
1.178 |
int nsteals; // number of steals |
692 |
dl |
1.300 |
int id; // index, mode, tag |
693 |
|
|
volatile int source; // source queue id, or sentinel |
694 |
dl |
1.78 |
volatile int base; // index of next slot for poll |
695 |
|
|
int top; // index of next slot for push |
696 |
|
|
ForkJoinTask<?>[] array; // the elements (initially unallocated) |
697 |
dl |
1.90 |
final ForkJoinPool pool; // the containing pool (may be null) |
698 |
dl |
1.78 |
final ForkJoinWorkerThread owner; // owning thread or null if shared |
699 |
dl |
1.112 |
|
700 |
dl |
1.200 |
WorkQueue(ForkJoinPool pool, ForkJoinWorkerThread owner) { |
701 |
dl |
1.90 |
this.pool = pool; |
702 |
dl |
1.78 |
this.owner = owner; |
703 |
dl |
1.115 |
// Place indices in the center of array (that is not yet allocated) |
704 |
dl |
1.78 |
base = top = INITIAL_QUEUE_CAPACITY >>> 1; |
705 |
|
|
} |
706 |
|
|
|
707 |
|
|
/** |
708 |
jsr166 |
1.220 |
* Returns an exportable index (used by ForkJoinWorkerThread). |
709 |
dl |
1.200 |
*/ |
710 |
|
|
final int getPoolIndex() { |
711 |
dl |
1.300 |
return (id & 0xffff) >>> 1; // ignore odd/even tag bit |
712 |
dl |
1.200 |
} |
713 |
|
|
|
714 |
|
|
/** |
715 |
dl |
1.115 |
* Returns the approximate number of tasks in the queue. |
716 |
|
|
*/ |
717 |
|
|
final int queueSize() { |
718 |
dl |
1.243 |
int n = base - top; // read base first |
719 |
dl |
1.115 |
return (n >= 0) ? 0 : -n; // ignore transient negative |
720 |
|
|
} |
721 |
|
|
|
722 |
jsr166 |
1.180 |
/** |
723 |
dl |
1.115 |
* Provides a more accurate estimate of whether this queue has |
724 |
|
|
* any tasks than does queueSize, by checking whether a |
725 |
|
|
* near-empty queue has at least one unclaimed task. |
726 |
|
|
*/ |
727 |
|
|
final boolean isEmpty() { |
728 |
dl |
1.300 |
ForkJoinTask<?>[] a; int n, al, b; |
729 |
|
|
return ((n = (b = base) - top) >= 0 || // possibly one task |
730 |
dl |
1.243 |
(n == -1 && ((a = array) == null || |
731 |
|
|
(al = a.length) == 0 || |
732 |
dl |
1.300 |
a[(al - 1) & b] == null))); |
733 |
dl |
1.115 |
} |
734 |
|
|
|
735 |
dl |
1.300 |
|
736 |
dl |
1.115 |
/** |
737 |
dl |
1.256 |
* Pushes a task. Call only by owner in unshared queues. |
738 |
dl |
1.78 |
* |
739 |
|
|
* @param task the task. Caller must ensure non-null. |
740 |
jsr166 |
1.146 |
* @throws RejectedExecutionException if array cannot be resized |
741 |
dl |
1.78 |
*/ |
742 |
dl |
1.90 |
final void push(ForkJoinTask<?> task) { |
743 |
dl |
1.300 |
int s = top; ForkJoinTask<?>[] a; int al, d; |
744 |
dl |
1.243 |
if ((a = array) != null && (al = a.length) > 0) { |
745 |
dl |
1.300 |
int index = (al - 1) & s; |
746 |
|
|
ForkJoinPool p = pool; |
747 |
dl |
1.243 |
top = s + 1; |
748 |
dl |
1.314 |
QA.setRelease(a, index, task); |
749 |
dl |
1.292 |
if ((d = base - s) == 0 && p != null) { |
750 |
dl |
1.314 |
VarHandle.fullFence(); |
751 |
dl |
1.253 |
p.signalWork(); |
752 |
dl |
1.284 |
} |
753 |
dl |
1.300 |
else if (d + al == 1) |
754 |
dl |
1.243 |
growArray(); |
755 |
dl |
1.78 |
} |
756 |
|
|
} |
757 |
|
|
|
758 |
dl |
1.178 |
/** |
759 |
dl |
1.112 |
* Initializes or doubles the capacity of array. Call either |
760 |
|
|
* by owner or with lock held -- it is OK for base, but not |
761 |
|
|
* top, to move while resizings are in progress. |
762 |
|
|
*/ |
763 |
|
|
final ForkJoinTask<?>[] growArray() { |
764 |
|
|
ForkJoinTask<?>[] oldA = array; |
765 |
dl |
1.300 |
int oldSize = oldA != null ? oldA.length : 0; |
766 |
|
|
int size = oldSize > 0 ? oldSize << 1 : INITIAL_QUEUE_CAPACITY; |
767 |
dl |
1.225 |
if (size < INITIAL_QUEUE_CAPACITY || size > MAXIMUM_QUEUE_CAPACITY) |
768 |
dl |
1.112 |
throw new RejectedExecutionException("Queue capacity exceeded"); |
769 |
|
|
int oldMask, t, b; |
770 |
|
|
ForkJoinTask<?>[] a = array = new ForkJoinTask<?>[size]; |
771 |
dl |
1.300 |
if (oldA != null && (oldMask = oldSize - 1) > 0 && |
772 |
dl |
1.112 |
(t = top) - (b = base) > 0) { |
773 |
|
|
int mask = size - 1; |
774 |
dl |
1.200 |
do { // emulate poll from old array, push to new array |
775 |
dl |
1.256 |
int index = b & oldMask; |
776 |
dl |
1.243 |
ForkJoinTask<?> x = (ForkJoinTask<?>) |
777 |
dl |
1.316 |
QA.getAcquire(oldA, index); |
778 |
dl |
1.243 |
if (x != null && |
779 |
dl |
1.314 |
QA.compareAndSet(oldA, index, x, null)) |
780 |
dl |
1.243 |
a[b & mask] = x; |
781 |
dl |
1.112 |
} while (++b != t); |
782 |
dl |
1.314 |
VarHandle.releaseFence(); |
783 |
dl |
1.78 |
} |
784 |
dl |
1.112 |
return a; |
785 |
dl |
1.78 |
} |
786 |
|
|
|
787 |
|
|
/** |
788 |
dl |
1.90 |
* Takes next task, if one exists, in LIFO order. Call only |
789 |
dl |
1.102 |
* by owner in unshared queues. |
790 |
dl |
1.90 |
*/ |
791 |
|
|
final ForkJoinTask<?> pop() { |
792 |
dl |
1.243 |
int b = base, s = top, al, i; ForkJoinTask<?>[] a; |
793 |
dl |
1.256 |
if ((a = array) != null && b != s && (al = a.length) > 0) { |
794 |
|
|
int index = (al - 1) & --s; |
795 |
dl |
1.262 |
ForkJoinTask<?> t = (ForkJoinTask<?>) |
796 |
dl |
1.314 |
QA.get(a, index); |
797 |
dl |
1.262 |
if (t != null && |
798 |
dl |
1.314 |
QA.compareAndSet(a, index, t, null)) { |
799 |
dl |
1.256 |
top = s; |
800 |
dl |
1.314 |
VarHandle.releaseFence(); |
801 |
dl |
1.78 |
return t; |
802 |
|
|
} |
803 |
|
|
} |
804 |
|
|
return null; |
805 |
|
|
} |
806 |
|
|
|
807 |
|
|
/** |
808 |
dl |
1.90 |
* Takes next task, if one exists, in FIFO order. |
809 |
dl |
1.78 |
*/ |
810 |
dl |
1.90 |
final ForkJoinTask<?> poll() { |
811 |
dl |
1.243 |
for (;;) { |
812 |
|
|
int b = base, s = top, d, al; ForkJoinTask<?>[] a; |
813 |
|
|
if ((a = array) != null && (d = b - s) < 0 && |
814 |
|
|
(al = a.length) > 0) { |
815 |
dl |
1.256 |
int index = (al - 1) & b; |
816 |
dl |
1.243 |
ForkJoinTask<?> t = (ForkJoinTask<?>) |
817 |
dl |
1.316 |
QA.getAcquire(a, index); |
818 |
dl |
1.243 |
if (b++ == base) { |
819 |
|
|
if (t != null) { |
820 |
dl |
1.314 |
if (QA.compareAndSet(a, index, t, null)) { |
821 |
dl |
1.243 |
base = b; |
822 |
|
|
return t; |
823 |
|
|
} |
824 |
dl |
1.200 |
} |
825 |
dl |
1.243 |
else if (d == -1) |
826 |
|
|
break; // now empty |
827 |
dl |
1.78 |
} |
828 |
dl |
1.90 |
} |
829 |
dl |
1.243 |
else |
830 |
|
|
break; |
831 |
dl |
1.78 |
} |
832 |
|
|
return null; |
833 |
|
|
} |
834 |
|
|
|
835 |
|
|
/** |
836 |
|
|
* Takes next task, if one exists, in order specified by mode. |
837 |
|
|
*/ |
838 |
|
|
final ForkJoinTask<?> nextLocalTask() { |
839 |
dl |
1.300 |
return ((id & FIFO) != 0) ? poll() : pop(); |
840 |
dl |
1.78 |
} |
841 |
|
|
|
842 |
|
|
/** |
843 |
|
|
* Returns next task, if one exists, in order specified by mode. |
844 |
|
|
*/ |
845 |
|
|
final ForkJoinTask<?> peek() { |
846 |
dl |
1.292 |
int al; ForkJoinTask<?>[] a; |
847 |
dl |
1.243 |
return ((a = array) != null && (al = a.length) > 0) ? |
848 |
dl |
1.300 |
a[(al - 1) & |
849 |
|
|
((id & FIFO) != 0 ? base : top - 1)] : null; |
850 |
dl |
1.78 |
} |
851 |
|
|
|
852 |
|
|
/** |
853 |
|
|
* Pops the given task only if it is at the current top. |
854 |
jsr166 |
1.251 |
*/ |
855 |
dl |
1.243 |
final boolean tryUnpush(ForkJoinTask<?> task) { |
856 |
dl |
1.256 |
int b = base, s = top, al; ForkJoinTask<?>[] a; |
857 |
|
|
if ((a = array) != null && b != s && (al = a.length) > 0) { |
858 |
|
|
int index = (al - 1) & --s; |
859 |
dl |
1.314 |
if (QA.compareAndSet(a, index, task, null)) { |
860 |
dl |
1.243 |
top = s; |
861 |
dl |
1.314 |
VarHandle.releaseFence(); |
862 |
dl |
1.224 |
return true; |
863 |
|
|
} |
864 |
dl |
1.78 |
} |
865 |
|
|
return false; |
866 |
|
|
} |
867 |
|
|
|
868 |
|
|
/** |
869 |
jsr166 |
1.84 |
* Removes and cancels all known tasks, ignoring any exceptions. |
870 |
dl |
1.78 |
*/ |
871 |
|
|
final void cancelAll() { |
872 |
dl |
1.300 |
for (ForkJoinTask<?> t; (t = poll()) != null; ) |
873 |
dl |
1.78 |
ForkJoinTask.cancelIgnoringExceptions(t); |
874 |
|
|
} |
875 |
|
|
|
876 |
dl |
1.104 |
// Specialized execution methods |
877 |
dl |
1.78 |
|
878 |
|
|
/** |
879 |
dl |
1.300 |
* Pops and executes up to limit consecutive tasks or until empty. |
880 |
|
|
* |
881 |
|
|
* @param limit max runs, or zero for no limit |
882 |
dl |
1.253 |
*/ |
883 |
dl |
1.300 |
final void localPopAndExec(int limit) { |
884 |
|
|
for (;;) { |
885 |
dl |
1.253 |
int b = base, s = top, al; ForkJoinTask<?>[] a; |
886 |
|
|
if ((a = array) != null && b != s && (al = a.length) > 0) { |
887 |
dl |
1.256 |
int index = (al - 1) & --s; |
888 |
dl |
1.253 |
ForkJoinTask<?> t = (ForkJoinTask<?>) |
889 |
dl |
1.314 |
QA.getAndSet(a, index, null); |
890 |
dl |
1.253 |
if (t != null) { |
891 |
|
|
top = s; |
892 |
dl |
1.314 |
VarHandle.releaseFence(); |
893 |
dl |
1.300 |
t.doExec(); |
894 |
|
|
if (limit != 0 && --limit == 0) |
895 |
dl |
1.253 |
break; |
896 |
|
|
} |
897 |
|
|
else |
898 |
|
|
break; |
899 |
|
|
} |
900 |
|
|
else |
901 |
|
|
break; |
902 |
|
|
} |
903 |
|
|
} |
904 |
|
|
|
905 |
|
|
/** |
906 |
dl |
1.300 |
* Polls and executes up to limit consecutive tasks or until empty. |
907 |
|
|
* |
908 |
|
|
* @param limit, or zero for no limit |
909 |
dl |
1.253 |
*/ |
910 |
dl |
1.300 |
final void localPollAndExec(int limit) { |
911 |
|
|
for (int polls = 0;;) { |
912 |
|
|
int b = base, s = top, d, al; ForkJoinTask<?>[] a; |
913 |
|
|
if ((a = array) != null && (d = b - s) < 0 && |
914 |
|
|
(al = a.length) > 0) { |
915 |
dl |
1.256 |
int index = (al - 1) & b++; |
916 |
dl |
1.253 |
ForkJoinTask<?> t = (ForkJoinTask<?>) |
917 |
dl |
1.314 |
QA.getAndSet(a, index, null); |
918 |
dl |
1.253 |
if (t != null) { |
919 |
|
|
base = b; |
920 |
dl |
1.255 |
t.doExec(); |
921 |
dl |
1.300 |
if (limit != 0 && ++polls == limit) |
922 |
dl |
1.253 |
break; |
923 |
|
|
} |
924 |
dl |
1.300 |
else if (d == -1) |
925 |
|
|
break; // now empty |
926 |
|
|
else |
927 |
|
|
polls = 0; // stolen; reset |
928 |
dl |
1.253 |
} |
929 |
|
|
else |
930 |
|
|
break; |
931 |
|
|
} |
932 |
|
|
} |
933 |
|
|
|
934 |
|
|
/** |
935 |
jsr166 |
1.302 |
* If present, removes task from queue and executes it. |
936 |
dl |
1.94 |
*/ |
937 |
dl |
1.300 |
final void tryRemoveAndExec(ForkJoinTask<?> task) { |
938 |
|
|
ForkJoinTask<?>[] wa; int s, wal; |
939 |
|
|
if (base - (s = top) < 0 && // traverse from top |
940 |
|
|
(wa = array) != null && (wal = wa.length) > 0) { |
941 |
|
|
for (int m = wal - 1, ns = s - 1, i = ns; ; --i) { |
942 |
|
|
int index = i & m; |
943 |
|
|
ForkJoinTask<?> t = (ForkJoinTask<?>) |
944 |
dl |
1.314 |
QA.get(wa, index); |
945 |
dl |
1.300 |
if (t == null) |
946 |
|
|
break; |
947 |
|
|
else if (t == task) { |
948 |
dl |
1.314 |
if (QA.compareAndSet(wa, index, t, null)) { |
949 |
dl |
1.300 |
top = ns; // safely shift down |
950 |
|
|
for (int j = i; j != ns; ++j) { |
951 |
|
|
ForkJoinTask<?> f; |
952 |
|
|
int pindex = (j + 1) & m; |
953 |
dl |
1.314 |
f = (ForkJoinTask<?>)QA.get(wa, pindex); |
954 |
|
|
QA.setVolatile(wa, pindex, null); |
955 |
dl |
1.300 |
int jindex = j & m; |
956 |
dl |
1.314 |
QA.setRelease(wa, jindex, f); |
957 |
dl |
1.300 |
} |
958 |
dl |
1.314 |
VarHandle.releaseFence(); |
959 |
dl |
1.300 |
t.doExec(); |
960 |
|
|
} |
961 |
|
|
break; |
962 |
|
|
} |
963 |
dl |
1.262 |
} |
964 |
dl |
1.215 |
} |
965 |
|
|
} |
966 |
|
|
|
967 |
|
|
/** |
968 |
dl |
1.300 |
* Tries to steal and run tasks within the target's |
969 |
jsr166 |
1.302 |
* computation until done, not found, or limit exceeded. |
970 |
dl |
1.94 |
* |
971 |
dl |
1.300 |
* @param task root of CountedCompleter computation |
972 |
|
|
* @param limit max runs, or zero for no limit |
973 |
|
|
* @return task status on exit |
974 |
|
|
*/ |
975 |
|
|
final int localHelpCC(CountedCompleter<?> task, int limit) { |
976 |
|
|
int status = 0; |
977 |
|
|
if (task != null && (status = task.status) >= 0) { |
978 |
|
|
for (;;) { |
979 |
|
|
boolean help = false; |
980 |
|
|
int b = base, s = top, al; ForkJoinTask<?>[] a; |
981 |
|
|
if ((a = array) != null && b != s && (al = a.length) > 0) { |
982 |
|
|
int index = (al - 1) & (s - 1); |
983 |
|
|
ForkJoinTask<?> o = (ForkJoinTask<?>) |
984 |
dl |
1.314 |
QA.get(a, index); |
985 |
dl |
1.300 |
if (o instanceof CountedCompleter) { |
986 |
|
|
CountedCompleter<?> t = (CountedCompleter<?>)o; |
987 |
|
|
for (CountedCompleter<?> f = t;;) { |
988 |
|
|
if (f != task) { |
989 |
|
|
if ((f = f.completer) == null) // try parent |
990 |
|
|
break; |
991 |
|
|
} |
992 |
|
|
else { |
993 |
dl |
1.314 |
if (QA.compareAndSet(a, index, t, null)) { |
994 |
dl |
1.300 |
top = s - 1; |
995 |
dl |
1.314 |
VarHandle.releaseFence(); |
996 |
dl |
1.300 |
t.doExec(); |
997 |
|
|
help = true; |
998 |
|
|
} |
999 |
|
|
break; |
1000 |
dl |
1.200 |
} |
1001 |
|
|
} |
1002 |
dl |
1.243 |
} |
1003 |
dl |
1.104 |
} |
1004 |
dl |
1.300 |
if ((status = task.status) < 0 || !help || |
1005 |
|
|
(limit != 0 && --limit == 0)) |
1006 |
|
|
break; |
1007 |
dl |
1.104 |
} |
1008 |
|
|
} |
1009 |
dl |
1.300 |
return status; |
1010 |
|
|
} |
1011 |
|
|
|
1012 |
|
|
// Operations on shared queues |
1013 |
|
|
|
1014 |
|
|
/** |
1015 |
jsr166 |
1.302 |
* Tries to lock shared queue by CASing phase field. |
1016 |
dl |
1.300 |
*/ |
1017 |
|
|
final boolean tryLockSharedQueue() { |
1018 |
dl |
1.314 |
return PHASE.compareAndSet(this, 0, QLOCK); |
1019 |
dl |
1.104 |
} |
1020 |
|
|
|
1021 |
|
|
/** |
1022 |
dl |
1.300 |
* Shared version of tryUnpush. |
1023 |
dl |
1.78 |
*/ |
1024 |
dl |
1.300 |
final boolean trySharedUnpush(ForkJoinTask<?> task) { |
1025 |
|
|
boolean popped = false; |
1026 |
|
|
int s = top - 1, al; ForkJoinTask<?>[] a; |
1027 |
|
|
if ((a = array) != null && (al = a.length) > 0) { |
1028 |
|
|
int index = (al - 1) & s; |
1029 |
dl |
1.314 |
ForkJoinTask<?> t = (ForkJoinTask<?>) QA.get(a, index); |
1030 |
dl |
1.300 |
if (t == task && |
1031 |
dl |
1.314 |
PHASE.compareAndSet(this, 0, QLOCK)) { |
1032 |
dl |
1.300 |
if (top == s + 1 && array == a && |
1033 |
dl |
1.314 |
QA.compareAndSet(a, index, task, null)) { |
1034 |
dl |
1.300 |
popped = true; |
1035 |
|
|
top = s; |
1036 |
dl |
1.178 |
} |
1037 |
dl |
1.314 |
PHASE.setRelease(this, 0); |
1038 |
dl |
1.94 |
} |
1039 |
dl |
1.78 |
} |
1040 |
dl |
1.300 |
return popped; |
1041 |
dl |
1.78 |
} |
1042 |
|
|
|
1043 |
|
|
/** |
1044 |
dl |
1.300 |
* Shared version of localHelpCC. |
1045 |
|
|
*/ |
1046 |
|
|
final int sharedHelpCC(CountedCompleter<?> task, int limit) { |
1047 |
|
|
int status = 0; |
1048 |
|
|
if (task != null && (status = task.status) >= 0) { |
1049 |
|
|
for (;;) { |
1050 |
|
|
boolean help = false; |
1051 |
|
|
int b = base, s = top, al; ForkJoinTask<?>[] a; |
1052 |
|
|
if ((a = array) != null && b != s && (al = a.length) > 0) { |
1053 |
|
|
int index = (al - 1) & (s - 1); |
1054 |
|
|
ForkJoinTask<?> o = (ForkJoinTask<?>) |
1055 |
dl |
1.314 |
QA.get(a, index); |
1056 |
dl |
1.300 |
if (o instanceof CountedCompleter) { |
1057 |
|
|
CountedCompleter<?> t = (CountedCompleter<?>)o; |
1058 |
|
|
for (CountedCompleter<?> f = t;;) { |
1059 |
|
|
if (f != task) { |
1060 |
|
|
if ((f = f.completer) == null) |
1061 |
|
|
break; |
1062 |
|
|
} |
1063 |
|
|
else { |
1064 |
dl |
1.314 |
if (PHASE.compareAndSet(this, 0, QLOCK)) { |
1065 |
dl |
1.300 |
if (top == s && array == a && |
1066 |
dl |
1.314 |
QA.compareAndSet(a, index, t, null)) { |
1067 |
dl |
1.300 |
help = true; |
1068 |
|
|
top = s - 1; |
1069 |
|
|
} |
1070 |
dl |
1.314 |
PHASE.setRelease(this, 0); |
1071 |
dl |
1.300 |
if (help) |
1072 |
|
|
t.doExec(); |
1073 |
|
|
} |
1074 |
|
|
break; |
1075 |
|
|
} |
1076 |
dl |
1.243 |
} |
1077 |
dl |
1.200 |
} |
1078 |
dl |
1.178 |
} |
1079 |
dl |
1.300 |
if ((status = task.status) < 0 || !help || |
1080 |
|
|
(limit != 0 && --limit == 0)) |
1081 |
|
|
break; |
1082 |
dl |
1.178 |
} |
1083 |
dl |
1.78 |
} |
1084 |
dl |
1.300 |
return status; |
1085 |
dl |
1.78 |
} |
1086 |
|
|
|
1087 |
|
|
/** |
1088 |
dl |
1.86 |
* Returns true if owned and not known to be blocked. |
1089 |
|
|
*/ |
1090 |
|
|
final boolean isApparentlyUnblocked() { |
1091 |
|
|
Thread wt; Thread.State s; |
1092 |
dl |
1.300 |
return ((wt = owner) != null && |
1093 |
dl |
1.86 |
(s = wt.getState()) != Thread.State.BLOCKED && |
1094 |
|
|
s != Thread.State.WAITING && |
1095 |
|
|
s != Thread.State.TIMED_WAITING); |
1096 |
|
|
} |
1097 |
|
|
|
1098 |
dl |
1.314 |
// VarHandle mechanics. |
1099 |
|
|
private static final VarHandle PHASE; |
1100 |
dl |
1.78 |
static { |
1101 |
|
|
try { |
1102 |
dl |
1.314 |
MethodHandles.Lookup l = MethodHandles.lookup(); |
1103 |
|
|
PHASE = l.findVarHandle(WorkQueue.class, "phase", int.class); |
1104 |
jsr166 |
1.231 |
} catch (ReflectiveOperationException e) { |
1105 |
dl |
1.78 |
throw new Error(e); |
1106 |
|
|
} |
1107 |
|
|
} |
1108 |
|
|
} |
1109 |
dl |
1.14 |
|
1110 |
dl |
1.112 |
// static fields (initialized in static initializer below) |
1111 |
|
|
|
1112 |
|
|
/** |
1113 |
|
|
* Creates a new ForkJoinWorkerThread. This factory is used unless |
1114 |
|
|
* overridden in ForkJoinPool constructors. |
1115 |
|
|
*/ |
1116 |
|
|
public static final ForkJoinWorkerThreadFactory |
1117 |
|
|
defaultForkJoinWorkerThreadFactory; |
1118 |
|
|
|
1119 |
jsr166 |
1.1 |
/** |
1120 |
dl |
1.115 |
* Permission required for callers of methods that may start or |
1121 |
dl |
1.300 |
* kill threads. |
1122 |
dl |
1.115 |
*/ |
1123 |
jsr166 |
1.276 |
static final RuntimePermission modifyThreadPermission; |
1124 |
dl |
1.115 |
|
1125 |
|
|
/** |
1126 |
dl |
1.101 |
* Common (static) pool. Non-null for public use unless a static |
1127 |
dl |
1.105 |
* construction exception, but internal usages null-check on use |
1128 |
|
|
* to paranoically avoid potential initialization circularities |
1129 |
|
|
* as well as to simplify generated code. |
1130 |
dl |
1.101 |
*/ |
1131 |
dl |
1.134 |
static final ForkJoinPool common; |
1132 |
dl |
1.101 |
|
1133 |
|
|
/** |
1134 |
dl |
1.160 |
* Common pool parallelism. To allow simpler use and management |
1135 |
|
|
* when common pool threads are disabled, we allow the underlying |
1136 |
dl |
1.185 |
* common.parallelism field to be zero, but in that case still report |
1137 |
dl |
1.160 |
* parallelism as 1 to reflect resulting caller-runs mechanics. |
1138 |
dl |
1.90 |
*/ |
1139 |
jsr166 |
1.274 |
static final int COMMON_PARALLELISM; |
1140 |
dl |
1.90 |
|
1141 |
|
|
/** |
1142 |
dl |
1.208 |
* Limit on spare thread construction in tryCompensate. |
1143 |
|
|
*/ |
1144 |
jsr166 |
1.273 |
private static final int COMMON_MAX_SPARES; |
1145 |
dl |
1.208 |
|
1146 |
|
|
/** |
1147 |
dl |
1.105 |
* Sequence number for creating workerNamePrefix. |
1148 |
dl |
1.86 |
*/ |
1149 |
dl |
1.105 |
private static int poolNumberSequence; |
1150 |
dl |
1.86 |
|
1151 |
jsr166 |
1.1 |
/** |
1152 |
jsr166 |
1.132 |
* Returns the next sequence number. We don't expect this to |
1153 |
|
|
* ever contend, so use simple builtin sync. |
1154 |
dl |
1.83 |
*/ |
1155 |
dl |
1.105 |
private static final synchronized int nextPoolId() { |
1156 |
|
|
return ++poolNumberSequence; |
1157 |
|
|
} |
1158 |
dl |
1.86 |
|
1159 |
dl |
1.200 |
// static configuration constants |
1160 |
dl |
1.86 |
|
1161 |
|
|
/** |
1162 |
dl |
1.300 |
* Default idle timeout value (in milliseconds) for the thread |
1163 |
|
|
* triggering quiescence to park waiting for new work |
1164 |
dl |
1.86 |
*/ |
1165 |
dl |
1.300 |
private static final long DEFAULT_KEEPALIVE = 60000L; |
1166 |
dl |
1.86 |
|
1167 |
|
|
/** |
1168 |
dl |
1.300 |
* Undershoot tolerance for idle timeouts |
1169 |
dl |
1.120 |
*/ |
1170 |
dl |
1.300 |
private static final long TIMEOUT_SLOP = 20L; |
1171 |
dl |
1.200 |
|
1172 |
|
|
/** |
1173 |
jsr166 |
1.273 |
* The default value for COMMON_MAX_SPARES. Overridable using the |
1174 |
|
|
* "java.util.concurrent.ForkJoinPool.common.maximumSpares" system |
1175 |
|
|
* property. The default value is far in excess of normal |
1176 |
|
|
* requirements, but also far short of MAX_CAP and typical OS |
1177 |
|
|
* thread limits, so allows JVMs to catch misuse/abuse before |
1178 |
|
|
* running out of resources needed to do so. |
1179 |
dl |
1.200 |
*/ |
1180 |
dl |
1.208 |
private static final int DEFAULT_COMMON_MAX_SPARES = 256; |
1181 |
dl |
1.120 |
|
1182 |
|
|
/** |
1183 |
dl |
1.90 |
* Increment for seed generators. See class ThreadLocal for |
1184 |
|
|
* explanation. |
1185 |
|
|
*/ |
1186 |
dl |
1.193 |
private static final int SEED_INCREMENT = 0x9e3779b9; |
1187 |
dl |
1.83 |
|
1188 |
jsr166 |
1.163 |
/* |
1189 |
dl |
1.200 |
* Bits and masks for field ctl, packed with 4 16 bit subfields: |
1190 |
dl |
1.300 |
* RC: Number of released (unqueued) workers minus target parallelism |
1191 |
dl |
1.200 |
* TC: Number of total workers minus target parallelism |
1192 |
|
|
* SS: version count and status of top waiting thread |
1193 |
|
|
* ID: poolIndex of top of Treiber stack of waiters |
1194 |
|
|
* |
1195 |
|
|
* When convenient, we can extract the lower 32 stack top bits |
1196 |
|
|
* (including version bits) as sp=(int)ctl. The offsets of counts |
1197 |
|
|
* by the target parallelism and the positionings of fields makes |
1198 |
|
|
* it possible to perform the most common checks via sign tests of |
1199 |
dl |
1.300 |
* fields: When ac is negative, there are not enough unqueued |
1200 |
dl |
1.200 |
* workers, when tc is negative, there are not enough total |
1201 |
|
|
* workers. When sp is non-zero, there are waiting workers. To |
1202 |
|
|
* deal with possibly negative fields, we use casts in and out of |
1203 |
|
|
* "short" and/or signed shifts to maintain signedness. |
1204 |
|
|
* |
1205 |
dl |
1.300 |
* Because it occupies uppermost bits, we can add one release count |
1206 |
|
|
* using getAndAddLong of RC_UNIT, rather than CAS, when returning |
1207 |
dl |
1.200 |
* from a blocked join. Other updates entail multiple subfields |
1208 |
|
|
* and masking, requiring CAS. |
1209 |
dl |
1.300 |
* |
1210 |
|
|
* The limits packed in field "bounds" are also offset by the |
1211 |
|
|
* parallelism level to make them comparable to the ctl rc and tc |
1212 |
|
|
* fields. |
1213 |
dl |
1.200 |
*/ |
1214 |
|
|
|
1215 |
|
|
// Lower and upper word masks |
1216 |
|
|
private static final long SP_MASK = 0xffffffffL; |
1217 |
|
|
private static final long UC_MASK = ~SP_MASK; |
1218 |
dl |
1.86 |
|
1219 |
dl |
1.300 |
// Release counts |
1220 |
|
|
private static final int RC_SHIFT = 48; |
1221 |
|
|
private static final long RC_UNIT = 0x0001L << RC_SHIFT; |
1222 |
|
|
private static final long RC_MASK = 0xffffL << RC_SHIFT; |
1223 |
dl |
1.200 |
|
1224 |
|
|
// Total counts |
1225 |
dl |
1.86 |
private static final int TC_SHIFT = 32; |
1226 |
dl |
1.200 |
private static final long TC_UNIT = 0x0001L << TC_SHIFT; |
1227 |
|
|
private static final long TC_MASK = 0xffffL << TC_SHIFT; |
1228 |
|
|
private static final long ADD_WORKER = 0x0001L << (TC_SHIFT + 15); // sign |
1229 |
|
|
|
1230 |
dl |
1.300 |
// Instance fields |
1231 |
dl |
1.86 |
|
1232 |
dl |
1.300 |
volatile long stealCount; // collects worker nsteals |
1233 |
|
|
final long keepAlive; // milliseconds before dropping if idle |
1234 |
|
|
int indexSeed; // next worker index |
1235 |
|
|
final int bounds; // min, max threads packed as shorts |
1236 |
|
|
volatile int mode; // parallelism, runstate, queue mode |
1237 |
|
|
WorkQueue[] workQueues; // main registry |
1238 |
|
|
final String workerNamePrefix; // for worker thread string; sync lock |
1239 |
dl |
1.112 |
final ForkJoinWorkerThreadFactory factory; |
1240 |
dl |
1.200 |
final UncaughtExceptionHandler ueh; // per-worker UEH |
1241 |
dl |
1.307 |
final Predicate<? super ForkJoinPool> saturate; |
1242 |
dl |
1.101 |
|
1243 |
dl |
1.308 |
@jdk.internal.vm.annotation.Contended("fjpctl") // segregate |
1244 |
|
|
volatile long ctl; // main pool control |
1245 |
jsr166 |
1.309 |
|
1246 |
dl |
1.200 |
// Creating, registering and deregistering workers |
1247 |
|
|
|
1248 |
dl |
1.112 |
/** |
1249 |
dl |
1.200 |
* Tries to construct and start one worker. Assumes that total |
1250 |
|
|
* count has already been incremented as a reservation. Invokes |
1251 |
|
|
* deregisterWorker on any failure. |
1252 |
|
|
* |
1253 |
|
|
* @return true if successful |
1254 |
dl |
1.115 |
*/ |
1255 |
dl |
1.300 |
private boolean createWorker() { |
1256 |
dl |
1.200 |
ForkJoinWorkerThreadFactory fac = factory; |
1257 |
|
|
Throwable ex = null; |
1258 |
|
|
ForkJoinWorkerThread wt = null; |
1259 |
|
|
try { |
1260 |
|
|
if (fac != null && (wt = fac.newThread(this)) != null) { |
1261 |
|
|
wt.start(); |
1262 |
|
|
return true; |
1263 |
dl |
1.115 |
} |
1264 |
dl |
1.200 |
} catch (Throwable rex) { |
1265 |
|
|
ex = rex; |
1266 |
dl |
1.112 |
} |
1267 |
dl |
1.200 |
deregisterWorker(wt, ex); |
1268 |
|
|
return false; |
1269 |
dl |
1.112 |
} |
1270 |
|
|
|
1271 |
dl |
1.200 |
/** |
1272 |
|
|
* Tries to add one worker, incrementing ctl counts before doing |
1273 |
|
|
* so, relying on createWorker to back out on failure. |
1274 |
|
|
* |
1275 |
|
|
* @param c incoming ctl value, with total count negative and no |
1276 |
|
|
* idle workers. On CAS failure, c is refreshed and retried if |
1277 |
jsr166 |
1.202 |
* this holds (otherwise, a new worker is not needed). |
1278 |
dl |
1.200 |
*/ |
1279 |
|
|
private void tryAddWorker(long c) { |
1280 |
|
|
do { |
1281 |
dl |
1.300 |
long nc = ((RC_MASK & (c + RC_UNIT)) | |
1282 |
dl |
1.200 |
(TC_MASK & (c + TC_UNIT))); |
1283 |
dl |
1.314 |
if (ctl == c && CTL.compareAndSet(this, c, nc)) { |
1284 |
dl |
1.300 |
createWorker(); |
1285 |
dl |
1.243 |
break; |
1286 |
dl |
1.200 |
} |
1287 |
|
|
} while (((c = ctl) & ADD_WORKER) != 0L && (int)c == 0); |
1288 |
|
|
} |
1289 |
dl |
1.112 |
|
1290 |
|
|
/** |
1291 |
dl |
1.200 |
* Callback from ForkJoinWorkerThread constructor to establish and |
1292 |
|
|
* record its WorkQueue. |
1293 |
dl |
1.112 |
* |
1294 |
|
|
* @param wt the worker thread |
1295 |
dl |
1.115 |
* @return the worker's queue |
1296 |
dl |
1.112 |
*/ |
1297 |
dl |
1.115 |
final WorkQueue registerWorker(ForkJoinWorkerThread wt) { |
1298 |
dl |
1.200 |
UncaughtExceptionHandler handler; |
1299 |
dl |
1.300 |
wt.setDaemon(true); // configure thread |
1300 |
dl |
1.115 |
if ((handler = ueh) != null) |
1301 |
|
|
wt.setUncaughtExceptionHandler(handler); |
1302 |
dl |
1.200 |
WorkQueue w = new WorkQueue(this, wt); |
1303 |
dl |
1.300 |
int tid = 0; // for thread name |
1304 |
|
|
int fifo = mode & FIFO; |
1305 |
|
|
String prefix = workerNamePrefix; |
1306 |
|
|
if (prefix != null) { |
1307 |
jsr166 |
1.301 |
synchronized (prefix) { |
1308 |
dl |
1.300 |
WorkQueue[] ws = workQueues; int n; |
1309 |
|
|
int s = indexSeed += SEED_INCREMENT; |
1310 |
|
|
if (ws != null && (n = ws.length) > 1) { |
1311 |
|
|
int m = n - 1; |
1312 |
|
|
tid = s & m; |
1313 |
|
|
int i = m & ((s << 1) | 1); // odd-numbered indices |
1314 |
|
|
for (int probes = n >>> 1;;) { // find empty slot |
1315 |
|
|
WorkQueue q; |
1316 |
|
|
if ((q = ws[i]) == null || q.phase == QUIET) |
1317 |
|
|
break; |
1318 |
|
|
else if (--probes == 0) { |
1319 |
|
|
i = n | 1; // resize below |
1320 |
|
|
break; |
1321 |
|
|
} |
1322 |
|
|
else |
1323 |
|
|
i = (i + 2) & m; |
1324 |
|
|
} |
1325 |
|
|
|
1326 |
|
|
int id = i | fifo | (s & ~(SMASK | FIFO | DORMANT)); |
1327 |
|
|
w.phase = w.id = id; // now publishable |
1328 |
|
|
|
1329 |
|
|
if (i < n) |
1330 |
|
|
ws[i] = w; |
1331 |
|
|
else { // expand array |
1332 |
|
|
int an = n << 1; |
1333 |
|
|
WorkQueue[] as = new WorkQueue[an]; |
1334 |
|
|
as[i] = w; |
1335 |
|
|
int am = an - 1; |
1336 |
|
|
for (int j = 0; j < n; ++j) { |
1337 |
|
|
WorkQueue v; // copy external queue |
1338 |
|
|
if ((v = ws[j]) != null) // position may change |
1339 |
|
|
as[v.id & am & SQMASK] = v; |
1340 |
|
|
if (++j >= n) |
1341 |
|
|
break; |
1342 |
|
|
as[j] = ws[j]; // copy worker |
1343 |
dl |
1.94 |
} |
1344 |
dl |
1.300 |
workQueues = as; |
1345 |
dl |
1.94 |
} |
1346 |
|
|
} |
1347 |
dl |
1.78 |
} |
1348 |
dl |
1.300 |
wt.setName(prefix.concat(Integer.toString(tid))); |
1349 |
dl |
1.78 |
} |
1350 |
dl |
1.115 |
return w; |
1351 |
dl |
1.78 |
} |
1352 |
dl |
1.19 |
|
1353 |
jsr166 |
1.1 |
/** |
1354 |
dl |
1.86 |
* Final callback from terminating worker, as well as upon failure |
1355 |
dl |
1.105 |
* to construct or start a worker. Removes record of worker from |
1356 |
|
|
* array, and adjusts counts. If pool is shutting down, tries to |
1357 |
|
|
* complete termination. |
1358 |
dl |
1.78 |
* |
1359 |
jsr166 |
1.151 |
* @param wt the worker thread, or null if construction failed |
1360 |
dl |
1.78 |
* @param ex the exception causing failure, or null if none |
1361 |
dl |
1.45 |
*/ |
1362 |
dl |
1.78 |
final void deregisterWorker(ForkJoinWorkerThread wt, Throwable ex) { |
1363 |
|
|
WorkQueue w = null; |
1364 |
dl |
1.300 |
int phase = 0; |
1365 |
dl |
1.78 |
if (wt != null && (w = wt.workQueue) != null) { |
1366 |
dl |
1.300 |
Object lock = workerNamePrefix; |
1367 |
|
|
long ns = (long)w.nsteals & 0xffffffffL; |
1368 |
|
|
int idx = w.id & SMASK; |
1369 |
|
|
if (lock != null) { |
1370 |
|
|
WorkQueue[] ws; // remove index from array |
1371 |
jsr166 |
1.301 |
synchronized (lock) { |
1372 |
dl |
1.243 |
if ((ws = workQueues) != null && ws.length > idx && |
1373 |
|
|
ws[idx] == w) |
1374 |
|
|
ws[idx] = null; |
1375 |
dl |
1.300 |
stealCount += ns; |
1376 |
dl |
1.243 |
} |
1377 |
|
|
} |
1378 |
dl |
1.300 |
phase = w.phase; |
1379 |
dl |
1.243 |
} |
1380 |
dl |
1.300 |
if (phase != QUIET) { // else pre-adjusted |
1381 |
dl |
1.243 |
long c; // decrement counts |
1382 |
jsr166 |
1.325 |
do {} while (!CTL.weakCompareAndSet |
1383 |
dl |
1.314 |
(this, c = ctl, ((RC_MASK & (c - RC_UNIT)) | |
1384 |
|
|
(TC_MASK & (c - TC_UNIT)) | |
1385 |
|
|
(SP_MASK & c)))); |
1386 |
dl |
1.243 |
} |
1387 |
dl |
1.300 |
if (w != null) |
1388 |
dl |
1.200 |
w.cancelAll(); // cancel remaining tasks |
1389 |
dl |
1.300 |
|
1390 |
|
|
if (!tryTerminate(false, false) && // possibly replace worker |
1391 |
|
|
w != null && w.array != null) // avoid repeated failures |
1392 |
|
|
signalWork(); |
1393 |
|
|
|
1394 |
dl |
1.200 |
if (ex == null) // help clean on way out |
1395 |
dl |
1.120 |
ForkJoinTask.helpExpungeStaleExceptions(); |
1396 |
dl |
1.200 |
else // rethrow |
1397 |
dl |
1.104 |
ForkJoinTask.rethrow(ex); |
1398 |
dl |
1.78 |
} |
1399 |
dl |
1.52 |
|
1400 |
dl |
1.19 |
/** |
1401 |
dl |
1.300 |
* Tries to create or release a worker if too few are running. |
1402 |
dl |
1.105 |
*/ |
1403 |
dl |
1.253 |
final void signalWork() { |
1404 |
dl |
1.243 |
for (;;) { |
1405 |
dl |
1.300 |
long c; int sp; WorkQueue[] ws; int i; WorkQueue v; |
1406 |
dl |
1.243 |
if ((c = ctl) >= 0L) // enough workers |
1407 |
|
|
break; |
1408 |
|
|
else if ((sp = (int)c) == 0) { // no idle workers |
1409 |
|
|
if ((c & ADD_WORKER) != 0L) // too few workers |
1410 |
dl |
1.200 |
tryAddWorker(c); |
1411 |
|
|
break; |
1412 |
|
|
} |
1413 |
dl |
1.243 |
else if ((ws = workQueues) == null) |
1414 |
|
|
break; // unstarted/terminated |
1415 |
|
|
else if (ws.length <= (i = sp & SMASK)) |
1416 |
|
|
break; // terminated |
1417 |
|
|
else if ((v = ws[i]) == null) |
1418 |
|
|
break; // terminating |
1419 |
|
|
else { |
1420 |
dl |
1.300 |
int np = sp & ~UNSIGNALLED; |
1421 |
|
|
int vp = v.phase; |
1422 |
|
|
long nc = (v.stackPred & SP_MASK) | (UC_MASK & (c + RC_UNIT)); |
1423 |
|
|
Thread vt = v.owner; |
1424 |
dl |
1.314 |
if (sp == vp && CTL.compareAndSet(this, c, nc)) { |
1425 |
dl |
1.300 |
v.phase = np; |
1426 |
|
|
if (v.source < 0) |
1427 |
|
|
LockSupport.unpark(vt); |
1428 |
dl |
1.243 |
break; |
1429 |
|
|
} |
1430 |
dl |
1.174 |
} |
1431 |
dl |
1.52 |
} |
1432 |
dl |
1.14 |
} |
1433 |
|
|
|
1434 |
dl |
1.200 |
/** |
1435 |
dl |
1.300 |
* Tries to decrement counts (sometimes implicitly) and possibly |
1436 |
|
|
* arrange for a compensating worker in preparation for blocking: |
1437 |
|
|
* If not all core workers yet exist, creates one, else if any are |
1438 |
|
|
* unreleased (possibly including caller) releases one, else if |
1439 |
|
|
* fewer than the minimum allowed number of workers running, |
1440 |
|
|
* checks to see that they are all active, and if so creates an |
1441 |
|
|
* extra worker unless over maximum limit and policy is to |
1442 |
|
|
* saturate. Most of these steps can fail due to interference, in |
1443 |
|
|
* which case 0 is returned so caller will retry. A negative |
1444 |
|
|
* return value indicates that the caller doesn't need to |
1445 |
|
|
* re-adjust counts when later unblocked. |
1446 |
dl |
1.243 |
* |
1447 |
dl |
1.300 |
* @return 1: block then adjust, -1: block without adjust, 0 : retry |
1448 |
dl |
1.243 |
*/ |
1449 |
dl |
1.300 |
private int tryCompensate(WorkQueue w) { |
1450 |
|
|
int t, n, sp; |
1451 |
|
|
long c = ctl; |
1452 |
|
|
WorkQueue[] ws = workQueues; |
1453 |
dl |
1.310 |
if ((t = (short)(c >>> TC_SHIFT)) >= 0) { |
1454 |
dl |
1.300 |
if (ws == null || (n = ws.length) <= 0 || w == null) |
1455 |
|
|
return 0; // disabled |
1456 |
|
|
else if ((sp = (int)c) != 0) { // replace or release |
1457 |
|
|
WorkQueue v = ws[sp & (n - 1)]; |
1458 |
|
|
int wp = w.phase; |
1459 |
|
|
long uc = UC_MASK & ((wp < 0) ? c + RC_UNIT : c); |
1460 |
|
|
int np = sp & ~UNSIGNALLED; |
1461 |
|
|
if (v != null) { |
1462 |
|
|
int vp = v.phase; |
1463 |
|
|
Thread vt = v.owner; |
1464 |
|
|
long nc = ((long)v.stackPred & SP_MASK) | uc; |
1465 |
dl |
1.314 |
if (vp == sp && CTL.compareAndSet(this, c, nc)) { |
1466 |
dl |
1.300 |
v.phase = np; |
1467 |
|
|
if (v.source < 0) |
1468 |
|
|
LockSupport.unpark(vt); |
1469 |
|
|
return (wp < 0) ? -1 : 1; |
1470 |
|
|
} |
1471 |
|
|
} |
1472 |
|
|
return 0; |
1473 |
|
|
} |
1474 |
|
|
else if ((int)(c >> RC_SHIFT) - // reduce parallelism |
1475 |
|
|
(short)(bounds & SMASK) > 0) { |
1476 |
|
|
long nc = ((RC_MASK & (c - RC_UNIT)) | (~RC_MASK & c)); |
1477 |
dl |
1.314 |
return CTL.compareAndSet(this, c, nc) ? 1 : 0; |
1478 |
dl |
1.300 |
} |
1479 |
|
|
else { // validate |
1480 |
|
|
int md = mode, pc = md & SMASK, tc = pc + t, bc = 0; |
1481 |
|
|
boolean unstable = false; |
1482 |
|
|
for (int i = 1; i < n; i += 2) { |
1483 |
|
|
WorkQueue q; Thread wt; Thread.State ts; |
1484 |
|
|
if ((q = ws[i]) != null) { |
1485 |
|
|
if (q.source == 0) { |
1486 |
|
|
unstable = true; |
1487 |
|
|
break; |
1488 |
|
|
} |
1489 |
|
|
else { |
1490 |
|
|
--tc; |
1491 |
|
|
if ((wt = q.owner) != null && |
1492 |
|
|
((ts = wt.getState()) == Thread.State.BLOCKED || |
1493 |
|
|
ts == Thread.State.WAITING)) |
1494 |
|
|
++bc; // worker is blocking |
1495 |
|
|
} |
1496 |
dl |
1.243 |
} |
1497 |
|
|
} |
1498 |
dl |
1.300 |
if (unstable || tc != 0 || ctl != c) |
1499 |
|
|
return 0; // inconsistent |
1500 |
|
|
else if (t + pc >= MAX_CAP || t >= (bounds >>> SWIDTH)) { |
1501 |
dl |
1.307 |
Predicate<? super ForkJoinPool> sat; |
1502 |
|
|
if ((sat = saturate) != null && sat.test(this)) |
1503 |
dl |
1.300 |
return -1; |
1504 |
|
|
else if (bc < pc) { // lagging |
1505 |
|
|
Thread.yield(); // for retry spins |
1506 |
|
|
return 0; |
1507 |
dl |
1.243 |
} |
1508 |
|
|
else |
1509 |
dl |
1.300 |
throw new RejectedExecutionException( |
1510 |
|
|
"Thread limit exceeded replacing blocked worker"); |
1511 |
dl |
1.200 |
} |
1512 |
dl |
1.177 |
} |
1513 |
dl |
1.243 |
} |
1514 |
dl |
1.300 |
|
1515 |
|
|
long nc = ((c + TC_UNIT) & TC_MASK) | (c & ~TC_MASK); // expand pool |
1516 |
dl |
1.314 |
return CTL.compareAndSet(this, c, nc) && createWorker() ? 1 : 0; |
1517 |
dl |
1.243 |
} |
1518 |
|
|
|
1519 |
|
|
/** |
1520 |
|
|
* Top-level runloop for workers, called by ForkJoinWorkerThread.run. |
1521 |
dl |
1.300 |
* See above for explanation. |
1522 |
dl |
1.243 |
*/ |
1523 |
|
|
final void runWorker(WorkQueue w) { |
1524 |
dl |
1.300 |
WorkQueue[] ws; |
1525 |
dl |
1.243 |
w.growArray(); // allocate queue |
1526 |
dl |
1.300 |
int r = w.id ^ ThreadLocalRandom.nextSecondarySeed(); |
1527 |
|
|
if (r == 0) // initial nonzero seed |
1528 |
|
|
r = 1; |
1529 |
|
|
int lastSignalId = 0; // avoid unneeded signals |
1530 |
|
|
while ((ws = workQueues) != null) { |
1531 |
|
|
boolean nonempty = false; // scan |
1532 |
|
|
for (int n = ws.length, j = n, m = n - 1; j > 0; --j) { |
1533 |
|
|
WorkQueue q; int i, b, al; ForkJoinTask<?>[] a; |
1534 |
|
|
if ((i = r & m) >= 0 && i < n && // always true |
1535 |
|
|
(q = ws[i]) != null && (b = q.base) - q.top < 0 && |
1536 |
dl |
1.243 |
(a = q.array) != null && (al = a.length) > 0) { |
1537 |
dl |
1.300 |
int qid = q.id; // (never zero) |
1538 |
dl |
1.256 |
int index = (al - 1) & b; |
1539 |
dl |
1.243 |
ForkJoinTask<?> t = (ForkJoinTask<?>) |
1540 |
dl |
1.316 |
QA.getAcquire(a, index); |
1541 |
dl |
1.300 |
if (t != null && b++ == q.base && |
1542 |
dl |
1.314 |
QA.compareAndSet(a, index, t, null)) { |
1543 |
dl |
1.300 |
if ((q.base = b) - q.top < 0 && qid != lastSignalId) |
1544 |
|
|
signalWork(); // propagate signal |
1545 |
|
|
w.source = lastSignalId = qid; |
1546 |
|
|
t.doExec(); |
1547 |
|
|
if ((w.id & FIFO) != 0) // run remaining locals |
1548 |
|
|
w.localPollAndExec(POLL_LIMIT); |
1549 |
|
|
else |
1550 |
|
|
w.localPopAndExec(POLL_LIMIT); |
1551 |
|
|
ForkJoinWorkerThread thread = w.owner; |
1552 |
|
|
++w.nsteals; |
1553 |
|
|
w.source = 0; // now idle |
1554 |
|
|
if (thread != null) |
1555 |
|
|
thread.afterTopLevelExec(); |
1556 |
dl |
1.243 |
} |
1557 |
dl |
1.300 |
nonempty = true; |
1558 |
dl |
1.178 |
} |
1559 |
dl |
1.300 |
else if (nonempty) |
1560 |
dl |
1.243 |
break; |
1561 |
dl |
1.300 |
else |
1562 |
|
|
++r; |
1563 |
dl |
1.120 |
} |
1564 |
dl |
1.178 |
|
1565 |
dl |
1.300 |
if (nonempty) { // move (xorshift) |
1566 |
|
|
r ^= r << 13; r ^= r >>> 17; r ^= r << 5; |
1567 |
|
|
} |
1568 |
|
|
else { |
1569 |
|
|
int phase; |
1570 |
|
|
lastSignalId = 0; // clear for next scan |
1571 |
|
|
if ((phase = w.phase) >= 0) { // enqueue |
1572 |
|
|
int np = w.phase = (phase + SS_SEQ) | UNSIGNALLED; |
1573 |
|
|
long c, nc; |
1574 |
|
|
do { |
1575 |
|
|
w.stackPred = (int)(c = ctl); |
1576 |
|
|
nc = ((c - RC_UNIT) & UC_MASK) | (SP_MASK & np); |
1577 |
jsr166 |
1.325 |
} while (!CTL.weakCompareAndSet(this, c, nc)); |
1578 |
dl |
1.300 |
} |
1579 |
|
|
else { // already queued |
1580 |
|
|
int pred = w.stackPred; |
1581 |
|
|
w.source = DORMANT; // enable signal |
1582 |
|
|
for (int steps = 0;;) { |
1583 |
|
|
int md, rc; long c; |
1584 |
|
|
if (w.phase >= 0) { |
1585 |
|
|
w.source = 0; |
1586 |
|
|
break; |
1587 |
|
|
} |
1588 |
|
|
else if ((md = mode) < 0) // shutting down |
1589 |
|
|
return; |
1590 |
|
|
else if ((rc = ((md & SMASK) + // possibly quiescent |
1591 |
|
|
(int)((c = ctl) >> RC_SHIFT))) <= 0 && |
1592 |
|
|
(md & SHUTDOWN) != 0 && |
1593 |
|
|
tryTerminate(false, false)) |
1594 |
|
|
return; // help terminate |
1595 |
|
|
else if ((++steps & 1) == 0) |
1596 |
|
|
Thread.interrupted(); // clear between parks |
1597 |
|
|
else if (rc <= 0 && pred != 0 && phase == (int)c) { |
1598 |
|
|
long d = keepAlive + System.currentTimeMillis(); |
1599 |
|
|
LockSupport.parkUntil(this, d); |
1600 |
|
|
if (ctl == c && |
1601 |
|
|
d - System.currentTimeMillis() <= TIMEOUT_SLOP) { |
1602 |
|
|
long nc = ((UC_MASK & (c - TC_UNIT)) | |
1603 |
|
|
(SP_MASK & pred)); |
1604 |
dl |
1.314 |
if (CTL.compareAndSet(this, c, nc)) { |
1605 |
dl |
1.300 |
w.phase = QUIET; |
1606 |
|
|
return; // drop on timeout |
1607 |
|
|
} |
1608 |
|
|
} |
1609 |
|
|
} |
1610 |
|
|
else |
1611 |
|
|
LockSupport.park(this); |
1612 |
|
|
} |
1613 |
|
|
} |
1614 |
|
|
} |
1615 |
|
|
} |
1616 |
|
|
} |
1617 |
dl |
1.200 |
|
1618 |
dl |
1.305 |
/** |
1619 |
|
|
* Helps and/or blocks until the given task is done or timeout. |
1620 |
|
|
* First tries locally helping, then scans other queues for a task |
1621 |
|
|
* produced by one of w's stealers; compensating and blocking if |
1622 |
|
|
* none are found (rescanning if tryCompensate fails). |
1623 |
|
|
* |
1624 |
|
|
* @param w caller |
1625 |
|
|
* @param task the task |
1626 |
|
|
* @param deadline for timed waits, if nonzero |
1627 |
|
|
* @return task status on exit |
1628 |
|
|
*/ |
1629 |
dl |
1.300 |
final int awaitJoin(WorkQueue w, ForkJoinTask<?> task, long deadline) { |
1630 |
|
|
int s = 0; |
1631 |
|
|
if (w != null && task != null && |
1632 |
|
|
(!(task instanceof CountedCompleter) || |
1633 |
|
|
(s = w.localHelpCC((CountedCompleter<?>)task, 0)) >= 0)) { |
1634 |
|
|
w.tryRemoveAndExec(task); |
1635 |
|
|
int src = w.source, id = w.id; |
1636 |
|
|
s = task.status; |
1637 |
|
|
while (s >= 0) { |
1638 |
|
|
WorkQueue[] ws; |
1639 |
|
|
boolean nonempty = false; |
1640 |
|
|
int r = ThreadLocalRandom.nextSecondarySeed() | 1; // odd indices |
1641 |
|
|
if ((ws = workQueues) != null) { // scan for matching id |
1642 |
|
|
for (int n = ws.length, m = n - 1, j = -n; j < n; j += 2) { |
1643 |
|
|
WorkQueue q; int i, b, al; ForkJoinTask<?>[] a; |
1644 |
|
|
if ((i = (r + j) & m) >= 0 && i < n && |
1645 |
|
|
(q = ws[i]) != null && q.source == id && |
1646 |
|
|
(b = q.base) - q.top < 0 && |
1647 |
|
|
(a = q.array) != null && (al = a.length) > 0) { |
1648 |
|
|
int qid = q.id; |
1649 |
|
|
int index = (al - 1) & b; |
1650 |
|
|
ForkJoinTask<?> t = (ForkJoinTask<?>) |
1651 |
dl |
1.316 |
QA.getAcquire(a, index); |
1652 |
dl |
1.300 |
if (t != null && b++ == q.base && id == q.source && |
1653 |
dl |
1.314 |
QA.compareAndSet(a, index, t, null)) { |
1654 |
dl |
1.300 |
q.base = b; |
1655 |
|
|
w.source = qid; |
1656 |
|
|
t.doExec(); |
1657 |
|
|
w.source = src; |
1658 |
|
|
} |
1659 |
|
|
nonempty = true; |
1660 |
dl |
1.200 |
break; |
1661 |
dl |
1.300 |
} |
1662 |
dl |
1.200 |
} |
1663 |
dl |
1.300 |
} |
1664 |
|
|
if ((s = task.status) < 0) |
1665 |
|
|
break; |
1666 |
|
|
else if (!nonempty) { |
1667 |
|
|
long ms, ns; int block; |
1668 |
|
|
if (deadline == 0L) |
1669 |
|
|
ms = 0L; // untimed |
1670 |
|
|
else if ((ns = deadline - System.nanoTime()) <= 0L) |
1671 |
|
|
break; // timeout |
1672 |
|
|
else if ((ms = TimeUnit.NANOSECONDS.toMillis(ns)) <= 0L) |
1673 |
|
|
ms = 1L; // avoid 0 for timed wait |
1674 |
|
|
if ((block = tryCompensate(w)) != 0) { |
1675 |
|
|
task.internalWait(ms); |
1676 |
dl |
1.314 |
CTL.getAndAdd(this, (block > 0) ? RC_UNIT : 0L); |
1677 |
dl |
1.200 |
} |
1678 |
dl |
1.300 |
s = task.status; |
1679 |
dl |
1.200 |
} |
1680 |
dl |
1.178 |
} |
1681 |
|
|
} |
1682 |
dl |
1.200 |
return s; |
1683 |
dl |
1.120 |
} |
1684 |
|
|
|
1685 |
|
|
/** |
1686 |
dl |
1.300 |
* Runs tasks until {@code isQuiescent()}. Rather than blocking |
1687 |
|
|
* when tasks cannot be found, rescans until all others cannot |
1688 |
|
|
* find tasks either. |
1689 |
dl |
1.78 |
*/ |
1690 |
dl |
1.300 |
final void helpQuiescePool(WorkQueue w) { |
1691 |
|
|
int prevSrc = w.source, fifo = w.id & FIFO; |
1692 |
|
|
for (int source = prevSrc, released = -1;;) { // -1 until known |
1693 |
|
|
WorkQueue[] ws; |
1694 |
|
|
if (fifo != 0) |
1695 |
|
|
w.localPollAndExec(0); |
1696 |
|
|
else |
1697 |
|
|
w.localPopAndExec(0); |
1698 |
|
|
if (released == -1 && w.phase >= 0) |
1699 |
|
|
released = 1; |
1700 |
|
|
boolean quiet = true, empty = true; |
1701 |
|
|
int r = ThreadLocalRandom.nextSecondarySeed(); |
1702 |
|
|
if ((ws = workQueues) != null) { |
1703 |
|
|
for (int n = ws.length, j = n, m = n - 1; j > 0; --j) { |
1704 |
|
|
WorkQueue q; int i, b, al; ForkJoinTask<?>[] a; |
1705 |
|
|
if ((i = (r - j) & m) >= 0 && i < n && (q = ws[i]) != null) { |
1706 |
|
|
if ((b = q.base) - q.top < 0 && |
1707 |
|
|
(a = q.array) != null && (al = a.length) > 0) { |
1708 |
|
|
int qid = q.id; |
1709 |
|
|
if (released == 0) { // increment |
1710 |
|
|
released = 1; |
1711 |
dl |
1.314 |
CTL.getAndAdd(this, RC_UNIT); |
1712 |
dl |
1.95 |
} |
1713 |
dl |
1.256 |
int index = (al - 1) & b; |
1714 |
dl |
1.300 |
ForkJoinTask<?> t = (ForkJoinTask<?>) |
1715 |
dl |
1.316 |
QA.getAcquire(a, index); |
1716 |
dl |
1.300 |
if (t != null && b++ == q.base && |
1717 |
dl |
1.314 |
QA.compareAndSet(a, index, t, null)) { |
1718 |
dl |
1.300 |
q.base = b; |
1719 |
|
|
w.source = source = q.id; |
1720 |
|
|
t.doExec(); |
1721 |
|
|
w.source = source = prevSrc; |
1722 |
dl |
1.243 |
} |
1723 |
dl |
1.300 |
quiet = empty = false; |
1724 |
dl |
1.200 |
break; |
1725 |
dl |
1.95 |
} |
1726 |
dl |
1.300 |
else if ((q.source & QUIET) == 0) |
1727 |
|
|
quiet = false; |
1728 |
dl |
1.52 |
} |
1729 |
dl |
1.19 |
} |
1730 |
dl |
1.243 |
} |
1731 |
dl |
1.300 |
if (quiet) { |
1732 |
|
|
if (released == 0) |
1733 |
dl |
1.314 |
CTL.getAndAdd(this, RC_UNIT); |
1734 |
dl |
1.300 |
w.source = prevSrc; |
1735 |
|
|
break; |
1736 |
|
|
} |
1737 |
|
|
else if (empty) { |
1738 |
|
|
if (source != QUIET) |
1739 |
|
|
w.source = source = QUIET; |
1740 |
|
|
if (released == 1) { // decrement |
1741 |
|
|
released = 0; |
1742 |
dl |
1.314 |
CTL.getAndAdd(this, RC_MASK & -RC_UNIT); |
1743 |
dl |
1.300 |
} |
1744 |
|
|
} |
1745 |
dl |
1.14 |
} |
1746 |
dl |
1.22 |
} |
1747 |
|
|
|
1748 |
dl |
1.52 |
/** |
1749 |
dl |
1.300 |
* Scans for and returns a polled task, if available. |
1750 |
|
|
* Used only for untracked polls. |
1751 |
dl |
1.105 |
* |
1752 |
dl |
1.300 |
* @param submissionsOnly if true, only scan submission queues |
1753 |
dl |
1.19 |
*/ |
1754 |
dl |
1.300 |
private ForkJoinTask<?> pollScan(boolean submissionsOnly) { |
1755 |
|
|
WorkQueue[] ws; int n; |
1756 |
|
|
rescan: while ((mode & STOP) == 0 && (ws = workQueues) != null && |
1757 |
|
|
(n = ws.length) > 0) { |
1758 |
|
|
int m = n - 1; |
1759 |
|
|
int r = ThreadLocalRandom.nextSecondarySeed(); |
1760 |
|
|
int h = r >>> 16; |
1761 |
|
|
int origin, step; |
1762 |
|
|
if (submissionsOnly) { |
1763 |
|
|
origin = (r & ~1) & m; // even indices and steps |
1764 |
|
|
step = (h & ~1) | 2; |
1765 |
|
|
} |
1766 |
|
|
else { |
1767 |
|
|
origin = r & m; |
1768 |
|
|
step = h | 1; |
1769 |
|
|
} |
1770 |
|
|
for (int k = origin, oldSum = 0, checkSum = 0;;) { |
1771 |
|
|
WorkQueue q; int b, al; ForkJoinTask<?>[] a; |
1772 |
|
|
if ((q = ws[k]) != null) { |
1773 |
|
|
checkSum += b = q.base; |
1774 |
|
|
if (b - q.top < 0 && |
1775 |
|
|
(a = q.array) != null && (al = a.length) > 0) { |
1776 |
|
|
int index = (al - 1) & b; |
1777 |
|
|
ForkJoinTask<?> t = (ForkJoinTask<?>) |
1778 |
dl |
1.316 |
QA.getAcquire(a, index); |
1779 |
dl |
1.300 |
if (t != null && b++ == q.base && |
1780 |
dl |
1.314 |
QA.compareAndSet(a, index, t, null)) { |
1781 |
dl |
1.300 |
q.base = b; |
1782 |
|
|
return t; |
1783 |
|
|
} |
1784 |
|
|
else |
1785 |
|
|
break; // restart |
1786 |
|
|
} |
1787 |
|
|
} |
1788 |
|
|
if ((k = (k + step) & m) == origin) { |
1789 |
|
|
if (oldSum == (oldSum = checkSum)) |
1790 |
|
|
break rescan; |
1791 |
|
|
checkSum = 0; |
1792 |
dl |
1.178 |
} |
1793 |
dl |
1.52 |
} |
1794 |
dl |
1.90 |
} |
1795 |
dl |
1.300 |
return null; |
1796 |
|
|
} |
1797 |
|
|
|
1798 |
|
|
/** |
1799 |
|
|
* Gets and removes a local or stolen task for the given worker. |
1800 |
|
|
* |
1801 |
|
|
* @return a task, if available |
1802 |
|
|
*/ |
1803 |
|
|
final ForkJoinTask<?> nextTaskFor(WorkQueue w) { |
1804 |
|
|
ForkJoinTask<?> t; |
1805 |
|
|
if (w != null && |
1806 |
|
|
(t = (w.id & FIFO) != 0 ? w.poll() : w.pop()) != null) |
1807 |
|
|
return t; |
1808 |
|
|
else |
1809 |
|
|
return pollScan(false); |
1810 |
dl |
1.90 |
} |
1811 |
|
|
|
1812 |
dl |
1.300 |
// External operations |
1813 |
|
|
|
1814 |
dl |
1.90 |
/** |
1815 |
dl |
1.300 |
* Adds the given task to a submission queue at submitter's |
1816 |
|
|
* current queue, creating one if null or contended. |
1817 |
dl |
1.90 |
* |
1818 |
dl |
1.300 |
* @param task the task. Caller must ensure non-null. |
1819 |
dl |
1.90 |
*/ |
1820 |
dl |
1.300 |
final void externalPush(ForkJoinTask<?> task) { |
1821 |
|
|
int r; // initialize caller's probe |
1822 |
|
|
if ((r = ThreadLocalRandom.getProbe()) == 0) { |
1823 |
|
|
ThreadLocalRandom.localInit(); |
1824 |
|
|
r = ThreadLocalRandom.getProbe(); |
1825 |
|
|
} |
1826 |
|
|
for (;;) { |
1827 |
|
|
int md = mode, n; |
1828 |
|
|
WorkQueue[] ws = workQueues; |
1829 |
|
|
if ((md & SHUTDOWN) != 0 || ws == null || (n = ws.length) <= 0) |
1830 |
|
|
throw new RejectedExecutionException(); |
1831 |
|
|
else { |
1832 |
|
|
WorkQueue q; |
1833 |
|
|
boolean push = false, grow = false; |
1834 |
|
|
if ((q = ws[(n - 1) & r & SQMASK]) == null) { |
1835 |
|
|
Object lock = workerNamePrefix; |
1836 |
|
|
int qid = (r | QUIET) & ~(FIFO | OWNED); |
1837 |
|
|
q = new WorkQueue(this, null); |
1838 |
|
|
q.id = qid; |
1839 |
|
|
q.source = QUIET; |
1840 |
|
|
q.phase = QLOCK; // lock queue |
1841 |
|
|
if (lock != null) { |
1842 |
jsr166 |
1.301 |
synchronized (lock) { // lock pool to install |
1843 |
dl |
1.300 |
int i; |
1844 |
|
|
if ((ws = workQueues) != null && |
1845 |
|
|
(n = ws.length) > 0 && |
1846 |
|
|
ws[i = qid & (n - 1) & SQMASK] == null) { |
1847 |
|
|
ws[i] = q; |
1848 |
|
|
push = grow = true; |
1849 |
|
|
} |
1850 |
|
|
} |
1851 |
|
|
} |
1852 |
|
|
} |
1853 |
|
|
else if (q.tryLockSharedQueue()) { |
1854 |
|
|
int b = q.base, s = q.top, al, d; ForkJoinTask<?>[] a; |
1855 |
|
|
if ((a = q.array) != null && (al = a.length) > 0 && |
1856 |
|
|
al - 1 + (d = b - s) > 0) { |
1857 |
|
|
a[(al - 1) & s] = task; |
1858 |
|
|
q.top = s + 1; // relaxed writes OK here |
1859 |
|
|
q.phase = 0; |
1860 |
dl |
1.311 |
if (d < 0 && q.base - s < -1) |
1861 |
dl |
1.300 |
break; // no signal needed |
1862 |
|
|
} |
1863 |
dl |
1.243 |
else |
1864 |
dl |
1.300 |
grow = true; |
1865 |
|
|
push = true; |
1866 |
|
|
} |
1867 |
|
|
if (push) { |
1868 |
|
|
if (grow) { |
1869 |
|
|
try { |
1870 |
|
|
q.growArray(); |
1871 |
|
|
int s = q.top, al; ForkJoinTask<?>[] a; |
1872 |
|
|
if ((a = q.array) != null && (al = a.length) > 0) { |
1873 |
|
|
a[(al - 1) & s] = task; |
1874 |
|
|
q.top = s + 1; |
1875 |
|
|
} |
1876 |
|
|
} finally { |
1877 |
|
|
q.phase = 0; |
1878 |
|
|
} |
1879 |
dl |
1.243 |
} |
1880 |
dl |
1.300 |
signalWork(); |
1881 |
|
|
break; |
1882 |
dl |
1.90 |
} |
1883 |
dl |
1.300 |
else // move if busy |
1884 |
|
|
r = ThreadLocalRandom.advanceProbe(r); |
1885 |
dl |
1.90 |
} |
1886 |
|
|
} |
1887 |
|
|
} |
1888 |
|
|
|
1889 |
dl |
1.300 |
/** |
1890 |
|
|
* Pushes a possibly-external submission. |
1891 |
|
|
*/ |
1892 |
|
|
private <T> ForkJoinTask<T> externalSubmit(ForkJoinTask<T> task) { |
1893 |
|
|
Thread t; ForkJoinWorkerThread w; WorkQueue q; |
1894 |
|
|
if (task == null) |
1895 |
|
|
throw new NullPointerException(); |
1896 |
|
|
if (((t = Thread.currentThread()) instanceof ForkJoinWorkerThread) && |
1897 |
|
|
(w = (ForkJoinWorkerThread)t).pool == this && |
1898 |
|
|
(q = w.workQueue) != null) |
1899 |
|
|
q.push(task); |
1900 |
|
|
else |
1901 |
|
|
externalPush(task); |
1902 |
|
|
return task; |
1903 |
|
|
} |
1904 |
|
|
|
1905 |
|
|
/** |
1906 |
|
|
* Returns common pool queue for an external thread. |
1907 |
|
|
*/ |
1908 |
|
|
static WorkQueue commonSubmitterQueue() { |
1909 |
|
|
ForkJoinPool p = common; |
1910 |
|
|
int r = ThreadLocalRandom.getProbe(); |
1911 |
|
|
WorkQueue[] ws; int n; |
1912 |
|
|
return (p != null && (ws = p.workQueues) != null && |
1913 |
|
|
(n = ws.length) > 0) ? |
1914 |
|
|
ws[(n - 1) & r & SQMASK] : null; |
1915 |
|
|
} |
1916 |
dl |
1.90 |
|
1917 |
|
|
/** |
1918 |
dl |
1.300 |
* Performs tryUnpush for an external submitter. |
1919 |
|
|
*/ |
1920 |
|
|
final boolean tryExternalUnpush(ForkJoinTask<?> task) { |
1921 |
|
|
int r = ThreadLocalRandom.getProbe(); |
1922 |
|
|
WorkQueue[] ws; WorkQueue w; int n; |
1923 |
|
|
return ((ws = workQueues) != null && |
1924 |
|
|
(n = ws.length) > 0 && |
1925 |
|
|
(w = ws[(n - 1) & r & SQMASK]) != null && |
1926 |
|
|
w.trySharedUnpush(task)); |
1927 |
dl |
1.22 |
} |
1928 |
|
|
|
1929 |
|
|
/** |
1930 |
dl |
1.300 |
* Performs helpComplete for an external submitter. |
1931 |
dl |
1.78 |
*/ |
1932 |
dl |
1.300 |
final int externalHelpComplete(CountedCompleter<?> task, int maxTasks) { |
1933 |
|
|
int r = ThreadLocalRandom.getProbe(); |
1934 |
|
|
WorkQueue[] ws; WorkQueue w; int n; |
1935 |
|
|
return ((ws = workQueues) != null && (n = ws.length) > 0 && |
1936 |
|
|
(w = ws[(n - 1) & r & SQMASK]) != null) ? |
1937 |
|
|
w.sharedHelpCC(task, maxTasks) : 0; |
1938 |
dl |
1.22 |
} |
1939 |
|
|
|
1940 |
|
|
/** |
1941 |
dl |
1.300 |
* Tries to steal and run tasks within the target's computation. |
1942 |
|
|
* The maxTasks argument supports external usages; internal calls |
1943 |
|
|
* use zero, allowing unbounded steps (external calls trap |
1944 |
|
|
* non-positive values). |
1945 |
dl |
1.78 |
* |
1946 |
dl |
1.300 |
* @param w caller |
1947 |
|
|
* @param maxTasks if non-zero, the maximum number of other tasks to run |
1948 |
|
|
* @return task status on exit |
1949 |
dl |
1.22 |
*/ |
1950 |
dl |
1.300 |
final int helpComplete(WorkQueue w, CountedCompleter<?> task, |
1951 |
|
|
int maxTasks) { |
1952 |
|
|
return (w == null) ? 0 : w.localHelpCC(task, maxTasks); |
1953 |
dl |
1.14 |
} |
1954 |
|
|
|
1955 |
|
|
/** |
1956 |
dl |
1.105 |
* Returns a cheap heuristic guide for task partitioning when |
1957 |
|
|
* programmers, frameworks, tools, or languages have little or no |
1958 |
jsr166 |
1.222 |
* idea about task granularity. In essence, by offering this |
1959 |
dl |
1.105 |
* method, we ask users only about tradeoffs in overhead vs |
1960 |
|
|
* expected throughput and its variance, rather than how finely to |
1961 |
|
|
* partition tasks. |
1962 |
|
|
* |
1963 |
|
|
* In a steady state strict (tree-structured) computation, each |
1964 |
|
|
* thread makes available for stealing enough tasks for other |
1965 |
|
|
* threads to remain active. Inductively, if all threads play by |
1966 |
|
|
* the same rules, each thread should make available only a |
1967 |
|
|
* constant number of tasks. |
1968 |
|
|
* |
1969 |
|
|
* The minimum useful constant is just 1. But using a value of 1 |
1970 |
|
|
* would require immediate replenishment upon each steal to |
1971 |
|
|
* maintain enough tasks, which is infeasible. Further, |
1972 |
|
|
* partitionings/granularities of offered tasks should minimize |
1973 |
|
|
* steal rates, which in general means that threads nearer the top |
1974 |
|
|
* of computation tree should generate more than those nearer the |
1975 |
|
|
* bottom. In perfect steady state, each thread is at |
1976 |
|
|
* approximately the same level of computation tree. However, |
1977 |
|
|
* producing extra tasks amortizes the uncertainty of progress and |
1978 |
|
|
* diffusion assumptions. |
1979 |
|
|
* |
1980 |
jsr166 |
1.161 |
* So, users will want to use values larger (but not much larger) |
1981 |
dl |
1.105 |
* than 1 to both smooth over transient shortages and hedge |
1982 |
|
|
* against uneven progress; as traded off against the cost of |
1983 |
|
|
* extra task overhead. We leave the user to pick a threshold |
1984 |
|
|
* value to compare with the results of this call to guide |
1985 |
|
|
* decisions, but recommend values such as 3. |
1986 |
|
|
* |
1987 |
|
|
* When all threads are active, it is on average OK to estimate |
1988 |
|
|
* surplus strictly locally. In steady-state, if one thread is |
1989 |
|
|
* maintaining say 2 surplus tasks, then so are others. So we can |
1990 |
|
|
* just use estimated queue length. However, this strategy alone |
1991 |
|
|
* leads to serious mis-estimates in some non-steady-state |
1992 |
|
|
* conditions (ramp-up, ramp-down, other stalls). We can detect |
1993 |
|
|
* many of these by further considering the number of "idle" |
1994 |
|
|
* threads, that are known to have zero queued tasks, so |
1995 |
|
|
* compensate by a factor of (#idle/#active) threads. |
1996 |
|
|
*/ |
1997 |
|
|
static int getSurplusQueuedTaskCount() { |
1998 |
|
|
Thread t; ForkJoinWorkerThread wt; ForkJoinPool pool; WorkQueue q; |
1999 |
dl |
1.300 |
if (((t = Thread.currentThread()) instanceof ForkJoinWorkerThread) && |
2000 |
|
|
(pool = (wt = (ForkJoinWorkerThread)t).pool) != null && |
2001 |
|
|
(q = wt.workQueue) != null) { |
2002 |
|
|
int p = pool.mode & SMASK; |
2003 |
|
|
int a = p + (int)(pool.ctl >> RC_SHIFT); |
2004 |
|
|
int n = q.top - q.base; |
2005 |
dl |
1.112 |
return n - (a > (p >>>= 1) ? 0 : |
2006 |
|
|
a > (p >>>= 1) ? 1 : |
2007 |
|
|
a > (p >>>= 1) ? 2 : |
2008 |
|
|
a > (p >>>= 1) ? 4 : |
2009 |
|
|
8); |
2010 |
dl |
1.105 |
} |
2011 |
|
|
return 0; |
2012 |
dl |
1.100 |
} |
2013 |
|
|
|
2014 |
dl |
1.300 |
// Termination |
2015 |
dl |
1.14 |
|
2016 |
|
|
/** |
2017 |
dl |
1.210 |
* Possibly initiates and/or completes termination. |
2018 |
dl |
1.14 |
* |
2019 |
|
|
* @param now if true, unconditionally terminate, else only |
2020 |
dl |
1.78 |
* if no work and no active workers |
2021 |
dl |
1.243 |
* @param enable if true, terminate when next possible |
2022 |
dl |
1.300 |
* @return true if terminating or terminated |
2023 |
jsr166 |
1.1 |
*/ |
2024 |
dl |
1.300 |
private boolean tryTerminate(boolean now, boolean enable) { |
2025 |
|
|
int md; // 3 phases: try to set SHUTDOWN, then STOP, then TERMINATED |
2026 |
dl |
1.289 |
|
2027 |
dl |
1.300 |
while (((md = mode) & SHUTDOWN) == 0) { |
2028 |
dl |
1.294 |
if (!enable || this == common) // cannot shutdown |
2029 |
dl |
1.300 |
return false; |
2030 |
dl |
1.294 |
else |
2031 |
dl |
1.314 |
MODE.compareAndSet(this, md, md | SHUTDOWN); |
2032 |
dl |
1.289 |
} |
2033 |
|
|
|
2034 |
dl |
1.300 |
while (((md = mode) & STOP) == 0) { // try to initiate termination |
2035 |
|
|
if (!now) { // check if quiescent & empty |
2036 |
dl |
1.211 |
for (long oldSum = 0L;;) { // repeat until stable |
2037 |
dl |
1.300 |
boolean running = false; |
2038 |
dl |
1.210 |
long checkSum = ctl; |
2039 |
dl |
1.300 |
WorkQueue[] ws = workQueues; |
2040 |
|
|
if ((md & SMASK) + (int)(checkSum >> RC_SHIFT) > 0) |
2041 |
|
|
running = true; |
2042 |
|
|
else if (ws != null) { |
2043 |
|
|
WorkQueue w; int b; |
2044 |
dl |
1.289 |
for (int i = 0; i < ws.length; ++i) { |
2045 |
|
|
if ((w = ws[i]) != null) { |
2046 |
dl |
1.300 |
checkSum += (b = w.base) + w.id; |
2047 |
|
|
if (b != w.top || |
2048 |
|
|
((i & 1) == 1 && w.source >= 0)) { |
2049 |
|
|
running = true; |
2050 |
|
|
break; |
2051 |
|
|
} |
2052 |
dl |
1.289 |
} |
2053 |
dl |
1.206 |
} |
2054 |
dl |
1.203 |
} |
2055 |
dl |
1.300 |
if (((md = mode) & STOP) != 0) |
2056 |
|
|
break; // already triggered |
2057 |
|
|
else if (running) |
2058 |
|
|
return false; |
2059 |
|
|
else if (workQueues == ws && oldSum == (oldSum = checkSum)) |
2060 |
dl |
1.210 |
break; |
2061 |
dl |
1.203 |
} |
2062 |
|
|
} |
2063 |
dl |
1.300 |
if ((md & STOP) == 0) |
2064 |
dl |
1.314 |
MODE.compareAndSet(this, md, md | STOP); |
2065 |
dl |
1.200 |
} |
2066 |
dl |
1.210 |
|
2067 |
dl |
1.300 |
while (((md = mode) & TERMINATED) == 0) { // help terminate others |
2068 |
|
|
for (long oldSum = 0L;;) { // repeat until stable |
2069 |
|
|
WorkQueue[] ws; WorkQueue w; |
2070 |
|
|
long checkSum = ctl; |
2071 |
|
|
if ((ws = workQueues) != null) { |
2072 |
|
|
for (int i = 0; i < ws.length; ++i) { |
2073 |
|
|
if ((w = ws[i]) != null) { |
2074 |
|
|
ForkJoinWorkerThread wt = w.owner; |
2075 |
|
|
w.cancelAll(); // clear queues |
2076 |
|
|
if (wt != null) { |
2077 |
dl |
1.289 |
try { // unblock join or park |
2078 |
dl |
1.210 |
wt.interrupt(); |
2079 |
|
|
} catch (Throwable ignore) { |
2080 |
dl |
1.200 |
} |
2081 |
|
|
} |
2082 |
dl |
1.300 |
checkSum += w.base + w.id; |
2083 |
dl |
1.200 |
} |
2084 |
dl |
1.101 |
} |
2085 |
dl |
1.78 |
} |
2086 |
dl |
1.300 |
if (((md = mode) & TERMINATED) != 0 || |
2087 |
|
|
(workQueues == ws && oldSum == (oldSum = checkSum))) |
2088 |
|
|
break; |
2089 |
dl |
1.78 |
} |
2090 |
dl |
1.300 |
if ((md & TERMINATED) != 0) |
2091 |
|
|
break; |
2092 |
|
|
else if ((md & SMASK) + (short)(ctl >>> TC_SHIFT) > 0) |
2093 |
dl |
1.210 |
break; |
2094 |
dl |
1.314 |
else if (MODE.compareAndSet(this, md, md | TERMINATED)) { |
2095 |
dl |
1.300 |
synchronized (this) { |
2096 |
|
|
notifyAll(); // for awaitTermination |
2097 |
dl |
1.243 |
} |
2098 |
dl |
1.256 |
break; |
2099 |
dl |
1.200 |
} |
2100 |
dl |
1.52 |
} |
2101 |
dl |
1.300 |
return true; |
2102 |
dl |
1.105 |
} |
2103 |
|
|
|
2104 |
dl |
1.52 |
// Exported methods |
2105 |
jsr166 |
1.1 |
|
2106 |
|
|
// Constructors |
2107 |
|
|
|
2108 |
|
|
/** |
2109 |
jsr166 |
1.9 |
* Creates a {@code ForkJoinPool} with parallelism equal to {@link |
2110 |
dl |
1.300 |
* java.lang.Runtime#availableProcessors}, using defaults for all |
2111 |
dl |
1.319 |
* other parameters (see {@link #ForkJoinPool(int, |
2112 |
|
|
* ForkJoinWorkerThreadFactory, UncaughtExceptionHandler, boolean, |
2113 |
|
|
* int, int, int, Predicate, long, TimeUnit)}). |
2114 |
jsr166 |
1.1 |
* |
2115 |
|
|
* @throws SecurityException if a security manager exists and |
2116 |
|
|
* the caller is not permitted to modify threads |
2117 |
|
|
* because it does not hold {@link |
2118 |
|
|
* java.lang.RuntimePermission}{@code ("modifyThread")} |
2119 |
|
|
*/ |
2120 |
|
|
public ForkJoinPool() { |
2121 |
jsr166 |
1.148 |
this(Math.min(MAX_CAP, Runtime.getRuntime().availableProcessors()), |
2122 |
dl |
1.300 |
defaultForkJoinWorkerThreadFactory, null, false, |
2123 |
dl |
1.307 |
0, MAX_CAP, 1, null, DEFAULT_KEEPALIVE, TimeUnit.MILLISECONDS); |
2124 |
jsr166 |
1.1 |
} |
2125 |
|
|
|
2126 |
|
|
/** |
2127 |
jsr166 |
1.9 |
* Creates a {@code ForkJoinPool} with the indicated parallelism |
2128 |
dl |
1.319 |
* level, using defaults for all other parameters (see {@link |
2129 |
|
|
* #ForkJoinPool(int, ForkJoinWorkerThreadFactory, |
2130 |
|
|
* UncaughtExceptionHandler, boolean, int, int, int, Predicate, |
2131 |
|
|
* long, TimeUnit)}). |
2132 |
jsr166 |
1.1 |
* |
2133 |
jsr166 |
1.9 |
* @param parallelism the parallelism level |
2134 |
jsr166 |
1.1 |
* @throws IllegalArgumentException if parallelism less than or |
2135 |
jsr166 |
1.11 |
* equal to zero, or greater than implementation limit |
2136 |
jsr166 |
1.1 |
* @throws SecurityException if a security manager exists and |
2137 |
|
|
* the caller is not permitted to modify threads |
2138 |
|
|
* because it does not hold {@link |
2139 |
|
|
* java.lang.RuntimePermission}{@code ("modifyThread")} |
2140 |
|
|
*/ |
2141 |
|
|
public ForkJoinPool(int parallelism) { |
2142 |
dl |
1.300 |
this(parallelism, defaultForkJoinWorkerThreadFactory, null, false, |
2143 |
dl |
1.307 |
0, MAX_CAP, 1, null, DEFAULT_KEEPALIVE, TimeUnit.MILLISECONDS); |
2144 |
jsr166 |
1.1 |
} |
2145 |
|
|
|
2146 |
|
|
/** |
2147 |
dl |
1.300 |
* Creates a {@code ForkJoinPool} with the given parameters (using |
2148 |
dl |
1.319 |
* defaults for others -- see {@link #ForkJoinPool(int, |
2149 |
|
|
* ForkJoinWorkerThreadFactory, UncaughtExceptionHandler, boolean, |
2150 |
|
|
* int, int, int, Predicate, long, TimeUnit)}). |
2151 |
jsr166 |
1.1 |
* |
2152 |
dl |
1.18 |
* @param parallelism the parallelism level. For default value, |
2153 |
|
|
* use {@link java.lang.Runtime#availableProcessors}. |
2154 |
|
|
* @param factory the factory for creating new threads. For default value, |
2155 |
|
|
* use {@link #defaultForkJoinWorkerThreadFactory}. |
2156 |
dl |
1.19 |
* @param handler the handler for internal worker threads that |
2157 |
|
|
* terminate due to unrecoverable errors encountered while executing |
2158 |
jsr166 |
1.31 |
* tasks. For default value, use {@code null}. |
2159 |
dl |
1.19 |
* @param asyncMode if true, |
2160 |
dl |
1.18 |
* establishes local first-in-first-out scheduling mode for forked |
2161 |
|
|
* tasks that are never joined. This mode may be more appropriate |
2162 |
|
|
* than default locally stack-based mode in applications in which |
2163 |
|
|
* worker threads only process event-style asynchronous tasks. |
2164 |
jsr166 |
1.31 |
* For default value, use {@code false}. |
2165 |
jsr166 |
1.1 |
* @throws IllegalArgumentException if parallelism less than or |
2166 |
jsr166 |
1.11 |
* equal to zero, or greater than implementation limit |
2167 |
|
|
* @throws NullPointerException if the factory is null |
2168 |
jsr166 |
1.1 |
* @throws SecurityException if a security manager exists and |
2169 |
|
|
* the caller is not permitted to modify threads |
2170 |
|
|
* because it does not hold {@link |
2171 |
|
|
* java.lang.RuntimePermission}{@code ("modifyThread")} |
2172 |
|
|
*/ |
2173 |
dl |
1.19 |
public ForkJoinPool(int parallelism, |
2174 |
dl |
1.18 |
ForkJoinWorkerThreadFactory factory, |
2175 |
jsr166 |
1.156 |
UncaughtExceptionHandler handler, |
2176 |
dl |
1.18 |
boolean asyncMode) { |
2177 |
dl |
1.300 |
this(parallelism, factory, handler, asyncMode, |
2178 |
dl |
1.307 |
0, MAX_CAP, 1, null, DEFAULT_KEEPALIVE, TimeUnit.MILLISECONDS); |
2179 |
dl |
1.152 |
} |
2180 |
|
|
|
2181 |
dl |
1.300 |
/** |
2182 |
|
|
* Creates a {@code ForkJoinPool} with the given parameters. |
2183 |
|
|
* |
2184 |
|
|
* @param parallelism the parallelism level. For default value, |
2185 |
|
|
* use {@link java.lang.Runtime#availableProcessors}. |
2186 |
|
|
* |
2187 |
|
|
* @param factory the factory for creating new threads. For |
2188 |
|
|
* default value, use {@link #defaultForkJoinWorkerThreadFactory}. |
2189 |
|
|
* |
2190 |
|
|
* @param handler the handler for internal worker threads that |
2191 |
|
|
* terminate due to unrecoverable errors encountered while |
2192 |
|
|
* executing tasks. For default value, use {@code null}. |
2193 |
|
|
* |
2194 |
|
|
* @param asyncMode if true, establishes local first-in-first-out |
2195 |
|
|
* scheduling mode for forked tasks that are never joined. This |
2196 |
|
|
* mode may be more appropriate than default locally stack-based |
2197 |
|
|
* mode in applications in which worker threads only process |
2198 |
|
|
* event-style asynchronous tasks. For default value, use {@code |
2199 |
|
|
* false}. |
2200 |
|
|
* |
2201 |
|
|
* @param corePoolSize the number of threads to keep in the pool |
2202 |
|
|
* (unless timed out after an elapsed keep-alive). Normally (and |
2203 |
|
|
* by default) this is the same value as the parallelism level, |
2204 |
|
|
* but may be set to a larger value to reduce dynamic overhead if |
2205 |
|
|
* tasks regularly block. Using a smaller value (for example |
2206 |
|
|
* {@code 0}) has the same effect as the default. |
2207 |
|
|
* |
2208 |
|
|
* @param maximumPoolSize the maximum number of threads allowed. |
2209 |
|
|
* When the maximum is reached, attempts to replace blocked |
2210 |
|
|
* threads fail. (However, because creation and termination of |
2211 |
|
|
* different threads may overlap, and may be managed by the given |
2212 |
dl |
1.307 |
* thread factory, this value may be transiently exceeded.) To |
2213 |
|
|
* arrange the same value as is used by default for the common |
2214 |
dl |
1.319 |
* pool, use {@code 256} plus the {@code parallelism} level. (By |
2215 |
|
|
* default, the common pool allows a maximum of 256 spare |
2216 |
|
|
* threads.) Using a value (for example {@code |
2217 |
|
|
* Integer.MAX_VALUE}) larger than the implementation's total |
2218 |
|
|
* thread limit has the same effect as using this limit (which is |
2219 |
|
|
* the default). |
2220 |
dl |
1.300 |
* |
2221 |
|
|
* @param minimumRunnable the minimum allowed number of core |
2222 |
|
|
* threads not blocked by a join or {@link ManagedBlocker}. To |
2223 |
|
|
* ensure progress, when too few unblocked threads exist and |
2224 |
|
|
* unexecuted tasks may exist, new threads are constructed, up to |
2225 |
|
|
* the given maximumPoolSize. For the default value, use {@code |
2226 |
|
|
* 1}, that ensures liveness. A larger value might improve |
2227 |
|
|
* throughput in the presence of blocked activities, but might |
2228 |
|
|
* not, due to increased overhead. A value of zero may be |
2229 |
|
|
* acceptable when submitted tasks cannot have dependencies |
2230 |
|
|
* requiring additional threads. |
2231 |
|
|
* |
2232 |
jsr166 |
1.318 |
* @param saturate if non-null, a predicate invoked upon attempts |
2233 |
dl |
1.307 |
* to create more than the maximum total allowed threads. By |
2234 |
|
|
* default, when a thread is about to block on a join or {@link |
2235 |
|
|
* ManagedBlocker}, but cannot be replaced because the |
2236 |
|
|
* maximumPoolSize would be exceeded, a {@link |
2237 |
|
|
* RejectedExecutionException} is thrown. But if this predicate |
2238 |
|
|
* returns {@code true}, then no exception is thrown, so the pool |
2239 |
|
|
* continues to operate with fewer than the target number of |
2240 |
|
|
* runnable threads, which might not ensure progress. |
2241 |
dl |
1.300 |
* |
2242 |
|
|
* @param keepAliveTime the elapsed time since last use before |
2243 |
|
|
* a thread is terminated (and then later replaced if needed). |
2244 |
|
|
* For the default value, use {@code 60, TimeUnit.SECONDS}. |
2245 |
|
|
* |
2246 |
|
|
* @param unit the time unit for the {@code keepAliveTime} argument |
2247 |
|
|
* |
2248 |
|
|
* @throws IllegalArgumentException if parallelism is less than or |
2249 |
|
|
* equal to zero, or is greater than implementation limit, |
2250 |
|
|
* or if maximumPoolSize is less than parallelism, |
2251 |
|
|
* of if the keepAliveTime is less than or equal to zero. |
2252 |
|
|
* @throws NullPointerException if the factory is null |
2253 |
|
|
* @throws SecurityException if a security manager exists and |
2254 |
|
|
* the caller is not permitted to modify threads |
2255 |
|
|
* because it does not hold {@link |
2256 |
|
|
* java.lang.RuntimePermission}{@code ("modifyThread")} |
2257 |
jsr166 |
1.306 |
* @since 9 |
2258 |
dl |
1.300 |
*/ |
2259 |
|
|
public ForkJoinPool(int parallelism, |
2260 |
|
|
ForkJoinWorkerThreadFactory factory, |
2261 |
|
|
UncaughtExceptionHandler handler, |
2262 |
|
|
boolean asyncMode, |
2263 |
|
|
int corePoolSize, |
2264 |
|
|
int maximumPoolSize, |
2265 |
|
|
int minimumRunnable, |
2266 |
dl |
1.307 |
Predicate<? super ForkJoinPool> saturate, |
2267 |
dl |
1.300 |
long keepAliveTime, |
2268 |
|
|
TimeUnit unit) { |
2269 |
|
|
// check, encode, pack parameters |
2270 |
|
|
if (parallelism <= 0 || parallelism > MAX_CAP || |
2271 |
|
|
maximumPoolSize < parallelism || keepAliveTime <= 0L) |
2272 |
dl |
1.152 |
throw new IllegalArgumentException(); |
2273 |
dl |
1.14 |
if (factory == null) |
2274 |
|
|
throw new NullPointerException(); |
2275 |
dl |
1.300 |
long ms = Math.max(unit.toMillis(keepAliveTime), TIMEOUT_SLOP); |
2276 |
|
|
|
2277 |
|
|
String prefix = "ForkJoinPool-" + nextPoolId() + "-worker-"; |
2278 |
|
|
int corep = Math.min(Math.max(corePoolSize, parallelism), MAX_CAP); |
2279 |
|
|
long c = ((((long)(-corep) << TC_SHIFT) & TC_MASK) | |
2280 |
|
|
(((long)(-parallelism) << RC_SHIFT) & RC_MASK)); |
2281 |
dl |
1.307 |
int m = parallelism | (asyncMode ? FIFO : 0); |
2282 |
dl |
1.300 |
int maxSpares = Math.min(maximumPoolSize, MAX_CAP) - parallelism; |
2283 |
|
|
int minAvail = Math.min(Math.max(minimumRunnable, 0), MAX_CAP); |
2284 |
|
|
int b = ((minAvail - parallelism) & SMASK) | (maxSpares << SWIDTH); |
2285 |
|
|
int n = (parallelism > 1) ? parallelism - 1 : 1; // at least 2 slots |
2286 |
|
|
n |= n >>> 1; n |= n >>> 2; n |= n >>> 4; n |= n >>> 8; n |= n >>> 16; |
2287 |
|
|
n = (n + 1) << 1; // power of two, including space for submission queues |
2288 |
|
|
|
2289 |
|
|
this.workQueues = new WorkQueue[n]; |
2290 |
|
|
this.workerNamePrefix = prefix; |
2291 |
|
|
this.factory = factory; |
2292 |
|
|
this.ueh = handler; |
2293 |
dl |
1.307 |
this.saturate = saturate; |
2294 |
dl |
1.300 |
this.keepAlive = ms; |
2295 |
|
|
this.bounds = b; |
2296 |
|
|
this.mode = m; |
2297 |
|
|
this.ctl = c; |
2298 |
|
|
checkPermission(); |
2299 |
dl |
1.152 |
} |
2300 |
|
|
|
2301 |
|
|
/** |
2302 |
dl |
1.300 |
* Constructor for common pool using parameters possibly |
2303 |
|
|
* overridden by system properties |
2304 |
|
|
*/ |
2305 |
jsr166 |
1.313 |
@SuppressWarnings("deprecation") // Class.newInstance |
2306 |
dl |
1.300 |
private ForkJoinPool(byte forCommonPoolOnly) { |
2307 |
|
|
int parallelism = -1; |
2308 |
|
|
ForkJoinWorkerThreadFactory fac = null; |
2309 |
|
|
UncaughtExceptionHandler handler = null; |
2310 |
|
|
try { // ignore exceptions in accessing/parsing properties |
2311 |
|
|
String pp = System.getProperty |
2312 |
|
|
("java.util.concurrent.ForkJoinPool.common.parallelism"); |
2313 |
|
|
String fp = System.getProperty |
2314 |
|
|
("java.util.concurrent.ForkJoinPool.common.threadFactory"); |
2315 |
|
|
String hp = System.getProperty |
2316 |
|
|
("java.util.concurrent.ForkJoinPool.common.exceptionHandler"); |
2317 |
|
|
if (pp != null) |
2318 |
|
|
parallelism = Integer.parseInt(pp); |
2319 |
|
|
if (fp != null) |
2320 |
|
|
fac = ((ForkJoinWorkerThreadFactory)ClassLoader. |
2321 |
|
|
getSystemClassLoader().loadClass(fp).newInstance()); |
2322 |
|
|
if (hp != null) |
2323 |
|
|
handler = ((UncaughtExceptionHandler)ClassLoader. |
2324 |
|
|
getSystemClassLoader().loadClass(hp).newInstance()); |
2325 |
|
|
} catch (Exception ignore) { |
2326 |
|
|
} |
2327 |
|
|
|
2328 |
|
|
if (fac == null) { |
2329 |
|
|
if (System.getSecurityManager() == null) |
2330 |
|
|
fac = defaultForkJoinWorkerThreadFactory; |
2331 |
|
|
else // use security-managed default |
2332 |
|
|
fac = new InnocuousForkJoinWorkerThreadFactory(); |
2333 |
|
|
} |
2334 |
|
|
if (parallelism < 0 && // default 1 less than #cores |
2335 |
|
|
(parallelism = Runtime.getRuntime().availableProcessors() - 1) <= 0) |
2336 |
|
|
parallelism = 1; |
2337 |
|
|
if (parallelism > MAX_CAP) |
2338 |
|
|
parallelism = MAX_CAP; |
2339 |
|
|
|
2340 |
|
|
long c = ((((long)(-parallelism) << TC_SHIFT) & TC_MASK) | |
2341 |
|
|
(((long)(-parallelism) << RC_SHIFT) & RC_MASK)); |
2342 |
|
|
int b = ((1 - parallelism) & SMASK) | (COMMON_MAX_SPARES << SWIDTH); |
2343 |
|
|
int n = (parallelism > 1) ? parallelism - 1 : 1; |
2344 |
|
|
n |= n >>> 1; n |= n >>> 2; n |= n >>> 4; n |= n >>> 8; n |= n >>> 16; |
2345 |
|
|
n = (n + 1) << 1; |
2346 |
|
|
|
2347 |
|
|
this.workQueues = new WorkQueue[n]; |
2348 |
|
|
this.workerNamePrefix = "ForkJoinPool.commonPool-worker-"; |
2349 |
|
|
this.factory = fac; |
2350 |
dl |
1.18 |
this.ueh = handler; |
2351 |
dl |
1.307 |
this.saturate = null; |
2352 |
dl |
1.300 |
this.keepAlive = DEFAULT_KEEPALIVE; |
2353 |
|
|
this.bounds = b; |
2354 |
dl |
1.310 |
this.mode = parallelism; |
2355 |
dl |
1.300 |
this.ctl = c; |
2356 |
dl |
1.101 |
} |
2357 |
|
|
|
2358 |
|
|
/** |
2359 |
dl |
1.128 |
* Returns the common pool instance. This pool is statically |
2360 |
dl |
1.134 |
* constructed; its run state is unaffected by attempts to {@link |
2361 |
|
|
* #shutdown} or {@link #shutdownNow}. However this pool and any |
2362 |
|
|
* ongoing processing are automatically terminated upon program |
2363 |
|
|
* {@link System#exit}. Any program that relies on asynchronous |
2364 |
|
|
* task processing to complete before program termination should |
2365 |
jsr166 |
1.158 |
* invoke {@code commonPool().}{@link #awaitQuiescence awaitQuiescence}, |
2366 |
|
|
* before exit. |
2367 |
dl |
1.100 |
* |
2368 |
|
|
* @return the common pool instance |
2369 |
jsr166 |
1.138 |
* @since 1.8 |
2370 |
dl |
1.100 |
*/ |
2371 |
|
|
public static ForkJoinPool commonPool() { |
2372 |
dl |
1.134 |
// assert common != null : "static init error"; |
2373 |
|
|
return common; |
2374 |
dl |
1.100 |
} |
2375 |
|
|
|
2376 |
jsr166 |
1.1 |
// Execution methods |
2377 |
|
|
|
2378 |
|
|
/** |
2379 |
|
|
* Performs the given task, returning its result upon completion. |
2380 |
dl |
1.52 |
* If the computation encounters an unchecked Exception or Error, |
2381 |
|
|
* it is rethrown as the outcome of this invocation. Rethrown |
2382 |
|
|
* exceptions behave in the same way as regular exceptions, but, |
2383 |
|
|
* when possible, contain stack traces (as displayed for example |
2384 |
|
|
* using {@code ex.printStackTrace()}) of both the current thread |
2385 |
|
|
* as well as the thread actually encountering the exception; |
2386 |
|
|
* minimally only the latter. |
2387 |
jsr166 |
1.1 |
* |
2388 |
|
|
* @param task the task |
2389 |
jsr166 |
1.191 |
* @param <T> the type of the task's result |
2390 |
jsr166 |
1.1 |
* @return the task's result |
2391 |
jsr166 |
1.11 |
* @throws NullPointerException if the task is null |
2392 |
|
|
* @throws RejectedExecutionException if the task cannot be |
2393 |
|
|
* scheduled for execution |
2394 |
jsr166 |
1.1 |
*/ |
2395 |
|
|
public <T> T invoke(ForkJoinTask<T> task) { |
2396 |
dl |
1.90 |
if (task == null) |
2397 |
|
|
throw new NullPointerException(); |
2398 |
dl |
1.243 |
externalSubmit(task); |
2399 |
dl |
1.78 |
return task.join(); |
2400 |
jsr166 |
1.1 |
} |
2401 |
|
|
|
2402 |
|
|
/** |
2403 |
|
|
* Arranges for (asynchronous) execution of the given task. |
2404 |
|
|
* |
2405 |
|
|
* @param task the task |
2406 |
jsr166 |
1.11 |
* @throws NullPointerException if the task is null |
2407 |
|
|
* @throws RejectedExecutionException if the task cannot be |
2408 |
|
|
* scheduled for execution |
2409 |
jsr166 |
1.1 |
*/ |
2410 |
jsr166 |
1.8 |
public void execute(ForkJoinTask<?> task) { |
2411 |
dl |
1.243 |
externalSubmit(task); |
2412 |
jsr166 |
1.1 |
} |
2413 |
|
|
|
2414 |
|
|
// AbstractExecutorService methods |
2415 |
|
|
|
2416 |
jsr166 |
1.11 |
/** |
2417 |
|
|
* @throws NullPointerException if the task is null |
2418 |
|
|
* @throws RejectedExecutionException if the task cannot be |
2419 |
|
|
* scheduled for execution |
2420 |
|
|
*/ |
2421 |
jsr166 |
1.1 |
public void execute(Runnable task) { |
2422 |
dl |
1.41 |
if (task == null) |
2423 |
|
|
throw new NullPointerException(); |
2424 |
jsr166 |
1.2 |
ForkJoinTask<?> job; |
2425 |
jsr166 |
1.3 |
if (task instanceof ForkJoinTask<?>) // avoid re-wrap |
2426 |
|
|
job = (ForkJoinTask<?>) task; |
2427 |
jsr166 |
1.2 |
else |
2428 |
dl |
1.152 |
job = new ForkJoinTask.RunnableExecuteAction(task); |
2429 |
dl |
1.243 |
externalSubmit(job); |
2430 |
jsr166 |
1.1 |
} |
2431 |
|
|
|
2432 |
jsr166 |
1.11 |
/** |
2433 |
dl |
1.18 |
* Submits a ForkJoinTask for execution. |
2434 |
|
|
* |
2435 |
|
|
* @param task the task to submit |
2436 |
jsr166 |
1.191 |
* @param <T> the type of the task's result |
2437 |
dl |
1.18 |
* @return the task |
2438 |
|
|
* @throws NullPointerException if the task is null |
2439 |
|
|
* @throws RejectedExecutionException if the task cannot be |
2440 |
|
|
* scheduled for execution |
2441 |
|
|
*/ |
2442 |
|
|
public <T> ForkJoinTask<T> submit(ForkJoinTask<T> task) { |
2443 |
dl |
1.243 |
return externalSubmit(task); |
2444 |
dl |
1.18 |
} |
2445 |
|
|
|
2446 |
|
|
/** |
2447 |
jsr166 |
1.11 |
* @throws NullPointerException if the task is null |
2448 |
|
|
* @throws RejectedExecutionException if the task cannot be |
2449 |
|
|
* scheduled for execution |
2450 |
|
|
*/ |
2451 |
jsr166 |
1.1 |
public <T> ForkJoinTask<T> submit(Callable<T> task) { |
2452 |
dl |
1.243 |
return externalSubmit(new ForkJoinTask.AdaptedCallable<T>(task)); |
2453 |
jsr166 |
1.1 |
} |
2454 |
|
|
|
2455 |
jsr166 |
1.11 |
/** |
2456 |
|
|
* @throws NullPointerException if the task is null |
2457 |
|
|
* @throws RejectedExecutionException if the task cannot be |
2458 |
|
|
* scheduled for execution |
2459 |
|
|
*/ |
2460 |
jsr166 |
1.1 |
public <T> ForkJoinTask<T> submit(Runnable task, T result) { |
2461 |
dl |
1.243 |
return externalSubmit(new ForkJoinTask.AdaptedRunnable<T>(task, result)); |
2462 |
jsr166 |
1.1 |
} |
2463 |
|
|
|
2464 |
jsr166 |
1.11 |
/** |
2465 |
|
|
* @throws NullPointerException if the task is null |
2466 |
|
|
* @throws RejectedExecutionException if the task cannot be |
2467 |
|
|
* scheduled for execution |
2468 |
|
|
*/ |
2469 |
jsr166 |
1.1 |
public ForkJoinTask<?> submit(Runnable task) { |
2470 |
dl |
1.41 |
if (task == null) |
2471 |
|
|
throw new NullPointerException(); |
2472 |
jsr166 |
1.2 |
ForkJoinTask<?> job; |
2473 |
jsr166 |
1.3 |
if (task instanceof ForkJoinTask<?>) // avoid re-wrap |
2474 |
|
|
job = (ForkJoinTask<?>) task; |
2475 |
jsr166 |
1.2 |
else |
2476 |
dl |
1.90 |
job = new ForkJoinTask.AdaptedRunnableAction(task); |
2477 |
dl |
1.243 |
return externalSubmit(job); |
2478 |
jsr166 |
1.1 |
} |
2479 |
|
|
|
2480 |
|
|
/** |
2481 |
jsr166 |
1.11 |
* @throws NullPointerException {@inheritDoc} |
2482 |
|
|
* @throws RejectedExecutionException {@inheritDoc} |
2483 |
|
|
*/ |
2484 |
jsr166 |
1.1 |
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) { |
2485 |
dl |
1.86 |
// In previous versions of this class, this method constructed |
2486 |
|
|
// a task to run ForkJoinTask.invokeAll, but now external |
2487 |
|
|
// invocation of multiple tasks is at least as efficient. |
2488 |
jsr166 |
1.199 |
ArrayList<Future<T>> futures = new ArrayList<>(tasks.size()); |
2489 |
jsr166 |
1.1 |
|
2490 |
dl |
1.86 |
try { |
2491 |
|
|
for (Callable<T> t : tasks) { |
2492 |
dl |
1.90 |
ForkJoinTask<T> f = new ForkJoinTask.AdaptedCallable<T>(t); |
2493 |
jsr166 |
1.144 |
futures.add(f); |
2494 |
dl |
1.243 |
externalSubmit(f); |
2495 |
dl |
1.86 |
} |
2496 |
jsr166 |
1.143 |
for (int i = 0, size = futures.size(); i < size; i++) |
2497 |
|
|
((ForkJoinTask<?>)futures.get(i)).quietlyJoin(); |
2498 |
dl |
1.86 |
return futures; |
2499 |
jsr166 |
1.226 |
} catch (Throwable t) { |
2500 |
|
|
for (int i = 0, size = futures.size(); i < size; i++) |
2501 |
|
|
futures.get(i).cancel(false); |
2502 |
|
|
throw t; |
2503 |
jsr166 |
1.1 |
} |
2504 |
|
|
} |
2505 |
|
|
|
2506 |
|
|
/** |
2507 |
|
|
* Returns the factory used for constructing new workers. |
2508 |
|
|
* |
2509 |
|
|
* @return the factory used for constructing new workers |
2510 |
|
|
*/ |
2511 |
|
|
public ForkJoinWorkerThreadFactory getFactory() { |
2512 |
|
|
return factory; |
2513 |
|
|
} |
2514 |
|
|
|
2515 |
|
|
/** |
2516 |
|
|
* Returns the handler for internal worker threads that terminate |
2517 |
|
|
* due to unrecoverable errors encountered while executing tasks. |
2518 |
|
|
* |
2519 |
jsr166 |
1.4 |
* @return the handler, or {@code null} if none |
2520 |
jsr166 |
1.1 |
*/ |
2521 |
jsr166 |
1.156 |
public UncaughtExceptionHandler getUncaughtExceptionHandler() { |
2522 |
dl |
1.14 |
return ueh; |
2523 |
jsr166 |
1.1 |
} |
2524 |
|
|
|
2525 |
|
|
/** |
2526 |
jsr166 |
1.9 |
* Returns the targeted parallelism level of this pool. |
2527 |
jsr166 |
1.1 |
* |
2528 |
jsr166 |
1.9 |
* @return the targeted parallelism level of this pool |
2529 |
jsr166 |
1.1 |
*/ |
2530 |
|
|
public int getParallelism() { |
2531 |
dl |
1.310 |
int par = mode & SMASK; |
2532 |
|
|
return (par > 0) ? par : 1; |
2533 |
jsr166 |
1.1 |
} |
2534 |
|
|
|
2535 |
|
|
/** |
2536 |
dl |
1.100 |
* Returns the targeted parallelism level of the common pool. |
2537 |
|
|
* |
2538 |
|
|
* @return the targeted parallelism level of the common pool |
2539 |
jsr166 |
1.138 |
* @since 1.8 |
2540 |
dl |
1.100 |
*/ |
2541 |
|
|
public static int getCommonPoolParallelism() { |
2542 |
jsr166 |
1.274 |
return COMMON_PARALLELISM; |
2543 |
dl |
1.100 |
} |
2544 |
|
|
|
2545 |
|
|
/** |
2546 |
jsr166 |
1.1 |
* Returns the number of worker threads that have started but not |
2547 |
jsr166 |
1.34 |
* yet terminated. The result returned by this method may differ |
2548 |
jsr166 |
1.4 |
* from {@link #getParallelism} when threads are created to |
2549 |
jsr166 |
1.1 |
* maintain parallelism when others are cooperatively blocked. |
2550 |
|
|
* |
2551 |
|
|
* @return the number of worker threads |
2552 |
|
|
*/ |
2553 |
|
|
public int getPoolSize() { |
2554 |
dl |
1.300 |
return ((mode & SMASK) + (short)(ctl >>> TC_SHIFT)); |
2555 |
jsr166 |
1.1 |
} |
2556 |
|
|
|
2557 |
|
|
/** |
2558 |
jsr166 |
1.4 |
* Returns {@code true} if this pool uses local first-in-first-out |
2559 |
jsr166 |
1.1 |
* scheduling mode for forked tasks that are never joined. |
2560 |
|
|
* |
2561 |
jsr166 |
1.4 |
* @return {@code true} if this pool uses async mode |
2562 |
jsr166 |
1.1 |
*/ |
2563 |
|
|
public boolean getAsyncMode() { |
2564 |
dl |
1.300 |
return (mode & FIFO) != 0; |
2565 |
jsr166 |
1.1 |
} |
2566 |
|
|
|
2567 |
|
|
/** |
2568 |
|
|
* Returns an estimate of the number of worker threads that are |
2569 |
|
|
* not blocked waiting to join tasks or for other managed |
2570 |
dl |
1.14 |
* synchronization. This method may overestimate the |
2571 |
|
|
* number of running threads. |
2572 |
jsr166 |
1.1 |
* |
2573 |
|
|
* @return the number of worker threads |
2574 |
|
|
*/ |
2575 |
|
|
public int getRunningThreadCount() { |
2576 |
dl |
1.78 |
int rc = 0; |
2577 |
|
|
WorkQueue[] ws; WorkQueue w; |
2578 |
|
|
if ((ws = workQueues) != null) { |
2579 |
dl |
1.86 |
for (int i = 1; i < ws.length; i += 2) { |
2580 |
|
|
if ((w = ws[i]) != null && w.isApparentlyUnblocked()) |
2581 |
dl |
1.78 |
++rc; |
2582 |
|
|
} |
2583 |
|
|
} |
2584 |
|
|
return rc; |
2585 |
jsr166 |
1.1 |
} |
2586 |
|
|
|
2587 |
|
|
/** |
2588 |
|
|
* Returns an estimate of the number of threads that are currently |
2589 |
|
|
* stealing or executing tasks. This method may overestimate the |
2590 |
|
|
* number of active threads. |
2591 |
|
|
* |
2592 |
|
|
* @return the number of active threads |
2593 |
|
|
*/ |
2594 |
|
|
public int getActiveThreadCount() { |
2595 |
dl |
1.300 |
int r = (mode & SMASK) + (int)(ctl >> RC_SHIFT); |
2596 |
jsr166 |
1.63 |
return (r <= 0) ? 0 : r; // suppress momentarily negative values |
2597 |
jsr166 |
1.1 |
} |
2598 |
|
|
|
2599 |
|
|
/** |
2600 |
jsr166 |
1.4 |
* Returns {@code true} if all worker threads are currently idle. |
2601 |
|
|
* An idle worker is one that cannot obtain a task to execute |
2602 |
|
|
* because none are available to steal from other threads, and |
2603 |
|
|
* there are no pending submissions to the pool. This method is |
2604 |
|
|
* conservative; it might not return {@code true} immediately upon |
2605 |
|
|
* idleness of all threads, but will eventually become true if |
2606 |
|
|
* threads remain inactive. |
2607 |
jsr166 |
1.1 |
* |
2608 |
jsr166 |
1.4 |
* @return {@code true} if all threads are currently idle |
2609 |
jsr166 |
1.1 |
*/ |
2610 |
|
|
public boolean isQuiescent() { |
2611 |
dl |
1.300 |
for (;;) { |
2612 |
|
|
long c = ctl; |
2613 |
|
|
int md = mode, pc = md & SMASK; |
2614 |
dl |
1.310 |
int tc = pc + (short)(c >>> TC_SHIFT); |
2615 |
dl |
1.300 |
int rc = pc + (int)(c >> RC_SHIFT); |
2616 |
|
|
if ((md & (STOP | TERMINATED)) != 0) |
2617 |
|
|
return true; |
2618 |
|
|
else if (rc > 0) |
2619 |
|
|
return false; |
2620 |
|
|
else { |
2621 |
|
|
WorkQueue[] ws; WorkQueue v; |
2622 |
|
|
if ((ws = workQueues) != null) { |
2623 |
|
|
for (int i = 1; i < ws.length; i += 2) { |
2624 |
|
|
if ((v = ws[i]) != null) { |
2625 |
|
|
if ((v.source & QUIET) == 0) |
2626 |
|
|
return false; |
2627 |
|
|
--tc; |
2628 |
|
|
} |
2629 |
|
|
} |
2630 |
|
|
} |
2631 |
|
|
if (tc == 0 && ctl == c) |
2632 |
|
|
return true; |
2633 |
|
|
} |
2634 |
|
|
} |
2635 |
jsr166 |
1.1 |
} |
2636 |
|
|
|
2637 |
|
|
/** |
2638 |
|
|
* Returns an estimate of the total number of tasks stolen from |
2639 |
|
|
* one thread's work queue by another. The reported value |
2640 |
|
|
* underestimates the actual total number of steals when the pool |
2641 |
|
|
* is not quiescent. This value may be useful for monitoring and |
2642 |
|
|
* tuning fork/join programs: in general, steal counts should be |
2643 |
|
|
* high enough to keep threads busy, but low enough to avoid |
2644 |
|
|
* overhead and contention across threads. |
2645 |
|
|
* |
2646 |
|
|
* @return the number of steals |
2647 |
|
|
*/ |
2648 |
|
|
public long getStealCount() { |
2649 |
dl |
1.300 |
long count = stealCount; |
2650 |
dl |
1.78 |
WorkQueue[] ws; WorkQueue w; |
2651 |
|
|
if ((ws = workQueues) != null) { |
2652 |
dl |
1.86 |
for (int i = 1; i < ws.length; i += 2) { |
2653 |
dl |
1.78 |
if ((w = ws[i]) != null) |
2654 |
dl |
1.300 |
count += (long)w.nsteals & 0xffffffffL; |
2655 |
dl |
1.78 |
} |
2656 |
|
|
} |
2657 |
|
|
return count; |
2658 |
jsr166 |
1.1 |
} |
2659 |
|
|
|
2660 |
|
|
/** |
2661 |
|
|
* Returns an estimate of the total number of tasks currently held |
2662 |
|
|
* in queues by worker threads (but not including tasks submitted |
2663 |
|
|
* to the pool that have not begun executing). This value is only |
2664 |
|
|
* an approximation, obtained by iterating across all threads in |
2665 |
|
|
* the pool. This method may be useful for tuning task |
2666 |
|
|
* granularities. |
2667 |
|
|
* |
2668 |
|
|
* @return the number of queued tasks |
2669 |
|
|
*/ |
2670 |
|
|
public long getQueuedTaskCount() { |
2671 |
|
|
long count = 0; |
2672 |
dl |
1.78 |
WorkQueue[] ws; WorkQueue w; |
2673 |
|
|
if ((ws = workQueues) != null) { |
2674 |
dl |
1.86 |
for (int i = 1; i < ws.length; i += 2) { |
2675 |
dl |
1.78 |
if ((w = ws[i]) != null) |
2676 |
|
|
count += w.queueSize(); |
2677 |
|
|
} |
2678 |
dl |
1.52 |
} |
2679 |
jsr166 |
1.1 |
return count; |
2680 |
|
|
} |
2681 |
|
|
|
2682 |
|
|
/** |
2683 |
jsr166 |
1.8 |
* Returns an estimate of the number of tasks submitted to this |
2684 |
dl |
1.55 |
* pool that have not yet begun executing. This method may take |
2685 |
dl |
1.52 |
* time proportional to the number of submissions. |
2686 |
jsr166 |
1.1 |
* |
2687 |
|
|
* @return the number of queued submissions |
2688 |
|
|
*/ |
2689 |
|
|
public int getQueuedSubmissionCount() { |
2690 |
dl |
1.78 |
int count = 0; |
2691 |
|
|
WorkQueue[] ws; WorkQueue w; |
2692 |
|
|
if ((ws = workQueues) != null) { |
2693 |
dl |
1.86 |
for (int i = 0; i < ws.length; i += 2) { |
2694 |
dl |
1.78 |
if ((w = ws[i]) != null) |
2695 |
|
|
count += w.queueSize(); |
2696 |
|
|
} |
2697 |
|
|
} |
2698 |
|
|
return count; |
2699 |
jsr166 |
1.1 |
} |
2700 |
|
|
|
2701 |
|
|
/** |
2702 |
jsr166 |
1.4 |
* Returns {@code true} if there are any tasks submitted to this |
2703 |
|
|
* pool that have not yet begun executing. |
2704 |
jsr166 |
1.1 |
* |
2705 |
|
|
* @return {@code true} if there are any queued submissions |
2706 |
|
|
*/ |
2707 |
|
|
public boolean hasQueuedSubmissions() { |
2708 |
dl |
1.78 |
WorkQueue[] ws; WorkQueue w; |
2709 |
|
|
if ((ws = workQueues) != null) { |
2710 |
dl |
1.86 |
for (int i = 0; i < ws.length; i += 2) { |
2711 |
dl |
1.115 |
if ((w = ws[i]) != null && !w.isEmpty()) |
2712 |
dl |
1.78 |
return true; |
2713 |
|
|
} |
2714 |
|
|
} |
2715 |
|
|
return false; |
2716 |
jsr166 |
1.1 |
} |
2717 |
|
|
|
2718 |
|
|
/** |
2719 |
|
|
* Removes and returns the next unexecuted submission if one is |
2720 |
|
|
* available. This method may be useful in extensions to this |
2721 |
|
|
* class that re-assign work in systems with multiple pools. |
2722 |
|
|
* |
2723 |
jsr166 |
1.4 |
* @return the next submission, or {@code null} if none |
2724 |
jsr166 |
1.1 |
*/ |
2725 |
|
|
protected ForkJoinTask<?> pollSubmission() { |
2726 |
dl |
1.300 |
return pollScan(true); |
2727 |
jsr166 |
1.1 |
} |
2728 |
|
|
|
2729 |
|
|
/** |
2730 |
|
|
* Removes all available unexecuted submitted and forked tasks |
2731 |
|
|
* from scheduling queues and adds them to the given collection, |
2732 |
|
|
* without altering their execution status. These may include |
2733 |
jsr166 |
1.8 |
* artificially generated or wrapped tasks. This method is |
2734 |
|
|
* designed to be invoked only when the pool is known to be |
2735 |
jsr166 |
1.1 |
* quiescent. Invocations at other times may not remove all |
2736 |
|
|
* tasks. A failure encountered while attempting to add elements |
2737 |
|
|
* to collection {@code c} may result in elements being in |
2738 |
|
|
* neither, either or both collections when the associated |
2739 |
|
|
* exception is thrown. The behavior of this operation is |
2740 |
|
|
* undefined if the specified collection is modified while the |
2741 |
|
|
* operation is in progress. |
2742 |
|
|
* |
2743 |
|
|
* @param c the collection to transfer elements into |
2744 |
|
|
* @return the number of elements transferred |
2745 |
|
|
*/ |
2746 |
jsr166 |
1.5 |
protected int drainTasksTo(Collection<? super ForkJoinTask<?>> c) { |
2747 |
dl |
1.52 |
int count = 0; |
2748 |
dl |
1.78 |
WorkQueue[] ws; WorkQueue w; ForkJoinTask<?> t; |
2749 |
|
|
if ((ws = workQueues) != null) { |
2750 |
dl |
1.86 |
for (int i = 0; i < ws.length; ++i) { |
2751 |
dl |
1.78 |
if ((w = ws[i]) != null) { |
2752 |
|
|
while ((t = w.poll()) != null) { |
2753 |
|
|
c.add(t); |
2754 |
|
|
++count; |
2755 |
|
|
} |
2756 |
|
|
} |
2757 |
dl |
1.52 |
} |
2758 |
|
|
} |
2759 |
dl |
1.18 |
return count; |
2760 |
|
|
} |
2761 |
|
|
|
2762 |
|
|
/** |
2763 |
jsr166 |
1.1 |
* Returns a string identifying this pool, as well as its state, |
2764 |
|
|
* including indications of run state, parallelism level, and |
2765 |
|
|
* worker and task counts. |
2766 |
|
|
* |
2767 |
|
|
* @return a string identifying this pool, as well as its state |
2768 |
|
|
*/ |
2769 |
|
|
public String toString() { |
2770 |
dl |
1.86 |
// Use a single pass through workQueues to collect counts |
2771 |
|
|
long qt = 0L, qs = 0L; int rc = 0; |
2772 |
dl |
1.300 |
long st = stealCount; |
2773 |
dl |
1.86 |
WorkQueue[] ws; WorkQueue w; |
2774 |
|
|
if ((ws = workQueues) != null) { |
2775 |
|
|
for (int i = 0; i < ws.length; ++i) { |
2776 |
|
|
if ((w = ws[i]) != null) { |
2777 |
|
|
int size = w.queueSize(); |
2778 |
|
|
if ((i & 1) == 0) |
2779 |
|
|
qs += size; |
2780 |
|
|
else { |
2781 |
|
|
qt += size; |
2782 |
dl |
1.300 |
st += (long)w.nsteals & 0xffffffffL; |
2783 |
dl |
1.86 |
if (w.isApparentlyUnblocked()) |
2784 |
|
|
++rc; |
2785 |
|
|
} |
2786 |
|
|
} |
2787 |
|
|
} |
2788 |
|
|
} |
2789 |
dl |
1.300 |
|
2790 |
|
|
int md = mode; |
2791 |
|
|
int pc = (md & SMASK); |
2792 |
|
|
long c = ctl; |
2793 |
dl |
1.52 |
int tc = pc + (short)(c >>> TC_SHIFT); |
2794 |
dl |
1.300 |
int ac = pc + (int)(c >> RC_SHIFT); |
2795 |
dl |
1.78 |
if (ac < 0) // ignore transient negative |
2796 |
|
|
ac = 0; |
2797 |
dl |
1.300 |
String level = ((md & TERMINATED) != 0 ? "Terminated" : |
2798 |
|
|
(md & STOP) != 0 ? "Terminating" : |
2799 |
|
|
(md & SHUTDOWN) != 0 ? "Shutting down" : |
2800 |
dl |
1.200 |
"Running"); |
2801 |
jsr166 |
1.1 |
return super.toString() + |
2802 |
dl |
1.52 |
"[" + level + |
2803 |
dl |
1.14 |
", parallelism = " + pc + |
2804 |
|
|
", size = " + tc + |
2805 |
|
|
", active = " + ac + |
2806 |
|
|
", running = " + rc + |
2807 |
jsr166 |
1.1 |
", steals = " + st + |
2808 |
|
|
", tasks = " + qt + |
2809 |
|
|
", submissions = " + qs + |
2810 |
|
|
"]"; |
2811 |
|
|
} |
2812 |
|
|
|
2813 |
|
|
/** |
2814 |
dl |
1.100 |
* Possibly initiates an orderly shutdown in which previously |
2815 |
|
|
* submitted tasks are executed, but no new tasks will be |
2816 |
|
|
* accepted. Invocation has no effect on execution state if this |
2817 |
jsr166 |
1.137 |
* is the {@link #commonPool()}, and no additional effect if |
2818 |
dl |
1.100 |
* already shut down. Tasks that are in the process of being |
2819 |
|
|
* submitted concurrently during the course of this method may or |
2820 |
|
|
* may not be rejected. |
2821 |
jsr166 |
1.1 |
* |
2822 |
|
|
* @throws SecurityException if a security manager exists and |
2823 |
|
|
* the caller is not permitted to modify threads |
2824 |
|
|
* because it does not hold {@link |
2825 |
|
|
* java.lang.RuntimePermission}{@code ("modifyThread")} |
2826 |
|
|
*/ |
2827 |
|
|
public void shutdown() { |
2828 |
|
|
checkPermission(); |
2829 |
dl |
1.105 |
tryTerminate(false, true); |
2830 |
jsr166 |
1.1 |
} |
2831 |
|
|
|
2832 |
|
|
/** |
2833 |
dl |
1.100 |
* Possibly attempts to cancel and/or stop all tasks, and reject |
2834 |
|
|
* all subsequently submitted tasks. Invocation has no effect on |
2835 |
jsr166 |
1.137 |
* execution state if this is the {@link #commonPool()}, and no |
2836 |
dl |
1.100 |
* additional effect if already shut down. Otherwise, tasks that |
2837 |
|
|
* are in the process of being submitted or executed concurrently |
2838 |
|
|
* during the course of this method may or may not be |
2839 |
|
|
* rejected. This method cancels both existing and unexecuted |
2840 |
|
|
* tasks, in order to permit termination in the presence of task |
2841 |
|
|
* dependencies. So the method always returns an empty list |
2842 |
|
|
* (unlike the case for some other Executors). |
2843 |
jsr166 |
1.1 |
* |
2844 |
|
|
* @return an empty list |
2845 |
|
|
* @throws SecurityException if a security manager exists and |
2846 |
|
|
* the caller is not permitted to modify threads |
2847 |
|
|
* because it does not hold {@link |
2848 |
|
|
* java.lang.RuntimePermission}{@code ("modifyThread")} |
2849 |
|
|
*/ |
2850 |
|
|
public List<Runnable> shutdownNow() { |
2851 |
|
|
checkPermission(); |
2852 |
dl |
1.105 |
tryTerminate(true, true); |
2853 |
jsr166 |
1.1 |
return Collections.emptyList(); |
2854 |
|
|
} |
2855 |
|
|
|
2856 |
|
|
/** |
2857 |
|
|
* Returns {@code true} if all tasks have completed following shut down. |
2858 |
|
|
* |
2859 |
|
|
* @return {@code true} if all tasks have completed following shut down |
2860 |
|
|
*/ |
2861 |
|
|
public boolean isTerminated() { |
2862 |
dl |
1.300 |
return (mode & TERMINATED) != 0; |
2863 |
jsr166 |
1.1 |
} |
2864 |
|
|
|
2865 |
|
|
/** |
2866 |
|
|
* Returns {@code true} if the process of termination has |
2867 |
jsr166 |
1.9 |
* commenced but not yet completed. This method may be useful for |
2868 |
|
|
* debugging. A return of {@code true} reported a sufficient |
2869 |
|
|
* period after shutdown may indicate that submitted tasks have |
2870 |
jsr166 |
1.119 |
* ignored or suppressed interruption, or are waiting for I/O, |
2871 |
dl |
1.49 |
* causing this executor not to properly terminate. (See the |
2872 |
|
|
* advisory notes for class {@link ForkJoinTask} stating that |
2873 |
|
|
* tasks should not normally entail blocking operations. But if |
2874 |
|
|
* they do, they must abort them on interrupt.) |
2875 |
jsr166 |
1.1 |
* |
2876 |
jsr166 |
1.9 |
* @return {@code true} if terminating but not yet terminated |
2877 |
jsr166 |
1.1 |
*/ |
2878 |
|
|
public boolean isTerminating() { |
2879 |
dl |
1.300 |
int md = mode; |
2880 |
|
|
return (md & STOP) != 0 && (md & TERMINATED) == 0; |
2881 |
jsr166 |
1.1 |
} |
2882 |
|
|
|
2883 |
|
|
/** |
2884 |
|
|
* Returns {@code true} if this pool has been shut down. |
2885 |
|
|
* |
2886 |
|
|
* @return {@code true} if this pool has been shut down |
2887 |
|
|
*/ |
2888 |
|
|
public boolean isShutdown() { |
2889 |
dl |
1.300 |
return (mode & SHUTDOWN) != 0; |
2890 |
jsr166 |
1.9 |
} |
2891 |
|
|
|
2892 |
|
|
/** |
2893 |
dl |
1.105 |
* Blocks until all tasks have completed execution after a |
2894 |
|
|
* shutdown request, or the timeout occurs, or the current thread |
2895 |
dl |
1.134 |
* is interrupted, whichever happens first. Because the {@link |
2896 |
|
|
* #commonPool()} never terminates until program shutdown, when |
2897 |
|
|
* applied to the common pool, this method is equivalent to {@link |
2898 |
jsr166 |
1.158 |
* #awaitQuiescence(long, TimeUnit)} but always returns {@code false}. |
2899 |
jsr166 |
1.1 |
* |
2900 |
|
|
* @param timeout the maximum time to wait |
2901 |
|
|
* @param unit the time unit of the timeout argument |
2902 |
|
|
* @return {@code true} if this executor terminated and |
2903 |
|
|
* {@code false} if the timeout elapsed before termination |
2904 |
|
|
* @throws InterruptedException if interrupted while waiting |
2905 |
|
|
*/ |
2906 |
|
|
public boolean awaitTermination(long timeout, TimeUnit unit) |
2907 |
|
|
throws InterruptedException { |
2908 |
dl |
1.134 |
if (Thread.interrupted()) |
2909 |
|
|
throw new InterruptedException(); |
2910 |
|
|
if (this == common) { |
2911 |
|
|
awaitQuiescence(timeout, unit); |
2912 |
|
|
return false; |
2913 |
|
|
} |
2914 |
dl |
1.52 |
long nanos = unit.toNanos(timeout); |
2915 |
dl |
1.101 |
if (isTerminated()) |
2916 |
|
|
return true; |
2917 |
dl |
1.183 |
if (nanos <= 0L) |
2918 |
|
|
return false; |
2919 |
|
|
long deadline = System.nanoTime() + nanos; |
2920 |
jsr166 |
1.103 |
synchronized (this) { |
2921 |
jsr166 |
1.184 |
for (;;) { |
2922 |
dl |
1.183 |
if (isTerminated()) |
2923 |
|
|
return true; |
2924 |
|
|
if (nanos <= 0L) |
2925 |
|
|
return false; |
2926 |
|
|
long millis = TimeUnit.NANOSECONDS.toMillis(nanos); |
2927 |
|
|
wait(millis > 0L ? millis : 1L); |
2928 |
|
|
nanos = deadline - System.nanoTime(); |
2929 |
dl |
1.52 |
} |
2930 |
dl |
1.18 |
} |
2931 |
jsr166 |
1.1 |
} |
2932 |
|
|
|
2933 |
|
|
/** |
2934 |
dl |
1.134 |
* If called by a ForkJoinTask operating in this pool, equivalent |
2935 |
|
|
* in effect to {@link ForkJoinTask#helpQuiesce}. Otherwise, |
2936 |
|
|
* waits and/or attempts to assist performing tasks until this |
2937 |
|
|
* pool {@link #isQuiescent} or the indicated timeout elapses. |
2938 |
|
|
* |
2939 |
|
|
* @param timeout the maximum time to wait |
2940 |
|
|
* @param unit the time unit of the timeout argument |
2941 |
|
|
* @return {@code true} if quiescent; {@code false} if the |
2942 |
|
|
* timeout elapsed. |
2943 |
|
|
*/ |
2944 |
|
|
public boolean awaitQuiescence(long timeout, TimeUnit unit) { |
2945 |
|
|
long nanos = unit.toNanos(timeout); |
2946 |
|
|
ForkJoinWorkerThread wt; |
2947 |
|
|
Thread thread = Thread.currentThread(); |
2948 |
|
|
if ((thread instanceof ForkJoinWorkerThread) && |
2949 |
|
|
(wt = (ForkJoinWorkerThread)thread).pool == this) { |
2950 |
|
|
helpQuiescePool(wt.workQueue); |
2951 |
|
|
return true; |
2952 |
|
|
} |
2953 |
dl |
1.300 |
else { |
2954 |
|
|
for (long startTime = System.nanoTime();;) { |
2955 |
|
|
ForkJoinTask<?> t; |
2956 |
|
|
if ((t = pollScan(false)) != null) |
2957 |
|
|
t.doExec(); |
2958 |
|
|
else if (isQuiescent()) |
2959 |
|
|
return true; |
2960 |
|
|
else if ((System.nanoTime() - startTime) > nanos) |
2961 |
dl |
1.134 |
return false; |
2962 |
dl |
1.300 |
else |
2963 |
|
|
Thread.yield(); // cannot block |
2964 |
dl |
1.134 |
} |
2965 |
|
|
} |
2966 |
|
|
} |
2967 |
|
|
|
2968 |
|
|
/** |
2969 |
|
|
* Waits and/or attempts to assist performing tasks indefinitely |
2970 |
jsr166 |
1.141 |
* until the {@link #commonPool()} {@link #isQuiescent}. |
2971 |
dl |
1.134 |
*/ |
2972 |
dl |
1.136 |
static void quiesceCommonPool() { |
2973 |
dl |
1.134 |
common.awaitQuiescence(Long.MAX_VALUE, TimeUnit.NANOSECONDS); |
2974 |
|
|
} |
2975 |
|
|
|
2976 |
|
|
/** |
2977 |
jsr166 |
1.1 |
* Interface for extending managed parallelism for tasks running |
2978 |
jsr166 |
1.8 |
* in {@link ForkJoinPool}s. |
2979 |
|
|
* |
2980 |
dl |
1.19 |
* <p>A {@code ManagedBlocker} provides two methods. Method |
2981 |
jsr166 |
1.218 |
* {@link #isReleasable} must return {@code true} if blocking is |
2982 |
|
|
* not necessary. Method {@link #block} blocks the current thread |
2983 |
dl |
1.19 |
* if necessary (perhaps internally invoking {@code isReleasable} |
2984 |
dl |
1.54 |
* before actually blocking). These actions are performed by any |
2985 |
jsr166 |
1.157 |
* thread invoking {@link ForkJoinPool#managedBlock(ManagedBlocker)}. |
2986 |
|
|
* The unusual methods in this API accommodate synchronizers that |
2987 |
|
|
* may, but don't usually, block for long periods. Similarly, they |
2988 |
dl |
1.54 |
* allow more efficient internal handling of cases in which |
2989 |
|
|
* additional workers may be, but usually are not, needed to |
2990 |
|
|
* ensure sufficient parallelism. Toward this end, |
2991 |
|
|
* implementations of method {@code isReleasable} must be amenable |
2992 |
|
|
* to repeated invocation. |
2993 |
jsr166 |
1.1 |
* |
2994 |
|
|
* <p>For example, here is a ManagedBlocker based on a |
2995 |
|
|
* ReentrantLock: |
2996 |
jsr166 |
1.239 |
* <pre> {@code |
2997 |
jsr166 |
1.1 |
* class ManagedLocker implements ManagedBlocker { |
2998 |
|
|
* final ReentrantLock lock; |
2999 |
|
|
* boolean hasLock = false; |
3000 |
|
|
* ManagedLocker(ReentrantLock lock) { this.lock = lock; } |
3001 |
|
|
* public boolean block() { |
3002 |
|
|
* if (!hasLock) |
3003 |
|
|
* lock.lock(); |
3004 |
|
|
* return true; |
3005 |
|
|
* } |
3006 |
|
|
* public boolean isReleasable() { |
3007 |
|
|
* return hasLock || (hasLock = lock.tryLock()); |
3008 |
|
|
* } |
3009 |
|
|
* }}</pre> |
3010 |
dl |
1.19 |
* |
3011 |
|
|
* <p>Here is a class that possibly blocks waiting for an |
3012 |
|
|
* item on a given queue: |
3013 |
jsr166 |
1.239 |
* <pre> {@code |
3014 |
dl |
1.19 |
* class QueueTaker<E> implements ManagedBlocker { |
3015 |
|
|
* final BlockingQueue<E> queue; |
3016 |
|
|
* volatile E item = null; |
3017 |
|
|
* QueueTaker(BlockingQueue<E> q) { this.queue = q; } |
3018 |
|
|
* public boolean block() throws InterruptedException { |
3019 |
|
|
* if (item == null) |
3020 |
dl |
1.23 |
* item = queue.take(); |
3021 |
dl |
1.19 |
* return true; |
3022 |
|
|
* } |
3023 |
|
|
* public boolean isReleasable() { |
3024 |
dl |
1.23 |
* return item != null || (item = queue.poll()) != null; |
3025 |
dl |
1.19 |
* } |
3026 |
|
|
* public E getItem() { // call after pool.managedBlock completes |
3027 |
|
|
* return item; |
3028 |
|
|
* } |
3029 |
|
|
* }}</pre> |
3030 |
jsr166 |
1.1 |
*/ |
3031 |
|
|
public static interface ManagedBlocker { |
3032 |
|
|
/** |
3033 |
|
|
* Possibly blocks the current thread, for example waiting for |
3034 |
|
|
* a lock or condition. |
3035 |
|
|
* |
3036 |
jsr166 |
1.4 |
* @return {@code true} if no additional blocking is necessary |
3037 |
|
|
* (i.e., if isReleasable would return true) |
3038 |
jsr166 |
1.1 |
* @throws InterruptedException if interrupted while waiting |
3039 |
|
|
* (the method is not required to do so, but is allowed to) |
3040 |
|
|
*/ |
3041 |
|
|
boolean block() throws InterruptedException; |
3042 |
|
|
|
3043 |
|
|
/** |
3044 |
jsr166 |
1.4 |
* Returns {@code true} if blocking is unnecessary. |
3045 |
jsr166 |
1.154 |
* @return {@code true} if blocking is unnecessary |
3046 |
jsr166 |
1.1 |
*/ |
3047 |
|
|
boolean isReleasable(); |
3048 |
|
|
} |
3049 |
|
|
|
3050 |
|
|
/** |
3051 |
jsr166 |
1.217 |
* Runs the given possibly blocking task. When {@linkplain |
3052 |
|
|
* ForkJoinTask#inForkJoinPool() running in a ForkJoinPool}, this |
3053 |
|
|
* method possibly arranges for a spare thread to be activated if |
3054 |
|
|
* necessary to ensure sufficient parallelism while the current |
3055 |
|
|
* thread is blocked in {@link ManagedBlocker#block blocker.block()}. |
3056 |
jsr166 |
1.1 |
* |
3057 |
jsr166 |
1.217 |
* <p>This method repeatedly calls {@code blocker.isReleasable()} and |
3058 |
|
|
* {@code blocker.block()} until either method returns {@code true}. |
3059 |
|
|
* Every call to {@code blocker.block()} is preceded by a call to |
3060 |
|
|
* {@code blocker.isReleasable()} that returned {@code false}. |
3061 |
|
|
* |
3062 |
|
|
* <p>If not running in a ForkJoinPool, this method is |
3063 |
jsr166 |
1.8 |
* behaviorally equivalent to |
3064 |
jsr166 |
1.239 |
* <pre> {@code |
3065 |
jsr166 |
1.1 |
* while (!blocker.isReleasable()) |
3066 |
|
|
* if (blocker.block()) |
3067 |
jsr166 |
1.217 |
* break;}</pre> |
3068 |
jsr166 |
1.8 |
* |
3069 |
jsr166 |
1.217 |
* If running in a ForkJoinPool, the pool may first be expanded to |
3070 |
|
|
* ensure sufficient parallelism available during the call to |
3071 |
|
|
* {@code blocker.block()}. |
3072 |
jsr166 |
1.1 |
* |
3073 |
jsr166 |
1.217 |
* @param blocker the blocker task |
3074 |
|
|
* @throws InterruptedException if {@code blocker.block()} did so |
3075 |
jsr166 |
1.1 |
*/ |
3076 |
dl |
1.18 |
public static void managedBlock(ManagedBlocker blocker) |
3077 |
jsr166 |
1.1 |
throws InterruptedException { |
3078 |
dl |
1.200 |
ForkJoinPool p; |
3079 |
|
|
ForkJoinWorkerThread wt; |
3080 |
dl |
1.300 |
WorkQueue w; |
3081 |
jsr166 |
1.1 |
Thread t = Thread.currentThread(); |
3082 |
dl |
1.200 |
if ((t instanceof ForkJoinWorkerThread) && |
3083 |
dl |
1.300 |
(p = (wt = (ForkJoinWorkerThread)t).pool) != null && |
3084 |
|
|
(w = wt.workQueue) != null) { |
3085 |
|
|
int block; |
3086 |
dl |
1.172 |
while (!blocker.isReleasable()) { |
3087 |
dl |
1.300 |
if ((block = p.tryCompensate(w)) != 0) { |
3088 |
dl |
1.105 |
try { |
3089 |
|
|
do {} while (!blocker.isReleasable() && |
3090 |
|
|
!blocker.block()); |
3091 |
|
|
} finally { |
3092 |
dl |
1.314 |
CTL.getAndAdd(p, (block > 0) ? RC_UNIT : 0L); |
3093 |
dl |
1.105 |
} |
3094 |
|
|
break; |
3095 |
dl |
1.78 |
} |
3096 |
|
|
} |
3097 |
dl |
1.18 |
} |
3098 |
dl |
1.105 |
else { |
3099 |
|
|
do {} while (!blocker.isReleasable() && |
3100 |
|
|
!blocker.block()); |
3101 |
|
|
} |
3102 |
jsr166 |
1.1 |
} |
3103 |
|
|
|
3104 |
dl |
1.310 |
/** |
3105 |
|
|
* If the given executor is a ForkJoinPool, poll and execute |
3106 |
|
|
* AsynchronousCompletionTasks from worker's queue until none are |
3107 |
|
|
* available or blocker is released. |
3108 |
|
|
*/ |
3109 |
|
|
static void helpAsyncBlocker(Executor e, ManagedBlocker blocker) { |
3110 |
|
|
if (blocker != null && (e instanceof ForkJoinPool)) { |
3111 |
|
|
WorkQueue w; ForkJoinWorkerThread wt; WorkQueue[] ws; int r, n; |
3112 |
|
|
ForkJoinPool p = (ForkJoinPool)e; |
3113 |
|
|
Thread thread = Thread.currentThread(); |
3114 |
|
|
if (thread instanceof ForkJoinWorkerThread && |
3115 |
|
|
(wt = (ForkJoinWorkerThread)thread).pool == p) |
3116 |
|
|
w = wt.workQueue; |
3117 |
|
|
else if ((r = ThreadLocalRandom.getProbe()) != 0 && |
3118 |
|
|
(ws = p.workQueues) != null && (n = ws.length) > 0) |
3119 |
|
|
w = ws[(n - 1) & r & SQMASK]; |
3120 |
|
|
else |
3121 |
|
|
w = null; |
3122 |
|
|
if (w != null) { |
3123 |
|
|
for (;;) { |
3124 |
|
|
int b = w.base, s = w.top, d, al; ForkJoinTask<?>[] a; |
3125 |
|
|
if ((a = w.array) != null && (d = b - s) < 0 && |
3126 |
|
|
(al = a.length) > 0) { |
3127 |
|
|
int index = (al - 1) & b; |
3128 |
|
|
ForkJoinTask<?> t = (ForkJoinTask<?>) |
3129 |
dl |
1.316 |
QA.getAcquire(a, index); |
3130 |
dl |
1.310 |
if (blocker.isReleasable()) |
3131 |
|
|
break; |
3132 |
|
|
else if (b++ == w.base) { |
3133 |
|
|
if (t == null) { |
3134 |
|
|
if (d == -1) |
3135 |
|
|
break; |
3136 |
|
|
} |
3137 |
|
|
else if (!(t instanceof CompletableFuture. |
3138 |
|
|
AsynchronousCompletionTask)) |
3139 |
|
|
break; |
3140 |
dl |
1.314 |
else if (QA.compareAndSet(a, index, t, null)) { |
3141 |
dl |
1.310 |
w.base = b; |
3142 |
|
|
t.doExec(); |
3143 |
|
|
} |
3144 |
|
|
} |
3145 |
|
|
} |
3146 |
|
|
else |
3147 |
|
|
break; |
3148 |
|
|
} |
3149 |
|
|
} |
3150 |
|
|
} |
3151 |
|
|
} |
3152 |
|
|
|
3153 |
jsr166 |
1.7 |
// AbstractExecutorService overrides. These rely on undocumented |
3154 |
|
|
// fact that ForkJoinTask.adapt returns ForkJoinTasks that also |
3155 |
|
|
// implement RunnableFuture. |
3156 |
jsr166 |
1.1 |
|
3157 |
|
|
protected <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value) { |
3158 |
dl |
1.90 |
return new ForkJoinTask.AdaptedRunnable<T>(runnable, value); |
3159 |
jsr166 |
1.1 |
} |
3160 |
|
|
|
3161 |
|
|
protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) { |
3162 |
dl |
1.90 |
return new ForkJoinTask.AdaptedCallable<T>(callable); |
3163 |
jsr166 |
1.1 |
} |
3164 |
|
|
|
3165 |
dl |
1.314 |
// VarHandle mechanics |
3166 |
|
|
private static final VarHandle CTL; |
3167 |
|
|
private static final VarHandle MODE; |
3168 |
|
|
private static final VarHandle QA; |
3169 |
dl |
1.52 |
|
3170 |
|
|
static { |
3171 |
jsr166 |
1.3 |
try { |
3172 |
dl |
1.314 |
MethodHandles.Lookup l = MethodHandles.lookup(); |
3173 |
|
|
CTL = l.findVarHandle(ForkJoinPool.class, "ctl", long.class); |
3174 |
|
|
MODE = l.findVarHandle(ForkJoinPool.class, "mode", int.class); |
3175 |
|
|
QA = MethodHandles.arrayElementVarHandle(ForkJoinTask[].class); |
3176 |
jsr166 |
1.231 |
} catch (ReflectiveOperationException e) { |
3177 |
dl |
1.52 |
throw new Error(e); |
3178 |
|
|
} |
3179 |
dl |
1.105 |
|
3180 |
dl |
1.243 |
// Reduce the risk of rare disastrous classloading in first call to |
3181 |
|
|
// LockSupport.park: https://bugs.openjdk.java.net/browse/JDK-8074773 |
3182 |
|
|
Class<?> ensureLoaded = LockSupport.class; |
3183 |
|
|
|
3184 |
jsr166 |
1.273 |
int commonMaxSpares = DEFAULT_COMMON_MAX_SPARES; |
3185 |
|
|
try { |
3186 |
|
|
String p = System.getProperty |
3187 |
|
|
("java.util.concurrent.ForkJoinPool.common.maximumSpares"); |
3188 |
|
|
if (p != null) |
3189 |
|
|
commonMaxSpares = Integer.parseInt(p); |
3190 |
|
|
} catch (Exception ignore) {} |
3191 |
|
|
COMMON_MAX_SPARES = commonMaxSpares; |
3192 |
|
|
|
3193 |
dl |
1.152 |
defaultForkJoinWorkerThreadFactory = |
3194 |
dl |
1.112 |
new DefaultForkJoinWorkerThreadFactory(); |
3195 |
dl |
1.115 |
modifyThreadPermission = new RuntimePermission("modifyThread"); |
3196 |
|
|
|
3197 |
dl |
1.152 |
common = java.security.AccessController.doPrivileged |
3198 |
|
|
(new java.security.PrivilegedAction<ForkJoinPool>() { |
3199 |
dl |
1.300 |
public ForkJoinPool run() { |
3200 |
|
|
return new ForkJoinPool((byte)0); }}); |
3201 |
jsr166 |
1.275 |
|
3202 |
dl |
1.310 |
COMMON_PARALLELISM = Math.max(common.mode & SMASK, 1); |
3203 |
jsr166 |
1.3 |
} |
3204 |
dl |
1.52 |
|
3205 |
dl |
1.197 |
/** |
3206 |
jsr166 |
1.279 |
* Factory for innocuous worker threads. |
3207 |
dl |
1.197 |
*/ |
3208 |
jsr166 |
1.278 |
private static final class InnocuousForkJoinWorkerThreadFactory |
3209 |
dl |
1.197 |
implements ForkJoinWorkerThreadFactory { |
3210 |
|
|
|
3211 |
|
|
/** |
3212 |
|
|
* An ACC to restrict permissions for the factory itself. |
3213 |
|
|
* The constructed workers have no permissions set. |
3214 |
|
|
*/ |
3215 |
jsr166 |
1.324 |
private static final AccessControlContext INNOCUOUS_ACC; |
3216 |
dl |
1.197 |
static { |
3217 |
|
|
Permissions innocuousPerms = new Permissions(); |
3218 |
|
|
innocuousPerms.add(modifyThreadPermission); |
3219 |
|
|
innocuousPerms.add(new RuntimePermission( |
3220 |
|
|
"enableContextClassLoaderOverride")); |
3221 |
|
|
innocuousPerms.add(new RuntimePermission( |
3222 |
|
|
"modifyThreadGroup")); |
3223 |
jsr166 |
1.324 |
INNOCUOUS_ACC = new AccessControlContext(new ProtectionDomain[] { |
3224 |
dl |
1.197 |
new ProtectionDomain(null, innocuousPerms) |
3225 |
|
|
}); |
3226 |
|
|
} |
3227 |
|
|
|
3228 |
|
|
public final ForkJoinWorkerThread newThread(ForkJoinPool pool) { |
3229 |
jsr166 |
1.227 |
return java.security.AccessController.doPrivileged( |
3230 |
|
|
new java.security.PrivilegedAction<ForkJoinWorkerThread>() { |
3231 |
dl |
1.197 |
public ForkJoinWorkerThread run() { |
3232 |
|
|
return new ForkJoinWorkerThread. |
3233 |
|
|
InnocuousForkJoinWorkerThread(pool); |
3234 |
jsr166 |
1.324 |
}}, INNOCUOUS_ACC); |
3235 |
dl |
1.197 |
} |
3236 |
|
|
} |
3237 |
|
|
|
3238 |
jsr166 |
1.1 |
} |