1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
|
package jsr166y; |
8 |
– |
import java.util.*; |
9 |
– |
import java.util.concurrent.*; |
10 |
– |
import java.util.concurrent.atomic.*; |
11 |
– |
import java.util.concurrent.locks.*; |
12 |
– |
import sun.misc.Unsafe; |
13 |
– |
import java.lang.reflect.*; |
8 |
|
|
9 |
|
/** |
10 |
< |
* A thread managed by a {@link ForkJoinPool}. This class is |
11 |
< |
* subclassable solely for the sake of adding functionality -- there |
12 |
< |
* are no overridable methods dealing with scheduling or |
13 |
< |
* execution. However, you can override initialization and termination |
14 |
< |
* methods surrounding the main task processing loop. If you do |
15 |
< |
* create such a subclass, you will also need to supply a custom |
16 |
< |
* ForkJoinWorkerThreadFactory to use it in a ForkJoinPool. |
10 |
> |
* A thread managed by a {@link ForkJoinPool}, which executes |
11 |
> |
* {@link ForkJoinTask}s. |
12 |
> |
* This class is subclassable solely for the sake of adding |
13 |
> |
* functionality -- there are no overridable methods dealing with |
14 |
> |
* scheduling or execution. However, you can override initialization |
15 |
> |
* and termination methods surrounding the main task processing loop. |
16 |
> |
* If you do create such a subclass, you will also need to supply a |
17 |
> |
* custom {@link ForkJoinPool.ForkJoinWorkerThreadFactory} to use it |
18 |
> |
* in a {@code ForkJoinPool}. |
19 |
|
* |
20 |
+ |
* @since 1.7 |
21 |
+ |
* @author Doug Lea |
22 |
|
*/ |
23 |
|
public class ForkJoinWorkerThread extends Thread { |
24 |
|
/* |
25 |
< |
* Algorithm overview: |
26 |
< |
* |
27 |
< |
* 1. Work-Stealing: Work-stealing queues are special forms of |
30 |
< |
* Deques that support only three of the four possible |
31 |
< |
* end-operations -- push, pop, and deq (aka steal), and only do |
32 |
< |
* so under the constraints that push and pop are called only from |
33 |
< |
* the owning thread, while deq may be called from other threads. |
34 |
< |
* (If you are unfamiliar with them, you probably want to read |
35 |
< |
* Herlihy and Shavit's book "The Art of Multiprocessor |
36 |
< |
* programming", chapter 16 describing these in more detail before |
37 |
< |
* proceeding.) The main work-stealing queue design is roughly |
38 |
< |
* similar to "Dynamic Circular Work-Stealing Deque" by David |
39 |
< |
* Chase and Yossi Lev, SPAA 2005 |
40 |
< |
* (http://research.sun.com/scalable/pubs/index.html). The main |
41 |
< |
* difference ultimately stems from gc requirements that we null |
42 |
< |
* out taken slots as soon as we can, to maintain as small a |
43 |
< |
* footprint as possible even in programs generating huge numbers |
44 |
< |
* of tasks. To accomplish this, we shift the CAS arbitrating pop |
45 |
< |
* vs deq (steal) from being on the indices ("base" and "sp") to |
46 |
< |
* the slots themselves (mainly via method "casSlotNull()"). So, |
47 |
< |
* both a successful pop and deq mainly entail CAS'ing a nonnull |
48 |
< |
* slot to null. Because we rely on CASes of references, we do |
49 |
< |
* not need tag bits on base or sp. They are simple ints as used |
50 |
< |
* in any circular array-based queue (see for example ArrayDeque). |
51 |
< |
* Updates to the indices must still be ordered in a way that |
52 |
< |
* guarantees that (sp - base) > 0 means the queue is empty, but |
53 |
< |
* otherwise may err on the side of possibly making the queue |
54 |
< |
* appear nonempty when a push, pop, or deq have not fully |
55 |
< |
* committed. Note that this means that the deq operation, |
56 |
< |
* considered individually, is not wait-free. One thief cannot |
57 |
< |
* successfully continue until another in-progress one (or, if |
58 |
< |
* previously empty, a push) completes. However, in the |
59 |
< |
* aggregate, we ensure at least probabilistic non-blockingness. If |
60 |
< |
* an attempted steal fails, a thief always chooses a different |
61 |
< |
* random victim target to try next. So, in order for one thief to |
62 |
< |
* progress, it suffices for any in-progress deq or new push on |
63 |
< |
* any empty queue to complete. One reason this works well here is |
64 |
< |
* that apparently-nonempty often means soon-to-be-stealable, |
65 |
< |
* which gives threads a chance to activate if necessary before |
66 |
< |
* stealing (see below). |
67 |
< |
* |
68 |
< |
* Efficient implementation of this approach currently relies on |
69 |
< |
* an uncomfortable amount of "Unsafe" mechanics. To maintain |
70 |
< |
* correct orderings, reads and writes of variable base require |
71 |
< |
* volatile ordering. Variable sp does not require volatile write |
72 |
< |
* but needs cheaper store-ordering on writes. Because they are |
73 |
< |
* protected by volatile base reads, reads of the queue array and |
74 |
< |
* its slots do not need volatile load semantics, but writes (in |
75 |
< |
* push) require store order and CASes (in pop and deq) require |
76 |
< |
* (volatile) CAS semantics. Since these combinations aren't |
77 |
< |
* supported using ordinary volatiles, the only way to accomplish |
78 |
< |
* these efficiently is to use direct Unsafe calls. (Using external |
79 |
< |
* AtomicIntegers and AtomicReferenceArrays for the indices and |
80 |
< |
* array is significantly slower because of memory locality and |
81 |
< |
* indirection effects.) Further, performance on most platforms is |
82 |
< |
* very sensitive to placement and sizing of the (resizable) queue |
83 |
< |
* array. Even though these queues don't usually become all that |
84 |
< |
* big, the initial size must be large enough to counteract cache |
85 |
< |
* contention effects across multiple queues (especially in the |
86 |
< |
* presence of GC cardmarking). Also, to improve thread-locality, |
87 |
< |
* queues are currently initialized immediately after the thread |
88 |
< |
* gets the initial signal to start processing tasks. However, |
89 |
< |
* all queue-related methods except pushTask are written in a way |
90 |
< |
* that allows them to instead be lazily allocated and/or disposed |
91 |
< |
* of when empty. All together, these low-level implementation |
92 |
< |
* choices produce as much as a factor of 4 performance |
93 |
< |
* improvement compared to naive implementations, and enable the |
94 |
< |
* processing of billions of tasks per second, sometimes at the |
95 |
< |
* expense of ugliness. |
96 |
< |
* |
97 |
< |
* 2. Run control: The primary run control is based on a global |
98 |
< |
* counter (activeCount) held by the pool. It uses an algorithm |
99 |
< |
* similar to that in Herlihy and Shavit section 17.6 to cause |
100 |
< |
* threads to eventually block when all threads declare they are |
101 |
< |
* inactive. (See variable "scans".) For this to work, threads |
102 |
< |
* must be declared active when executing tasks, and before |
103 |
< |
* stealing a task. They must be inactive before blocking on the |
104 |
< |
* Pool Barrier (awaiting a new submission or other Pool |
105 |
< |
* event). In between, there is some free play which we take |
106 |
< |
* advantage of to avoid contention and rapid flickering of the |
107 |
< |
* global activeCount: If inactive, we activate only if a victim |
108 |
< |
* queue appears to be nonempty (see above). Similarly, a thread |
109 |
< |
* tries to inactivate only after a full scan of other threads. |
110 |
< |
* The net effect is that contention on activeCount is rarely a |
111 |
< |
* measurable performance issue. (There are also a few other cases |
112 |
< |
* where we scan for work rather than retry/block upon |
113 |
< |
* contention.) |
114 |
< |
* |
115 |
< |
* 3. Selection control. We maintain policy of always choosing to |
116 |
< |
* run local tasks rather than stealing, and always trying to |
117 |
< |
* steal tasks before trying to run a new submission. All steals |
118 |
< |
* are currently performed in randomly-chosen deq-order. It may be |
119 |
< |
* worthwhile to bias these with locality / anti-locality |
120 |
< |
* information, but doing this well probably requires more |
121 |
< |
* lower-level information from JVMs than currently provided. |
122 |
< |
*/ |
123 |
< |
|
124 |
< |
/** |
125 |
< |
* Capacity of work-stealing queue array upon initialization. |
126 |
< |
* Must be a power of two. Initial size must be at least 2, but is |
127 |
< |
* padded to minimize cache effects. |
128 |
< |
*/ |
129 |
< |
private static final int INITIAL_QUEUE_CAPACITY = 1 << 13; |
130 |
< |
|
131 |
< |
/** |
132 |
< |
* Maximum work-stealing queue array size. Must be less than or |
133 |
< |
* equal to 1 << 28 to ensure lack of index wraparound. (This |
134 |
< |
* is less than usual bounds, because we need leftshift by 3 |
135 |
< |
* to be in int range). |
136 |
< |
*/ |
137 |
< |
private static final int MAXIMUM_QUEUE_CAPACITY = 1 << 28; |
138 |
< |
|
139 |
< |
/** |
140 |
< |
* The pool this thread works in. Accessed directly by ForkJoinTask |
141 |
< |
*/ |
142 |
< |
final ForkJoinPool pool; |
143 |
< |
|
144 |
< |
/** |
145 |
< |
* The work-stealing queue array. Size must be a power of two. |
146 |
< |
* Initialized when thread starts, to improve memory locality. |
147 |
< |
*/ |
148 |
< |
private ForkJoinTask<?>[] queue; |
149 |
< |
|
150 |
< |
/** |
151 |
< |
* Index (mod queue.length) of next queue slot to push to or pop |
152 |
< |
* from. It is written only by owner thread, via ordered store. |
153 |
< |
* Both sp and base are allowed to wrap around on overflow, but |
154 |
< |
* (sp - base) still estimates size. |
155 |
< |
*/ |
156 |
< |
private volatile int sp; |
157 |
< |
|
158 |
< |
/** |
159 |
< |
* Index (mod queue.length) of least valid queue slot, which is |
160 |
< |
* always the next position to steal from if nonempty. |
161 |
< |
*/ |
162 |
< |
private volatile int base; |
163 |
< |
|
164 |
< |
/** |
165 |
< |
* Activity status. When true, this worker is considered active. |
166 |
< |
* Must be false upon construction. It must be true when executing |
167 |
< |
* tasks, and BEFORE stealing a task. It must be false before |
168 |
< |
* calling pool.sync |
169 |
< |
*/ |
170 |
< |
private boolean active; |
171 |
< |
|
172 |
< |
/** |
173 |
< |
* Run state of this worker. Supports simple versions of the usual |
174 |
< |
* shutdown/shutdownNow control. |
175 |
< |
*/ |
176 |
< |
private volatile int runState; |
177 |
< |
|
178 |
< |
/** |
179 |
< |
* Seed for random number generator for choosing steal victims. |
180 |
< |
* Uses Marsaglia xorshift. Must be nonzero upon initialization. |
181 |
< |
*/ |
182 |
< |
private int seed; |
183 |
< |
|
184 |
< |
/** |
185 |
< |
* Number of steals, transferred to pool when idle |
25 |
> |
* ForkJoinWorkerThreads are managed by ForkJoinPools and perform |
26 |
> |
* ForkJoinTasks. For explanation, see the internal documentation |
27 |
> |
* of class ForkJoinPool. |
28 |
|
*/ |
187 |
– |
private int stealCount; |
29 |
|
|
30 |
< |
/** |
31 |
< |
* Index of this worker in pool array. Set once by pool before |
191 |
< |
* running, and accessed directly by pool during cleanup etc |
192 |
< |
*/ |
193 |
< |
int poolIndex; |
194 |
< |
|
195 |
< |
/** |
196 |
< |
* The last barrier event waited for. Accessed in pool callback |
197 |
< |
* methods, but only by current thread. |
198 |
< |
*/ |
199 |
< |
long lastEventCount; |
200 |
< |
|
201 |
< |
/** |
202 |
< |
* True if use local fifo, not default lifo, for local polling |
203 |
< |
*/ |
204 |
< |
private boolean locallyFifo; |
30 |
> |
final ForkJoinPool.WorkQueue workQueue; // Work-stealing mechanics |
31 |
> |
final ForkJoinPool pool; // the pool this thread works in |
32 |
|
|
33 |
|
/** |
34 |
|
* Creates a ForkJoinWorkerThread operating in the given pool. |
35 |
+ |
* |
36 |
|
* @param pool the pool this thread works in |
37 |
|
* @throws NullPointerException if pool is null |
38 |
|
*/ |
39 |
|
protected ForkJoinWorkerThread(ForkJoinPool pool) { |
40 |
< |
if (pool == null) throw new NullPointerException(); |
40 |
> |
super(pool.nextWorkerName()); |
41 |
> |
setDaemon(true); |
42 |
> |
Thread.UncaughtExceptionHandler ueh = pool.ueh; |
43 |
> |
if (ueh != null) |
44 |
> |
setUncaughtExceptionHandler(ueh); |
45 |
|
this.pool = pool; |
46 |
< |
// Note: poolIndex is set by pool during construction |
47 |
< |
// Remaining initialization is deferred to onStart |
46 |
> |
this.workQueue = new ForkJoinPool.WorkQueue(this, pool.localMode); |
47 |
> |
pool.registerWorker(this); |
48 |
|
} |
49 |
|
|
218 |
– |
// Public access methods |
219 |
– |
|
50 |
|
/** |
51 |
< |
* Returns the pool hosting this thread |
51 |
> |
* Returns the pool hosting this thread. |
52 |
> |
* |
53 |
|
* @return the pool |
54 |
|
*/ |
55 |
|
public ForkJoinPool getPool() { |
62 |
|
* threads (minus one) that have ever been created in the pool. |
63 |
|
* This method may be useful for applications that track status or |
64 |
|
* collect results per-worker rather than per-task. |
65 |
< |
* @return the index number. |
65 |
> |
* |
66 |
> |
* @return the index number |
67 |
|
*/ |
68 |
|
public int getPoolIndex() { |
69 |
< |
return poolIndex; |
238 |
< |
} |
239 |
< |
|
240 |
< |
/** |
241 |
< |
* Establishes local first-in-first-out scheduling mode for forked |
242 |
< |
* tasks that are never joined. |
243 |
< |
* @param async if true, use locally FIFO scheduling |
244 |
< |
*/ |
245 |
< |
void setAsyncMode(boolean async) { |
246 |
< |
locallyFifo = async; |
247 |
< |
} |
248 |
< |
|
249 |
< |
// Runstate management |
250 |
< |
|
251 |
< |
// Runstate values. Order matters |
252 |
< |
private static final int RUNNING = 0; |
253 |
< |
private static final int SHUTDOWN = 1; |
254 |
< |
private static final int TERMINATING = 2; |
255 |
< |
private static final int TERMINATED = 3; |
256 |
< |
|
257 |
< |
final boolean isShutdown() { return runState >= SHUTDOWN; } |
258 |
< |
final boolean isTerminating() { return runState >= TERMINATING; } |
259 |
< |
final boolean isTerminated() { return runState == TERMINATED; } |
260 |
< |
final boolean shutdown() { return transitionRunStateTo(SHUTDOWN); } |
261 |
< |
final boolean shutdownNow() { return transitionRunStateTo(TERMINATING); } |
262 |
< |
|
263 |
< |
/** |
264 |
< |
* Transition to at least the given state. Return true if not |
265 |
< |
* already at least given state. |
266 |
< |
*/ |
267 |
< |
private boolean transitionRunStateTo(int state) { |
268 |
< |
for (;;) { |
269 |
< |
int s = runState; |
270 |
< |
if (s >= state) |
271 |
< |
return false; |
272 |
< |
if (_unsafe.compareAndSwapInt(this, runStateOffset, s, state)) |
273 |
< |
return true; |
274 |
< |
} |
275 |
< |
} |
276 |
< |
|
277 |
< |
/** |
278 |
< |
* Try to set status to active; fail on contention |
279 |
< |
*/ |
280 |
< |
private boolean tryActivate() { |
281 |
< |
if (!active) { |
282 |
< |
if (!pool.tryIncrementActiveCount()) |
283 |
< |
return false; |
284 |
< |
active = true; |
285 |
< |
} |
286 |
< |
return true; |
287 |
< |
} |
288 |
< |
|
289 |
< |
/** |
290 |
< |
* Try to set status to active; fail on contention |
291 |
< |
*/ |
292 |
< |
private boolean tryInactivate() { |
293 |
< |
if (active) { |
294 |
< |
if (!pool.tryDecrementActiveCount()) |
295 |
< |
return false; |
296 |
< |
active = false; |
297 |
< |
} |
298 |
< |
return true; |
299 |
< |
} |
300 |
< |
|
301 |
< |
/** |
302 |
< |
* Computes next value for random victim probe. Scans don't |
303 |
< |
* require a very high quality generator, but also not a crummy |
304 |
< |
* one. Marsaglia xor-shift is cheap and works well. |
305 |
< |
*/ |
306 |
< |
private static int xorShift(int r) { |
307 |
< |
r ^= r << 1; |
308 |
< |
r ^= r >>> 3; |
309 |
< |
r ^= r << 10; |
310 |
< |
return r; |
311 |
< |
} |
312 |
< |
|
313 |
< |
// Lifecycle methods |
314 |
< |
|
315 |
< |
/** |
316 |
< |
* This method is required to be public, but should never be |
317 |
< |
* called explicitly. It performs the main run loop to execute |
318 |
< |
* ForkJoinTasks. |
319 |
< |
*/ |
320 |
< |
public void run() { |
321 |
< |
Throwable exception = null; |
322 |
< |
try { |
323 |
< |
onStart(); |
324 |
< |
pool.sync(this); // await first pool event |
325 |
< |
mainLoop(); |
326 |
< |
} catch (Throwable ex) { |
327 |
< |
exception = ex; |
328 |
< |
} finally { |
329 |
< |
onTermination(exception); |
330 |
< |
} |
331 |
< |
} |
332 |
< |
|
333 |
< |
/** |
334 |
< |
* Execute tasks until shut down. |
335 |
< |
*/ |
336 |
< |
private void mainLoop() { |
337 |
< |
while (!isShutdown()) { |
338 |
< |
ForkJoinTask<?> t = pollTask(); |
339 |
< |
if (t != null || (t = pollSubmission()) != null) |
340 |
< |
t.quietlyExec(); |
341 |
< |
else if (tryInactivate()) |
342 |
< |
pool.sync(this); |
343 |
< |
} |
69 |
> |
return workQueue.poolIndex; |
70 |
|
} |
71 |
|
|
72 |
|
/** |
73 |
|
* Initializes internal state after construction but before |
74 |
|
* processing any tasks. If you override this method, you must |
75 |
< |
* invoke super.onStart() at the beginning of the method. |
75 |
> |
* invoke {@code super.onStart()} at the beginning of the method. |
76 |
|
* Initialization requires care: Most fields must have legal |
77 |
|
* default values, to ensure that attempted accesses from other |
78 |
|
* threads work correctly even before this thread starts |
79 |
|
* processing tasks. |
80 |
|
*/ |
81 |
|
protected void onStart() { |
356 |
– |
// Allocate while starting to improve chances of thread-local |
357 |
– |
// isolation |
358 |
– |
queue = new ForkJoinTask<?>[INITIAL_QUEUE_CAPACITY]; |
359 |
– |
// Initial value of seed need not be especially random but |
360 |
– |
// should differ across workers and must be nonzero |
361 |
– |
int p = poolIndex + 1; |
362 |
– |
seed = p + (p << 8) + (p << 16) + (p << 24); // spread bits |
82 |
|
} |
83 |
|
|
84 |
|
/** |
85 |
< |
* Perform cleanup associated with termination of this worker |
85 |
> |
* Performs cleanup associated with termination of this worker |
86 |
|
* thread. If you override this method, you must invoke |
87 |
< |
* super.onTermination at the end of the overridden method. |
87 |
> |
* {@code super.onTermination} at the end of the overridden method. |
88 |
|
* |
89 |
|
* @param exception the exception causing this thread to abort due |
90 |
< |
* to an unrecoverable error, or null if completed normally. |
90 |
> |
* to an unrecoverable error, or {@code null} if completed normally |
91 |
|
*/ |
92 |
|
protected void onTermination(Throwable exception) { |
374 |
– |
// Execute remaining local tasks unless aborting or terminating |
375 |
– |
while (exception == null && !pool.isTerminating() && base != sp) { |
376 |
– |
try { |
377 |
– |
ForkJoinTask<?> t = popTask(); |
378 |
– |
if (t != null) |
379 |
– |
t.quietlyExec(); |
380 |
– |
} catch(Throwable ex) { |
381 |
– |
exception = ex; |
382 |
– |
} |
383 |
– |
} |
384 |
– |
// Cancel other tasks, transition status, notify pool, and |
385 |
– |
// propagate exception to uncaught exception handler |
386 |
– |
try { |
387 |
– |
do;while (!tryInactivate()); // ensure inactive |
388 |
– |
cancelTasks(); |
389 |
– |
runState = TERMINATED; |
390 |
– |
pool.workerTerminated(this); |
391 |
– |
} catch (Throwable ex) { // Shouldn't ever happen |
392 |
– |
if (exception == null) // but if so, at least rethrown |
393 |
– |
exception = ex; |
394 |
– |
} finally { |
395 |
– |
if (exception != null) |
396 |
– |
ForkJoinTask.rethrowException(exception); |
397 |
– |
} |
93 |
|
} |
94 |
|
|
400 |
– |
// Intrinsics-based support for queue operations. |
401 |
– |
|
95 |
|
/** |
96 |
< |
* Add in store-order the given task at given slot of q to |
97 |
< |
* null. Caller must ensure q is nonnull and index is in range. |
98 |
< |
*/ |
406 |
< |
private static void setSlot(ForkJoinTask<?>[] q, int i, |
407 |
< |
ForkJoinTask<?> t){ |
408 |
< |
_unsafe.putOrderedObject(q, (i << qShift) + qBase, t); |
409 |
< |
} |
410 |
< |
|
411 |
< |
/** |
412 |
< |
* CAS given slot of q to null. Caller must ensure q is nonnull |
413 |
< |
* and index is in range. |
414 |
< |
*/ |
415 |
< |
private static boolean casSlotNull(ForkJoinTask<?>[] q, int i, |
416 |
< |
ForkJoinTask<?> t) { |
417 |
< |
return _unsafe.compareAndSwapObject(q, (i << qShift) + qBase, t, null); |
418 |
< |
} |
419 |
< |
|
420 |
< |
/** |
421 |
< |
* Sets sp in store-order. |
422 |
< |
*/ |
423 |
< |
private void storeSp(int s) { |
424 |
< |
_unsafe.putOrderedInt(this, spOffset, s); |
425 |
< |
} |
426 |
< |
|
427 |
< |
// Main queue methods |
428 |
< |
|
429 |
< |
/** |
430 |
< |
* Pushes a task. Called only by current thread. |
431 |
< |
* @param t the task. Caller must ensure nonnull |
432 |
< |
*/ |
433 |
< |
final void pushTask(ForkJoinTask<?> t) { |
434 |
< |
ForkJoinTask<?>[] q = queue; |
435 |
< |
int mask = q.length - 1; |
436 |
< |
int s = sp; |
437 |
< |
setSlot(q, s & mask, t); |
438 |
< |
storeSp(++s); |
439 |
< |
if ((s -= base) == 1) |
440 |
< |
pool.signalWork(); |
441 |
< |
else if (s >= mask) |
442 |
< |
growQueue(); |
443 |
< |
} |
444 |
< |
|
445 |
< |
/** |
446 |
< |
* Tries to take a task from the base of the queue, failing if |
447 |
< |
* either empty or contended. |
448 |
< |
* @return a task, or null if none or contended. |
449 |
< |
*/ |
450 |
< |
final ForkJoinTask<?> deqTask() { |
451 |
< |
ForkJoinTask<?> t; |
452 |
< |
ForkJoinTask<?>[] q; |
453 |
< |
int i; |
454 |
< |
int b; |
455 |
< |
if (sp != (b = base) && |
456 |
< |
(q = queue) != null && // must read q after b |
457 |
< |
(t = q[i = (q.length - 1) & b]) != null && |
458 |
< |
casSlotNull(q, i, t)) { |
459 |
< |
base = b + 1; |
460 |
< |
return t; |
461 |
< |
} |
462 |
< |
return null; |
463 |
< |
} |
464 |
< |
|
465 |
< |
/** |
466 |
< |
* Returns a popped task, or null if empty. Ensures active status |
467 |
< |
* if nonnull. Called only by current thread. |
468 |
< |
*/ |
469 |
< |
final ForkJoinTask<?> popTask() { |
470 |
< |
int s = sp; |
471 |
< |
while (s != base) { |
472 |
< |
if (tryActivate()) { |
473 |
< |
ForkJoinTask<?>[] q = queue; |
474 |
< |
int mask = q.length - 1; |
475 |
< |
int i = (s - 1) & mask; |
476 |
< |
ForkJoinTask<?> t = q[i]; |
477 |
< |
if (t == null || !casSlotNull(q, i, t)) |
478 |
< |
break; |
479 |
< |
storeSp(s - 1); |
480 |
< |
return t; |
481 |
< |
} |
482 |
< |
} |
483 |
< |
return null; |
484 |
< |
} |
485 |
< |
|
486 |
< |
/** |
487 |
< |
* Specialized version of popTask to pop only if |
488 |
< |
* topmost element is the given task. Called only |
489 |
< |
* by current thread while active. |
490 |
< |
* @param t the task. Caller must ensure nonnull |
491 |
< |
*/ |
492 |
< |
final boolean unpushTask(ForkJoinTask<?> t) { |
493 |
< |
ForkJoinTask<?>[] q = queue; |
494 |
< |
int mask = q.length - 1; |
495 |
< |
int s = sp - 1; |
496 |
< |
if (casSlotNull(q, s & mask, t)) { |
497 |
< |
storeSp(s); |
498 |
< |
return true; |
499 |
< |
} |
500 |
< |
return false; |
501 |
< |
} |
502 |
< |
|
503 |
< |
/** |
504 |
< |
* Returns next task. |
505 |
< |
*/ |
506 |
< |
final ForkJoinTask<?> peekTask() { |
507 |
< |
ForkJoinTask<?>[] q = queue; |
508 |
< |
if (q == null) |
509 |
< |
return null; |
510 |
< |
int mask = q.length - 1; |
511 |
< |
int i = locallyFifo? base : (sp - 1); |
512 |
< |
return q[i & mask]; |
513 |
< |
} |
514 |
< |
|
515 |
< |
/** |
516 |
< |
* Doubles queue array size. Transfers elements by emulating |
517 |
< |
* steals (deqs) from old array and placing, oldest first, into |
518 |
< |
* new array. |
519 |
< |
*/ |
520 |
< |
private void growQueue() { |
521 |
< |
ForkJoinTask<?>[] oldQ = queue; |
522 |
< |
int oldSize = oldQ.length; |
523 |
< |
int newSize = oldSize << 1; |
524 |
< |
if (newSize > MAXIMUM_QUEUE_CAPACITY) |
525 |
< |
throw new RejectedExecutionException("Queue capacity exceeded"); |
526 |
< |
ForkJoinTask<?>[] newQ = queue = new ForkJoinTask<?>[newSize]; |
527 |
< |
|
528 |
< |
int b = base; |
529 |
< |
int bf = b + oldSize; |
530 |
< |
int oldMask = oldSize - 1; |
531 |
< |
int newMask = newSize - 1; |
532 |
< |
do { |
533 |
< |
int oldIndex = b & oldMask; |
534 |
< |
ForkJoinTask<?> t = oldQ[oldIndex]; |
535 |
< |
if (t != null && !casSlotNull(oldQ, oldIndex, t)) |
536 |
< |
t = null; |
537 |
< |
setSlot(newQ, b & newMask, t); |
538 |
< |
} while (++b != bf); |
539 |
< |
pool.signalWork(); |
540 |
< |
} |
541 |
< |
|
542 |
< |
/** |
543 |
< |
* Tries to steal a task from another worker. Starts at a random |
544 |
< |
* index of workers array, and probes workers until finding one |
545 |
< |
* with non-empty queue or finding that all are empty. It |
546 |
< |
* randomly selects the first n probes. If these are empty, it |
547 |
< |
* resorts to a full circular traversal, which is necessary to |
548 |
< |
* accurately set active status by caller. Also restarts if pool |
549 |
< |
* events occurred since last scan, which forces refresh of |
550 |
< |
* workers array, in case barrier was associated with resize. |
551 |
< |
* |
552 |
< |
* This method must be both fast and quiet -- usually avoiding |
553 |
< |
* memory accesses that could disrupt cache sharing etc other than |
554 |
< |
* those needed to check for and take tasks. This accounts for, |
555 |
< |
* among other things, updating random seed in place without |
556 |
< |
* storing it until exit. |
557 |
< |
* |
558 |
< |
* @return a task, or null if none found |
559 |
< |
*/ |
560 |
< |
private ForkJoinTask<?> scan() { |
561 |
< |
ForkJoinTask<?> t = null; |
562 |
< |
int r = seed; // extract once to keep scan quiet |
563 |
< |
ForkJoinWorkerThread[] ws; // refreshed on outer loop |
564 |
< |
int mask; // must be power 2 minus 1 and > 0 |
565 |
< |
outer:do { |
566 |
< |
if ((ws = pool.workers) != null && (mask = ws.length - 1) > 0) { |
567 |
< |
int idx = r; |
568 |
< |
int probes = ~mask; // use random index while negative |
569 |
< |
for (;;) { |
570 |
< |
r = xorShift(r); // update random seed |
571 |
< |
ForkJoinWorkerThread v = ws[mask & idx]; |
572 |
< |
if (v == null || v.sp == v.base) { |
573 |
< |
if (probes <= mask) |
574 |
< |
idx = (probes++ < 0)? r : (idx + 1); |
575 |
< |
else |
576 |
< |
break; |
577 |
< |
} |
578 |
< |
else if (!tryActivate() || (t = v.deqTask()) == null) |
579 |
< |
continue outer; // restart on contention |
580 |
< |
else |
581 |
< |
break outer; |
582 |
< |
} |
583 |
< |
} |
584 |
< |
} while (pool.hasNewSyncEvent(this)); // retry on pool events |
585 |
< |
seed = r; |
586 |
< |
return t; |
587 |
< |
} |
588 |
< |
|
589 |
< |
/** |
590 |
< |
* gets and removes a local or stolen a task |
591 |
< |
* @return a task, if available |
592 |
< |
*/ |
593 |
< |
final ForkJoinTask<?> pollTask() { |
594 |
< |
ForkJoinTask<?> t = locallyFifo? deqTask() : popTask(); |
595 |
< |
if (t == null && (t = scan()) != null) |
596 |
< |
++stealCount; |
597 |
< |
return t; |
598 |
< |
} |
599 |
< |
|
600 |
< |
/** |
601 |
< |
* gets a local task |
602 |
< |
* @return a task, if available |
603 |
< |
*/ |
604 |
< |
final ForkJoinTask<?> pollLocalTask() { |
605 |
< |
return locallyFifo? deqTask() : popTask(); |
606 |
< |
} |
607 |
< |
|
608 |
< |
/** |
609 |
< |
* Returns a pool submission, if one exists, activating first. |
610 |
< |
* @return a submission, if available |
611 |
< |
*/ |
612 |
< |
private ForkJoinTask<?> pollSubmission() { |
613 |
< |
ForkJoinPool p = pool; |
614 |
< |
while (p.hasQueuedSubmissions()) { |
615 |
< |
ForkJoinTask<?> t; |
616 |
< |
if (tryActivate() && (t = p.pollSubmission()) != null) |
617 |
< |
return t; |
618 |
< |
} |
619 |
< |
return null; |
620 |
< |
} |
621 |
< |
|
622 |
< |
// Methods accessed only by Pool |
623 |
< |
|
624 |
< |
/** |
625 |
< |
* Removes and cancels all tasks in queue. Can be called from any |
626 |
< |
* thread. |
627 |
< |
*/ |
628 |
< |
final void cancelTasks() { |
629 |
< |
ForkJoinTask<?> t; |
630 |
< |
while (base != sp && (t = deqTask()) != null) |
631 |
< |
t.cancelIgnoringExceptions(); |
632 |
< |
} |
633 |
< |
|
634 |
< |
/** |
635 |
< |
* Drains tasks to given collection c |
636 |
< |
* @return the number of tasks drained |
637 |
< |
*/ |
638 |
< |
final int drainTasksTo(Collection<ForkJoinTask<?>> c) { |
639 |
< |
int n = 0; |
640 |
< |
ForkJoinTask<?> t; |
641 |
< |
while (base != sp && (t = deqTask()) != null) { |
642 |
< |
c.add(t); |
643 |
< |
++n; |
644 |
< |
} |
645 |
< |
return n; |
646 |
< |
} |
647 |
< |
|
648 |
< |
/** |
649 |
< |
* Get and clear steal count for accumulation by pool. Called |
650 |
< |
* only when known to be idle (in pool.sync and termination). |
651 |
< |
*/ |
652 |
< |
final int getAndClearStealCount() { |
653 |
< |
int sc = stealCount; |
654 |
< |
stealCount = 0; |
655 |
< |
return sc; |
656 |
< |
} |
657 |
< |
|
658 |
< |
/** |
659 |
< |
* Returns true if at least one worker in the given array appears |
660 |
< |
* to have at least one queued task. |
661 |
< |
* @param ws array of workers |
662 |
< |
*/ |
663 |
< |
static boolean hasQueuedTasks(ForkJoinWorkerThread[] ws) { |
664 |
< |
if (ws != null) { |
665 |
< |
int len = ws.length; |
666 |
< |
for (int j = 0; j < 2; ++j) { // need two passes for clean sweep |
667 |
< |
for (int i = 0; i < len; ++i) { |
668 |
< |
ForkJoinWorkerThread w = ws[i]; |
669 |
< |
if (w != null && w.sp != w.base) |
670 |
< |
return true; |
671 |
< |
} |
672 |
< |
} |
673 |
< |
} |
674 |
< |
return false; |
675 |
< |
} |
676 |
< |
|
677 |
< |
// Support methods for ForkJoinTask |
678 |
< |
|
679 |
< |
/** |
680 |
< |
* Returns an estimate of the number of tasks in the queue. |
681 |
< |
*/ |
682 |
< |
final int getQueueSize() { |
683 |
< |
int n = sp - base; |
684 |
< |
return n < 0? 0 : n; // suppress momentarily negative values |
685 |
< |
} |
686 |
< |
|
687 |
< |
/** |
688 |
< |
* Returns an estimate of the number of tasks, offset by a |
689 |
< |
* function of number of idle workers. |
690 |
< |
*/ |
691 |
< |
final int getEstimatedSurplusTaskCount() { |
692 |
< |
// The halving approximates weighting idle vs non-idle workers |
693 |
< |
return (sp - base) - (pool.getIdleThreadCount() >>> 1); |
694 |
< |
} |
695 |
< |
|
696 |
< |
/** |
697 |
< |
* Scan, returning early if joinMe done |
698 |
< |
*/ |
699 |
< |
final ForkJoinTask<?> scanWhileJoining(ForkJoinTask<?> joinMe) { |
700 |
< |
ForkJoinTask<?> t = pollTask(); |
701 |
< |
if (t != null && joinMe.status < 0 && sp == base) { |
702 |
< |
pushTask(t); // unsteal if done and this task would be stealable |
703 |
< |
t = null; |
704 |
< |
} |
705 |
< |
return t; |
706 |
< |
} |
707 |
< |
|
708 |
< |
/** |
709 |
< |
* Runs tasks until pool isQuiescent |
96 |
> |
* This method is required to be public, but should never be |
97 |
> |
* called explicitly. It performs the main run loop to execute |
98 |
> |
* {@link ForkJoinTask}s. |
99 |
|
*/ |
100 |
< |
final void helpQuiescePool() { |
101 |
< |
for (;;) { |
713 |
< |
ForkJoinTask<?> t = pollTask(); |
714 |
< |
if (t != null) |
715 |
< |
t.quietlyExec(); |
716 |
< |
else if (tryInactivate() && pool.isQuiescent()) |
717 |
< |
break; |
718 |
< |
} |
719 |
< |
do;while (!tryActivate()); // re-activate on exit |
720 |
< |
} |
721 |
< |
|
722 |
< |
// Temporary Unsafe mechanics for preliminary release |
723 |
< |
private static Unsafe getUnsafe() throws Throwable { |
100 |
> |
public void run() { |
101 |
> |
Throwable exception = null; |
102 |
|
try { |
103 |
< |
return Unsafe.getUnsafe(); |
104 |
< |
} catch (SecurityException se) { |
103 |
> |
onStart(); |
104 |
> |
pool.runWorker(this); |
105 |
> |
} catch (Throwable ex) { |
106 |
> |
exception = ex; |
107 |
> |
} finally { |
108 |
|
try { |
109 |
< |
return java.security.AccessController.doPrivileged |
110 |
< |
(new java.security.PrivilegedExceptionAction<Unsafe>() { |
111 |
< |
public Unsafe run() throws Exception { |
112 |
< |
return getUnsafePrivileged(); |
113 |
< |
}}); |
114 |
< |
} catch (java.security.PrivilegedActionException e) { |
734 |
< |
throw e.getCause(); |
109 |
> |
onTermination(exception); |
110 |
> |
} catch (Throwable ex) { |
111 |
> |
if (exception == null) |
112 |
> |
exception = ex; |
113 |
> |
} finally { |
114 |
> |
pool.deregisterWorker(this, exception); |
115 |
|
} |
116 |
|
} |
117 |
|
} |
738 |
– |
|
739 |
– |
private static Unsafe getUnsafePrivileged() |
740 |
– |
throws NoSuchFieldException, IllegalAccessException { |
741 |
– |
Field f = Unsafe.class.getDeclaredField("theUnsafe"); |
742 |
– |
f.setAccessible(true); |
743 |
– |
return (Unsafe) f.get(null); |
744 |
– |
} |
745 |
– |
|
746 |
– |
private static long fieldOffset(String fieldName) |
747 |
– |
throws NoSuchFieldException { |
748 |
– |
return _unsafe.objectFieldOffset |
749 |
– |
(ForkJoinWorkerThread.class.getDeclaredField(fieldName)); |
750 |
– |
} |
751 |
– |
|
752 |
– |
static final Unsafe _unsafe; |
753 |
– |
static final long baseOffset; |
754 |
– |
static final long spOffset; |
755 |
– |
static final long runStateOffset; |
756 |
– |
static final long qBase; |
757 |
– |
static final int qShift; |
758 |
– |
static { |
759 |
– |
try { |
760 |
– |
_unsafe = getUnsafe(); |
761 |
– |
baseOffset = fieldOffset("base"); |
762 |
– |
spOffset = fieldOffset("sp"); |
763 |
– |
runStateOffset = fieldOffset("runState"); |
764 |
– |
qBase = _unsafe.arrayBaseOffset(ForkJoinTask[].class); |
765 |
– |
int s = _unsafe.arrayIndexScale(ForkJoinTask[].class); |
766 |
– |
if ((s & (s-1)) != 0) |
767 |
– |
throw new Error("data type scale not a power of two"); |
768 |
– |
qShift = 31 - Integer.numberOfLeadingZeros(s); |
769 |
– |
} catch (Throwable e) { |
770 |
– |
throw new RuntimeException("Could not initialize intrinsics", e); |
771 |
– |
} |
772 |
– |
} |
118 |
|
} |
119 |
+ |
|