ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/jsr166y/ForkJoinWorkerThread.java
Revision: 1.15
Committed: Thu Jul 23 19:25:45 2009 UTC (14 years, 9 months ago) by jsr166
Branch: MAIN
Changes since 1.14: +6 -6 lines
Log Message:
j.u.c. coding standards

File Contents

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