ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/jsr166y/ForkJoinWorkerThread.java
(Generate patch)

Comparing jsr166/src/jsr166y/ForkJoinWorkerThread.java (file contents):
Revision 1.2 by dl, Wed Jan 7 16:07:37 2009 UTC vs.
Revision 1.21 by jsr166, Mon Jul 27 20:57:44 2009 UTC

# Line 5 | Line 5
5   */
6  
7   package jsr166y;
8 < import java.util.*;
8 >
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.*;
10 >
11 > import java.util.Collection;
12  
13   /**
14   * A thread managed by a {@link ForkJoinPool}.  This class is
15   * subclassable solely for the sake of adding functionality -- there
16   * are no overridable methods dealing with scheduling or
17   * execution. However, you can override initialization and termination
18 < * cleanup methods surrounding the main task processing loop.  If you
19 < * do create such a subclass, you will also need to supply a custom
18 > * methods surrounding the main task processing loop.  If you do
19 > * create such a subclass, you will also need to supply a custom
20   * ForkJoinWorkerThreadFactory to use it in a ForkJoinPool.
21 < *
22 < * <p>This class also provides methods for generating per-thread
23 < * random numbers, with the same properties as {@link
26 < * java.util.Random} but with each generator isolated from those of
27 < * other threads.
21 > *
22 > * @since 1.7
23 > * @author Doug Lea
24   */
25   public class ForkJoinWorkerThread extends Thread {
26      /*
# Line 48 | Line 44 | public class ForkJoinWorkerThread extend
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
47 >     * both a successful pop and deq mainly entail CAS'ing a non-null
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).
# Line 60 | Line 56 | public class ForkJoinWorkerThread extend
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
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
# Line 79 | Line 75 | public class ForkJoinWorkerThread extend
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
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
# Line 134 | Line 130 | public class ForkJoinWorkerThread extend
130  
131      /**
132       * Maximum work-stealing queue array size.  Must be less than or
133 <     * equal to 1 << 30 to ensure lack of index wraparound.
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 << 30;
137 >    private static final int MAXIMUM_QUEUE_CAPACITY = 1 << 28;
138  
139      /**
140 <     * Generator of seeds for per-thread random numbers.
140 >     * The pool this thread works in. Accessed directly by ForkJoinTask.
141       */
142 <    private static final Random randomSeedGenerator = new Random();
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  
# Line 163 | Line 162 | public class ForkJoinWorkerThread extend
162      private volatile int base;
163  
164      /**
165 <     * The pool this thread works in.
166 <     */
167 <    final ForkJoinPool pool;
168 <
170 <    /**
171 <     * Index of this worker in pool array. Set once by pool before
172 <     * running, and accessed directly by pool during cleanup etc
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 <    int poolIndex;
170 >    private boolean active;
171  
172      /**
173       * Run state of this worker. Supports simple versions of the usual
# Line 179 | Line 175 | public class ForkJoinWorkerThread extend
175       */
176      private volatile int runState;
177  
182    // Runstate values. Order matters
183    private static final int RUNNING     = 0;
184    private static final int SHUTDOWN    = 1;
185    private static final int TERMINATING = 2;
186    private static final int TERMINATED  = 3;
187
178      /**
179 <     * Activity status. When true, this worker is considered active.
180 <     * Must be false upon construction. It must be true when executing
191 <     * tasks, and BEFORE stealing a task. It must be false before
192 <     * blocking on the Pool Barrier.
179 >     * Seed for random number generator for choosing steal victims.
180 >     * Uses Marsaglia xorshift. Must be nonzero upon initialization.
181       */
182 <    private boolean active;
182 >    private int seed;
183  
184      /**
185       * Number of steals, transferred to pool when idle
# Line 199 | Line 187 | public class ForkJoinWorkerThread extend
187      private int stealCount;
188  
189      /**
190 <     * Seed for random number generator for choosing steal victims
190 >     * Index of this worker in pool array. Set once by pool before
191 >     * running, and accessed directly by pool during cleanup etc.
192       */
193 <    private int randomVictimSeed;
193 >    int poolIndex;
194  
195      /**
196 <     * Seed for embedded Jurandom
196 >     * The last barrier event waited for. Accessed in pool callback
197 >     * methods, but only by current thread.
198       */
199 <    private long juRandomSeed;
199 >    long lastEventCount;
200  
201      /**
202 <     * The last barrier event waited for
202 >     * True if use local fifo, not default lifo, for local polling
203       */
204 <    private long eventCount;
204 >    private boolean locallyFifo;
205  
206      /**
207       * Creates a ForkJoinWorkerThread operating in the given pool.
208 +     *
209       * @param pool the pool this thread works in
210       * @throws NullPointerException if pool is null
211       */
212      protected ForkJoinWorkerThread(ForkJoinPool pool) {
213          if (pool == null) throw new NullPointerException();
214          this.pool = pool;
215 <        // remaining initialization deferred to onStart
215 >        // Note: poolIndex is set by pool during construction
216 >        // Remaining initialization is deferred to onStart
217      }
218  
219 <    // public access methods
219 >    // Public access methods
220  
221      /**
222 <     * Returns the pool hosting the current task execution.
222 >     * Returns the pool hosting this thread.
223 >     *
224       * @return the pool
225       */
226 <    public static ForkJoinPool getPool() {
227 <        return ((ForkJoinWorkerThread)(Thread.currentThread())).pool;
226 >    public ForkJoinPool getPool() {
227 >        return pool;
228      }
229  
230      /**
231 <     * Returns the index number of the current worker thread in its
232 <     * pool.  The returned value ranges from zero to the maximum
233 <     * number of threads (minus one) that have ever been created in
234 <     * the pool.  This method may be useful for applications that
235 <     * track status or collect results on a per-worker basis.
236 <     * @return the index number.
231 >     * Returns the index number of this thread in its pool.  The
232 >     * returned value ranges from zero to the maximum number of
233 >     * threads (minus one) that have ever been created in the pool.
234 >     * This method may be useful for applications that track status or
235 >     * collect results per-worker rather than per-task.
236 >     *
237 >     * @return the index number
238 >     */
239 >    public int getPoolIndex() {
240 >        return poolIndex;
241 >    }
242 >
243 >    /**
244 >     * Establishes local first-in-first-out scheduling mode for forked
245 >     * tasks that are never joined.
246 >     *
247 >     * @param async if true, use locally FIFO scheduling
248       */
249 <    public static int getPoolIndex() {
250 <        return ((ForkJoinWorkerThread)(Thread.currentThread())).poolIndex;
249 >    void setAsyncMode(boolean async) {
250 >        locallyFifo = async;
251      }
252  
253 <    //  Access methods used by Pool
253 >    // Runstate management
254 >
255 >    // Runstate values. Order matters
256 >    private static final int RUNNING     = 0;
257 >    private static final int SHUTDOWN    = 1;
258 >    private static final int TERMINATING = 2;
259 >    private static final int TERMINATED  = 3;
260 >
261 >    final boolean isShutdown()    { return runState >= SHUTDOWN;  }
262 >    final boolean isTerminating() { return runState >= TERMINATING;  }
263 >    final boolean isTerminated()  { return runState == TERMINATED; }
264 >    final boolean shutdown()      { return transitionRunStateTo(SHUTDOWN); }
265 >    final boolean shutdownNow()   { return transitionRunStateTo(TERMINATING); }
266  
267      /**
268 <     * Get and clear steal count for accumulation by pool.  Called
269 <     * only when known to be idle (in pool.sync and termination).
268 >     * Transitions to at least the given state.
269 >     *
270 >     * @return {@code true} if not already at least at given state
271       */
272 <    final int getAndClearStealCount() {
273 <        int sc = stealCount;
274 <        stealCount = 0;
275 <        return sc;
272 >    private boolean transitionRunStateTo(int state) {
273 >        for (;;) {
274 >            int s = runState;
275 >            if (s >= state)
276 >                return false;
277 >            if (UNSAFE.compareAndSwapInt(this, runStateOffset, s, state))
278 >                return true;
279 >        }
280      }
281  
282      /**
283 <     * Returns estimate of the number of tasks in the queue, without
263 <     * correcting for transient negative values
283 >     * Tries to set status to active; fails on contention.
284       */
285 <    final int getRawQueueSize() {
286 <        return sp - base;
285 >    private boolean tryActivate() {
286 >        if (!active) {
287 >            if (!pool.tryIncrementActiveCount())
288 >                return false;
289 >            active = true;
290 >        }
291 >        return true;
292      }
293  
294 <    // Intrinsics-based support for queue operations.
295 <    // Currently these three (setSp, setSlot, casSlotNull) are
296 <    // usually manually inlined to improve performance
294 >    /**
295 >     * Tries to set status to inactive; fails on contention.
296 >     */
297 >    private boolean tryInactivate() {
298 >        if (active) {
299 >            if (!pool.tryDecrementActiveCount())
300 >                return false;
301 >            active = false;
302 >        }
303 >        return true;
304 >    }
305  
306      /**
307 <     * Sets sp in store-order.
307 >     * Computes next value for random victim probe.  Scans don't
308 >     * require a very high quality generator, but also not a crummy
309 >     * one.  Marsaglia xor-shift is cheap and works well.
310 >     */
311 >    private static int xorShift(int r) {
312 >        r ^= r << 1;
313 >        r ^= r >>> 3;
314 >        r ^= r << 10;
315 >        return r;
316 >    }
317 >
318 >    // Lifecycle methods
319 >
320 >    /**
321 >     * This method is required to be public, but should never be
322 >     * called explicitly. It performs the main run loop to execute
323 >     * ForkJoinTasks.
324       */
325 <    private void setSp(int s) {
326 <        _unsafe.putOrderedInt(this, spOffset, s);
325 >    public void run() {
326 >        Throwable exception = null;
327 >        try {
328 >            onStart();
329 >            pool.sync(this); // await first pool event
330 >            mainLoop();
331 >        } catch (Throwable ex) {
332 >            exception = ex;
333 >        } finally {
334 >            onTermination(exception);
335 >        }
336 >    }
337 >
338 >    /**
339 >     * Executes tasks until shut down.
340 >     */
341 >    private void mainLoop() {
342 >        while (!isShutdown()) {
343 >            ForkJoinTask<?> t = pollTask();
344 >            if (t != null || (t = pollSubmission()) != null)
345 >                t.quietlyExec();
346 >            else if (tryInactivate())
347 >                pool.sync(this);
348 >        }
349 >    }
350 >
351 >    /**
352 >     * Initializes internal state after construction but before
353 >     * processing any tasks. If you override this method, you must
354 >     * invoke super.onStart() at the beginning of the method.
355 >     * Initialization requires care: Most fields must have legal
356 >     * default values, to ensure that attempted accesses from other
357 >     * threads work correctly even before this thread starts
358 >     * processing tasks.
359 >     */
360 >    protected void onStart() {
361 >        // Allocate while starting to improve chances of thread-local
362 >        // isolation
363 >        queue = new ForkJoinTask<?>[INITIAL_QUEUE_CAPACITY];
364 >        // Initial value of seed need not be especially random but
365 >        // should differ across workers and must be nonzero
366 >        int p = poolIndex + 1;
367 >        seed = p + (p << 8) + (p << 16) + (p << 24); // spread bits
368      }
369  
370      /**
371 <     * Add in store-order the given task at given slot of q to
372 <     * null. Caller must ensure q is nonnull and index is in range.
371 >     * Performs cleanup associated with termination of this worker
372 >     * thread.  If you override this method, you must invoke
373 >     * {@code super.onTermination} at the end of the overridden method.
374 >     *
375 >     * @param exception the exception causing this thread to abort due
376 >     * to an unrecoverable error, or {@code null} if completed normally
377 >     */
378 >    protected void onTermination(Throwable exception) {
379 >        // Execute remaining local tasks unless aborting or terminating
380 >        while (exception == null &&  !pool.isTerminating() && base != sp) {
381 >            try {
382 >                ForkJoinTask<?> t = popTask();
383 >                if (t != null)
384 >                    t.quietlyExec();
385 >            } catch (Throwable ex) {
386 >                exception = ex;
387 >            }
388 >        }
389 >        // Cancel other tasks, transition status, notify pool, and
390 >        // propagate exception to uncaught exception handler
391 >        try {
392 >            do {} while (!tryInactivate()); // ensure inactive
393 >            cancelTasks();
394 >            runState = TERMINATED;
395 >            pool.workerTerminated(this);
396 >        } catch (Throwable ex) {        // Shouldn't ever happen
397 >            if (exception == null)      // but if so, at least rethrown
398 >                exception = ex;
399 >        } finally {
400 >            if (exception != null)
401 >                ForkJoinTask.rethrowException(exception);
402 >        }
403 >    }
404 >
405 >    // Intrinsics-based support for queue operations.
406 >
407 >    /**
408 >     * Adds in store-order the given task at given slot of q to null.
409 >     * Caller must ensure q is non-null and index is in range.
410       */
411      private static void setSlot(ForkJoinTask<?>[] q, int i,
412 <                                ForkJoinTask<?> t){
413 <        _unsafe.putOrderedObject(q, (i << qShift) + qBase, t);
412 >                                ForkJoinTask<?> t) {
413 >        UNSAFE.putOrderedObject(q, (i << qShift) + qBase, t);
414      }
415  
416      /**
417 <     * CAS given slot of q to null. Caller must ensure q is nonnull
417 >     * CAS given slot of q to null. Caller must ensure q is non-null
418       * and index is in range.
419       */
420      private static boolean casSlotNull(ForkJoinTask<?>[] q, int i,
421                                         ForkJoinTask<?> t) {
422 <        return _unsafe.compareAndSwapObject(q, (i << qShift) + qBase, t, null);
422 >        return UNSAFE.compareAndSwapObject(q, (i << qShift) + qBase, t, null);
423 >    }
424 >
425 >    /**
426 >     * Sets sp in store-order.
427 >     */
428 >    private void storeSp(int s) {
429 >        UNSAFE.putOrderedInt(this, spOffset, s);
430      }
431  
432      // Main queue methods
433  
434      /**
435       * Pushes a task. Called only by current thread.
436 <     * @param t the task. Caller must ensure nonnull
436 >     *
437 >     * @param t the task. Caller must ensure non-null.
438       */
439      final void pushTask(ForkJoinTask<?> t) {
440          ForkJoinTask<?>[] q = queue;
441          int mask = q.length - 1;
442          int s = sp;
443 <        _unsafe.putOrderedObject(q, ((s & mask) << qShift) + qBase, t);
444 <        _unsafe.putOrderedInt(this, spOffset, ++s);
443 >        setSlot(q, s & mask, t);
444 >        storeSp(++s);
445          if ((s -= base) == 1)
446 <            pool.signalNonEmptyWorkerQueue();
446 >            pool.signalWork();
447          else if (s >= mask)
448              growQueue();
449      }
# Line 316 | Line 451 | public class ForkJoinWorkerThread extend
451      /**
452       * Tries to take a task from the base of the queue, failing if
453       * either empty or contended.
454 <     * @return a task, or null if none or contended.
454 >     *
455 >     * @return a task, or null if none or contended
456       */
457 <    private ForkJoinTask<?> deqTask() {
322 <        ForkJoinTask<?>[] q;
457 >    final ForkJoinTask<?> deqTask() {
458          ForkJoinTask<?> t;
459 +        ForkJoinTask<?>[] q;
460          int i;
461          int b;
462          if (sp != (b = base) &&
463              (q = queue) != null && // must read q after b
464              (t = q[i = (q.length - 1) & b]) != null &&
465 <            _unsafe.compareAndSwapObject(q, (i << qShift) + qBase, t, null)) {
465 >            casSlotNull(q, i, t)) {
466              base = b + 1;
467              return t;
468          }
# Line 334 | Line 470 | public class ForkJoinWorkerThread extend
470      }
471  
472      /**
473 <     * Returns a popped task, or null if empty.  Called only by
474 <     * current thread.
473 >     * Returns a popped task, or null if empty. Ensures active status
474 >     * if non-null. Called only by current thread.
475       */
476      final ForkJoinTask<?> popTask() {
341        ForkJoinTask<?> t;
342        int i;
343        ForkJoinTask<?>[] q = queue;
344        int mask = q.length - 1;
477          int s = sp;
478 <        if (s != base &&
479 <            (t = q[i = (s - 1) & mask]) != null &&
480 <            _unsafe.compareAndSwapObject(q, (i << qShift) + qBase, t, null)) {
481 <            _unsafe.putOrderedInt(this, spOffset, s - 1);
482 <            return t;
478 >        while (s != base) {
479 >            if (tryActivate()) {
480 >                ForkJoinTask<?>[] q = queue;
481 >                int mask = q.length - 1;
482 >                int i = (s - 1) & mask;
483 >                ForkJoinTask<?> t = q[i];
484 >                if (t == null || !casSlotNull(q, i, t))
485 >                    break;
486 >                storeSp(s - 1);
487 >                return t;
488 >            }
489          }
490          return null;
491      }
# Line 355 | Line 493 | public class ForkJoinWorkerThread extend
493      /**
494       * Specialized version of popTask to pop only if
495       * topmost element is the given task. Called only
496 <     * by current thread.
497 <     * @param t the task. Caller must ensure nonnull
496 >     * by current thread while active.
497 >     *
498 >     * @param t the task. Caller must ensure non-null.
499       */
500      final boolean unpushTask(ForkJoinTask<?> t) {
501          ForkJoinTask<?>[] q = queue;
502          int mask = q.length - 1;
503          int s = sp - 1;
504 <        if (_unsafe.compareAndSwapObject(q, ((s & mask) << qShift) + qBase,
505 <                                         t, null)) {
367 <            _unsafe.putOrderedInt(this, spOffset, s);
504 >        if (casSlotNull(q, s & mask, t)) {
505 >            storeSp(s);
506              return true;
507          }
508          return false;
509      }
510  
511      /**
512 <     * Returns next task to pop.
512 >     * Returns next task.
513       */
514      final ForkJoinTask<?> peekTask() {
515          ForkJoinTask<?>[] q = queue;
516 <        return q == null? null : q[(sp - 1) & (q.length - 1)];
516 >        if (q == null)
517 >            return null;
518 >        int mask = q.length - 1;
519 >        int i = locallyFifo ? base : (sp - 1);
520 >        return q[i & mask];
521      }
522  
523      /**
# Line 402 | Line 544 | public class ForkJoinWorkerThread extend
544                  t = null;
545              setSlot(newQ, b & newMask, t);
546          } while (++b != bf);
547 <        pool.signalIdleWorkers(false);
547 >        pool.signalWork();
548      }
549  
408    // Runstate management
409
410    final boolean isShutdown()    { return runState >= SHUTDOWN;  }
411    final boolean isTerminating() { return runState >= TERMINATING;  }
412    final boolean isTerminated()  { return runState == TERMINATED; }
413    final boolean shutdown()      { return transitionRunStateTo(SHUTDOWN); }
414    final boolean shutdownNow()   { return transitionRunStateTo(TERMINATING); }
415
550      /**
551 <     * Transition to at least the given state. Return true if not
552 <     * already at least given state.
553 <     */
554 <    private boolean transitionRunStateTo(int state) {
555 <        for (;;) {
556 <            int s = runState;
557 <            if (s >= state)
558 <                return false;
559 <            if (_unsafe.compareAndSwapInt(this, runStateOffset, s, state))
560 <                return true;
561 <        }
562 <    }
563 <
564 <    /**
565 <     * Ensure status is active and if necessary adjust pool active count
566 <     */
433 <    final void activate() {
434 <        if (!active) {
435 <            active = true;
436 <            pool.incrementActiveCount();
437 <        }
438 <    }
439 <
440 <    /**
441 <     * Ensure status is inactive and if necessary adjust pool active count
551 >     * Tries to steal a task from another worker. Starts at a random
552 >     * index of workers array, and probes workers until finding one
553 >     * with non-empty queue or finding that all are empty.  It
554 >     * randomly selects the first n probes. If these are empty, it
555 >     * resorts to a full circular traversal, which is necessary to
556 >     * accurately set active status by caller. Also restarts if pool
557 >     * events occurred since last scan, which forces refresh of
558 >     * workers array, in case barrier was associated with resize.
559 >     *
560 >     * This method must be both fast and quiet -- usually avoiding
561 >     * memory accesses that could disrupt cache sharing etc other than
562 >     * those needed to check for and take tasks. This accounts for,
563 >     * among other things, updating random seed in place without
564 >     * storing it until exit.
565 >     *
566 >     * @return a task, or null if none found
567       */
568 <    final void inactivate() {
569 <        if (active) {
570 <            active = false;
571 <            pool.decrementActiveCount();
572 <        }
568 >    private ForkJoinTask<?> scan() {
569 >        ForkJoinTask<?> t = null;
570 >        int r = seed;                    // extract once to keep scan quiet
571 >        ForkJoinWorkerThread[] ws;       // refreshed on outer loop
572 >        int mask;                        // must be power 2 minus 1 and > 0
573 >        outer:do {
574 >            if ((ws = pool.workers) != null && (mask = ws.length - 1) > 0) {
575 >                int idx = r;
576 >                int probes = ~mask;      // use random index while negative
577 >                for (;;) {
578 >                    r = xorShift(r);     // update random seed
579 >                    ForkJoinWorkerThread v = ws[mask & idx];
580 >                    if (v == null || v.sp == v.base) {
581 >                        if (probes <= mask)
582 >                            idx = (probes++ < 0) ? r : (idx + 1);
583 >                        else
584 >                            break;
585 >                    }
586 >                    else if (!tryActivate() || (t = v.deqTask()) == null)
587 >                        continue outer;  // restart on contention
588 >                    else
589 >                        break outer;
590 >                }
591 >            }
592 >        } while (pool.hasNewSyncEvent(this)); // retry on pool events
593 >        seed = r;
594 >        return t;
595      }
596  
450    // Lifecycle methods
451
597      /**
598 <     * Initializes internal state after construction but before
599 <     * processing any tasks. If you override this method, you must
600 <     * invoke super.onStart() at the beginning of the method.
456 <     * Initialization requires care: Most fields must have legal
457 <     * default values, to ensure that attempted accesses from other
458 <     * threads work correctly even before this thread starts
459 <     * processing tasks.
598 >     * Gets and removes a local or stolen task.
599 >     *
600 >     * @return a task, if available
601       */
602 <    protected void onStart() {
603 <        juRandomSeed = randomSeedGenerator.nextLong();
604 <        do;while((randomVictimSeed = nextRandomInt()) == 0); // must be nonzero
605 <        if (queue == null)
606 <            queue = new ForkJoinTask<?>[INITIAL_QUEUE_CAPACITY];
466 <
467 <        // Heuristically allow one initial thread to warm up; others wait
468 <        if (poolIndex < pool.getParallelism() - 1) {
469 <            eventCount = pool.sync(this, 0);
470 <            activate();
471 <        }
602 >    final ForkJoinTask<?> pollTask() {
603 >        ForkJoinTask<?> t = locallyFifo ? deqTask() : popTask();
604 >        if (t == null && (t = scan()) != null)
605 >            ++stealCount;
606 >        return t;
607      }
608  
609      /**
610 <     * Perform cleanup associated with termination of this worker
476 <     * thread.  If you override this method, you must invoke
477 <     * super.onTermination at the end of the overridden method.
610 >     * Gets a local task.
611       *
612 <     * @param exception the exception causing this thread to abort due
480 <     * to an unrecoverable error, or null if completed normally.
612 >     * @return a task, if available
613       */
614 <    protected void onTermination(Throwable exception) {
615 <        try {
484 <            clearLocalTasks();
485 <            inactivate();
486 <            cancelTasks();
487 <        } finally {
488 <            terminate(exception);
489 <        }
614 >    final ForkJoinTask<?> pollLocalTask() {
615 >        return locallyFifo ? deqTask() : popTask();
616      }
617  
618      /**
619 <     * Notify pool of termination and, if exception is nonnull,
620 <     * rethrow it to trigger this thread's uncaughtExceptionHandler
619 >     * Returns a pool submission, if one exists, activating first.
620 >     *
621 >     * @return a submission, if available
622       */
623 <    private void terminate(Throwable exception) {
624 <        transitionRunStateTo(TERMINATED);
625 <        try {
626 <            pool.workerTerminated(this);
627 <        } finally {
628 <            if (exception != null)
502 <                ForkJoinTask.rethrowException(exception);
623 >    private ForkJoinTask<?> pollSubmission() {
624 >        ForkJoinPool p = pool;
625 >        while (p.hasQueuedSubmissions()) {
626 >            ForkJoinTask<?> t;
627 >            if (tryActivate() && (t = p.pollSubmission()) != null)
628 >                return t;
629          }
630 +        return null;
631      }
632  
633 <    /**
507 <     * Run local tasks on exit from main.
508 <     */
509 <    private void clearLocalTasks() {
510 <        while (base != sp && !pool.isTerminating()) {
511 <            ForkJoinTask<?> t = popTask();
512 <            if (t != null) {
513 <                activate(); // ensure active status
514 <                t.quietlyExec();
515 <            }
516 <        }
517 <    }
633 >    // Methods accessed only by Pool
634  
635      /**
636       * Removes and cancels all tasks in queue.  Can be called from any
637       * thread.
638       */
639      final void cancelTasks() {
640 <        while (base != sp) {
641 <            ForkJoinTask<?> t = deqTask();
642 <            if (t != null)
527 <                t.cancelIgnoreExceptions();
528 <        }
529 <    }
530 <
531 <    /**
532 <     * This method is required to be public, but should never be
533 <     * called explicitly. It performs the main run loop to execute
534 <     * ForkJoinTasks.
535 <     */
536 <    public void run() {
537 <        Throwable exception = null;
538 <        try {
539 <            onStart();
540 <            while (!isShutdown())
541 <                step();
542 <        } catch (Throwable ex) {
543 <            exception = ex;
544 <        } finally {
545 <            onTermination(exception);
546 <        }
640 >        ForkJoinTask<?> t;
641 >        while (base != sp && (t = deqTask()) != null)
642 >            t.cancelIgnoringExceptions();
643      }
644  
645      /**
646 <     * Main top-level action.
646 >     * Drains tasks to given collection c.
647 >     *
648 >     * @return the number of tasks drained
649       */
650 <    private void step() {
651 <        ForkJoinTask<?> t = sp != base? popTask() : null;
652 <        if (t != null || (t = scan(null, true)) != null) {
653 <            activate();
654 <            t.quietlyExec();
655 <        }
558 <        else {
559 <            inactivate();
560 <            eventCount = pool.sync(this, eventCount);
650 >    final int drainTasksTo(Collection<ForkJoinTask<?>> c) {
651 >        int n = 0;
652 >        ForkJoinTask<?> t;
653 >        while (base != sp && (t = deqTask()) != null) {
654 >            c.add(t);
655 >            ++n;
656          }
657 +        return n;
658      }
659  
564    // scanning for and stealing tasks
565
660      /**
661 <     * Computes next value for random victim probe. Scans don't
662 <     * require a very high quality generator, but also not a crummy
569 <     * one. Marsaglia xor-shift is cheap and works well.
570 <     *
571 <     * This is currently unused, and manually inlined
661 >     * Gets and clears steal count for accumulation by pool.  Called
662 >     * only when known to be idle (in pool.sync and termination).
663       */
664 <    private static int xorShift(int r) {
665 <        r ^= r << 1;
666 <        r ^= r >>> 3;
667 <        r ^= r << 10;
577 <        return r;
664 >    final int getAndClearStealCount() {
665 >        int sc = stealCount;
666 >        stealCount = 0;
667 >        return sc;
668      }
669  
670      /**
671 <     * Tries to steal a task from another worker and/or, if enabled,
672 <     * submission queue. Starts at a random index of workers array,
583 <     * and probes workers until finding one with non-empty queue or
584 <     * finding that all are empty.  It randomly selects the first n-1
585 <     * probes. If these are empty, it resorts to full circular
586 <     * traversal, which is necessary to accurately set active status
587 <     * by caller. Also restarts if pool barrier has tripped since last
588 <     * scan, which forces refresh of workers array, in case barrier
589 <     * was associated with resize.
671 >     * Returns {@code true} if at least one worker in the given array
672 >     * appears to have at least one queued task.
673       *
674 <     * This method must be both fast and quiet -- usually avoiding
592 <     * memory accesses that could disrupt cache sharing etc other than
593 <     * those needed to check for and take tasks. This accounts for,
594 <     * among other things, updating random seed in place without
595 <     * storing it until exit. (Note that we only need to store it if
596 <     * we found a task; otherwise it doesn't matter if we start at the
597 <     * same place next time.)
598 <     *
599 <     * @param joinMe if non null; exit early if done
600 <     * @param checkSubmissions true if OK to take submissions
601 <     * @return a task, or null if none found
674 >     * @param ws array of workers
675       */
676 <    private ForkJoinTask<?> scan(ForkJoinTask<?> joinMe,
677 <                                 boolean checkSubmissions) {
678 <        ForkJoinPool p = pool;
679 <        if (p == null)                    // Never null, but avoids
680 <            return null;                  //   implicit nullchecks below
681 <        int r = randomVictimSeed;         // extract once to keep scan quiet
682 <        restart:                          // outer loop refreshes ws array
683 <        while (joinMe == null || joinMe.status >= 0) {
611 <            int mask;
612 <            ForkJoinWorkerThread[] ws = p.workers;
613 <            if (ws != null && (mask = ws.length - 1) > 0) {
614 <                int probes = -mask;       // use random index while negative
615 <                int idx = r;
616 <                for (;;) {
617 <                    ForkJoinWorkerThread v;
618 <                    // inlined xorshift to update seed
619 <                    r ^= r << 1;  r ^= r >>> 3; r ^= r << 10;
620 <                    if ((v = ws[mask & idx]) != null && v.sp != v.base) {
621 <                        ForkJoinTask<?> t;
622 <                        activate();
623 <                        if ((joinMe == null || joinMe.status >= 0) &&
624 <                            (t = v.deqTask()) != null) {
625 <                            randomVictimSeed = r;
626 <                            ++stealCount;
627 <                            return t;
628 <                        }
629 <                        continue restart; // restart on contention
630 <                    }
631 <                    if ((probes >> 1) <= mask) // n-1 random then circular
632 <                        idx = (probes++ < 0)? r : (idx + 1);
633 <                    else
634 <                        break;
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                  }
685              }
637            if (checkSubmissions && p.hasQueuedSubmissions()) {
638                activate();
639                ForkJoinTask<?> t = p.pollSubmission();
640                if (t != null)
641                    return t;
642            }
643            else {
644                long ec = eventCount;     // restart on pool event
645                if ((eventCount = p.getEventCount()) == ec)
646                    break;
647            }
686          }
687 <        return null;
650 <    }
651 <
652 <    /**
653 <     * Callback from pool.sync to rescan before blocking.  If a
654 <     * task is found, it is pushed so it can be executed upon return.
655 <     * @return true if found and pushed a task
656 <     */
657 <    final boolean prescan() {
658 <        ForkJoinTask<?> t = scan(null, true);
659 <        if (t != null) {
660 <            pushTask(t);
661 <            return true;
662 <        }
663 <        else {
664 <            inactivate();
665 <            return false;
666 <        }
667 <    }
668 <
669 <    // Support for ForkJoinTask methods
670 <
671 <    /**
672 <     * Implements ForkJoinTask.helpJoin
673 <     */
674 <    final int helpJoinTask(ForkJoinTask<?> joinMe) {
675 <        ForkJoinTask<?> t = null;
676 <        int s;
677 <        while ((s = joinMe.status) >= 0) {
678 <            if (t == null) {
679 <                if ((t = scan(joinMe, false)) == null)  // block if no work
680 <                    return joinMe.awaitDone(this, false);
681 <                // else recheck status before exec
682 <            }
683 <            else {
684 <                t.quietlyExec();
685 <                t = null;
686 <            }
687 <        }
688 <        if (t != null) // unsteal
689 <            pushTask(t);
690 <        return s;
691 <    }
692 <
693 <    /**
694 <     * Pops or steals a task
695 <     * @return task, or null if none available
696 <     */
697 <    final ForkJoinTask<?> getLocalOrStolenTask() {
698 <        ForkJoinTask<?> t = popTask();
699 <        return t != null? t : scan(null, false);
687 >        return false;
688      }
689  
690 <    /**
703 <     * Runs tasks until pool isQuiescent
704 <     */
705 <    final void helpQuiescePool() {
706 <        for (;;) {
707 <            ForkJoinTask<?> t = getLocalOrStolenTask();
708 <            if (t != null) {
709 <                activate();
710 <                t.quietlyExec();
711 <            }
712 <            else {
713 <                inactivate();
714 <                if (pool.isQuiescent()) {
715 <                    activate(); // re-activate on exit
716 <                    break;
717 <                }
718 <            }
719 <        }
720 <    }
690 >    // Support methods for ForkJoinTask
691  
692      /**
693       * Returns an estimate of the number of tasks in the queue.
694       */
695      final int getQueueSize() {
696 <        int b = base;
697 <        int n = sp - b;
728 <        return n <= 0? 0 : n; // suppress momentarily negative values
696 >        // suppress momentarily negative values
697 >        return Math.max(0, sp - base);
698      }
699  
700      /**
# Line 733 | Line 702 | public class ForkJoinWorkerThread extend
702       * function of number of idle workers.
703       */
704      final int getEstimatedSurplusTaskCount() {
705 +        // The halving approximates weighting idle vs non-idle workers
706          return (sp - base) - (pool.getIdleThreadCount() >>> 1);
707      }
708  
739    // Per-worker exported random numbers
740
741    // Same constants as java.util.Random
742    final static long JURandomMultiplier = 0x5DEECE66DL;
743    final static long JURandomAddend = 0xBL;
744    final static long JURandomMask = (1L << 48) - 1;
745
746    private final int nextJURandom(int bits) {
747        long next = (juRandomSeed * JURandomMultiplier + JURandomAddend) &
748            JURandomMask;
749        juRandomSeed = next;
750        return (int)(next >>> (48 - bits));
751    }
752
753    private final int nextJURandomInt(int n) {
754        if (n <= 0)
755            throw new IllegalArgumentException("n must be positive");
756        int bits = nextJURandom(31);
757        if ((n & -n) == n)
758            return (int)((n * (long)bits) >> 31);
759
760        for (;;) {
761            int val = bits % n;
762            if (bits - val + (n-1) >= 0)
763                return val;
764            bits = nextJURandom(31);
765        }
766    }
767
768    private final long nextJURandomLong() {
769        return ((long)(nextJURandom(32)) << 32) + nextJURandom(32);
770    }
771
772    private final long nextJURandomLong(long n) {
773        if (n <= 0)
774            throw new IllegalArgumentException("n must be positive");
775        long offset = 0;
776        while (n >= Integer.MAX_VALUE) { // randomly pick half range
777            int bits = nextJURandom(2); // 2nd bit for odd vs even split
778            long half = n >>> 1;
779            long nextn = ((bits & 2) == 0)? half : n - half;
780            if ((bits & 1) == 0)
781                offset += n - nextn;
782            n = nextn;
783        }
784        return offset + nextJURandomInt((int)n);
785    }
786
787    private final double nextJURandomDouble() {
788        return (((long)(nextJURandom(26)) << 27) + nextJURandom(27))
789            / (double)(1L << 53);
790    }
791
709      /**
710 <     * Returns a random integer using a per-worker random
794 <     * number generator with the same properties as
795 <     * {@link java.util.Random#nextInt}
796 <     * @return the next pseudorandom, uniformly distributed {@code int}
797 <     *         value from this worker's random number generator's sequence
710 >     * Scans, returning early if joinMe done.
711       */
712 <    public static int nextRandomInt() {
713 <        return ((ForkJoinWorkerThread)(Thread.currentThread())).
714 <            nextJURandom(32);
712 >    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      }
720  
721      /**
722 <     * Returns a random integer using a per-worker random
806 <     * number generator with the same properties as
807 <     * {@link java.util.Random#nextInt(int)}
808 <     * @param n the bound on the random number to be returned.  Must be
809 <     *        positive.
810 <     * @return the next pseudorandom, uniformly distributed {@code int}
811 <     *         value between {@code 0} (inclusive) and {@code n} (exclusive)
812 <     *         from this worker's random number generator's sequence
813 <     * @throws IllegalArgumentException if n is not positive
722 >     * Runs tasks until {@code pool.isQuiescent()}.
723       */
724 <    public static int nextRandomInt(int n) {
725 <        return ((ForkJoinWorkerThread)(Thread.currentThread())).
726 <            nextJURandomInt(n);
724 >    final void helpQuiescePool() {
725 >        for (;;) {
726 >            ForkJoinTask<?> t = pollTask();
727 >            if (t != null)
728 >                t.quietlyExec();
729 >            else if (tryInactivate() && pool.isQuiescent())
730 >                break;
731 >        }
732 >        do {} while (!tryActivate()); // re-activate on exit
733      }
734  
735 <    /**
736 <     * Returns a random long using a per-worker random
737 <     * number generator with the same properties as
738 <     * {@link java.util.Random#nextLong}
739 <     * @return the next pseudorandom, uniformly distributed {@code long}
740 <     *         value from this worker's random number generator's sequence
741 <     */
742 <    public static long nextRandomLong() {
743 <        return ((ForkJoinWorkerThread)(Thread.currentThread())).
744 <            nextJURandomLong();
735 >    // Unsafe mechanics
736 >
737 >    private static final sun.misc.Unsafe UNSAFE = getUnsafe();
738 >    private static final long spOffset =
739 >        objectFieldOffset("sp", ForkJoinWorkerThread.class);
740 >    private static final long runStateOffset =
741 >        objectFieldOffset("runState", ForkJoinWorkerThread.class);
742 >    private static final long qBase;
743 >    private static final int qShift;
744 >
745 >    static {
746 >        qBase = UNSAFE.arrayBaseOffset(ForkJoinTask[].class);
747 >        int s = UNSAFE.arrayIndexScale(ForkJoinTask[].class);
748 >        if ((s & (s-1)) != 0)
749 >            throw new Error("data type scale not a power of two");
750 >        qShift = 31 - Integer.numberOfLeadingZeros(s);
751      }
752  
753 <    /**
754 <     * Returns a random integer using a per-worker random
755 <     * number generator with the same properties as
756 <     * {@link java.util.Random#nextInt(int)}
757 <     * @param n the bound on the random number to be returned.  Must be
758 <     *        positive.
759 <     * @return the next pseudorandom, uniformly distributed {@code int}
760 <     *         value between {@code 0} (inclusive) and {@code n} (exclusive)
761 <     *         from this worker's random number generator's sequence
841 <     * @throws IllegalArgumentException if n is not positive
842 <     */
843 <    public static long nextRandomLong(long n) {
844 <        return ((ForkJoinWorkerThread)(Thread.currentThread())).
845 <            nextJURandomLong(n);
753 >    private static long objectFieldOffset(String field, Class<?> klazz) {
754 >        try {
755 >            return UNSAFE.objectFieldOffset(klazz.getDeclaredField(field));
756 >        } catch (NoSuchFieldException e) {
757 >            // Convert Exception to corresponding Error
758 >            NoSuchFieldError error = new NoSuchFieldError(field);
759 >            error.initCause(e);
760 >            throw error;
761 >        }
762      }
763  
764      /**
765 <     * Returns a random double using a per-worker random
766 <     * number generator with the same properties as
767 <     * {@link java.util.Random#nextDouble}
768 <     * @return the next pseudorandom, uniformly distributed {@code double}
769 <     *         value between {@code 0.0} and {@code 1.0} from this
854 <     *         worker's random number generator's sequence
765 >     * Returns a sun.misc.Unsafe.  Suitable for use in a 3rd party package.
766 >     * Replace with a simple call to Unsafe.getUnsafe when integrating
767 >     * into a jdk.
768 >     *
769 >     * @return a sun.misc.Unsafe
770       */
771 <    public static double nextRandomDouble() {
857 <        return ((ForkJoinWorkerThread)(Thread.currentThread())).
858 <            nextJURandomDouble();
859 <    }
860 <
861 <    // Temporary Unsafe mechanics for preliminary release
862 <
863 <    static final Unsafe _unsafe;
864 <    static final long baseOffset;
865 <    static final long spOffset;
866 <    static final long qBase;
867 <    static final int qShift;
868 <    static final long runStateOffset;
869 <    static {
771 >    private static sun.misc.Unsafe getUnsafe() {
772          try {
773 <            if (ForkJoinWorkerThread.class.getClassLoader() != null) {
774 <                Field f = Unsafe.class.getDeclaredField("theUnsafe");
775 <                f.setAccessible(true);
776 <                _unsafe = (Unsafe)f.get(null);
773 >            return sun.misc.Unsafe.getUnsafe();
774 >        } catch (SecurityException se) {
775 >            try {
776 >                return java.security.AccessController.doPrivileged
777 >                    (new java.security
778 >                     .PrivilegedExceptionAction<sun.misc.Unsafe>() {
779 >                        public sun.misc.Unsafe run() throws Exception {
780 >                            java.lang.reflect.Field f = sun.misc
781 >                                .Unsafe.class.getDeclaredField("theUnsafe");
782 >                            f.setAccessible(true);
783 >                            return (sun.misc.Unsafe) f.get(null);
784 >                        }});
785 >            } catch (java.security.PrivilegedActionException e) {
786 >                throw new RuntimeException("Could not initialize intrinsics",
787 >                                           e.getCause());
788              }
876            else
877                _unsafe = Unsafe.getUnsafe();
878            baseOffset = _unsafe.objectFieldOffset
879                (ForkJoinWorkerThread.class.getDeclaredField("base"));
880            spOffset = _unsafe.objectFieldOffset
881                (ForkJoinWorkerThread.class.getDeclaredField("sp"));
882            runStateOffset = _unsafe.objectFieldOffset
883                (ForkJoinWorkerThread.class.getDeclaredField("runState"));
884            qBase = _unsafe.arrayBaseOffset(ForkJoinTask[].class);
885            int s = _unsafe.arrayIndexScale(ForkJoinTask[].class);
886            if ((s & (s-1)) != 0)
887                throw new Error("data type scale not a power of two");
888            qShift = 31 - Integer.numberOfLeadingZeros(s);
889        } catch (Exception e) {
890            throw new RuntimeException("Could not initialize intrinsics", e);
789          }
790      }
791   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines