17 |
|
* subclassable solely for the sake of adding functionality -- there |
18 |
|
* are no overridable methods dealing with scheduling or |
19 |
|
* execution. However, you can override initialization and termination |
20 |
< |
* cleanup methods surrounding the main task processing loop. If you |
21 |
< |
* do create such a subclass, you will also need to supply a custom |
20 |
> |
* methods surrounding the main task processing loop. If you do |
21 |
> |
* create such a subclass, you will also need to supply a custom |
22 |
|
* ForkJoinWorkerThreadFactory to use it in a ForkJoinPool. |
23 |
< |
* |
23 |
> |
* |
24 |
> |
* @since 1.7 |
25 |
> |
* @author Doug Lea |
26 |
|
*/ |
27 |
|
public class ForkJoinWorkerThread extends Thread { |
28 |
|
/* |
46 |
|
* of tasks. To accomplish this, we shift the CAS arbitrating pop |
47 |
|
* vs deq (steal) from being on the indices ("base" and "sp") to |
48 |
|
* the slots themselves (mainly via method "casSlotNull()"). So, |
49 |
< |
* both a successful pop and deq mainly entail CAS'ing a nonnull |
49 |
> |
* both a successful pop and deq mainly entail CAS'ing a non-null |
50 |
|
* slot to null. Because we rely on CASes of references, we do |
51 |
|
* not need tag bits on base or sp. They are simple ints as used |
52 |
|
* in any circular array-based queue (see for example ArrayDeque). |
58 |
|
* considered individually, is not wait-free. One thief cannot |
59 |
|
* successfully continue until another in-progress one (or, if |
60 |
|
* previously empty, a push) completes. However, in the |
61 |
< |
* aggregate, we ensure at least probablistic non-blockingness. If |
61 |
> |
* aggregate, we ensure at least probabilistic non-blockingness. If |
62 |
|
* an attempted steal fails, a thief always chooses a different |
63 |
|
* random victim target to try next. So, in order for one thief to |
64 |
|
* progress, it suffices for any in-progress deq or new push on |
77 |
|
* push) require store order and CASes (in pop and deq) require |
78 |
|
* (volatile) CAS semantics. Since these combinations aren't |
79 |
|
* supported using ordinary volatiles, the only way to accomplish |
80 |
< |
* these effciently is to use direct Unsafe calls. (Using external |
80 |
> |
* these efficiently is to use direct Unsafe calls. (Using external |
81 |
|
* AtomicIntegers and AtomicReferenceArrays for the indices and |
82 |
|
* array is significantly slower because of memory locality and |
83 |
|
* indirection effects.) Further, performance on most platforms is |
201 |
|
long lastEventCount; |
202 |
|
|
203 |
|
/** |
204 |
+ |
* True if use local fifo, not default lifo, for local polling |
205 |
+ |
*/ |
206 |
+ |
private boolean locallyFifo; |
207 |
+ |
|
208 |
+ |
/** |
209 |
|
* Creates a ForkJoinWorkerThread operating in the given pool. |
210 |
+ |
* |
211 |
|
* @param pool the pool this thread works in |
212 |
|
* @throws NullPointerException if pool is null |
213 |
|
*/ |
218 |
|
// Remaining initialization is deferred to onStart |
219 |
|
} |
220 |
|
|
221 |
< |
// Public access methods |
221 |
> |
// Public access methods |
222 |
|
|
223 |
|
/** |
224 |
< |
* Returns the pool hosting this thread |
224 |
> |
* Returns the pool hosting this thread. |
225 |
> |
* |
226 |
|
* @return the pool |
227 |
|
*/ |
228 |
|
public ForkJoinPool getPool() { |
235 |
|
* threads (minus one) that have ever been created in the pool. |
236 |
|
* This method may be useful for applications that track status or |
237 |
|
* collect results per-worker rather than per-task. |
238 |
< |
* @return the index number. |
238 |
> |
* |
239 |
> |
* @return the index number |
240 |
|
*/ |
241 |
|
public int getPoolIndex() { |
242 |
|
return poolIndex; |
243 |
|
} |
244 |
|
|
245 |
+ |
/** |
246 |
+ |
* Establishes local first-in-first-out scheduling mode for forked |
247 |
+ |
* tasks that are never joined. |
248 |
+ |
* |
249 |
+ |
* @param async if true, use locally FIFO scheduling |
250 |
+ |
*/ |
251 |
+ |
void setAsyncMode(boolean async) { |
252 |
+ |
locallyFifo = async; |
253 |
+ |
} |
254 |
|
|
255 |
|
// Runstate management |
256 |
|
|
267 |
|
final boolean shutdownNow() { return transitionRunStateTo(TERMINATING); } |
268 |
|
|
269 |
|
/** |
270 |
< |
* Transition to at least the given state. Return true if not |
271 |
< |
* already at least given state. |
270 |
> |
* Transitions to at least the given state. Returns true if not |
271 |
> |
* already at least at given state. |
272 |
|
*/ |
273 |
|
private boolean transitionRunStateTo(int state) { |
274 |
|
for (;;) { |
275 |
|
int s = runState; |
276 |
|
if (s >= state) |
277 |
|
return false; |
278 |
< |
if (_unsafe.compareAndSwapInt(this, runStateOffset, s, state)) |
278 |
> |
if (UNSAFE.compareAndSwapInt(this, runStateOffset, s, state)) |
279 |
|
return true; |
280 |
|
} |
281 |
|
} |
282 |
|
|
283 |
|
/** |
284 |
< |
* Try to set status to active; fail on contention |
284 |
> |
* Tries to set status to active; fails on contention. |
285 |
|
*/ |
286 |
|
private boolean tryActivate() { |
287 |
|
if (!active) { |
293 |
|
} |
294 |
|
|
295 |
|
/** |
296 |
< |
* Try to set status to active; fail on contention |
296 |
> |
* Tries to set status to active; fails on contention. |
297 |
|
*/ |
298 |
|
private boolean tryInactivate() { |
299 |
|
if (active) { |
305 |
|
} |
306 |
|
|
307 |
|
/** |
308 |
< |
* Computes next value for random victim probe. Scans don't |
308 |
> |
* Computes next value for random victim probe. Scans don't |
309 |
|
* require a very high quality generator, but also not a crummy |
310 |
< |
* one. Marsaglia xor-shift is cheap and works well. |
310 |
> |
* one. Marsaglia xor-shift is cheap and works well. |
311 |
|
*/ |
312 |
|
private static int xorShift(int r) { |
313 |
|
r ^= r << 1; |
337 |
|
} |
338 |
|
|
339 |
|
/** |
340 |
< |
* Execute tasks until shut down. |
340 |
> |
* Executes tasks until shut down. |
341 |
|
*/ |
342 |
|
private void mainLoop() { |
343 |
|
while (!isShutdown()) { |
344 |
|
ForkJoinTask<?> t = pollTask(); |
345 |
< |
if (t != null || (t = pollSubmission()) != null) |
345 |
> |
if (t != null || (t = pollSubmission()) != null) |
346 |
|
t.quietlyExec(); |
347 |
|
else if (tryInactivate()) |
348 |
|
pool.sync(this); |
369 |
|
} |
370 |
|
|
371 |
|
/** |
372 |
< |
* Perform cleanup associated with termination of this worker |
372 |
> |
* Performs cleanup associated with termination of this worker |
373 |
|
* thread. If you override this method, you must invoke |
374 |
|
* super.onTermination at the end of the overridden method. |
375 |
|
* |
376 |
|
* @param exception the exception causing this thread to abort due |
377 |
< |
* to an unrecoverable error, or null if completed normally. |
377 |
> |
* to an unrecoverable error, or null if completed normally |
378 |
|
*/ |
379 |
|
protected void onTermination(Throwable exception) { |
380 |
|
// Execute remaining local tasks unless aborting or terminating |
391 |
|
// propagate exception to uncaught exception handler |
392 |
|
try { |
393 |
|
do;while (!tryInactivate()); // ensure inactive |
394 |
< |
cancelTasks(); |
394 |
> |
cancelTasks(); |
395 |
|
runState = TERMINATED; |
396 |
|
pool.workerTerminated(this); |
397 |
|
} catch (Throwable ex) { // Shouldn't ever happen |
403 |
|
} |
404 |
|
} |
405 |
|
|
406 |
< |
// Intrinsics-based support for queue operations. |
406 |
> |
// Intrinsics-based support for queue operations. |
407 |
|
|
408 |
|
/** |
409 |
< |
* Add in store-order the given task at given slot of q to |
410 |
< |
* null. Caller must ensure q is nonnull and index is in range. |
409 |
> |
* Adds in store-order the given task at given slot of q to null. |
410 |
> |
* Caller must ensure q is non-null and index is in range. |
411 |
|
*/ |
412 |
|
private static void setSlot(ForkJoinTask<?>[] q, int i, |
413 |
< |
ForkJoinTask<?> t){ |
414 |
< |
_unsafe.putOrderedObject(q, (i << qShift) + qBase, t); |
413 |
> |
ForkJoinTask<?> t) { |
414 |
> |
UNSAFE.putOrderedObject(q, (i << qShift) + qBase, t); |
415 |
|
} |
416 |
|
|
417 |
|
/** |
418 |
< |
* CAS given slot of q to null. Caller must ensure q is nonnull |
418 |
> |
* CAS given slot of q to null. Caller must ensure q is non-null |
419 |
|
* and index is in range. |
420 |
|
*/ |
421 |
|
private static boolean casSlotNull(ForkJoinTask<?>[] q, int i, |
422 |
|
ForkJoinTask<?> t) { |
423 |
< |
return _unsafe.compareAndSwapObject(q, (i << qShift) + qBase, t, null); |
423 |
> |
return UNSAFE.compareAndSwapObject(q, (i << qShift) + qBase, t, null); |
424 |
|
} |
425 |
|
|
426 |
|
/** |
427 |
|
* Sets sp in store-order. |
428 |
|
*/ |
429 |
|
private void storeSp(int s) { |
430 |
< |
_unsafe.putOrderedInt(this, spOffset, s); |
430 |
> |
UNSAFE.putOrderedInt(this, spOffset, s); |
431 |
|
} |
432 |
|
|
433 |
|
// Main queue methods |
434 |
|
|
435 |
|
/** |
436 |
|
* Pushes a task. Called only by current thread. |
437 |
< |
* @param t the task. Caller must ensure nonnull |
437 |
> |
* |
438 |
> |
* @param t the task. Caller must ensure non-null. |
439 |
|
*/ |
440 |
|
final void pushTask(ForkJoinTask<?> t) { |
441 |
|
ForkJoinTask<?>[] q = queue; |
452 |
|
/** |
453 |
|
* Tries to take a task from the base of the queue, failing if |
454 |
|
* either empty or contended. |
455 |
< |
* @return a task, or null if none or contended. |
455 |
> |
* |
456 |
> |
* @return a task, or null if none or contended |
457 |
|
*/ |
458 |
< |
private ForkJoinTask<?> deqTask() { |
458 |
> |
final ForkJoinTask<?> deqTask() { |
459 |
|
ForkJoinTask<?> t; |
460 |
|
ForkJoinTask<?>[] q; |
461 |
|
int i; |
472 |
|
|
473 |
|
/** |
474 |
|
* Returns a popped task, or null if empty. Ensures active status |
475 |
< |
* if nonnull. Called only by current thread. |
475 |
> |
* if non-null. Called only by current thread. |
476 |
|
*/ |
477 |
|
final ForkJoinTask<?> popTask() { |
478 |
|
int s = sp; |
495 |
|
* Specialized version of popTask to pop only if |
496 |
|
* topmost element is the given task. Called only |
497 |
|
* by current thread while active. |
498 |
< |
* @param t the task. Caller must ensure nonnull |
498 |
> |
* |
499 |
> |
* @param t the task. Caller must ensure non-null. |
500 |
|
*/ |
501 |
|
final boolean unpushTask(ForkJoinTask<?> t) { |
502 |
|
ForkJoinTask<?>[] q = queue; |
510 |
|
} |
511 |
|
|
512 |
|
/** |
513 |
< |
* Returns next task to pop. |
513 |
> |
* Returns next task. |
514 |
|
*/ |
515 |
|
final ForkJoinTask<?> peekTask() { |
516 |
|
ForkJoinTask<?>[] q = queue; |
517 |
< |
return q == null? null : q[(sp - 1) & (q.length - 1)]; |
517 |
> |
if (q == null) |
518 |
> |
return null; |
519 |
> |
int mask = q.length - 1; |
520 |
> |
int i = locallyFifo? base : (sp - 1); |
521 |
> |
return q[i & mask]; |
522 |
|
} |
523 |
|
|
524 |
|
/** |
596 |
|
} |
597 |
|
|
598 |
|
/** |
599 |
< |
* Pops or steals a task |
599 |
> |
* Gets and removes a local or stolen task. |
600 |
> |
* |
601 |
|
* @return a task, if available |
602 |
|
*/ |
603 |
|
final ForkJoinTask<?> pollTask() { |
604 |
< |
ForkJoinTask<?> t = popTask(); |
604 |
> |
ForkJoinTask<?> t = locallyFifo? deqTask() : popTask(); |
605 |
|
if (t == null && (t = scan()) != null) |
606 |
|
++stealCount; |
607 |
|
return t; |
608 |
|
} |
609 |
|
|
610 |
|
/** |
611 |
+ |
* Gets a local task. |
612 |
+ |
* |
613 |
+ |
* @return a task, if available |
614 |
+ |
*/ |
615 |
+ |
final ForkJoinTask<?> pollLocalTask() { |
616 |
+ |
return locallyFifo? deqTask() : popTask(); |
617 |
+ |
} |
618 |
+ |
|
619 |
+ |
/** |
620 |
|
* Returns a pool submission, if one exists, activating first. |
621 |
+ |
* |
622 |
|
* @return a submission, if available |
623 |
|
*/ |
624 |
|
private ForkJoinTask<?> pollSubmission() { |
644 |
|
} |
645 |
|
|
646 |
|
/** |
647 |
< |
* Get and clear steal count for accumulation by pool. Called |
647 |
> |
* Drains tasks to given collection c. |
648 |
> |
* |
649 |
> |
* @return the number of tasks drained |
650 |
> |
*/ |
651 |
> |
final int drainTasksTo(Collection<ForkJoinTask<?>> c) { |
652 |
> |
int n = 0; |
653 |
> |
ForkJoinTask<?> t; |
654 |
> |
while (base != sp && (t = deqTask()) != null) { |
655 |
> |
c.add(t); |
656 |
> |
++n; |
657 |
> |
} |
658 |
> |
return n; |
659 |
> |
} |
660 |
> |
|
661 |
> |
/** |
662 |
> |
* Gets and clears steal count for accumulation by pool. Called |
663 |
|
* only when known to be idle (in pool.sync and termination). |
664 |
|
*/ |
665 |
|
final int getAndClearStealCount() { |
707 |
|
} |
708 |
|
|
709 |
|
/** |
710 |
< |
* Scan, returning early if joinMe done |
710 |
> |
* Scans, returning early if joinMe done |
711 |
|
*/ |
712 |
|
final ForkJoinTask<?> scanWhileJoining(ForkJoinTask<?> joinMe) { |
713 |
|
ForkJoinTask<?> t = pollTask(); |
717 |
|
} |
718 |
|
return t; |
719 |
|
} |
720 |
< |
|
720 |
> |
|
721 |
|
/** |
722 |
< |
* Runs tasks until pool isQuiescent |
722 |
> |
* Runs tasks until pool isQuiescent. |
723 |
|
*/ |
724 |
|
final void helpQuiescePool() { |
725 |
|
for (;;) { |
726 |
|
ForkJoinTask<?> t = pollTask(); |
727 |
< |
if (t != null) |
727 |
> |
if (t != null) |
728 |
|
t.quietlyExec(); |
729 |
|
else if (tryInactivate() && pool.isQuiescent()) |
730 |
|
break; |
733 |
|
} |
734 |
|
|
735 |
|
// Temporary Unsafe mechanics for preliminary release |
736 |
+ |
private static Unsafe getUnsafe() throws Throwable { |
737 |
+ |
try { |
738 |
+ |
return Unsafe.getUnsafe(); |
739 |
+ |
} catch (SecurityException se) { |
740 |
+ |
try { |
741 |
+ |
return java.security.AccessController.doPrivileged |
742 |
+ |
(new java.security.PrivilegedExceptionAction<Unsafe>() { |
743 |
+ |
public Unsafe run() throws Exception { |
744 |
+ |
return getUnsafePrivileged(); |
745 |
+ |
}}); |
746 |
+ |
} catch (java.security.PrivilegedActionException e) { |
747 |
+ |
throw e.getCause(); |
748 |
+ |
} |
749 |
+ |
} |
750 |
+ |
} |
751 |
|
|
752 |
< |
static final Unsafe _unsafe; |
752 |
> |
private static Unsafe getUnsafePrivileged() |
753 |
> |
throws NoSuchFieldException, IllegalAccessException { |
754 |
> |
Field f = Unsafe.class.getDeclaredField("theUnsafe"); |
755 |
> |
f.setAccessible(true); |
756 |
> |
return (Unsafe) f.get(null); |
757 |
> |
} |
758 |
> |
|
759 |
> |
private static long fieldOffset(String fieldName) |
760 |
> |
throws NoSuchFieldException { |
761 |
> |
return UNSAFE.objectFieldOffset |
762 |
> |
(ForkJoinWorkerThread.class.getDeclaredField(fieldName)); |
763 |
> |
} |
764 |
> |
|
765 |
> |
static final Unsafe UNSAFE; |
766 |
|
static final long baseOffset; |
767 |
|
static final long spOffset; |
768 |
|
static final long runStateOffset; |
770 |
|
static final int qShift; |
771 |
|
static { |
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); |
777 |
< |
} |
778 |
< |
else |
699 |
< |
_unsafe = Unsafe.getUnsafe(); |
700 |
< |
baseOffset = _unsafe.objectFieldOffset |
701 |
< |
(ForkJoinWorkerThread.class.getDeclaredField("base")); |
702 |
< |
spOffset = _unsafe.objectFieldOffset |
703 |
< |
(ForkJoinWorkerThread.class.getDeclaredField("sp")); |
704 |
< |
runStateOffset = _unsafe.objectFieldOffset |
705 |
< |
(ForkJoinWorkerThread.class.getDeclaredField("runState")); |
706 |
< |
qBase = _unsafe.arrayBaseOffset(ForkJoinTask[].class); |
707 |
< |
int s = _unsafe.arrayIndexScale(ForkJoinTask[].class); |
773 |
> |
UNSAFE = getUnsafe(); |
774 |
> |
baseOffset = fieldOffset("base"); |
775 |
> |
spOffset = fieldOffset("sp"); |
776 |
> |
runStateOffset = fieldOffset("runState"); |
777 |
> |
qBase = UNSAFE.arrayBaseOffset(ForkJoinTask[].class); |
778 |
> |
int s = UNSAFE.arrayIndexScale(ForkJoinTask[].class); |
779 |
|
if ((s & (s-1)) != 0) |
780 |
|
throw new Error("data type scale not a power of two"); |
781 |
|
qShift = 31 - Integer.numberOfLeadingZeros(s); |
782 |
< |
} catch (Exception e) { |
782 |
> |
} catch (Throwable e) { |
783 |
|
throw new RuntimeException("Could not initialize intrinsics", e); |
784 |
|
} |
785 |
|
} |