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.1 by dl, Tue Jan 6 14:30:31 2009 UTC vs.
Revision 1.22 by dl, Wed Jul 29 12:05:55 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 that is internally managed by a ForkJoinPool to execute
15 < * ForkJoinTasks. This class additionally provides public
16 < * <tt>static</tt> methods accessing some basic scheduling and
17 < * execution mechanics for the <em>current</em>
18 < * ForkJoinWorkerThread. These methods may be invoked only from within
19 < * other ForkJoinTask computations. Attempts to invoke in other
20 < * contexts result in exceptions or errors including
23 < * ClassCastException.  These methods enable construction of
24 < * special-purpose task classes, as well as specialized idioms
25 < * occasionally useful in ForkJoinTask processing.
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 > * 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>The form of supported static methods reflects the fact that
23 < * worker threads may access and process tasks obtained in any of
29 < * three ways. In preference order: <em>Local</em> tasks are processed
30 < * in LIFO (newest first) order. <em>Stolen</em> tasks are obtained
31 < * from other threads in FIFO (oldest first) order, only if there are
32 < * no local tasks to run.  <em>Submissions</em> form a FIFO queue
33 < * common to the entire pool, and are started only if no other
34 < * work is available.
35 < *
36 < * <p> This class is subclassable solely for the sake of adding
37 < * functionality -- there are no overridable methods dealing with
38 < * scheduling or execution. However, you can override initialization
39 < * and termination cleanup methods surrounding the main task
40 < * processing loop.  If you do create such a subclass, you will also
41 < * need to supply a custom ForkJoinWorkerThreadFactory to use it in a
42 < * ForkJoinPool.
22 > * @since 1.7
23 > * @author Doug Lea
24   */
25   public class ForkJoinWorkerThread extends Thread {
26      /*
# Line 63 | 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 75 | 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 94 | 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 149 | 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 178 | 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 <
185 <    /**
186 <     * Index of this worker in pool array. Set once by pool before
187 <     * 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 194 | Line 175 | public class ForkJoinWorkerThread extend
175       */
176      private volatile int runState;
177  
197    // Runstate values. Order matters
198    private static final int RUNNING     = 0;
199    private static final int SHUTDOWN    = 1;
200    private static final int TERMINATING = 2;
201    private static final int TERMINATED  = 3;
202
178      /**
179 <     * Activity status. When true, this worker is considered active.
180 <     * Must be false upon construction. It must be true when executing
206 <     * tasks, and BEFORE stealing a task. It must be false before
207 <     * 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 214 | 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 <    //  Access methods used by Pool
219 >    // Public access methods
220  
221      /**
222 <     * Get and clear steal count for accumulation by pool.  Called
223 <     * only when known to be idle (in pool.sync and termination).
222 >     * Returns the pool hosting this thread.
223 >     *
224 >     * @return the pool
225       */
226 <    final int getAndClearStealCount() {
227 <        int sc = stealCount;
250 <        stealCount = 0;
251 <        return sc;
226 >    public ForkJoinPool getPool() {
227 >        return pool;
228      }
229  
230      /**
231 <     * Returns estimate of the number of tasks in the queue, without
232 <     * correcting for transient negative values
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 <    final int getRawQueueSize() {
240 <        return sp - base;
239 >    public int getPoolIndex() {
240 >        return poolIndex;
241      }
242  
243 <    // Intrinsics-based support for queue operations.
244 <    // Currently these three (setSp, setSlot, casSlotNull) are
245 <    // usually manually inlined to improve performance
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 >    void setAsyncMode(boolean async) {
250 >        locallyFifo = async;
251 >    }
252 >
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 <     * Sets sp in store-order.
268 >     * Transitions to at least the given state.
269 >     *
270 >     * @return {@code true} if not already at least at given state
271 >     */
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 >     * Tries to set status to active; fails on contention.
284 >     */
285 >    private boolean tryActivate() {
286 >        if (!active) {
287 >            if (!pool.tryIncrementActiveCount())
288 >                return false;
289 >            active = true;
290 >        }
291 >        return true;
292 >    }
293 >
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 >     * 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 309 | 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() {
315 <        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 327 | 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() {
334        ForkJoinTask<?> t;
335        int i;
336        ForkJoinTask<?>[] q = queue;
337        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 348 | 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)) {
360 <            _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 <    private ForkJoinTask<?> peekTask() {
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 395 | Line 544 | public class ForkJoinWorkerThread extend
544                  t = null;
545              setSlot(newQ, b & newMask, t);
546          } while (++b != bf);
547 <        pool.signalIdleWorkers(false);
399 <    }
400 <
401 <    // Runstate management
402 <
403 <    final boolean isShutdown()    { return runState >= SHUTDOWN;  }
404 <    final boolean isTerminating() { return runState >= TERMINATING;  }
405 <    final boolean isTerminated()  { return runState == TERMINATED; }
406 <    final boolean shutdown()      { return transitionRunStateTo(SHUTDOWN); }
407 <    final boolean shutdownNow()   { return transitionRunStateTo(TERMINATING); }
408 <
409 <    /**
410 <     * Transition to at least the given state. Return true if not
411 <     * already at least given state.
412 <     */
413 <    private boolean transitionRunStateTo(int state) {
414 <        for (;;) {
415 <            int s = runState;
416 <            if (s >= state)
417 <                return false;
418 <            if (_unsafe.compareAndSwapInt(this, runStateOffset, s, state))
419 <                return true;
420 <        }
547 >        pool.signalWork();
548      }
549  
550      /**
551 <     * Ensure status is active and if necessary adjust pool active count
552 <     */
553 <    final void activate() {
554 <        if (!active) {
555 <            active = true;
556 <            pool.incrementActiveCount();
557 <        }
558 <    }
559 <
560 <    /**
561 <     * 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  
443    // Lifecycle methods
444
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.
449 <     * Initialization requires care: Most fields must have legal
450 <     * default values, to ensure that attempted accesses from other
451 <     * threads work correctly even before this thread starts
452 <     * 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];
459 <
460 <        // Heuristically allow one initial thread to warm up; others wait
461 <        if (poolIndex < pool.getParallelism() - 1) {
462 <            eventCount = pool.sync(this, 0);
463 <            activate();
464 <        }
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
469 <     * thread.  If you override this method, you must invoke
470 <     * 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
473 <     * to an unrecoverable error, or null if completed normally.
612 >     * @return a task, if available
613       */
614 <    protected void onTermination(Throwable exception) {
615 <        try {
477 <            clearLocalTasks();
478 <            inactivate();
479 <            cancelTasks();
480 <        } finally {
481 <            terminate(exception);
482 <        }
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)
495 <                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 <    /**
500 <     * Run local tasks on exit from main.
501 <     */
502 <    private void clearLocalTasks() {
503 <        while (base != sp && !pool.isTerminating()) {
504 <            ForkJoinTask<?> t = popTask();
505 <            if (t != null) {
506 <                activate(); // ensure active status
507 <                t.quietlyExec();
508 <            }
509 <        }
510 <    }
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)
520 <                t.cancelIgnoreExceptions();
521 <        }
522 <    }
523 <
524 <    /**
525 <     * This method is required to be public, but should never be
526 <     * called explicitly. It performs the main run loop to execute
527 <     * ForkJoinTasks.
528 <     */
529 <    public void run() {
530 <        Throwable exception = null;
531 <        try {
532 <            onStart();
533 <            while (!isShutdown())
534 <                step();
535 <        } catch (Throwable ex) {
536 <            exception = ex;
537 <        } finally {
538 <            onTermination(exception);
539 <        }
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 <        }
551 <        else {
552 <            inactivate();
553 <            eventCount = pool.sync(this, eventCount);
650 >    final int drainTasksTo(Collection<? super 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  
557    // scanning for and stealing tasks
558
660      /**
661 <     * Computes next value for random victim probe. Scans don't
662 <     * require a very high quality generator, but also not a crummy
562 <     * one. Marsaglia xor-shift is cheap and works well.
563 <     *
564 <     * 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;
570 <        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,
576 <     * and probes workers until finding one with non-empty queue or
577 <     * finding that all are empty.  It randomly selects the first n-1
578 <     * probes. If these are empty, it resorts to full circular
579 <     * traversal, which is necessary to accurately set active status
580 <     * by caller. Also restarts if pool barrier has tripped since last
581 <     * scan, which forces refresh of workers array, in case barrier
582 <     * was associated with resize.
583 <     *
584 <     * This method must be both fast and quiet -- usually avoiding
585 <     * memory accesses that could disrupt cache sharing etc other than
586 <     * those needed to check for and take tasks. This accounts for,
587 <     * among other things, updating random seed in place without
588 <     * storing it until exit. (Note that we only need to store it if
589 <     * we found a task; otherwise it doesn't matter if we start at the
590 <     * same place next time.)
671 >     * Returns {@code true} if at least one worker in the given array
672 >     * appears to have at least one queued task.
673       *
674 <     * @param joinMe if non null; exit early if done
593 <     * @param checkSubmissions true if OK to take submissions
594 <     * @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) {
604 <            int mask;
605 <            ForkJoinWorkerThread[] ws = p.workers;
606 <            if (ws != null && (mask = ws.length - 1) > 0) {
607 <                int probes = -mask;       // use random index while negative
608 <                int idx = r;
609 <                for (;;) {
610 <                    ForkJoinWorkerThread v;
611 <                    // inlined xorshift to update seed
612 <                    r ^= r << 1;  r ^= r >>> 3; r ^= r << 10;
613 <                    if ((v = ws[mask & idx]) != null && v.sp != v.base) {
614 <                        ForkJoinTask<?> t;
615 <                        activate();
616 <                        if ((joinMe == null || joinMe.status >= 0) &&
617 <                            (t = v.deqTask()) != null) {
618 <                            randomVictimSeed = r;
619 <                            ++stealCount;
620 <                            return t;
621 <                        }
622 <                        continue restart; // restart on contention
623 <                    }
624 <                    if ((probes >> 1) <= mask) // n-1 random then circular
625 <                        idx = (probes++ < 0)? r : (idx + 1);
626 <                    else
627 <                        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              }
630            if (checkSubmissions && p.hasQueuedSubmissions()) {
631                activate();
632                ForkJoinTask<?> t = p.pollSubmission();
633                if (t != null)
634                    return t;
635            }
636            else {
637                long ec = eventCount;     // restart on pool event
638                if ((eventCount = p.getEventCount()) == ec)
639                    break;
640            }
641        }
642        return null;
643    }
644
645    /**
646     * Callback from pool.sync to rescan before blocking.  If a
647     * task is found, it is pushed so it can be executed upon return.
648     * @return true if found and pushed a task
649     */
650    final boolean prescan() {
651        ForkJoinTask<?> t = scan(null, true);
652        if (t != null) {
653            pushTask(t);
654            return true;
655        }
656        else {
657            inactivate();
658            return false;
686          }
687 +        return false;
688      }
689  
690 <    /**
663 <     * Implements ForkJoinTask.helpJoin
664 <     */
665 <    final int helpJoinTask(ForkJoinTask<?> joinMe) {
666 <        ForkJoinTask<?> t = null;
667 <        int s;
668 <        while ((s = joinMe.status) >= 0) {
669 <            if (t == null) {
670 <                if ((t = scan(joinMe, false)) == null)  // block if no work
671 <                    return joinMe.awaitDone(this, false);
672 <                // else recheck status before exec
673 <            }
674 <            else {
675 <                t.quietlyExec();
676 <                t = null;
677 <            }
678 <        }
679 <        if (t != null) // unsteal
680 <            pushTask(t);
681 <        return s;
682 <    }
683 <
684 <    // Support for public static and/or ForkJoinTask methods
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;
692 <        return n <= 0? 0 : n; // suppress momentarily negative values
693 <    }
694 <
695 <    /**
696 <     * Runs one popped task, if available
697 <     * @return true if ran a task
698 <     */
699 <    private boolean runLocalTask() {
700 <        ForkJoinTask<?> t = popTask();
701 <        if (t == null)
702 <            return false;
703 <        t.quietlyExec();
704 <        return true;
705 <    }
706 <
707 <    /**
708 <     * Pops or steals a task
709 <     * @return task, or null if none available
710 <     */
711 <    private ForkJoinTask<?> getLocalOrStolenTask() {
712 <        ForkJoinTask<?> t = popTask();
713 <        return t != null? t : scan(null, false);
714 <    }
715 <
716 <    /**
717 <     * Runs a popped or stolen task, if available
718 <     * @return true if ran a task
719 <     */
720 <    private boolean runLocalOrStolenTask() {
721 <        ForkJoinTask<?> t = getLocalOrStolenTask();
722 <        if (t == null)
723 <            return false;
724 <        t.quietlyExec();
725 <        return true;
726 <    }
727 <
728 <    /**
729 <     * Runs tasks until pool isQuiescent
730 <     */
731 <    final void helpQuiescePool() {
732 <        activate();
733 <        for (;;) {
734 <            if (!runLocalOrStolenTask()) {
735 <                inactivate();
736 <                if (pool.isQuiescent()) {
737 <                    activate(); // re-activate on exit
738 <                    break;
739 <                }
740 <            }
741 <        }
696 >        // suppress momentarily negative values
697 >        return Math.max(0, sp - base);
698      }
699  
700      /**
# Line 746 | 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  
752    // Public methods on current thread
753
709      /**
710 <     * Returns the pool hosting the current task execution.
756 <     * @return the pool
710 >     * Scans, returning early if joinMe done.
711       */
712 <    public static ForkJoinPool getPool() {
713 <        return ((ForkJoinWorkerThread)(Thread.currentThread())).pool;
714 <    }
715 <
716 <    /**
717 <     * Returns the index number of the current worker thread in its
718 <     * pool.  The returned value ranges from zero to the maximum
765 <     * number of threads (minus one) that have ever been created in
766 <     * the pool.  This method may be useful for applications that
767 <     * track status or collect results per-worker rather than
768 <     * per-task.
769 <     * @return the index number.
770 <     */
771 <    public static int getPoolIndex() {
772 <        return ((ForkJoinWorkerThread)(Thread.currentThread())).poolIndex;
773 <    }
774 <
775 <    /**
776 <     * Returns an estimate of the number of tasks waiting to be run by
777 <     * the current worker thread. This value may be useful for
778 <     * heuristic decisions about whether to fork other tasks.
779 <     * @return the number of tasks
780 <     */
781 <    public static int getLocalQueueSize() {
782 <        return ((ForkJoinWorkerThread)(Thread.currentThread())).
783 <            getQueueSize();
784 <    }
785 <
786 <    /**
787 <     * Returns, but does not remove or execute, the next task locally
788 <     * queued for execution by the current worker thread. There is no
789 <     * guarantee that this task will be the next one actually returned
790 <     * or executed from other polling or execution methods.
791 <     * @return the next task or null if none
792 <     */
793 <    public static ForkJoinTask<?> peekLocalTask() {
794 <        return ((ForkJoinWorkerThread)(Thread.currentThread())).peekTask();
795 <    }
796 <
797 <    /**
798 <     * Removes and returns, without executing, the next task queued
799 <     * for execution in the current worker thread's local queue.
800 <     * @return the next task to execute, or null if none
801 <     */
802 <    public static ForkJoinTask<?> pollLocalTask() {
803 <        return ((ForkJoinWorkerThread)(Thread.currentThread())).popTask();
804 <    }
805 <
806 <    /**
807 <     * Execute the next task locally queued by the current worker, if
808 <     * one is available.
809 <     * @return true if a task was run; a false return indicates
810 <     * that no task was available.
811 <     */
812 <    public static boolean executeLocalTask() {
813 <        return ((ForkJoinWorkerThread)(Thread.currentThread())).
814 <            runLocalTask();
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 <     * Removes and returns, without executing, the next task queued
819 <     * for execution in the current worker thread's local queue or if
820 <     * none, a task stolen from another worker, if one is available.
821 <     * A null return does not necessarily imply that all tasks are
822 <     * completed, only that there are currently none available.
823 <     * @return the next task to execute, or null if none
722 >     * Runs tasks until {@code pool.isQuiescent()}.
723       */
724 <    public static ForkJoinTask<?> pollTask() {
826 <        return ((ForkJoinWorkerThread)(Thread.currentThread())).
827 <            getLocalOrStolenTask();
828 <    }
829 <
830 <    /**
831 <     * Helps this program complete by processing a local or stolen
832 <     * task, if one is available.  This method may be useful when
833 <     * several tasks are forked, and only one of them must be joined,
834 <     * as in:
835 <     *
836 <     * <pre>
837 <     *   while (!t1.isDone() &amp;&amp; !t2.isDone())
838 <     *     ForkJoinWorkerThread.executeTask();
839 <     * </pre>
840 <     *
841 <     * @return true if a task was run; a false return indicates
842 <     * that no task was available.
843 <     */
844 <    public static boolean executeTask() {
845 <        return ((ForkJoinWorkerThread)(Thread.currentThread())).
846 <            runLocalOrStolenTask();
847 <    }
848 <
849 <    // Per-worker exported random numbers
850 <
851 <    // Same constants as java.util.Random
852 <    final static long JURandomMultiplier = 0x5DEECE66DL;
853 <    final static long JURandomAddend = 0xBL;
854 <    final static long JURandomMask = (1L << 48) - 1;
855 <
856 <    private final int nextJURandom(int bits) {
857 <        long next = (juRandomSeed * JURandomMultiplier + JURandomAddend) &
858 <            JURandomMask;
859 <        juRandomSeed = next;
860 <        return (int)(next >>> (48 - bits));
861 <    }
862 <
863 <    private final int nextJURandomInt(int n) {
864 <        if (n <= 0)
865 <            throw new IllegalArgumentException("n must be positive");
866 <        int bits = nextJURandom(31);
867 <        if ((n & -n) == n)
868 <            return (int)((n * (long)bits) >> 31);
869 <
724 >    final void helpQuiescePool() {
725          for (;;) {
726 <            int val = bits % n;
727 <            if (bits - val + (n-1) >= 0)
728 <                return val;
729 <            bits = nextJURandom(31);
730 <        }
876 <    }
877 <
878 <    private final long nextJURandomLong() {
879 <        return ((long)(nextJURandom(32)) << 32) + nextJURandom(32);
880 <    }
881 <
882 <    private final long nextJURandomLong(long n) {
883 <        if (n <= 0)
884 <            throw new IllegalArgumentException("n must be positive");
885 <        long offset = 0;
886 <        while (n >= Integer.MAX_VALUE) { // randomly pick half range
887 <            int bits = nextJURandom(2); // 2nd bit for odd vs even split
888 <            long half = n >>> 1;
889 <            long nextn = ((bits & 2) == 0)? half : n - half;
890 <            if ((bits & 1) == 0)
891 <                offset += n - nextn;
892 <            n = nextn;
726 >            ForkJoinTask<?> t = pollTask();
727 >            if (t != null)
728 >                t.quietlyExec();
729 >            else if (tryInactivate() && pool.isQuiescent())
730 >                break;
731          }
732 <        return offset + nextJURandomInt((int)n);
895 <    }
896 <
897 <    private final double nextJURandomDouble() {
898 <        return (((long)(nextJURandom(26)) << 27) + nextJURandom(27))
899 <            / (double)(1L << 53);
732 >        do {} while (!tryActivate()); // re-activate on exit
733      }
734  
735 <    /**
903 <     * Returns a random integer using a per-worker random
904 <     * number generator with the same properties as
905 <     * {@link java.util.Random#nextInt}
906 <     * @return the next pseudorandom, uniformly distributed {@code int}
907 <     *         value from this worker's random number generator's sequence
908 <     */
909 <    public static int nextRandomInt() {
910 <        return ((ForkJoinWorkerThread)(Thread.currentThread())).
911 <            nextJURandom(32);
912 <    }
735 >    // Unsafe mechanics
736  
737 <    /**
738 <     * Returns a random integer using a per-worker random
739 <     * number generator with the same properties as
740 <     * {@link java.util.Random#nextInt(int)}
741 <     * @param n the bound on the random number to be returned.  Must be
742 <     *        positive.
743 <     * @return the next pseudorandom, uniformly distributed {@code int}
921 <     *         value between {@code 0} (inclusive) and {@code n} (exclusive)
922 <     *         from this worker's random number generator's sequence
923 <     * @throws IllegalArgumentException if n is not positive
924 <     */
925 <    public static int nextRandomInt(int n) {
926 <        return ((ForkJoinWorkerThread)(Thread.currentThread())).
927 <            nextJURandomInt(n);
928 <    }
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 <    /**
746 <     * Returns a random long using a per-worker random
747 <     * number generator with the same properties as
748 <     * {@link java.util.Random#nextLong}
749 <     * @return the next pseudorandom, uniformly distributed {@code long}
750 <     *         value from this worker's random number generator's sequence
936 <     */
937 <    public static long nextRandomLong() {
938 <        return ((ForkJoinWorkerThread)(Thread.currentThread())).
939 <            nextJURandomLong();
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
951 <     * @throws IllegalArgumentException if n is not positive
952 <     */
953 <    public static long nextRandomLong(long n) {
954 <        return ((ForkJoinWorkerThread)(Thread.currentThread())).
955 <            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
964 <     *         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() {
967 <        return ((ForkJoinWorkerThread)(Thread.currentThread())).
968 <            nextJURandomDouble();
969 <    }
970 <
971 <    // Temporary Unsafe mechanics for preliminary release
972 <
973 <    static final Unsafe _unsafe;
974 <    static final long baseOffset;
975 <    static final long spOffset;
976 <    static final long qBase;
977 <    static final int qShift;
978 <    static final long runStateOffset;
979 <    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              }
986            else
987                _unsafe = Unsafe.getUnsafe();
988            baseOffset = _unsafe.objectFieldOffset
989                (ForkJoinWorkerThread.class.getDeclaredField("base"));
990            spOffset = _unsafe.objectFieldOffset
991                (ForkJoinWorkerThread.class.getDeclaredField("sp"));
992            runStateOffset = _unsafe.objectFieldOffset
993                (ForkJoinWorkerThread.class.getDeclaredField("runState"));
994            qBase = _unsafe.arrayBaseOffset(ForkJoinTask[].class);
995            int s = _unsafe.arrayIndexScale(ForkJoinTask[].class);
996            if ((s & (s-1)) != 0)
997                throw new Error("data type scale not a power of two");
998            qShift = 31 - Integer.numberOfLeadingZeros(s);
999        } catch (Exception e) {
1000            throw new RuntimeException("Could not initialize intrinsics", e);
789          }
790      }
791   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines