ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/jsr166y/ForkJoinWorkerThread.java
Revision: 1.8
Committed: Mon Jul 20 21:45:06 2009 UTC (14 years, 10 months ago) by jsr166
Branch: MAIN
Changes since 1.7: +1 -1 lines
Log Message:
whitespace

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 dl 1.1 */
25     public class ForkJoinWorkerThread extends Thread {
26     /*
27     * Algorithm overview:
28     *
29     * 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 probablistic 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 effciently 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 dl 1.5 * 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 dl 1.1 */
137 dl 1.5 private static final int MAXIMUM_QUEUE_CAPACITY = 1 << 28;
138 dl 1.1
139     /**
140 dl 1.5 * The pool this thread works in. Accessed directly by ForkJoinTask
141 dl 1.1 */
142 dl 1.5 final ForkJoinPool pool;
143 dl 1.1
144     /**
145     * The work-stealing queue array. Size must be a power of two.
146 dl 1.5 * Initialized when thread starts, to improve memory locality.
147 dl 1.1 */
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 dl 1.5 * 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 dl 1.1 */
170 dl 1.5 private boolean active;
171 dl 1.1
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 dl 1.5 * Seed for random number generator for choosing steal victims.
180     * Uses Marsaglia xorshift. Must be nonzero upon initialization.
181 dl 1.1 */
182 dl 1.5 private int seed;
183 dl 1.1
184     /**
185     * Number of steals, transferred to pool when idle
186     */
187     private int stealCount;
188    
189     /**
190 dl 1.5 * Index of this worker in pool array. Set once by pool before
191     * running, and accessed directly by pool during cleanup etc
192 dl 1.1 */
193 dl 1.5 int poolIndex;
194 dl 1.1
195     /**
196 dl 1.5 * The last barrier event waited for. Accessed in pool callback
197     * methods, but only by current thread.
198 dl 1.1 */
199 dl 1.5 long lastEventCount;
200 dl 1.1
201     /**
202 dl 1.7 * True if use local fifo, not default lifo, for local polling
203     */
204     private boolean locallyFifo;
205    
206     /**
207 dl 1.1 * Creates a ForkJoinWorkerThread operating in the given pool.
208     * @param pool the pool this thread works in
209     * @throws NullPointerException if pool is null
210     */
211     protected ForkJoinWorkerThread(ForkJoinPool pool) {
212     if (pool == null) throw new NullPointerException();
213     this.pool = pool;
214 dl 1.5 // Note: poolIndex is set by pool during construction
215     // Remaining initialization is deferred to onStart
216 dl 1.1 }
217    
218 jsr166 1.6 // Public access methods
219 dl 1.2
220     /**
221 dl 1.4 * Returns the pool hosting this thread
222 dl 1.2 * @return the pool
223     */
224 dl 1.4 public ForkJoinPool getPool() {
225     return pool;
226 dl 1.2 }
227    
228     /**
229 dl 1.4 * Returns the index number of this thread in its pool. The
230     * returned value ranges from zero to the maximum number of
231     * threads (minus one) that have ever been created in the pool.
232     * This method may be useful for applications that track status or
233 dl 1.5 * collect results per-worker rather than per-task.
234 dl 1.2 * @return the index number.
235     */
236 dl 1.4 public int getPoolIndex() {
237     return poolIndex;
238 dl 1.2 }
239    
240 dl 1.7 /**
241     * Establishes local first-in-first-out scheduling mode for forked
242 jsr166 1.8 * tasks that are never joined.
243 dl 1.7 * @param async if true, use locally FIFO scheduling
244     */
245     void setAsyncMode(boolean async) {
246     locallyFifo = async;
247     }
248 dl 1.5
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 dl 1.1
315     /**
316 dl 1.5 * 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 dl 1.1 */
320 dl 1.5 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 dl 1.1 }
332    
333     /**
334 dl 1.5 * Execute tasks until shut down.
335 dl 1.1 */
336 dl 1.5 private void mainLoop() {
337     while (!isShutdown()) {
338     ForkJoinTask<?> t = pollTask();
339 jsr166 1.6 if (t != null || (t = pollSubmission()) != null)
340 dl 1.5 t.quietlyExec();
341     else if (tryInactivate())
342     pool.sync(this);
343     }
344 dl 1.1 }
345    
346 dl 1.5 /**
347     * Initializes internal state after construction but before
348     * processing any tasks. If you override this method, you must
349     * invoke super.onStart() at the beginning of the method.
350     * Initialization requires care: Most fields must have legal
351     * default values, to ensure that attempted accesses from other
352     * threads work correctly even before this thread starts
353     * processing tasks.
354     */
355     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
363     }
364 dl 1.1
365     /**
366 dl 1.5 * Perform cleanup associated with termination of this worker
367     * thread. If you override this method, you must invoke
368     * super.onTermination at the end of the overridden method.
369     *
370     * @param exception the exception causing this thread to abort due
371     * to an unrecoverable error, or null if completed normally.
372 dl 1.1 */
373 dl 1.5 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 jsr166 1.6 cancelTasks();
389 dl 1.5 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     }
398 dl 1.1 }
399    
400 jsr166 1.6 // Intrinsics-based support for queue operations.
401 dl 1.5
402 dl 1.1 /**
403     * Add in store-order the given task at given slot of q to
404     * null. Caller must ensure q is nonnull and index is in range.
405     */
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 dl 1.5 /**
421     * Sets sp in store-order.
422     */
423     private void storeSp(int s) {
424     _unsafe.putOrderedInt(this, spOffset, s);
425     }
426    
427 dl 1.1 // 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 dl 1.5 setSlot(q, s & mask, t);
438     storeSp(++s);
439 dl 1.1 if ((s -= base) == 1)
440 dl 1.5 pool.signalWork();
441 dl 1.1 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 dl 1.7 final ForkJoinTask<?> deqTask() {
451 dl 1.5 ForkJoinTask<?> t;
452 dl 1.1 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 dl 1.5 casSlotNull(q, i, t)) {
459 dl 1.1 base = b + 1;
460     return t;
461     }
462     return null;
463     }
464    
465     /**
466 dl 1.5 * Returns a popped task, or null if empty. Ensures active status
467     * if nonnull. Called only by current thread.
468 dl 1.1 */
469     final ForkJoinTask<?> popTask() {
470     int s = sp;
471 dl 1.5 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 dl 1.1 }
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 dl 1.5 * by current thread while active.
490 dl 1.1 * @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 dl 1.5 if (casSlotNull(q, s & mask, t)) {
497     storeSp(s);
498 dl 1.1 return true;
499     }
500     return false;
501     }
502    
503     /**
504 dl 1.7 * Returns next task.
505 dl 1.1 */
506 dl 1.2 final ForkJoinTask<?> peekTask() {
507 dl 1.1 ForkJoinTask<?>[] q = queue;
508 dl 1.7 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 dl 1.1 }
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 dl 1.5 pool.signalWork();
540 dl 1.1 }
541    
542     /**
543 dl 1.5 * 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 dl 1.1 *
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 dl 1.5 * storing it until exit.
557 dl 1.1 *
558     * @return a task, or null if none found
559     */
560 dl 1.5 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 dl 1.1 int idx = r;
568 dl 1.5 int probes = ~mask; // use random index while negative
569 dl 1.1 for (;;) {
570 dl 1.5 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 dl 1.1 }
578 dl 1.5 else if (!tryActivate() || (t = v.deqTask()) == null)
579     continue outer; // restart on contention
580 dl 1.1 else
581 dl 1.5 break outer;
582 dl 1.1 }
583     }
584 dl 1.5 } while (pool.hasNewSyncEvent(this)); // retry on pool events
585     seed = r;
586     return t;
587 dl 1.1 }
588    
589     /**
590 dl 1.7 * gets and removes a local or stolen a task
591 dl 1.5 * @return a task, if available
592     */
593     final ForkJoinTask<?> pollTask() {
594 dl 1.7 ForkJoinTask<?> t = locallyFifo? deqTask() : popTask();
595 dl 1.5 if (t == null && (t = scan()) != null)
596     ++stealCount;
597     return t;
598 dl 1.1 }
599    
600     /**
601 dl 1.7 * gets a local task
602     * @return a task, if available
603     */
604     final ForkJoinTask<?> pollLocalTask() {
605     return locallyFifo? deqTask() : popTask();
606     }
607    
608     /**
609 dl 1.5 * Returns a pool submission, if one exists, activating first.
610     * @return a submission, if available
611 dl 1.1 */
612 dl 1.5 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 dl 1.1 }
619 dl 1.5 return null;
620 dl 1.1 }
621 dl 1.5
622     // Methods accessed only by Pool
623    
624 dl 1.1 /**
625 dl 1.5 * Removes and cancels all tasks in queue. Can be called from any
626     * thread.
627 dl 1.1 */
628 dl 1.5 final void cancelTasks() {
629 dl 1.3 ForkJoinTask<?> t;
630 dl 1.5 while (base != sp && (t = deqTask()) != null)
631     t.cancelIgnoringExceptions();
632 dl 1.1 }
633    
634     /**
635 dl 1.7 * 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 dl 1.5 * Get and clear steal count for accumulation by pool. Called
650     * only when known to be idle (in pool.sync and termination).
651 dl 1.1 */
652 dl 1.5 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 dl 1.1 }
672     }
673     }
674 dl 1.5 return false;
675 dl 1.1 }
676    
677 dl 1.5 // Support methods for ForkJoinTask
678    
679 dl 1.1 /**
680 dl 1.2 * Returns an estimate of the number of tasks in the queue.
681     */
682     final int getQueueSize() {
683 dl 1.3 int n = sp - base;
684 dl 1.5 return n < 0? 0 : n; // suppress momentarily negative values
685 dl 1.2 }
686    
687     /**
688 dl 1.1 * Returns an estimate of the number of tasks, offset by a
689     * function of number of idle workers.
690     */
691     final int getEstimatedSurplusTaskCount() {
692 dl 1.3 // The halving approximates weighting idle vs non-idle workers
693 dl 1.1 return (sp - base) - (pool.getIdleThreadCount() >>> 1);
694     }
695    
696     /**
697 dl 1.5 * Scan, returning early if joinMe done
698 dl 1.1 */
699 dl 1.5 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 dl 1.1 }
707 jsr166 1.6
708 dl 1.1 /**
709 dl 1.5 * Runs tasks until pool isQuiescent
710 dl 1.1 */
711 dl 1.5 final void helpQuiescePool() {
712     for (;;) {
713     ForkJoinTask<?> t = pollTask();
714 jsr166 1.6 if (t != null)
715 dl 1.5 t.quietlyExec();
716     else if (tryInactivate() && pool.isQuiescent())
717     break;
718     }
719     do;while (!tryActivate()); // re-activate on exit
720 dl 1.1 }
721    
722     // Temporary Unsafe mechanics for preliminary release
723 jsr166 1.6 private static Unsafe getUnsafe() throws Throwable {
724     try {
725     return Unsafe.getUnsafe();
726     } catch (SecurityException se) {
727     try {
728     return java.security.AccessController.doPrivileged
729     (new java.security.PrivilegedExceptionAction<Unsafe>() {
730     public Unsafe run() throws Exception {
731     return getUnsafePrivileged();
732     }});
733     } catch (java.security.PrivilegedActionException e) {
734     throw e.getCause();
735     }
736     }
737     }
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 dl 1.1
752     static final Unsafe _unsafe;
753     static final long baseOffset;
754     static final long spOffset;
755 dl 1.5 static final long runStateOffset;
756 dl 1.1 static final long qBase;
757     static final int qShift;
758     static {
759     try {
760 jsr166 1.6 _unsafe = getUnsafe();
761     baseOffset = fieldOffset("base");
762     spOffset = fieldOffset("sp");
763     runStateOffset = fieldOffset("runState");
764 dl 1.1 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 jsr166 1.6 } catch (Throwable e) {
770 dl 1.1 throw new RuntimeException("Could not initialize intrinsics", e);
771     }
772     }
773     }