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