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.4 by dl, Wed Jan 7 20:51:36 2009 UTC vs.
Revision 1.5 by dl, Mon Jan 12 17:16:18 2009 UTC

# Line 21 | Line 21 | import java.lang.reflect.*;
21   * do create such a subclass, you will also need to supply a custom
22   * ForkJoinWorkerThreadFactory to use it in a ForkJoinPool.
23   *
24 * <p>This class also provides methods for generating per-thread
25 * random numbers, with the same properties as {@link
26 * java.util.Random} but with each generator isolated from those of
27 * other threads.
24   */
25   public class ForkJoinWorkerThread extends Thread {
26      /*
# 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
191 <     */
204 <    private int randomVictimSeed;
205 <
206 <    /**
207 <     * Seed for embedded Jurandom
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 long juRandomSeed;
193 >    int poolIndex;
194  
195      /**
196 <     * The last barrier event waited for
196 >     * The last barrier event waited for. Accessed in pool callback
197 >     * methods, but only by current thread.
198       */
199 <    private long eventCount;
199 >    long lastEventCount;
200  
201      /**
202       * Creates a ForkJoinWorkerThread operating in the given pool.
# Line 221 | Line 206 | public class ForkJoinWorkerThread extend
206      protected ForkJoinWorkerThread(ForkJoinPool pool) {
207          if (pool == null) throw new NullPointerException();
208          this.pool = pool;
209 <        // remaining initialization deferred to onStart
209 >        // Note: poolIndex is set by pool during construction
210 >        // Remaining initialization is deferred to onStart
211      }
212  
213 <    // public access methods
213 >    // Public access methods
214  
215      /**
216       * Returns the pool hosting this thread
# Line 239 | Line 225 | public class ForkJoinWorkerThread extend
225       * returned value ranges from zero to the maximum number of
226       * threads (minus one) that have ever been created in the pool.
227       * This method may be useful for applications that track status or
228 <     * collect results on a per-worker basis.
228 >     * collect results per-worker rather than per-task.
229       * @return the index number.
230       */
231      public int getPoolIndex() {
232          return poolIndex;
233      }
234  
235 <    //  Access methods used by Pool
235 >
236 >    // Runstate management
237 >
238 >    // Runstate values. Order matters
239 >    private static final int RUNNING     = 0;
240 >    private static final int SHUTDOWN    = 1;
241 >    private static final int TERMINATING = 2;
242 >    private static final int TERMINATED  = 3;
243 >
244 >    final boolean isShutdown()    { return runState >= SHUTDOWN;  }
245 >    final boolean isTerminating() { return runState >= TERMINATING;  }
246 >    final boolean isTerminated()  { return runState == TERMINATED; }
247 >    final boolean shutdown()      { return transitionRunStateTo(SHUTDOWN); }
248 >    final boolean shutdownNow()   { return transitionRunStateTo(TERMINATING); }
249  
250      /**
251 <     * Get and clear steal count for accumulation by pool.  Called
252 <     * only when known to be idle (in pool.sync and termination).
251 >     * Transition to at least the given state. Return true if not
252 >     * already at least given state.
253       */
254 <    final int getAndClearStealCount() {
255 <        int sc = stealCount;
256 <        stealCount = 0;
257 <        return sc;
254 >    private boolean transitionRunStateTo(int state) {
255 >        for (;;) {
256 >            int s = runState;
257 >            if (s >= state)
258 >                return false;
259 >            if (_unsafe.compareAndSwapInt(this, runStateOffset, s, state))
260 >                return true;
261 >        }
262      }
263  
264      /**
265 <     * Returns estimate of the number of tasks in the queue, without
263 <     * correcting for transient negative values
265 >     * Try to set status to active; fail on contention
266       */
267 <    final int getRawQueueSize() {
268 <        return sp - base;
267 >    private boolean tryActivate() {
268 >        if (!active) {
269 >            if (!pool.tryIncrementActiveCount())
270 >                return false;
271 >            active = true;
272 >        }
273 >        return true;
274      }
275  
276 <    // Intrinsics-based support for queue operations.
277 <    // Currently these three (setSp, setSlot, casSlotNull) are
278 <    // usually manually inlined to improve performance
276 >    /**
277 >     * Try to set status to active; fail on contention
278 >     */
279 >    private boolean tryInactivate() {
280 >        if (active) {
281 >            if (!pool.tryDecrementActiveCount())
282 >                return false;
283 >            active = false;
284 >        }
285 >        return true;
286 >    }
287  
288      /**
289 <     * Sets sp in store-order.
289 >     * Computes next value for random victim probe. Scans don't
290 >     * require a very high quality generator, but also not a crummy
291 >     * one. Marsaglia xor-shift is cheap and works well.
292       */
293 <    private void setSp(int s) {
294 <        _unsafe.putOrderedInt(this, spOffset, s);
293 >    private static int xorShift(int r) {
294 >        r ^= r << 1;
295 >        r ^= r >>> 3;
296 >        r ^= r << 10;
297 >        return r;
298      }
299  
300 +    // Lifecycle methods
301 +
302 +    /**
303 +     * This method is required to be public, but should never be
304 +     * called explicitly. It performs the main run loop to execute
305 +     * ForkJoinTasks.
306 +     */
307 +    public void run() {
308 +        Throwable exception = null;
309 +        try {
310 +            onStart();
311 +            pool.sync(this); // await first pool event
312 +            mainLoop();
313 +        } catch (Throwable ex) {
314 +            exception = ex;
315 +        } finally {
316 +            onTermination(exception);
317 +        }
318 +    }
319 +
320 +    /**
321 +     * Execute tasks until shut down.
322 +     */
323 +    private void mainLoop() {
324 +        while (!isShutdown()) {
325 +            ForkJoinTask<?> t = pollTask();
326 +            if (t != null || (t = pollSubmission()) != null)
327 +                t.quietlyExec();
328 +            else if (tryInactivate())
329 +                pool.sync(this);
330 +        }
331 +    }
332 +
333 +    /**
334 +     * Initializes internal state after construction but before
335 +     * processing any tasks. If you override this method, you must
336 +     * invoke super.onStart() at the beginning of the method.
337 +     * Initialization requires care: Most fields must have legal
338 +     * default values, to ensure that attempted accesses from other
339 +     * threads work correctly even before this thread starts
340 +     * processing tasks.
341 +     */
342 +    protected void onStart() {
343 +        // Allocate while starting to improve chances of thread-local
344 +        // isolation
345 +        queue = new ForkJoinTask<?>[INITIAL_QUEUE_CAPACITY];
346 +        // Initial value of seed need not be especially random but
347 +        // should differ across workers and must be nonzero
348 +        int p = poolIndex + 1;
349 +        seed = p + (p << 8) + (p << 16) + (p << 24); // spread bits
350 +    }
351 +
352 +    /**
353 +     * Perform cleanup associated with termination of this worker
354 +     * thread.  If you override this method, you must invoke
355 +     * super.onTermination at the end of the overridden method.
356 +     *
357 +     * @param exception the exception causing this thread to abort due
358 +     * to an unrecoverable error, or null if completed normally.
359 +     */
360 +    protected void onTermination(Throwable exception) {
361 +        // Execute remaining local tasks unless aborting or terminating
362 +        while (exception == null &&  !pool.isTerminating() && base != sp) {
363 +            try {
364 +                ForkJoinTask<?> t = popTask();
365 +                if (t != null)
366 +                    t.quietlyExec();
367 +            } catch(Throwable ex) {
368 +                exception = ex;
369 +            }
370 +        }
371 +        // Cancel other tasks, transition status, notify pool, and
372 +        // propagate exception to uncaught exception handler
373 +        try {
374 +            do;while (!tryInactivate()); // ensure inactive
375 +            cancelTasks();        
376 +            runState = TERMINATED;
377 +            pool.workerTerminated(this);
378 +        } catch (Throwable ex) {        // Shouldn't ever happen
379 +            if (exception == null)      // but if so, at least rethrown
380 +                exception = ex;
381 +        } finally {
382 +            if (exception != null)
383 +                ForkJoinTask.rethrowException(exception);
384 +        }
385 +    }
386 +
387 +    // Intrinsics-based support for queue operations.  
388 +
389      /**
390       * Add in store-order the given task at given slot of q to
391       * null. Caller must ensure q is nonnull and index is in range.
# Line 295 | Line 404 | public class ForkJoinWorkerThread extend
404          return _unsafe.compareAndSwapObject(q, (i << qShift) + qBase, t, null);
405      }
406  
407 +    /**
408 +     * Sets sp in store-order.
409 +     */
410 +    private void storeSp(int s) {
411 +        _unsafe.putOrderedInt(this, spOffset, s);
412 +    }
413 +
414      // Main queue methods
415  
416      /**
# Line 305 | Line 421 | public class ForkJoinWorkerThread extend
421          ForkJoinTask<?>[] q = queue;
422          int mask = q.length - 1;
423          int s = sp;
424 <        _unsafe.putOrderedObject(q, ((s & mask) << qShift) + qBase, t);
425 <        _unsafe.putOrderedInt(this, spOffset, ++s);
424 >        setSlot(q, s & mask, t);
425 >        storeSp(++s);
426          if ((s -= base) == 1)
427 <            pool.signalNonEmptyWorkerQueue();
427 >            pool.signalWork();
428          else if (s >= mask)
429              growQueue();
430      }
# Line 319 | Line 435 | public class ForkJoinWorkerThread extend
435       * @return a task, or null if none or contended.
436       */
437      private ForkJoinTask<?> deqTask() {
322        ForkJoinTask<?>[] q;
438          ForkJoinTask<?> t;
439 +        ForkJoinTask<?>[] q;
440          int i;
441          int b;
442          if (sp != (b = base) &&
443              (q = queue) != null && // must read q after b
444              (t = q[i = (q.length - 1) & b]) != null &&
445 <            _unsafe.compareAndSwapObject(q, (i << qShift) + qBase, t, null)) {
445 >            casSlotNull(q, i, t)) {
446              base = b + 1;
447              return t;
448          }
# Line 334 | Line 450 | public class ForkJoinWorkerThread extend
450      }
451  
452      /**
453 <     * Returns a popped task, or null if empty.  Called only by
454 <     * current thread.
453 >     * Returns a popped task, or null if empty. Ensures active status
454 >     * if nonnull. Called only by current thread.
455       */
456      final ForkJoinTask<?> popTask() {
341        ForkJoinTask<?> t;
342        int i;
343        ForkJoinTask<?>[] q = queue;
344        int mask = q.length - 1;
457          int s = sp;
458 <        if (s != base &&
459 <            (t = q[i = (s - 1) & mask]) != null &&
460 <            _unsafe.compareAndSwapObject(q, (i << qShift) + qBase, t, null)) {
461 <            _unsafe.putOrderedInt(this, spOffset, s - 1);
462 <            return t;
458 >        while (s != base) {
459 >            if (tryActivate()) {
460 >                ForkJoinTask<?>[] q = queue;
461 >                int mask = q.length - 1;
462 >                int i = (s - 1) & mask;
463 >                ForkJoinTask<?> t = q[i];
464 >                if (t == null || !casSlotNull(q, i, t))
465 >                    break;
466 >                storeSp(s - 1);
467 >                return t;
468 >            }
469          }
470          return null;
471      }
# Line 355 | Line 473 | public class ForkJoinWorkerThread extend
473      /**
474       * Specialized version of popTask to pop only if
475       * topmost element is the given task. Called only
476 <     * by current thread.
476 >     * by current thread while active.
477       * @param t the task. Caller must ensure nonnull
478       */
479      final boolean unpushTask(ForkJoinTask<?> t) {
480          ForkJoinTask<?>[] q = queue;
481          int mask = q.length - 1;
482          int s = sp - 1;
483 <        if (_unsafe.compareAndSwapObject(q, ((s & mask) << qShift) + qBase,
484 <                                         t, null)) {
367 <            _unsafe.putOrderedInt(this, spOffset, s);
483 >        if (casSlotNull(q, s & mask, t)) {
484 >            storeSp(s);
485              return true;
486          }
487          return false;
# Line 402 | Line 519 | public class ForkJoinWorkerThread extend
519                  t = null;
520              setSlot(newQ, b & newMask, t);
521          } while (++b != bf);
522 <        pool.signalIdleWorkers(false);
522 >        pool.signalWork();
523      }
524  
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
525      /**
526 <     * Transition to at least the given state. Return true if not
527 <     * already at least given state.
528 <     */
529 <    private boolean transitionRunStateTo(int state) {
530 <        for (;;) {
531 <            int s = runState;
532 <            if (s >= state)
533 <                return false;
425 <            if (_unsafe.compareAndSwapInt(this, runStateOffset, s, state))
426 <                return true;
427 <        }
428 <    }
429 <
430 <    /**
431 <     * Ensure status is active and if necessary adjust pool active count
432 <     */
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
442 <     */
443 <    final void inactivate() {
444 <        if (active) {
445 <            active = false;
446 <            pool.decrementActiveCount();
447 <        }
448 <    }
449 <
450 <    // Lifecycle methods
451 <
452 <    /**
453 <     * Initializes internal state after construction but before
454 <     * processing any tasks. If you override this method, you must
455 <     * 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.
460 <     */
461 <    protected void onStart() {
462 <        juRandomSeed = randomSeedGenerator.nextLong();
463 <        do;while((randomVictimSeed = nextRandomInt()) == 0); // must be nonzero
464 <        if (queue == null)
465 <            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 <        }
472 <    }
473 <
474 <    /**
475 <     * 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.
478 <     *
479 <     * @param exception the exception causing this thread to abort due
480 <     * to an unrecoverable error, or null if completed normally.
481 <     */
482 <    protected void onTermination(Throwable exception) {
483 <        try {
484 <            clearLocalTasks();
485 <            inactivate();
486 <            cancelTasks();
487 <        } finally {
488 <            terminate(exception);
489 <        }
490 <    }
491 <
492 <    /**
493 <     * Notify pool of termination and, if exception is nonnull,
494 <     * rethrow it to trigger this thread's uncaughtExceptionHandler
495 <     */
496 <    private void terminate(Throwable exception) {
497 <        transitionRunStateTo(TERMINATED);
498 <        try {
499 <            pool.workerTerminated(this);
500 <        } finally {
501 <            if (exception != null)
502 <                ForkJoinTask.rethrowException(exception);
503 <        }
504 <    }
505 <
506 <    /**
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 <    }
518 <
519 <    /**
520 <     * Removes and cancels all tasks in queue.  Can be called from any
521 <     * thread.
522 <     */
523 <    final void cancelTasks() {
524 <        while (base != sp) {
525 <            ForkJoinTask<?> t = deqTask();
526 <            if (t != null)
527 <                t.cancelIgnoringExceptions();
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 <        }
547 <    }
548 <
549 <    /**
550 <     * Main top-level action.
551 <     */
552 <    private void step() {
553 <        ForkJoinTask<?> t = sp != base? popTask() : null;
554 <        if (t != null || (t = scan(null, true)) != null) {
555 <            activate();
556 <            t.quietlyExec();
557 <        }
558 <        else {
559 <            inactivate();
560 <            eventCount = pool.sync(this, eventCount);
561 <        }
562 <    }
563 <
564 <    // scanning for and stealing tasks
565 <
566 <    /**
567 <     * Computes next value for random victim probe. Scans don't
568 <     * 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
572 <     */
573 <    private static int xorShift(int r) {
574 <        r ^= r << 1;
575 <        r ^= r >>> 3;
576 <        r ^= r << 10;
577 <        return r;
578 <    }
579 <
580 <    /**
581 <     * Tries to steal a task from another worker and/or, if enabled,
582 <     * 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.
526 >     * Tries to steal a task from another worker. Starts at a random
527 >     * index of workers array, and probes workers until finding one
528 >     * with non-empty queue or finding that all are empty.  It
529 >     * randomly selects the first n probes. If these are empty, it
530 >     * resorts to a full circular traversal, which is necessary to
531 >     * accurately set active status by caller. Also restarts if pool
532 >     * events occurred since last scan, which forces refresh of
533 >     * workers array, in case barrier was associated with resize.
534       *
535       * This method must be both fast and quiet -- usually avoiding
536       * memory accesses that could disrupt cache sharing etc other than
537       * those needed to check for and take tasks. This accounts for,
538       * among other things, updating random seed in place without
539 <     * 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.)
539 >     * storing it until exit.
540       *
599     * @param joinMe if non null; exit early if done
600     * @param checkSubmissions true if OK to take submissions
541       * @return a task, or null if none found
542       */
543 <    private ForkJoinTask<?> scan(ForkJoinTask<?> joinMe,
544 <                                 boolean checkSubmissions) {
545 <        ForkJoinPool p = pool;
546 <        if (p == null)                    // Never null, but avoids
547 <            return null;                  //   implicit nullchecks below
548 <        int r = randomVictimSeed;         // extract once to keep scan quiet
549 <        restart:                          // outer loop refreshes ws array
610 <        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
543 >    private ForkJoinTask<?> scan() {
544 >        ForkJoinTask<?> t = null;
545 >        int r = seed;                    // extract once to keep scan quiet
546 >        ForkJoinWorkerThread[] ws;       // refreshed on outer loop
547 >        int mask;                        // must be power 2 minus 1 and > 0
548 >        outer:do {
549 >            if ((ws = pool.workers) != null && (mask = ws.length - 1) > 0) {
550                  int idx = r;
551 +                int probes = ~mask;      // use random index while negative
552                  for (;;) {
553 <                    ForkJoinWorkerThread v;
554 <                    // inlined xorshift to update seed
555 <                    r ^= r << 1;  r ^= r >>> 3; r ^= r << 10;
556 <                    if ((v = ws[mask & idx]) != null && v.sp != v.base) {
557 <                        ForkJoinTask<?> t;
558 <                        activate();
559 <                        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
553 >                    r = xorShift(r);     // update random seed
554 >                    ForkJoinWorkerThread v = ws[mask & idx];
555 >                    if (v == null || v.sp == v.base) {
556 >                        if (probes <= mask)
557 >                            idx = (probes++ < 0)? r : (idx + 1);
558 >                        else
559 >                            break;
560                      }
561 <                    if ((probes >> 1) <= mask) // n-1 random then circular
562 <                        idx = (probes++ < 0)? r : (idx + 1);
561 >                    else if (!tryActivate() || (t = v.deqTask()) == null)
562 >                        continue outer;  // restart on contention
563                      else
564 <                        break;
564 >                        break outer;
565                  }
566              }
567 <            if (checkSubmissions && p.hasQueuedSubmissions()) {
568 <                activate();
569 <                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 <            }
648 <        }
649 <        return null;
567 >        } while (pool.hasNewSyncEvent(this)); // retry on pool events
568 >        seed = r;
569 >        return t;
570      }
571  
572      /**
573 <     * Callback from pool.sync to rescan before blocking.  If a
574 <     * task is found, it is pushed so it can be executed upon return.
575 <     * @return true if found and pushed a task
576 <     */
577 <    final boolean prescan() {
578 <        ForkJoinTask<?> t = scan(null, true);
579 <        if (t != null) {
580 <            pushTask(t);
661 <            return true;
662 <        }
663 <        else {
664 <            inactivate();
665 <            return false;
666 <        }
573 >     * Pops or steals a task
574 >     * @return a task, if available
575 >     */
576 >    final ForkJoinTask<?> pollTask() {
577 >        ForkJoinTask<?> t = popTask();
578 >        if (t == null && (t = scan()) != null)
579 >            ++stealCount;
580 >        return t;
581      }
582  
669    // Support for ForkJoinTask methods
670
583      /**
584 <     * Scan, returning early if joinMe done
584 >     * Returns a pool submission, if one exists, activating first.
585 >     * @return a submission, if available
586       */
587 <    final ForkJoinTask<?> scanWhileJoining(ForkJoinTask<?> joinMe) {
588 <        ForkJoinTask<?> t = scan(joinMe, false);
589 <        if (t != null && joinMe.status < 0 && sp == base) {
590 <            pushTask(t); // unsteal if done and this task would be stealable
591 <            t = null;
587 >    private ForkJoinTask<?> pollSubmission() {
588 >        ForkJoinPool p = pool;
589 >        while (p.hasQueuedSubmissions()) {
590 >            ForkJoinTask<?> t;
591 >            if (tryActivate() && (t = p.pollSubmission()) != null)
592 >                return t;
593          }
594 <        return t;
594 >        return null;
595      }
596 <    
596 >
597 >    // Methods accessed only by Pool
598 >
599      /**
600 <     * Pops or steals a task
601 <     * @return task, or null if none available
600 >     * Removes and cancels all tasks in queue.  Can be called from any
601 >     * thread.
602       */
603 <    final ForkJoinTask<?> pollLocalOrStolenTask() {
603 >    final void cancelTasks() {
604          ForkJoinTask<?> t;
605 <        return (t = popTask()) == null? scan(null, false) : t;
605 >        while (base != sp && (t = deqTask()) != null)
606 >            t.cancelIgnoringExceptions();
607      }
608  
609      /**
610 <     * Runs tasks until pool isQuiescent
610 >     * Get and clear steal count for accumulation by pool.  Called
611 >     * only when known to be idle (in pool.sync and termination).
612       */
613 <    final void helpQuiescePool() {
614 <        for (;;) {
615 <            ForkJoinTask<?> t = pollLocalOrStolenTask();
616 <            if (t != null) {
617 <                activate();
618 <                t.quietlyExec();
619 <            }
620 <            else {
621 <                inactivate();
622 <                if (pool.isQuiescent()) {
623 <                    activate(); // re-activate on exit
624 <                    break;
613 >    final int getAndClearStealCount() {
614 >        int sc = stealCount;
615 >        stealCount = 0;
616 >        return sc;
617 >    }
618 >
619 >    /**
620 >     * Returns true if at least one worker in the given array appears
621 >     * to have at least one queued task.
622 >     * @param ws array of workers
623 >     */
624 >    static boolean hasQueuedTasks(ForkJoinWorkerThread[] ws) {
625 >        if (ws != null) {
626 >            int len = ws.length;
627 >            for (int j = 0; j < 2; ++j) { // need two passes for clean sweep
628 >                for (int i = 0; i < len; ++i) {
629 >                    ForkJoinWorkerThread w = ws[i];
630 >                    if (w != null && w.sp != w.base)
631 >                        return true;
632                  }
633              }
634          }
635 +        return false;
636      }
637  
638 +    // Support methods for ForkJoinTask
639 +
640      /**
641       * Returns an estimate of the number of tasks in the queue.
642       */
643      final int getQueueSize() {
644          int n = sp - base;
645 <        return n <= 0? 0 : n; // suppress momentarily negative values
645 >        return n < 0? 0 : n; // suppress momentarily negative values
646      }
647  
648      /**
# Line 726 | Line 654 | public class ForkJoinWorkerThread extend
654          return (sp - base) - (pool.getIdleThreadCount() >>> 1);
655      }
656  
729    // Per-worker exported random numbers
730
731    // Same constants as java.util.Random
732    final static long JURandomMultiplier = 0x5DEECE66DL;
733    final static long JURandomAddend = 0xBL;
734    final static long JURandomMask = (1L << 48) - 1;
735
736    private final int nextJURandom(int bits) {
737        long next = (juRandomSeed * JURandomMultiplier + JURandomAddend) &
738            JURandomMask;
739        juRandomSeed = next;
740        return (int)(next >>> (48 - bits));
741    }
742
743    private final int nextJURandomInt(int n) {
744        if (n <= 0)
745            throw new IllegalArgumentException("n must be positive");
746        int bits = nextJURandom(31);
747        if ((n & -n) == n)
748            return (int)((n * (long)bits) >> 31);
749
750        for (;;) {
751            int val = bits % n;
752            if (bits - val + (n-1) >= 0)
753                return val;
754            bits = nextJURandom(31);
755        }
756    }
757
758    private final long nextJURandomLong() {
759        return ((long)(nextJURandom(32)) << 32) + nextJURandom(32);
760    }
761
762    private final long nextJURandomLong(long n) {
763        if (n <= 0)
764            throw new IllegalArgumentException("n must be positive");
765        long offset = 0;
766        while (n >= Integer.MAX_VALUE) { // randomly pick half range
767            int bits = nextJURandom(2); // 2nd bit for odd vs even split
768            long half = n >>> 1;
769            long nextn = ((bits & 2) == 0)? half : n - half;
770            if ((bits & 1) == 0)
771                offset += n - nextn;
772            n = nextn;
773        }
774        return offset + nextJURandomInt((int)n);
775    }
776
777    private final double nextJURandomDouble() {
778        return (((long)(nextJURandom(26)) << 27) + nextJURandom(27))
779            / (double)(1L << 53);
780    }
781
657      /**
658 <     * Returns a random integer using a per-worker random
784 <     * number generator with the same properties as
785 <     * {@link java.util.Random#nextInt}
786 <     * @return the next pseudorandom, uniformly distributed {@code int}
787 <     *         value from this worker's random number generator's sequence
788 <     */
789 <    public static int nextRandomInt() {
790 <        return ((ForkJoinWorkerThread)(Thread.currentThread())).
791 <            nextJURandom(32);
792 <    }
793 <
794 <    /**
795 <     * Returns a random integer using a per-worker random
796 <     * number generator with the same properties as
797 <     * {@link java.util.Random#nextInt(int)}
798 <     * @param n the bound on the random number to be returned.  Must be
799 <     *        positive.
800 <     * @return the next pseudorandom, uniformly distributed {@code int}
801 <     *         value between {@code 0} (inclusive) and {@code n} (exclusive)
802 <     *         from this worker's random number generator's sequence
803 <     * @throws IllegalArgumentException if n is not positive
804 <     */
805 <    public static int nextRandomInt(int n) {
806 <        return ((ForkJoinWorkerThread)(Thread.currentThread())).
807 <            nextJURandomInt(n);
808 <    }
809 <
810 <    /**
811 <     * Returns a random long using a per-worker random
812 <     * number generator with the same properties as
813 <     * {@link java.util.Random#nextLong}
814 <     * @return the next pseudorandom, uniformly distributed {@code long}
815 <     *         value from this worker's random number generator's sequence
816 <     */
817 <    public static long nextRandomLong() {
818 <        return ((ForkJoinWorkerThread)(Thread.currentThread())).
819 <            nextJURandomLong();
820 <    }
821 <
822 <    /**
823 <     * Returns a random integer using a per-worker random
824 <     * number generator with the same properties as
825 <     * {@link java.util.Random#nextInt(int)}
826 <     * @param n the bound on the random number to be returned.  Must be
827 <     *        positive.
828 <     * @return the next pseudorandom, uniformly distributed {@code int}
829 <     *         value between {@code 0} (inclusive) and {@code n} (exclusive)
830 <     *         from this worker's random number generator's sequence
831 <     * @throws IllegalArgumentException if n is not positive
658 >     * Scan, returning early if joinMe done
659       */
660 <    public static long nextRandomLong(long n) {
661 <        return ((ForkJoinWorkerThread)(Thread.currentThread())).
662 <            nextJURandomLong(n);
660 >    final ForkJoinTask<?> scanWhileJoining(ForkJoinTask<?> joinMe) {
661 >        ForkJoinTask<?> t = pollTask();
662 >        if (t != null && joinMe.status < 0 && sp == base) {
663 >            pushTask(t); // unsteal if done and this task would be stealable
664 >            t = null;
665 >        }
666 >        return t;
667      }
668 <
668 >    
669      /**
670 <     * Returns a random double using a per-worker random
840 <     * number generator with the same properties as
841 <     * {@link java.util.Random#nextDouble}
842 <     * @return the next pseudorandom, uniformly distributed {@code double}
843 <     *         value between {@code 0.0} and {@code 1.0} from this
844 <     *         worker's random number generator's sequence
670 >     * Runs tasks until pool isQuiescent
671       */
672 <    public static double nextRandomDouble() {
673 <        return ((ForkJoinWorkerThread)(Thread.currentThread())).
674 <            nextJURandomDouble();
672 >    final void helpQuiescePool() {
673 >        for (;;) {
674 >            ForkJoinTask<?> t = pollTask();
675 >            if (t != null)
676 >                t.quietlyExec();
677 >            else if (tryInactivate() && pool.isQuiescent())
678 >                break;
679 >        }
680 >        do;while (!tryActivate()); // re-activate on exit
681      }
682  
683      // Temporary Unsafe mechanics for preliminary release
# Line 853 | Line 685 | public class ForkJoinWorkerThread extend
685      static final Unsafe _unsafe;
686      static final long baseOffset;
687      static final long spOffset;
688 +    static final long runStateOffset;
689      static final long qBase;
690      static final int qShift;
858    static final long runStateOffset;
691      static {
692          try {
693              if (ForkJoinWorkerThread.class.getClassLoader() != null) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines