21 |
|
* create such a subclass, you will also need to supply a custom |
22 |
|
* ForkJoinWorkerThreadFactory to use it in a ForkJoinPool. |
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). |
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 |
|
*/ |
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; |
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) { |
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()) { |
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 |
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 |
|
final ForkJoinTask<?> deqTask() { |
459 |
|
ForkJoinTask<?> t; |
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; |
596 |
|
} |
597 |
|
|
598 |
|
/** |
599 |
< |
* gets and removes a local or stolen a task |
599 |
> |
* Gets and removes a local or stolen task. |
600 |
> |
* |
601 |
|
* @return a task, if available |
602 |
|
*/ |
603 |
|
final ForkJoinTask<?> pollTask() { |
608 |
|
} |
609 |
|
|
610 |
|
/** |
611 |
< |
* gets a local task |
611 |
> |
* Gets a local task. |
612 |
> |
* |
613 |
|
* @return a task, if available |
614 |
|
*/ |
615 |
|
final ForkJoinTask<?> pollLocalTask() { |
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 |
< |
* Drains tasks to given collection c |
647 |
> |
* Drains tasks to given collection c. |
648 |
> |
* |
649 |
|
* @return the number of tasks drained |
650 |
|
*/ |
651 |
|
final int drainTasksTo(Collection<ForkJoinTask<?>> c) { |
659 |
|
} |
660 |
|
|
661 |
|
/** |
662 |
< |
* Get and clear steal count for accumulation by pool. Called |
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(); |
719 |
|
} |
720 |
|
|
721 |
|
/** |
722 |
< |
* Runs tasks until pool isQuiescent |
722 |
> |
* Runs tasks until pool isQuiescent. |
723 |
|
*/ |
724 |
|
final void helpQuiescePool() { |
725 |
|
for (;;) { |
758 |
|
|
759 |
|
private static long fieldOffset(String fieldName) |
760 |
|
throws NoSuchFieldException { |
761 |
< |
return _unsafe.objectFieldOffset |
761 |
> |
return UNSAFE.objectFieldOffset |
762 |
|
(ForkJoinWorkerThread.class.getDeclaredField(fieldName)); |
763 |
|
} |
764 |
|
|
765 |
< |
static final Unsafe _unsafe; |
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 |
< |
_unsafe = getUnsafe(); |
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); |
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); |