5 |
|
*/ |
6 |
|
|
7 |
|
package jsr166y; |
8 |
< |
import java.util.*; |
8 |
> |
|
9 |
|
import java.util.concurrent.*; |
10 |
< |
import java.util.concurrent.atomic.*; |
11 |
< |
import java.util.concurrent.locks.*; |
12 |
< |
import sun.misc.Unsafe; |
13 |
< |
import java.lang.reflect.*; |
10 |
> |
|
11 |
> |
import java.util.Collection; |
12 |
|
|
13 |
|
/** |
14 |
|
* A thread managed by a {@link ForkJoinPool}. This class is |
15 |
|
* subclassable solely for the sake of adding functionality -- there |
16 |
|
* are no overridable methods dealing with scheduling or |
17 |
|
* execution. However, you can override initialization and termination |
18 |
< |
* cleanup methods surrounding the main task processing loop. If you |
19 |
< |
* do create such a subclass, you will also need to supply a custom |
18 |
> |
* methods surrounding the main task processing loop. If you do |
19 |
> |
* create such a subclass, you will also need to supply a custom |
20 |
|
* ForkJoinWorkerThreadFactory to use it in a ForkJoinPool. |
21 |
< |
* |
21 |
> |
* |
22 |
> |
* @since 1.7 |
23 |
> |
* @author Doug Lea |
24 |
|
*/ |
25 |
|
public class ForkJoinWorkerThread extends Thread { |
26 |
|
/* |
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). |
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 |
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 |
137 |
|
private static final int MAXIMUM_QUEUE_CAPACITY = 1 << 28; |
138 |
|
|
139 |
|
/** |
140 |
< |
* The pool this thread works in. Accessed directly by ForkJoinTask |
140 |
> |
* The pool this thread works in. Accessed directly by ForkJoinTask. |
141 |
|
*/ |
142 |
|
final ForkJoinPool pool; |
143 |
|
|
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 |
168 |
> |
* calling pool.sync. |
169 |
|
*/ |
170 |
|
private boolean active; |
171 |
|
|
188 |
|
|
189 |
|
/** |
190 |
|
* Index of this worker in pool array. Set once by pool before |
191 |
< |
* running, and accessed directly by pool during cleanup etc |
191 |
> |
* running, and accessed directly by pool during cleanup etc. |
192 |
|
*/ |
193 |
|
int poolIndex; |
194 |
|
|
199 |
|
long lastEventCount; |
200 |
|
|
201 |
|
/** |
202 |
+ |
* True if use local fifo, not default lifo, for local polling |
203 |
+ |
*/ |
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 |
|
*/ |
216 |
|
// Remaining initialization is deferred to onStart |
217 |
|
} |
218 |
|
|
219 |
< |
// Public access methods |
219 |
> |
// Public access methods |
220 |
|
|
221 |
|
/** |
222 |
< |
* Returns the pool hosting this thread |
222 |
> |
* Returns the pool hosting this thread. |
223 |
> |
* |
224 |
|
* @return the pool |
225 |
|
*/ |
226 |
|
public ForkJoinPool getPool() { |
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 |
< |
* @return the index number. |
236 |
> |
* |
237 |
> |
* @return the index number |
238 |
|
*/ |
239 |
|
public int getPoolIndex() { |
240 |
|
return poolIndex; |
241 |
|
} |
242 |
|
|
243 |
+ |
/** |
244 |
+ |
* Establishes local first-in-first-out scheduling mode for forked |
245 |
+ |
* tasks that are never joined. |
246 |
+ |
* |
247 |
+ |
* @param async if true, use locally FIFO scheduling |
248 |
+ |
*/ |
249 |
+ |
void setAsyncMode(boolean async) { |
250 |
+ |
locallyFifo = async; |
251 |
+ |
} |
252 |
|
|
253 |
|
// Runstate management |
254 |
|
|
265 |
|
final boolean shutdownNow() { return transitionRunStateTo(TERMINATING); } |
266 |
|
|
267 |
|
/** |
268 |
< |
* Transition to at least the given state. Return true if not |
269 |
< |
* already at least given state. |
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)) |
277 |
> |
if (UNSAFE.compareAndSwapInt(this, runStateOffset, s, state)) |
278 |
|
return true; |
279 |
|
} |
280 |
|
} |
281 |
|
|
282 |
|
/** |
283 |
< |
* Try to set status to active; fail on contention |
283 |
> |
* Tries to set status to active; fails on contention. |
284 |
|
*/ |
285 |
|
private boolean tryActivate() { |
286 |
|
if (!active) { |
292 |
|
} |
293 |
|
|
294 |
|
/** |
295 |
< |
* Try to set status to active; fail on contention |
295 |
> |
* Tries to set status to inactive; fails on contention. |
296 |
|
*/ |
297 |
|
private boolean tryInactivate() { |
298 |
|
if (active) { |
304 |
|
} |
305 |
|
|
306 |
|
/** |
307 |
< |
* Computes next value for random victim probe. Scans don't |
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. |
309 |
> |
* one. Marsaglia xor-shift is cheap and works well. |
310 |
|
*/ |
311 |
|
private static int xorShift(int r) { |
312 |
|
r ^= r << 1; |
336 |
|
} |
337 |
|
|
338 |
|
/** |
339 |
< |
* Execute tasks until shut down. |
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) |
344 |
> |
if (t != null || (t = pollSubmission()) != null) |
345 |
|
t.quietlyExec(); |
346 |
|
else if (tryInactivate()) |
347 |
|
pool.sync(this); |
368 |
|
} |
369 |
|
|
370 |
|
/** |
371 |
< |
* Perform cleanup associated with termination of this worker |
371 |
> |
* Performs cleanup associated with termination of this worker |
372 |
|
* thread. If you override this method, you must invoke |
373 |
< |
* super.onTermination at the end of the overridden method. |
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 null if completed normally. |
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 |
382 |
|
ForkJoinTask<?> t = popTask(); |
383 |
|
if (t != null) |
384 |
|
t.quietlyExec(); |
385 |
< |
} catch(Throwable ex) { |
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(); |
392 |
> |
do {} while (!tryInactivate()); // ensure inactive |
393 |
> |
cancelTasks(); |
394 |
|
runState = TERMINATED; |
395 |
|
pool.workerTerminated(this); |
396 |
|
} catch (Throwable ex) { // Shouldn't ever happen |
402 |
|
} |
403 |
|
} |
404 |
|
|
405 |
< |
// Intrinsics-based support for queue operations. |
405 |
> |
// Intrinsics-based support for queue operations. |
406 |
|
|
407 |
|
/** |
408 |
< |
* Add in store-order the given task at given slot of q to |
409 |
< |
* null. Caller must ensure q is nonnull and index is in range. |
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); |
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; |
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() { |
457 |
> |
final ForkJoinTask<?> deqTask() { |
458 |
|
ForkJoinTask<?> t; |
459 |
|
ForkJoinTask<?>[] q; |
460 |
|
int i; |
471 |
|
|
472 |
|
/** |
473 |
|
* Returns a popped task, or null if empty. Ensures active status |
474 |
< |
* if nonnull. Called only by current thread. |
474 |
> |
* if non-null. Called only by current thread. |
475 |
|
*/ |
476 |
|
final ForkJoinTask<?> popTask() { |
477 |
|
int s = sp; |
494 |
|
* Specialized version of popTask to pop only if |
495 |
|
* topmost element is the given task. Called only |
496 |
|
* by current thread while active. |
497 |
< |
* @param t the task. Caller must ensure nonnull |
497 |
> |
* |
498 |
> |
* @param t the task. Caller must ensure non-null. |
499 |
|
*/ |
500 |
|
final boolean unpushTask(ForkJoinTask<?> t) { |
501 |
|
ForkJoinTask<?>[] q = queue; |
509 |
|
} |
510 |
|
|
511 |
|
/** |
512 |
< |
* Returns next task to pop. |
512 |
> |
* Returns next task. |
513 |
|
*/ |
514 |
|
final ForkJoinTask<?> peekTask() { |
515 |
|
ForkJoinTask<?>[] q = queue; |
516 |
< |
return q == null? null : q[(sp - 1) & (q.length - 1)]; |
516 |
> |
if (q == null) |
517 |
> |
return null; |
518 |
> |
int mask = q.length - 1; |
519 |
> |
int i = locallyFifo ? base : (sp - 1); |
520 |
> |
return q[i & mask]; |
521 |
|
} |
522 |
|
|
523 |
|
/** |
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); |
582 |
> |
idx = (probes++ < 0) ? r : (idx + 1); |
583 |
|
else |
584 |
|
break; |
585 |
|
} |
595 |
|
} |
596 |
|
|
597 |
|
/** |
598 |
< |
* Pops or steals a task |
598 |
> |
* Gets and removes a local or stolen task. |
599 |
> |
* |
600 |
|
* @return a task, if available |
601 |
|
*/ |
602 |
|
final ForkJoinTask<?> pollTask() { |
603 |
< |
ForkJoinTask<?> t = popTask(); |
603 |
> |
ForkJoinTask<?> t = locallyFifo ? deqTask() : popTask(); |
604 |
|
if (t == null && (t = scan()) != null) |
605 |
|
++stealCount; |
606 |
|
return t; |
607 |
|
} |
608 |
|
|
609 |
|
/** |
610 |
+ |
* Gets a local task. |
611 |
+ |
* |
612 |
+ |
* @return a task, if available |
613 |
+ |
*/ |
614 |
+ |
final ForkJoinTask<?> pollLocalTask() { |
615 |
+ |
return locallyFifo ? deqTask() : popTask(); |
616 |
+ |
} |
617 |
+ |
|
618 |
+ |
/** |
619 |
|
* Returns a pool submission, if one exists, activating first. |
620 |
+ |
* |
621 |
|
* @return a submission, if available |
622 |
|
*/ |
623 |
|
private ForkJoinTask<?> pollSubmission() { |
643 |
|
} |
644 |
|
|
645 |
|
/** |
646 |
< |
* Get and clear steal count for accumulation by pool. Called |
646 |
> |
* Drains tasks to given collection c. |
647 |
> |
* |
648 |
> |
* @return the number of tasks drained |
649 |
> |
*/ |
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 |
> |
|
660 |
> |
/** |
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 |
|
final int getAndClearStealCount() { |
668 |
|
} |
669 |
|
|
670 |
|
/** |
671 |
< |
* Returns true if at least one worker in the given array appears |
672 |
< |
* to have at least one queued task. |
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 ws array of workers |
675 |
|
*/ |
676 |
|
static boolean hasQueuedTasks(ForkJoinWorkerThread[] ws) { |
693 |
|
* Returns an estimate of the number of tasks in the queue. |
694 |
|
*/ |
695 |
|
final int getQueueSize() { |
696 |
< |
int n = sp - base; |
697 |
< |
return n < 0? 0 : n; // suppress momentarily negative values |
696 |
> |
// suppress momentarily negative values |
697 |
> |
return Math.max(0, sp - base); |
698 |
|
} |
699 |
|
|
700 |
|
/** |
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 {@code 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; |
731 |
|
} |
732 |
< |
do;while (!tryActivate()); // re-activate on exit |
732 |
> |
do {} while (!tryActivate()); // re-activate on exit |
733 |
|
} |
734 |
|
|
735 |
< |
// Temporary Unsafe mechanics for preliminary release |
735 |
> |
// Unsafe mechanics |
736 |
> |
|
737 |
> |
private static final sun.misc.Unsafe UNSAFE = getUnsafe(); |
738 |
> |
private static final long spOffset = |
739 |
> |
objectFieldOffset("sp", ForkJoinWorkerThread.class); |
740 |
> |
private static final long runStateOffset = |
741 |
> |
objectFieldOffset("runState", ForkJoinWorkerThread.class); |
742 |
> |
private static final long qBase; |
743 |
> |
private static final int qShift; |
744 |
|
|
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; |
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 |
+ |
private static long objectFieldOffset(String field, Class<?> klazz) { |
754 |
|
try { |
755 |
< |
if (ForkJoinWorkerThread.class.getClassLoader() != null) { |
756 |
< |
Field f = Unsafe.class.getDeclaredField("theUnsafe"); |
757 |
< |
f.setAccessible(true); |
758 |
< |
_unsafe = (Unsafe)f.get(null); |
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 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 |
> |
private static sun.misc.Unsafe getUnsafe() { |
772 |
> |
try { |
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 |
|
} |
698 |
– |
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); |
708 |
– |
if ((s & (s-1)) != 0) |
709 |
– |
throw new Error("data type scale not a power of two"); |
710 |
– |
qShift = 31 - Integer.numberOfLeadingZeros(s); |
711 |
– |
} catch (Exception e) { |
712 |
– |
throw new RuntimeException("Could not initialize intrinsics", e); |
789 |
|
} |
790 |
|
} |
791 |
|
} |