17 |
|
import java.util.concurrent.Future; |
18 |
|
import java.util.concurrent.RejectedExecutionException; |
19 |
|
import java.util.concurrent.RunnableFuture; |
20 |
– |
import java.util.concurrent.ThreadLocalRandom; |
20 |
|
import java.util.concurrent.TimeUnit; |
21 |
|
|
22 |
|
/** |
37 |
|
* ForkJoinPool}s may also be appropriate for use with event-style |
38 |
|
* tasks that are never joined. |
39 |
|
* |
40 |
< |
* <p>A static {@link #commonPool} is available and appropriate for |
40 |
> |
* <p>A static {@link #commonPool()} is available and appropriate for |
41 |
|
* most applications. The common pool is used by any ForkJoinTask that |
42 |
|
* is not explicitly submitted to a specified pool. Using the common |
43 |
|
* pool normally reduces resource usage (its threads are slowly |
62 |
|
* {@link #toString} returns indications of pool state in a |
63 |
|
* convenient form for informal monitoring. |
64 |
|
* |
65 |
< |
* <p> As is the case with other ExecutorServices, there are three |
65 |
> |
* <p>As is the case with other ExecutorServices, there are three |
66 |
|
* main task execution methods summarized in the following table. |
67 |
|
* These are designed to be used primarily by clients not already |
68 |
|
* engaged in fork/join computations in the current pool. The main |
104 |
|
* an integer greater than zero, {@code threadFactory} -- the class |
105 |
|
* name of a {@link ForkJoinWorkerThreadFactory}, and {@code |
106 |
|
* exceptionHandler} -- the class name of a {@link |
107 |
+ |
* java.lang.Thread.UncaughtExceptionHandler |
108 |
|
* Thread.UncaughtExceptionHandler}. Upon any error in establishing |
109 |
|
* these settings, default parameters are used. |
110 |
|
* |
239 |
|
* enable shutdown. When used as a lock, it is normally only very |
240 |
|
* briefly held, so is nearly always available after at most a |
241 |
|
* brief spin, but we use a monitor-based backup strategy to |
242 |
< |
* blocking when needed. |
242 |
> |
* block when needed. |
243 |
|
* |
244 |
|
* Recording WorkQueues. WorkQueues are recorded in the |
245 |
|
* "workQueues" array that is created upon first use and expanded |
248 |
|
* by a lock but the array is otherwise concurrently readable, and |
249 |
|
* accessed directly. To simplify index-based operations, the |
250 |
|
* array size is always a power of two, and all readers must |
251 |
< |
* tolerate null slots. Worker queues are at odd indices Shared |
251 |
> |
* tolerate null slots. Worker queues are at odd indices. Shared |
252 |
|
* (submission) queues are at even indices, up to a maximum of 64 |
253 |
|
* slots, to limit growth even if array needs to expand to add |
254 |
|
* more workers. Grouping them together in this way simplifies and |
316 |
|
* execute. However, many other threads may notice the same task |
317 |
|
* and each signal to wake up a thread that might take it. So in |
318 |
|
* general, pools will be over-signalled. When a submission is |
319 |
< |
* added or another worker adds a task to a queue that is |
320 |
< |
* apparently empty, they signal waiting workers (or trigger |
319 |
> |
* added or another worker adds a task to a queue that has fewer |
320 |
> |
* than two tasks, they signal waiting workers (or trigger |
321 |
|
* creation of new ones if fewer than the given parallelism level |
322 |
< |
* -- see signalWork). These primary signals are buttressed by |
323 |
< |
* signals whenever other threads scan for work or do not have a |
324 |
< |
* task to process. On most platforms, signalling (unpark) |
325 |
< |
* overhead time is noticeably long, and the time between |
326 |
< |
* signalling a thread and it actually making progress can be very |
327 |
< |
* noticeably long, so it is worth offloading these delays from |
328 |
< |
* critical paths as much as possible. |
322 |
> |
* -- signalWork), and may leave a hint to the unparked worker to |
323 |
> |
* help signal others upon wakeup). These primary signals are |
324 |
> |
* buttressed by others (see method helpSignal) whenever other |
325 |
> |
* threads scan for work or do not have a task to process. On |
326 |
> |
* most platforms, signalling (unpark) overhead time is noticeably |
327 |
> |
* long, and the time between signalling a thread and it actually |
328 |
> |
* making progress can be very noticeably long, so it is worth |
329 |
> |
* offloading these delays from critical paths as much as |
330 |
> |
* possible. |
331 |
|
* |
332 |
|
* Trimming workers. To release resources after periods of lack of |
333 |
|
* use, a worker starting to wait when the pool is quiescent will |
395 |
|
* steals, rather than use per-task bookkeeping. This sometimes |
396 |
|
* requires a linear scan of workQueues array to locate stealers, |
397 |
|
* but often doesn't because stealers leave hints (that may become |
398 |
< |
* stale/wrong) of where to locate them. A stealHint is only a |
399 |
< |
* hint because a worker might have had multiple steals and the |
400 |
< |
* hint records only one of them (usually the most current). |
401 |
< |
* Hinting isolates cost to when it is needed, rather than adding |
402 |
< |
* to per-task overhead. (2) It is "shallow", ignoring nesting |
403 |
< |
* and potentially cyclic mutual steals. (3) It is intentionally |
398 |
> |
* stale/wrong) of where to locate them. It is only a hint |
399 |
> |
* because a worker might have had multiple steals and the hint |
400 |
> |
* records only one of them (usually the most current). Hinting |
401 |
> |
* isolates cost to when it is needed, rather than adding to |
402 |
> |
* per-task overhead. (2) It is "shallow", ignoring nesting and |
403 |
> |
* potentially cyclic mutual steals. (3) It is intentionally |
404 |
|
* racy: field currentJoin is updated only while actively joining, |
405 |
|
* which means that we miss links in the chain during long-lived |
406 |
|
* tasks, GC stalls etc (which is OK since blocking in such cases |
527 |
|
* Default ForkJoinWorkerThreadFactory implementation; creates a |
528 |
|
* new ForkJoinWorkerThread. |
529 |
|
*/ |
530 |
< |
static class DefaultForkJoinWorkerThreadFactory |
530 |
> |
static final class DefaultForkJoinWorkerThreadFactory |
531 |
|
implements ForkJoinWorkerThreadFactory { |
532 |
< |
public ForkJoinWorkerThread newThread(ForkJoinPool pool) { |
532 |
> |
public final ForkJoinWorkerThread newThread(ForkJoinPool pool) { |
533 |
|
return new ForkJoinWorkerThread(pool); |
534 |
|
} |
535 |
|
} |
536 |
|
|
537 |
|
/** |
538 |
+ |
* Per-thread records for threads that submit to pools. Currently |
539 |
+ |
* holds only pseudo-random seed / index that is used to choose |
540 |
+ |
* submission queues in method externalPush. In the future, this may |
541 |
+ |
* also incorporate a means to implement different task rejection |
542 |
+ |
* and resubmission policies. |
543 |
+ |
* |
544 |
+ |
* Seeds for submitters and workers/workQueues work in basically |
545 |
+ |
* the same way but are initialized and updated using slightly |
546 |
+ |
* different mechanics. Both are initialized using the same |
547 |
+ |
* approach as in class ThreadLocal, where successive values are |
548 |
+ |
* unlikely to collide with previous values. Seeds are then |
549 |
+ |
* randomly modified upon collisions using xorshifts, which |
550 |
+ |
* requires a non-zero seed. |
551 |
+ |
*/ |
552 |
+ |
static final class Submitter { |
553 |
+ |
int seed; |
554 |
+ |
Submitter(int s) { seed = s; } |
555 |
+ |
} |
556 |
+ |
|
557 |
+ |
/** |
558 |
|
* Class for artificial tasks that are used to replace the target |
559 |
|
* of local joins if they are removed from an interior queue slot |
560 |
|
* in WorkQueue.tryRemoveAndExec. We don't need the proxy to |
620 |
|
* trades off slightly slower average field access for the sake of |
621 |
|
* avoiding really bad worst-case access. (Until better JVM |
622 |
|
* support is in place, this padding is dependent on transient |
623 |
< |
* properties of JVM field layout rules.) We also take care in |
623 |
> |
* properties of JVM field layout rules.) We also take care in |
624 |
|
* allocating, sizing and resizing the array. Non-shared queue |
625 |
|
* arrays are initialized by workers before use. Others are |
626 |
|
* allocated on first use. |
646 |
|
*/ |
647 |
|
static final int MAXIMUM_QUEUE_CAPACITY = 1 << 26; // 64M |
648 |
|
|
649 |
+ |
// Heuristic padding to ameliorate unfortunate memory placements |
650 |
+ |
volatile long pad00, pad01, pad02, pad03, pad04, pad05, pad06; |
651 |
+ |
|
652 |
|
int seed; // for random scanning; initialize nonzero |
653 |
|
volatile int eventCount; // encoded inactivation count; < 0 if inactive |
654 |
|
int nextWait; // encoded record of next event waiter |
655 |
< |
final int mode; // lifo, fifo, or shared |
631 |
< |
int nsteals; // cumulative number of steals |
655 |
> |
int hint; // steal or signal hint (index) |
656 |
|
int poolIndex; // index of this queue in pool (or 0) |
657 |
< |
int stealHint; // index of most recent known stealer |
657 |
> |
final int mode; // 0: lifo, > 0: fifo, < 0: shared |
658 |
> |
int nsteals; // number of steals |
659 |
|
volatile int qlock; // 1: locked, -1: terminate; else 0 |
660 |
|
volatile int base; // index of next slot for poll |
661 |
|
int top; // index of next slot for push |
665 |
|
volatile Thread parker; // == owner during call to park; else null |
666 |
|
volatile ForkJoinTask<?> currentJoin; // task being joined in awaitJoin |
667 |
|
ForkJoinTask<?> currentSteal; // current non-local task being executed |
643 |
– |
// Heuristic padding to ameliorate unfortunate memory placements |
644 |
– |
Object p00, p01, p02, p03, p04, p05, p06, p07; |
645 |
– |
Object p08, p09, p0a, p0b, p0c, p0d, p0e; |
668 |
|
|
669 |
< |
WorkQueue(ForkJoinPool pool, ForkJoinWorkerThread owner, int mode) { |
670 |
< |
this.mode = mode; |
669 |
> |
volatile Object pad10, pad11, pad12, pad13, pad14, pad15, pad16, pad17; |
670 |
> |
volatile Object pad18, pad19, pad1a, pad1b, pad1c, pad1d; |
671 |
> |
|
672 |
> |
WorkQueue(ForkJoinPool pool, ForkJoinWorkerThread owner, int mode, |
673 |
> |
int seed) { |
674 |
|
this.pool = pool; |
675 |
|
this.owner = owner; |
676 |
+ |
this.mode = mode; |
677 |
+ |
this.seed = seed; |
678 |
|
// Place indices in the center of array (that is not yet allocated) |
679 |
|
base = top = INITIAL_QUEUE_CAPACITY >>> 1; |
680 |
|
} |
681 |
|
|
682 |
|
/** |
683 |
< |
* Pushes a task. Call only by owner in unshared queues. |
657 |
< |
* Cases needing resizing or rejection are relayed to fullPush |
658 |
< |
* (that also handles shared queues). |
659 |
< |
* |
660 |
< |
* @param task the task. Caller must ensure non-null. |
661 |
< |
* @throw RejectedExecutionException if array cannot be resized |
683 |
> |
* Returns the approximate number of tasks in the queue. |
684 |
|
*/ |
685 |
< |
final void push(ForkJoinTask<?> task) { |
686 |
< |
ForkJoinPool p; ForkJoinTask<?>[] a; |
687 |
< |
int s = top, n; |
688 |
< |
if ((a = array) != null && a.length > (n = s + 1 - base)) { |
689 |
< |
U.putOrderedObject |
690 |
< |
(a, (((a.length - 1) & s) << ASHIFT) + ABASE, task); |
691 |
< |
top = s + 1; |
692 |
< |
if (n <= 1 && (p = pool) != null) |
693 |
< |
p.signalWork(this, 1); |
694 |
< |
} |
695 |
< |
else |
696 |
< |
fullPush(task, true); |
685 |
> |
final int queueSize() { |
686 |
> |
int n = base - top; // non-owner callers must read base first |
687 |
> |
return (n >= 0) ? 0 : -n; // ignore transient negative |
688 |
> |
} |
689 |
> |
|
690 |
> |
/** |
691 |
> |
* Provides a more accurate estimate of whether this queue has |
692 |
> |
* any tasks than does queueSize, by checking whether a |
693 |
> |
* near-empty queue has at least one unclaimed task. |
694 |
> |
*/ |
695 |
> |
final boolean isEmpty() { |
696 |
> |
ForkJoinTask<?>[] a; int m, s; |
697 |
> |
int n = base - (s = top); |
698 |
> |
return (n >= 0 || |
699 |
> |
(n == -1 && |
700 |
> |
((a = array) == null || |
701 |
> |
(m = a.length - 1) < 0 || |
702 |
> |
U.getObject |
703 |
> |
(a, (long)((m & (s - 1)) << ASHIFT) + ABASE) == null))); |
704 |
|
} |
705 |
|
|
706 |
|
/** |
707 |
< |
* Pushes a task if lock is free and array is either big |
708 |
< |
* enough or can be resized to be big enough. Note: a |
680 |
< |
* specialization of a common fast path of this method is in |
681 |
< |
* ForkJoinPool.externalPush. When called from a FJWT queue, |
682 |
< |
* this can fail only if the pool has been shut down or |
683 |
< |
* an out of memory error. |
707 |
> |
* Pushes a task. Call only by owner in unshared queues. (The |
708 |
> |
* shared-queue version is embedded in method externalPush.) |
709 |
|
* |
710 |
|
* @param task the task. Caller must ensure non-null. |
711 |
< |
* @param owned if true, throw RJE on failure |
711 |
> |
* @throw RejectedExecutionException if array cannot be resized |
712 |
|
*/ |
713 |
< |
final boolean fullPush(ForkJoinTask<?> task, boolean owned) { |
714 |
< |
ForkJoinPool p; ForkJoinTask<?>[] a; |
715 |
< |
if (owned) { |
716 |
< |
if (qlock < 0) // must be shutting down |
717 |
< |
throw new RejectedExecutionException(); |
718 |
< |
} |
719 |
< |
else if (!U.compareAndSwapInt(this, QLOCK, 0, 1)) |
720 |
< |
return false; |
721 |
< |
try { |
697 |
< |
int s = top, oldLen, len; |
698 |
< |
if ((a = array) == null) |
699 |
< |
a = array = new ForkJoinTask<?>[len=INITIAL_QUEUE_CAPACITY]; |
700 |
< |
else if ((oldLen = a.length) > s + 1 - base) |
701 |
< |
len = oldLen; |
702 |
< |
else if ((len = oldLen << 1) > MAXIMUM_QUEUE_CAPACITY) |
703 |
< |
throw new RejectedExecutionException("Capacity exceeded"); |
704 |
< |
else { |
705 |
< |
int oldMask, b; |
706 |
< |
ForkJoinTask<?>[] oldA = a; |
707 |
< |
a = array = new ForkJoinTask<?>[len]; |
708 |
< |
if ((oldMask = oldLen - 1) >= 0 && s - (b = base) > 0) { |
709 |
< |
int mask = len - 1; |
710 |
< |
do { |
711 |
< |
ForkJoinTask<?> x; |
712 |
< |
int oldj = ((b & oldMask) << ASHIFT) + ABASE; |
713 |
< |
int j = ((b & mask) << ASHIFT) + ABASE; |
714 |
< |
x = (ForkJoinTask<?>) |
715 |
< |
U.getObjectVolatile(oldA, oldj); |
716 |
< |
if (x != null && |
717 |
< |
U.compareAndSwapObject(oldA, oldj, x, null)) |
718 |
< |
U.putObjectVolatile(a, j, x); |
719 |
< |
} while (++b != s); |
720 |
< |
} |
713 |
> |
final void push(ForkJoinTask<?> task) { |
714 |
> |
ForkJoinTask<?>[] a; ForkJoinPool p; |
715 |
> |
int s = top, m, n; |
716 |
> |
if ((a = array) != null) { // ignore if queue removed |
717 |
> |
int j = (((m = a.length - 1) & s) << ASHIFT) + ABASE; |
718 |
> |
U.putOrderedObject(a, j, task); |
719 |
> |
if ((n = (top = s + 1) - base) <= 2) { |
720 |
> |
if ((p = pool) != null) |
721 |
> |
p.signalWork(this); |
722 |
|
} |
723 |
< |
U.putOrderedObject |
724 |
< |
(a, (((len - 1) & s) << ASHIFT) + ABASE, task); |
724 |
< |
top = s + 1; |
725 |
< |
} finally { |
726 |
< |
if (!owned) |
727 |
< |
qlock = 0; |
723 |
> |
else if (n >= m) |
724 |
> |
growArray(); |
725 |
|
} |
726 |
< |
if ((p = pool) != null) |
727 |
< |
p.signalWork(this, 1); |
728 |
< |
return true; |
726 |
> |
} |
727 |
> |
|
728 |
> |
/** |
729 |
> |
* Initializes or doubles the capacity of array. Call either |
730 |
> |
* by owner or with lock held -- it is OK for base, but not |
731 |
> |
* top, to move while resizings are in progress. |
732 |
> |
*/ |
733 |
> |
final ForkJoinTask<?>[] growArray() { |
734 |
> |
ForkJoinTask<?>[] oldA = array; |
735 |
> |
int size = oldA != null ? oldA.length << 1 : INITIAL_QUEUE_CAPACITY; |
736 |
> |
if (size > MAXIMUM_QUEUE_CAPACITY) |
737 |
> |
throw new RejectedExecutionException("Queue capacity exceeded"); |
738 |
> |
int oldMask, t, b; |
739 |
> |
ForkJoinTask<?>[] a = array = new ForkJoinTask<?>[size]; |
740 |
> |
if (oldA != null && (oldMask = oldA.length - 1) >= 0 && |
741 |
> |
(t = top) - (b = base) > 0) { |
742 |
> |
int mask = size - 1; |
743 |
> |
do { |
744 |
> |
ForkJoinTask<?> x; |
745 |
> |
int oldj = ((b & oldMask) << ASHIFT) + ABASE; |
746 |
> |
int j = ((b & mask) << ASHIFT) + ABASE; |
747 |
> |
x = (ForkJoinTask<?>)U.getObjectVolatile(oldA, oldj); |
748 |
> |
if (x != null && |
749 |
> |
U.compareAndSwapObject(oldA, oldj, x, null)) |
750 |
> |
U.putObjectVolatile(a, j, x); |
751 |
> |
} while (++b != t); |
752 |
> |
} |
753 |
> |
return a; |
754 |
|
} |
755 |
|
|
756 |
|
/** |
874 |
|
return seed = r ^= r << 5; |
875 |
|
} |
876 |
|
|
855 |
– |
/** |
856 |
– |
* Provides a more accurate estimate of size than (top - base) |
857 |
– |
* by ordering reads and checking whether a near-empty queue |
858 |
– |
* has at least one unclaimed task. |
859 |
– |
*/ |
860 |
– |
final int queueSize() { |
861 |
– |
ForkJoinTask<?>[] a; int k, s, n; |
862 |
– |
return ((n = base - (s = top)) < 0 && |
863 |
– |
(n != -1 || |
864 |
– |
((a = array) != null && (k = a.length) > 0 && |
865 |
– |
U.getObject |
866 |
– |
(a, (long)((((k - 1) & (s - 1)) << ASHIFT) + ABASE)) != null))) ? |
867 |
– |
-n : 0; |
868 |
– |
} |
869 |
– |
|
877 |
|
// Specialized execution methods |
878 |
|
|
879 |
|
/** |
987 |
|
if (t != null) { |
988 |
|
(currentSteal = t).doExec(); |
989 |
|
currentSteal = null; |
990 |
< |
if (++nsteals < 0) { // spill on overflow |
984 |
< |
ForkJoinPool p; |
985 |
< |
if ((p = pool) != null) |
986 |
< |
p.collectStealCount(this); |
987 |
< |
} |
988 |
< |
if (top != base) { // process remaining local tasks |
990 |
> |
if (base - top < 0) { // process remaining local tasks |
991 |
|
if (mode == 0) |
992 |
|
popAndExecAll(); |
993 |
|
else |
994 |
|
pollAndExecAll(); |
995 |
|
} |
996 |
+ |
++nsteals; |
997 |
+ |
hint = -1; |
998 |
|
} |
999 |
|
} |
1000 |
|
|
1061 |
|
} |
1062 |
|
} |
1063 |
|
|
1060 |
– |
/** |
1061 |
– |
* Per-thread records for threads that submit to pools. Currently |
1062 |
– |
* holds only pseudo-random seed / index that is used to choose |
1063 |
– |
* submission queues in method externalPush. In the future, this may |
1064 |
– |
* also incorporate a means to implement different task rejection |
1065 |
– |
* and resubmission policies. |
1066 |
– |
* |
1067 |
– |
* Seeds for submitters and workers/workQueues work in basically |
1068 |
– |
* the same way but are initialized and updated using slightly |
1069 |
– |
* different mechanics. Both are initialized using the same |
1070 |
– |
* approach as in class ThreadLocal, where successive values are |
1071 |
– |
* unlikely to collide with previous values. Seeds are then |
1072 |
– |
* randomly modified upon collisions using xorshifts, which |
1073 |
– |
* requires a non-zero seed. |
1074 |
– |
*/ |
1075 |
– |
static final class Submitter { |
1076 |
– |
int seed; |
1077 |
– |
Submitter(int s) { seed = s; } |
1078 |
– |
} |
1079 |
– |
|
1080 |
– |
/** Property prefix for constructing common pool */ |
1081 |
– |
private static final String propPrefix = |
1082 |
– |
"java.util.concurrent.ForkJoinPool.common."; |
1083 |
– |
|
1064 |
|
// static fields (initialized in static initializer below) |
1065 |
|
|
1066 |
|
/** |
1071 |
|
defaultForkJoinWorkerThreadFactory; |
1072 |
|
|
1073 |
|
/** |
1074 |
< |
* Common (static) pool. Non-null for public use unless a static |
1075 |
< |
* construction exception, but internal usages null-check on use |
1076 |
< |
* to paranoically avoid potential initialization circularities |
1077 |
< |
* as well as to simplify generated code. |
1074 |
> |
* Per-thread submission bookkeeping. Shared across all pools |
1075 |
> |
* to reduce ThreadLocal pollution and because random motion |
1076 |
> |
* to avoid contention in one pool is likely to hold for others. |
1077 |
> |
* Lazily initialized on first submission (but null-checked |
1078 |
> |
* in other contexts to avoid unnecessary initialization). |
1079 |
|
*/ |
1080 |
< |
static final ForkJoinPool commonPool; |
1080 |
> |
static final ThreadLocal<Submitter> submitters; |
1081 |
|
|
1082 |
|
/** |
1083 |
|
* Permission required for callers of methods that may start or |
1086 |
|
private static final RuntimePermission modifyThreadPermission; |
1087 |
|
|
1088 |
|
/** |
1089 |
< |
* Per-thread submission bookkeeping. Shared across all pools |
1090 |
< |
* to reduce ThreadLocal pollution and because random motion |
1091 |
< |
* to avoid contention in one pool is likely to hold for others. |
1092 |
< |
* Lazily initialized on first submission (but null-checked |
1112 |
< |
* in other contexts to avoid unnecessary initialization). |
1089 |
> |
* Common (static) pool. Non-null for public use unless a static |
1090 |
> |
* construction exception, but internal usages null-check on use |
1091 |
> |
* to paranoically avoid potential initialization circularities |
1092 |
> |
* as well as to simplify generated code. |
1093 |
|
*/ |
1094 |
< |
static final ThreadLocal<Submitter> submitters; |
1094 |
> |
static final ForkJoinPool commonPool; |
1095 |
|
|
1096 |
|
/** |
1097 |
|
* Common pool parallelism. Must equal commonPool.parallelism. |
1228 |
|
static final int FIFO_QUEUE = 1; |
1229 |
|
static final int SHARED_QUEUE = -1; |
1230 |
|
|
1231 |
+ |
// bounds for #steps in scan loop -- must be power 2 minus 1 |
1232 |
+ |
private static final int MIN_SCAN = 0x1ff; // cover estimation slop |
1233 |
+ |
private static final int MAX_SCAN = 0x1ffff; // 4 * max workers |
1234 |
+ |
|
1235 |
|
// Instance fields |
1236 |
|
|
1237 |
|
/* |
1238 |
< |
* Field layout order in this class tends to matter more than one |
1239 |
< |
* would like. Runtime layout order is only loosely related to |
1238 |
> |
* Field layout of this class tends to matter more than one would |
1239 |
> |
* like. Runtime layout order is only loosely related to |
1240 |
|
* declaration order and may differ across JVMs, but the following |
1241 |
|
* empirically works OK on current JVMs. |
1242 |
|
*/ |
1243 |
+ |
|
1244 |
+ |
// Heuristic padding to ameliorate unfortunate memory placements |
1245 |
+ |
volatile long pad00, pad01, pad02, pad03, pad04, pad05, pad06; |
1246 |
+ |
|
1247 |
|
volatile long stealCount; // collects worker counts |
1248 |
|
volatile long ctl; // main pool control |
1261 |
– |
final int parallelism; // parallelism level |
1262 |
– |
final int localMode; // per-worker scheduling mode |
1263 |
– |
volatile int indexSeed; // worker/submitter index seed |
1249 |
|
volatile int plock; // shutdown status and seqLock |
1250 |
+ |
volatile int indexSeed; // worker/submitter index seed |
1251 |
+ |
final int config; // mode and parallelism level |
1252 |
|
WorkQueue[] workQueues; // main registry |
1253 |
< |
final ForkJoinWorkerThreadFactory factory; // factory for new workers |
1253 |
> |
final ForkJoinWorkerThreadFactory factory; |
1254 |
|
final Thread.UncaughtExceptionHandler ueh; // per-worker UEH |
1255 |
|
final String workerNamePrefix; // to create worker name string |
1256 |
|
|
1257 |
+ |
volatile Object pad10, pad11, pad12, pad13, pad14, pad15, pad16, pad17; |
1258 |
+ |
volatile Object pad18, pad19, pad1a, pad1b; |
1259 |
+ |
|
1260 |
|
/* |
1261 |
|
* Acquires the plock lock to protect worker array and related |
1262 |
|
* updates. This method is called only if an initial CAS on plock |
1273 |
|
if (((ps = plock) & PL_LOCK) == 0 && |
1274 |
|
U.compareAndSwapInt(this, PLOCK, ps, nps = ps + PL_LOCK)) |
1275 |
|
return nps; |
1276 |
< |
else if (r == 0) |
1277 |
< |
r = ThreadLocalRandom.current().nextInt(); // randomize spins |
1276 |
> |
else if (r == 0) { // randomize spins if possible |
1277 |
> |
Thread t = Thread.currentThread(); WorkQueue w; Submitter z; |
1278 |
> |
if ((t instanceof ForkJoinWorkerThread) && |
1279 |
> |
(w = ((ForkJoinWorkerThread)t).workQueue) != null) |
1280 |
> |
r = w.seed; |
1281 |
> |
else if ((z = submitters.get()) != null) |
1282 |
> |
r = z.seed; |
1283 |
> |
else |
1284 |
> |
r = 1; |
1285 |
> |
} |
1286 |
|
else if (spins >= 0) { |
1287 |
|
r ^= r << 1; r ^= r >>> 3; r ^= r << 10; // xorshift |
1288 |
|
if (r >= 0) |
1316 |
|
synchronized (this) { notifyAll(); } |
1317 |
|
} |
1318 |
|
|
1319 |
+ |
/** |
1320 |
+ |
* Performs secondary initialization, called when plock is zero. |
1321 |
+ |
* Creates workQueue array and sets plock to a valid value. The |
1322 |
+ |
* lock body must be exception-free (so no try/finally) so we |
1323 |
+ |
* optimistically allocate new array outside the lock and throw |
1324 |
+ |
* away if (very rarely) not needed. (A similar tactic is used in |
1325 |
+ |
* fullExternalPush.) Because the plock seq value can eventually |
1326 |
+ |
* wrap around zero, this method harmlessly fails to reinitialize |
1327 |
+ |
* if workQueues exists, while still advancing plock. |
1328 |
+ |
* |
1329 |
+ |
* Additonally tries to create the first worker. |
1330 |
+ |
*/ |
1331 |
+ |
private void initWorkers() { |
1332 |
+ |
WorkQueue[] ws, nws; int ps; |
1333 |
+ |
int p = config & SMASK; // find power of two table size |
1334 |
+ |
int n = (p > 1) ? p - 1 : 1; // ensure at least 2 slots |
1335 |
+ |
n |= n >>> 1; n |= n >>> 2; n |= n >>> 4; n |= n >>> 8; n |= n >>> 16; |
1336 |
+ |
n = (n + 1) << 1; |
1337 |
+ |
if ((ws = workQueues) == null || ws.length == 0) |
1338 |
+ |
nws = new WorkQueue[n]; |
1339 |
+ |
else |
1340 |
+ |
nws = null; |
1341 |
+ |
if (((ps = plock) & PL_LOCK) != 0 || |
1342 |
+ |
!U.compareAndSwapInt(this, PLOCK, ps, ps += PL_LOCK)) |
1343 |
+ |
ps = acquirePlock(); |
1344 |
+ |
if (((ws = workQueues) == null || ws.length == 0) && nws != null) |
1345 |
+ |
workQueues = nws; |
1346 |
+ |
int nps = (ps & SHUTDOWN) | ((ps + PL_LOCK) & ~SHUTDOWN); |
1347 |
+ |
if (!U.compareAndSwapInt(this, PLOCK, ps, nps)) |
1348 |
+ |
releasePlock(nps); |
1349 |
+ |
tryAddWorker(); |
1350 |
+ |
} |
1351 |
+ |
|
1352 |
+ |
/** |
1353 |
+ |
* Tries to create and start one worker. Adjusts counts etc on |
1354 |
+ |
* failure. |
1355 |
+ |
*/ |
1356 |
+ |
private void tryAddWorker() { |
1357 |
+ |
long c; int u; |
1358 |
+ |
while ((u = (int)((c = ctl) >>> 32)) < 0 && |
1359 |
+ |
(u & SHORT_SIGN) != 0 && (int)c == 0) { |
1360 |
+ |
long nc = (long)(((u + UTC_UNIT) & UTC_MASK) | |
1361 |
+ |
((u + UAC_UNIT) & UAC_MASK)) << 32; |
1362 |
+ |
if (U.compareAndSwapLong(this, CTL, c, nc)) { |
1363 |
+ |
ForkJoinWorkerThreadFactory fac; |
1364 |
+ |
Throwable ex = null; |
1365 |
+ |
ForkJoinWorkerThread wt = null; |
1366 |
+ |
try { |
1367 |
+ |
if ((fac = factory) != null && |
1368 |
+ |
(wt = fac.newThread(this)) != null) { |
1369 |
+ |
wt.start(); |
1370 |
+ |
break; |
1371 |
+ |
} |
1372 |
+ |
} catch (Throwable e) { |
1373 |
+ |
ex = e; |
1374 |
+ |
} |
1375 |
+ |
deregisterWorker(wt, ex); |
1376 |
+ |
break; |
1377 |
+ |
} |
1378 |
+ |
} |
1379 |
+ |
} |
1380 |
+ |
|
1381 |
|
// Registering and deregistering workers |
1382 |
|
|
1383 |
|
/** |
1384 |
< |
* Callback from ForkJoinWorkerThread constructor to establish its |
1385 |
< |
* poolIndex and record its WorkQueue. To avoid scanning bias due |
1386 |
< |
* to packing entries in front of the workQueues array, we treat |
1387 |
< |
* the array as a simple power-of-two hash table using per-thread |
1388 |
< |
* seed as hash, expanding as needed. |
1389 |
< |
* |
1390 |
< |
* @param w the worker's queue |
1391 |
< |
*/ |
1392 |
< |
final void registerWorker(WorkQueue w) { |
1393 |
< |
int s, ps; // generate a rarely colliding candidate index seed |
1394 |
< |
do {} while (!U.compareAndSwapInt(this, INDEXSEED, |
1395 |
< |
s = indexSeed, s += SEED_INCREMENT) || |
1384 |
> |
* Callback from ForkJoinWorkerThread to establish and record its |
1385 |
> |
* WorkQueue. To avoid scanning bias due to packing entries in |
1386 |
> |
* front of the workQueues array, we treat the array as a simple |
1387 |
> |
* power-of-two hash table using per-thread seed as hash, |
1388 |
> |
* expanding as needed. |
1389 |
> |
* |
1390 |
> |
* @param wt the worker thread |
1391 |
> |
* @return the worker's queue |
1392 |
> |
*/ |
1393 |
> |
final WorkQueue registerWorker(ForkJoinWorkerThread wt) { |
1394 |
> |
Thread.UncaughtExceptionHandler handler; WorkQueue[] ws; int s, ps; |
1395 |
> |
wt.setDaemon(true); |
1396 |
> |
if ((handler = ueh) != null) |
1397 |
> |
wt.setUncaughtExceptionHandler(handler); |
1398 |
> |
do {} while (!U.compareAndSwapInt(this, INDEXSEED, s = indexSeed, |
1399 |
> |
s += SEED_INCREMENT) || |
1400 |
|
s == 0); // skip 0 |
1401 |
+ |
WorkQueue w = new WorkQueue(this, wt, config >>> 16, s); |
1402 |
|
if (((ps = plock) & PL_LOCK) != 0 || |
1403 |
|
!U.compareAndSwapInt(this, PLOCK, ps, ps += PL_LOCK)) |
1404 |
|
ps = acquirePlock(); |
1405 |
|
int nps = (ps & SHUTDOWN) | ((ps + PL_LOCK) & ~SHUTDOWN); |
1406 |
|
try { |
1407 |
< |
WorkQueue[] ws; |
1343 |
< |
if (w != null && (ws = workQueues) != null) { |
1344 |
< |
w.seed = s; |
1407 |
> |
if ((ws = workQueues) != null) { // skip if shutting down |
1408 |
|
int n = ws.length, m = n - 1; |
1409 |
< |
int r = (s << 1) | 1; // use odd-numbered indices |
1410 |
< |
if (ws[r &= m] != null) { // collision |
1411 |
< |
int probes = 0; // step by approx half size |
1409 |
> |
int r = (s << 1) | 1; // use odd-numbered indices |
1410 |
> |
if (ws[r &= m] != null) { // collision |
1411 |
> |
int probes = 0; // step by approx half size |
1412 |
|
int step = (n <= 4) ? 2 : ((n >>> 1) & EVENMASK) + 2; |
1413 |
|
while (ws[r = (r + step) & m] != null) { |
1414 |
|
if (++probes >= n) { |
1418 |
|
} |
1419 |
|
} |
1420 |
|
} |
1421 |
< |
w.eventCount = w.poolIndex = r; // establish before recording |
1421 |
> |
w.eventCount = w.poolIndex = r; // volatile write orders |
1422 |
|
ws[r] = w; |
1423 |
|
} |
1424 |
|
} finally { |
1425 |
|
if (!U.compareAndSwapInt(this, PLOCK, ps, nps)) |
1426 |
|
releasePlock(nps); |
1427 |
|
} |
1428 |
+ |
wt.setName(workerNamePrefix.concat(Integer.toString(w.poolIndex))); |
1429 |
+ |
return w; |
1430 |
|
} |
1431 |
|
|
1432 |
|
/** |
1442 |
|
WorkQueue w = null; |
1443 |
|
if (wt != null && (w = wt.workQueue) != null) { |
1444 |
|
int ps; |
1380 |
– |
collectStealCount(w); |
1445 |
|
w.qlock = -1; // ensure set |
1446 |
+ |
long ns = w.nsteals, sc; // collect steal count |
1447 |
+ |
do {} while (!U.compareAndSwapLong(this, STEALCOUNT, |
1448 |
+ |
sc = stealCount, sc + ns)); |
1449 |
|
if (((ps = plock) & PL_LOCK) != 0 || |
1450 |
|
!U.compareAndSwapInt(this, PLOCK, ps, ps += PL_LOCK)) |
1451 |
|
ps = acquirePlock(); |
1470 |
|
if (!tryTerminate(false, false) && w != null) { |
1471 |
|
w.cancelAll(); // cancel remaining tasks |
1472 |
|
if (w.array != null) // suppress signal if never ran |
1473 |
< |
signalWork(null, 1); // wake up or create replacement |
1473 |
> |
tryAddWorker(); // create replacement |
1474 |
|
if (ex == null) // help clean refs on way out |
1475 |
|
ForkJoinTask.helpExpungeStaleExceptions(); |
1476 |
|
} |
1479 |
|
ForkJoinTask.rethrow(ex); |
1480 |
|
} |
1481 |
|
|
1415 |
– |
/** |
1416 |
– |
* Collect worker steal count into total. Called on termination |
1417 |
– |
* and upon int overflow of local count. (There is a possible race |
1418 |
– |
* in the latter case vs any caller of getStealCount, which can |
1419 |
– |
* make its results less accurate than usual.) |
1420 |
– |
*/ |
1421 |
– |
final void collectStealCount(WorkQueue w) { |
1422 |
– |
if (w != null) { |
1423 |
– |
long sc; |
1424 |
– |
int ns = w.nsteals; |
1425 |
– |
w.nsteals = 0; // handle overflow |
1426 |
– |
long steals = (ns >= 0) ? ns : 1L + (long)(Integer.MAX_VALUE); |
1427 |
– |
do {} while (!U.compareAndSwapLong(this, STEALCOUNT, |
1428 |
– |
sc = stealCount, sc + steals)); |
1429 |
– |
} |
1430 |
– |
} |
1431 |
– |
|
1482 |
|
// Submissions |
1483 |
|
|
1484 |
|
/** |
1495 |
|
(ws = workQueues) != null && (m = (ws.length - 1)) >= 0 && |
1496 |
|
(q = ws[m & z.seed & SQMASK]) != null && |
1497 |
|
U.compareAndSwapInt(q, QLOCK, 0, 1)) { // lock |
1498 |
< |
int s = q.top, n; |
1499 |
< |
if ((a = q.array) != null && a.length > (n = s + 1 - q.base)) { |
1500 |
< |
U.putObject(a, (long)(((a.length - 1) & s) << ASHIFT) + ABASE, |
1501 |
< |
task); |
1498 |
> |
int b = q.base, s = q.top, n, an; |
1499 |
> |
if ((a = q.array) != null && (an = a.length) > (n = s + 1 - b)) { |
1500 |
> |
int j = (((an - 1) & s) << ASHIFT) + ABASE; |
1501 |
> |
U.putOrderedObject(a, j, task); |
1502 |
|
q.top = s + 1; // push on to deque |
1503 |
|
q.qlock = 0; |
1504 |
< |
if (n <= 1) |
1505 |
< |
signalWork(q, 1); |
1504 |
> |
if (n <= 2) |
1505 |
> |
signalWork(q); |
1506 |
|
return; |
1507 |
|
} |
1508 |
|
q.qlock = 0; |
1513 |
|
/** |
1514 |
|
* Full version of externalPush. This method is called, among |
1515 |
|
* other times, upon the first submission of the first task to the |
1516 |
< |
* pool, so must perform secondary initialization: creating |
1517 |
< |
* workQueue array and setting plock to a valid value. It also |
1518 |
< |
* detects first submission by an external thread by looking up |
1519 |
< |
* its ThreadLocal, and creates a new shared queue if the one at |
1520 |
< |
* index if empty or contended. The lock bodies must be |
1521 |
< |
* exception-free (so no try/finally) so we optimistically |
1522 |
< |
* allocate new queues/arrays outside the locks and throw them |
1473 |
< |
* away if (very rarely) not needed. Note that the plock seq value |
1474 |
< |
* can eventually wrap around zero, but if so harmlessly fails to |
1475 |
< |
* reinitialize. |
1516 |
> |
* pool, so must perform secondary initialization (via |
1517 |
> |
* initWorkers). It also detects first submission by an external |
1518 |
> |
* thread by looking up its ThreadLocal, and creates a new shared |
1519 |
> |
* queue if the one at index if empty or contended. The plock lock |
1520 |
> |
* body must be exception-free (so no try/finally) so we |
1521 |
> |
* optimistically allocate new queues outside the lock and throw |
1522 |
> |
* them away if (very rarely) not needed. |
1523 |
|
*/ |
1524 |
|
private void fullExternalPush(ForkJoinTask<?> task) { |
1525 |
< |
for (Submitter z = null;;) { |
1526 |
< |
WorkQueue[] ws; WorkQueue q; int ps, m, r, s; |
1527 |
< |
if ((ps = plock) < 0) |
1525 |
> |
int r = 0; // random index seed |
1526 |
> |
for (Submitter z = submitters.get();;) { |
1527 |
> |
WorkQueue[] ws; WorkQueue q; int ps, m, k; |
1528 |
> |
if (z == null) { |
1529 |
> |
if (U.compareAndSwapInt(this, INDEXSEED, r = indexSeed, |
1530 |
> |
r += SEED_INCREMENT) && r != 0) |
1531 |
> |
submitters.set(z = new Submitter(r)); |
1532 |
> |
} |
1533 |
> |
else if (r == 0) { // move to a different index |
1534 |
> |
r = z.seed; |
1535 |
> |
r ^= r << 13; // same xorshift as WorkQueues |
1536 |
> |
r ^= r >>> 17; |
1537 |
> |
z.seed = r ^ (r << 5); |
1538 |
> |
} |
1539 |
> |
else if ((ps = plock) < 0) |
1540 |
|
throw new RejectedExecutionException(); |
1541 |
< |
else if ((ws = workQueues) == null || (m = ws.length - 1) < 0) { |
1542 |
< |
int n = parallelism - 1; n |= n >>> 1; n |= n >>> 2; |
1543 |
< |
n |= n >>> 4; n |= n >>> 8; n |= n >>> 16; |
1544 |
< |
WorkQueue[] nws = new WorkQueue[(n + 1) << 1]; // power of two |
1545 |
< |
if ((ps & PL_LOCK) != 0 || |
1541 |
> |
else if (ps == 0 || (ws = workQueues) == null || |
1542 |
> |
(m = ws.length - 1) < 0) |
1543 |
> |
initWorkers(); |
1544 |
> |
else if ((q = ws[k = r & m & SQMASK]) != null) { |
1545 |
> |
if (q.qlock == 0 && U.compareAndSwapInt(q, QLOCK, 0, 1)) { |
1546 |
> |
ForkJoinTask<?>[] a = q.array; |
1547 |
> |
int s = q.top; |
1548 |
> |
boolean submitted = false; |
1549 |
> |
try { // locked version of push |
1550 |
> |
if ((a != null && a.length > s + 1 - q.base) || |
1551 |
> |
(a = q.growArray()) != null) { // must presize |
1552 |
> |
int j = (((a.length - 1) & s) << ASHIFT) + ABASE; |
1553 |
> |
U.putOrderedObject(a, j, task); |
1554 |
> |
q.top = s + 1; |
1555 |
> |
submitted = true; |
1556 |
> |
} |
1557 |
> |
} finally { |
1558 |
> |
q.qlock = 0; // unlock |
1559 |
> |
} |
1560 |
> |
if (submitted) { |
1561 |
> |
signalWork(q); |
1562 |
> |
return; |
1563 |
> |
} |
1564 |
> |
} |
1565 |
> |
r = 0; // move on failure |
1566 |
> |
} |
1567 |
> |
else if (((ps = plock) & PL_LOCK) == 0) { // create new queue |
1568 |
> |
q = new WorkQueue(this, null, SHARED_QUEUE, r); |
1569 |
> |
if (((ps = plock) & PL_LOCK) != 0 || |
1570 |
|
!U.compareAndSwapInt(this, PLOCK, ps, ps += PL_LOCK)) |
1571 |
|
ps = acquirePlock(); |
1572 |
< |
if ((ws = workQueues) == null) |
1573 |
< |
workQueues = nws; |
1572 |
> |
if ((ws = workQueues) != null && k < ws.length && ws[k] == null) |
1573 |
> |
ws[k] = q; |
1574 |
|
int nps = (ps & SHUTDOWN) | ((ps + PL_LOCK) & ~SHUTDOWN); |
1575 |
|
if (!U.compareAndSwapInt(this, PLOCK, ps, nps)) |
1576 |
|
releasePlock(nps); |
1577 |
|
} |
1578 |
< |
else if (z == null && (z = submitters.get()) == null) { |
1579 |
< |
if (U.compareAndSwapInt(this, INDEXSEED, |
1497 |
< |
s = indexSeed, s += SEED_INCREMENT) && |
1498 |
< |
s != 0) // skip 0 |
1499 |
< |
submitters.set(z = new Submitter(s)); |
1500 |
< |
} |
1501 |
< |
else { |
1502 |
< |
int k = (r = z.seed) & m & SQMASK; |
1503 |
< |
if ((q = ws[k]) == null && (ps & PL_LOCK) == 0) { |
1504 |
< |
(q = new WorkQueue(this, null, SHARED_QUEUE)).poolIndex = k; |
1505 |
< |
if (((ps = plock) & PL_LOCK) != 0 || |
1506 |
< |
!U.compareAndSwapInt(this, PLOCK, ps, ps += PL_LOCK)) |
1507 |
< |
ps = acquirePlock(); |
1508 |
< |
WorkQueue w = null; |
1509 |
< |
if ((ws = workQueues) != null && k < ws.length && |
1510 |
< |
(w = ws[k]) == null) |
1511 |
< |
ws[k] = q; |
1512 |
< |
else |
1513 |
< |
q = w; |
1514 |
< |
int nps = (ps & SHUTDOWN) | ((ps + PL_LOCK) & ~SHUTDOWN); |
1515 |
< |
if (!U.compareAndSwapInt(this, PLOCK, ps, nps)) |
1516 |
< |
releasePlock(nps); |
1517 |
< |
} |
1518 |
< |
if (q != null && q.qlock == 0 && q.fullPush(task, false)) |
1519 |
< |
return; |
1520 |
< |
r ^= r << 13; // same xorshift as WorkQueues |
1521 |
< |
r ^= r >>> 17; |
1522 |
< |
z.seed = r ^= r << 5; // move to a different index |
1523 |
< |
} |
1578 |
> |
else |
1579 |
> |
r = 0; // try elsewhere while lock held |
1580 |
|
} |
1581 |
|
} |
1582 |
|
|
1591 |
|
} |
1592 |
|
|
1593 |
|
/** |
1594 |
< |
* Tries to create (at most one) or activate (possibly several) |
1539 |
< |
* workers if too few are active. On contention failure, continues |
1540 |
< |
* until at least one worker is signalled or the given queue is |
1541 |
< |
* empty or all workers are active. |
1594 |
> |
* Tries to create or activate a worker if too few are active. |
1595 |
|
* |
1596 |
< |
* @param q if non-null, the queue holding tasks to be signalled |
1544 |
< |
* @param signals the target number of signals. |
1596 |
> |
* @param q the (non-null) queue holding tasks to be signalled |
1597 |
|
*/ |
1598 |
< |
final void signalWork(WorkQueue q, int signals) { |
1599 |
< |
long c; int e, u, i; WorkQueue[] ws; WorkQueue w; Thread p; |
1598 |
> |
final void signalWork(WorkQueue q) { |
1599 |
> |
int hint = q.poolIndex; |
1600 |
> |
long c; int e, u, i, n; WorkQueue[] ws; WorkQueue w; Thread p; |
1601 |
|
while ((u = (int)((c = ctl) >>> 32)) < 0) { |
1602 |
|
if ((e = (int)c) > 0) { |
1603 |
|
if ((ws = workQueues) != null && ws.length > (i = e & SMASK) && |
1605 |
|
long nc = (((long)(w.nextWait & E_MASK)) | |
1606 |
|
((long)(u + UAC_UNIT) << 32)); |
1607 |
|
if (U.compareAndSwapLong(this, CTL, c, nc)) { |
1608 |
+ |
w.hint = hint; |
1609 |
|
w.eventCount = (e + E_SEQ) & E_MASK; |
1610 |
|
if ((p = w.parker) != null) |
1611 |
|
U.unpark(p); |
1612 |
< |
if (--signals <= 0) |
1559 |
< |
break; |
1612 |
> |
break; |
1613 |
|
} |
1614 |
< |
else |
1562 |
< |
signals = 1; |
1563 |
< |
if ((q != null && q.queueSize() == 0)) |
1614 |
> |
if (q.top - q.base <= 0) |
1615 |
|
break; |
1616 |
|
} |
1617 |
|
else |
1618 |
|
break; |
1619 |
|
} |
1620 |
< |
else if (e == 0 && (u & SHORT_SIGN) != 0) { |
1621 |
< |
long nc = (long)(((u + UTC_UNIT) & UTC_MASK) | |
1622 |
< |
((u + UAC_UNIT) & UAC_MASK)) << 32; |
1572 |
< |
if (U.compareAndSwapLong(this, CTL, c, nc)) { |
1573 |
< |
ForkJoinWorkerThread wt = null; |
1574 |
< |
Throwable ex = null; |
1575 |
< |
boolean started = false; |
1576 |
< |
try { |
1577 |
< |
ForkJoinWorkerThreadFactory fac; |
1578 |
< |
if ((fac = factory) != null && |
1579 |
< |
(wt = fac.newThread(this)) != null) { |
1580 |
< |
wt.start(); |
1581 |
< |
started = true; |
1582 |
< |
} |
1583 |
< |
} catch (Throwable rex) { |
1584 |
< |
ex = rex; |
1585 |
< |
} |
1586 |
< |
if (!started) |
1587 |
< |
deregisterWorker(wt, ex); // adjust counts on failure |
1588 |
< |
break; |
1589 |
< |
} |
1590 |
< |
} |
1591 |
< |
else |
1620 |
> |
else { |
1621 |
> |
if ((short)u < 0) |
1622 |
> |
tryAddWorker(); |
1623 |
|
break; |
1624 |
+ |
} |
1625 |
|
} |
1626 |
|
} |
1627 |
|
|
1631 |
|
* Top-level runloop for workers, called by ForkJoinWorkerThread.run. |
1632 |
|
*/ |
1633 |
|
final void runWorker(WorkQueue w) { |
1634 |
< |
// initialize queue array in this thread |
1603 |
< |
w.array = new ForkJoinTask<?>[WorkQueue.INITIAL_QUEUE_CAPACITY]; |
1634 |
> |
w.growArray(); // allocate queue |
1635 |
|
do { w.runTask(scan(w)); } while (w.qlock >= 0); |
1636 |
|
} |
1637 |
|
|
1643 |
|
* contention, or state changes that indicate possible success on |
1644 |
|
* re-invocation. |
1645 |
|
* |
1646 |
< |
* The scan searches for tasks across a random permutation of |
1647 |
< |
* queues (starting at a random index and stepping by a random |
1648 |
< |
* relative prime, checking each at least once). The scan |
1649 |
< |
* terminates upon either finding a non-empty queue, or completing |
1650 |
< |
* the sweep. If the worker is not inactivated, it takes and |
1651 |
< |
* returns a task from this queue. Otherwise, if not activated, it |
1652 |
< |
* signals workers (that may include itself) and returns so caller |
1653 |
< |
* can retry. Also returns for trtry if the worker array may have |
1654 |
< |
* changed during an empty scan. On failure to find a task, we |
1655 |
< |
* take one of the following actions, after which the caller will |
1656 |
< |
* retry calling this method unless terminated. |
1646 |
> |
* The scan searches for tasks across queues (starting at a random |
1647 |
> |
* index, and relying on registerWorker to irregularly scatter |
1648 |
> |
* them within array to avoid bias), checking each at least twice. |
1649 |
> |
* The scan terminates upon either finding a non-empty queue, or |
1650 |
> |
* completing the sweep. If the worker is not inactivated, it |
1651 |
> |
* takes and returns a task from this queue. Otherwise, if not |
1652 |
> |
* activated, it signals workers (that may include itself) and |
1653 |
> |
* returns so caller can retry. Also returns for true if the |
1654 |
> |
* worker array may have changed during an empty scan. On failure |
1655 |
> |
* to find a task, we take one of the following actions, after |
1656 |
> |
* which the caller will retry calling this method unless |
1657 |
> |
* terminated. |
1658 |
|
* |
1659 |
|
* * If pool is terminating, terminate the worker. |
1660 |
|
* |
1671 |
|
* @return a task or null if none found |
1672 |
|
*/ |
1673 |
|
private final ForkJoinTask<?> scan(WorkQueue w) { |
1674 |
< |
WorkQueue[] ws; WorkQueue q; // first update random seed |
1675 |
< |
int r = w.seed; r ^= r << 13; r ^= r >>> 17; w.seed = r ^= r << 5; |
1676 |
< |
int ps = plock, m; // volatile read order matters |
1677 |
< |
if ((ws = workQueues) != null && (m = ws.length - 1) > 0) { |
1678 |
< |
int ec = w.eventCount; // ec is negative if inactive |
1679 |
< |
int step = (r >>> 16) | 1; // relatively prime |
1680 |
< |
for (int j = (m + 1) << 2; ; --j, r += step) { |
1681 |
< |
ForkJoinTask<?> t; ForkJoinTask<?>[] a; int b, n; |
1682 |
< |
if ((q = ws[r & m]) != null && (b = q.base) - q.top < 0 && |
1683 |
< |
(a = q.array) != null) { // probably nonempty |
1674 |
> |
WorkQueue[] ws; int m; |
1675 |
> |
int ps = plock; // read plock before ws |
1676 |
> |
if (w != null && (ws = workQueues) != null && (m = ws.length - 1) >= 0) { |
1677 |
> |
int ec = w.eventCount; // ec is negative if inactive |
1678 |
> |
int r = w.seed; r ^= r << 13; r ^= r >>> 17; w.seed = r ^= r << 5; |
1679 |
> |
int j = ((m + m + 1) | MIN_SCAN) & MAX_SCAN; |
1680 |
> |
do { |
1681 |
> |
WorkQueue q; ForkJoinTask<?>[] a; int b; |
1682 |
> |
if ((q = ws[(r + j) & m]) != null && (b = q.base) - q.top < 0 && |
1683 |
> |
(a = q.array) != null) { // probably nonempty |
1684 |
|
int i = (((a.length - 1) & b) << ASHIFT) + ABASE; |
1685 |
< |
t = (ForkJoinTask<?>)U.getObjectVolatile(a, i); |
1685 |
> |
ForkJoinTask<?> t = (ForkJoinTask<?>) |
1686 |
> |
U.getObjectVolatile(a, i); |
1687 |
|
if (q.base == b && ec >= 0 && t != null && |
1688 |
|
U.compareAndSwapObject(a, i, t, null)) { |
1689 |
< |
if ((n = q.top - (q.base = b + 1)) > 0) |
1690 |
< |
signalWork(q, n); |
1691 |
< |
return t; // taken |
1692 |
< |
} |
1693 |
< |
if (j < m || (ec < 0 && (ec = w.eventCount) < 0)) { |
1694 |
< |
if ((n = q.queueSize() - 1) > 0) |
1695 |
< |
signalWork(q, n); |
1696 |
< |
break; // let caller retry after signal |
1697 |
< |
} |
1698 |
< |
} |
1699 |
< |
else if (j < 0) { // end of scan |
1700 |
< |
long c = ctl; int e; |
1701 |
< |
if (plock != ps) // incomplete sweep |
1702 |
< |
break; |
1703 |
< |
if ((e = (int)c) < 0) // pool is terminating |
1704 |
< |
w.qlock = -1; |
1705 |
< |
else if (ec >= 0) { // try to enqueue/inactivate |
1706 |
< |
long nc = ((long)ec | |
1707 |
< |
((c - AC_UNIT) & (AC_MASK|TC_MASK))); |
1708 |
< |
w.nextWait = e; |
1709 |
< |
w.eventCount = ec | INT_SIGN; // mark as inactive |
1710 |
< |
if (ctl != c || |
1711 |
< |
!U.compareAndSwapLong(this, CTL, c, nc)) |
1712 |
< |
w.eventCount = ec; // unmark on CAS failure |
1713 |
< |
else if ((int)(c >> AC_SHIFT) == 1 - parallelism) |
1714 |
< |
idleAwaitWork(w, nc, c); // quiescent |
1715 |
< |
} |
1716 |
< |
else if (w.seed >= 0 && w.eventCount < 0) { |
1717 |
< |
Thread wt = Thread.currentThread(); |
1718 |
< |
Thread.interrupted(); // clear status |
1719 |
< |
U.putObject(wt, PARKBLOCKER, this); |
1720 |
< |
w.parker = wt; // emulate LockSupport.park |
1721 |
< |
if (w.eventCount < 0) // recheck |
1722 |
< |
U.park(false, 0L); |
1723 |
< |
w.parker = null; |
1724 |
< |
U.putObject(wt, PARKBLOCKER, null); |
1725 |
< |
} |
1726 |
< |
break; |
1727 |
< |
} |
1689 |
> |
if ((q.base = b + 1) - q.top < 0) |
1690 |
> |
signalWork(q); |
1691 |
> |
return t; // taken |
1692 |
> |
} |
1693 |
> |
else if ((ec < 0 || j < m) && (int)(ctl >> AC_SHIFT) <= 0) { |
1694 |
> |
w.hint = (r + j) & m; // help signal below |
1695 |
> |
break; // cannot take |
1696 |
> |
} |
1697 |
> |
} |
1698 |
> |
} while (--j >= 0); |
1699 |
> |
|
1700 |
> |
long c, sc; int e, ns, h; |
1701 |
> |
if ((h = w.hint) < 0) { |
1702 |
> |
if ((ns = w.nsteals) != 0) { |
1703 |
> |
if (U.compareAndSwapLong(this, STEALCOUNT, |
1704 |
> |
sc = stealCount, sc + ns)) |
1705 |
> |
w.nsteals = 0; // collect steals |
1706 |
> |
} |
1707 |
> |
else if (plock != ps) // consistency check |
1708 |
> |
; // skip |
1709 |
> |
else if ((e = (int)(c = ctl)) < 0) |
1710 |
> |
w.qlock = -1; // pool is terminating |
1711 |
> |
else if (ec >= 0) { // try to enqueue/inactivate |
1712 |
> |
long nc = ((long)ec | ((c - AC_UNIT) & (AC_MASK|TC_MASK))); |
1713 |
> |
w.nextWait = e; // link and mark inactive |
1714 |
> |
w.eventCount = ec | INT_SIGN; |
1715 |
> |
if (ctl != c || !U.compareAndSwapLong(this, CTL, c, nc)) |
1716 |
> |
w.eventCount = ec; // unmark on CAS failure |
1717 |
> |
else if ((int)(c >> AC_SHIFT) == 1 - (config & SMASK)) |
1718 |
> |
idleAwaitWork(w, nc, c); |
1719 |
> |
} |
1720 |
> |
else if (w.eventCount < 0) { // block |
1721 |
> |
Thread wt = Thread.currentThread(); |
1722 |
> |
Thread.interrupted(); // clear status |
1723 |
> |
U.putObject(wt, PARKBLOCKER, this); |
1724 |
> |
w.parker = wt; // emulate LockSupport.park |
1725 |
> |
if (w.eventCount < 0) // recheck |
1726 |
> |
U.park(false, 0L); |
1727 |
> |
w.parker = null; |
1728 |
> |
U.putObject(wt, PARKBLOCKER, null); |
1729 |
> |
} |
1730 |
> |
} |
1731 |
> |
if (h >= 0 || (h = w.hint) >= 0) { // signal others before retry |
1732 |
> |
w.hint = -1; // reset |
1733 |
> |
helpSignal(null, h, true); |
1734 |
|
} |
1735 |
|
} |
1736 |
|
return null; |
1749 |
|
* @param prevCtl the ctl value to restore if thread is terminated |
1750 |
|
*/ |
1751 |
|
private void idleAwaitWork(WorkQueue w, long currentCtl, long prevCtl) { |
1752 |
< |
if (w.eventCount < 0 && |
1753 |
< |
(this == commonPool || !tryTerminate(false, false)) && |
1715 |
< |
(int)prevCtl != 0) { |
1752 |
> |
if (w != null && w.eventCount < 0 && |
1753 |
> |
!tryTerminate(false, false) && (int)prevCtl != 0) { |
1754 |
|
int dc = -(short)(currentCtl >>> TC_SHIFT); |
1755 |
|
long parkTime = dc < 0 ? FAST_IDLE_TIMEOUT: (dc + 1) * IDLE_TIMEOUT; |
1756 |
|
long deadline = System.nanoTime() + parkTime - 100000L; // 1ms slop |
1776 |
|
} |
1777 |
|
|
1778 |
|
/** |
1779 |
< |
* Scans through queues looking for work while joining a task; |
1780 |
< |
* if any are present, signals. |
1779 |
> |
* Scans through queues looking for work (optionally, while |
1780 |
> |
* joining a task); if any present, signals. May return early if |
1781 |
> |
* more signalling is detectably unneeded. |
1782 |
|
* |
1783 |
< |
* @param task to return early if done |
1783 |
> |
* @param task if non-null, return early if done |
1784 |
|
* @param origin an index to start scan |
1785 |
+ |
* @param once if only the origin should be checked |
1786 |
|
*/ |
1787 |
< |
final int helpSignal(ForkJoinTask<?> task, int origin) { |
1788 |
< |
WorkQueue[] ws; WorkQueue q; int m, n, s; |
1789 |
< |
if (task != null && (ws = workQueues) != null && |
1790 |
< |
(m = ws.length - 1) >= 0) { |
1791 |
< |
for (int i = 0; i <= m; ++i) { |
1792 |
< |
if ((s = task.status) < 0) |
1793 |
< |
return s; |
1794 |
< |
if ((q = ws[(i + origin) & m]) != null && |
1795 |
< |
(n = q.queueSize()) > 0) { |
1796 |
< |
signalWork(q, n); |
1797 |
< |
if ((int)(ctl >> AC_SHIFT) >= 0) |
1787 |
> |
private void helpSignal(ForkJoinTask<?> task, int origin, boolean once) { |
1788 |
> |
WorkQueue[] ws; WorkQueue w; Thread p; long c; int m, u, e, i, s; |
1789 |
> |
if ((u = (int)(ctl >>> 32)) < 0 && (u >> UAC_SHIFT) < 0 && |
1790 |
> |
(ws = workQueues) != null && (m = ws.length - 1) >= 0) { |
1791 |
> |
outer: for (int k = origin, j = once? 0 : m; j >= 0; --j) { |
1792 |
> |
WorkQueue q = ws[k++ & m]; |
1793 |
> |
for (int n = m;;) { // limit to at most m signals |
1794 |
> |
if (task != null && task.status < 0) |
1795 |
> |
break outer; |
1796 |
> |
if (q == null || |
1797 |
> |
((s = (task == null ? -1 : 0) - q.base + q.top) <= n && |
1798 |
> |
(n = s) <= 0)) |
1799 |
|
break; |
1800 |
+ |
if ((u = (int)((c = ctl) >>> 32)) >= 0 || |
1801 |
+ |
(e = (int)c) <= 0 || m < (i = e & SMASK) || |
1802 |
+ |
(w = ws[i]) == null) |
1803 |
+ |
break outer; |
1804 |
+ |
long nc = (((long)(w.nextWait & E_MASK)) | |
1805 |
+ |
((long)(u + UAC_UNIT) << 32)); |
1806 |
+ |
if (w.eventCount == (e | INT_SIGN) && |
1807 |
+ |
U.compareAndSwapLong(this, CTL, c, nc)) { |
1808 |
+ |
w.eventCount = (e + E_SEQ) & E_MASK; |
1809 |
+ |
if ((p = w.parker) != null) |
1810 |
+ |
U.unpark(p); |
1811 |
+ |
if (--n <= 0) |
1812 |
+ |
break; |
1813 |
+ |
} |
1814 |
|
} |
1815 |
|
} |
1816 |
|
} |
1762 |
– |
return 0; |
1817 |
|
} |
1818 |
|
|
1819 |
|
/** |
1847 |
|
} |
1848 |
|
if ((ws = workQueues) == null || (m = ws.length - 1) <= 0) |
1849 |
|
break restart; // shutting down |
1850 |
< |
if ((v = ws[h = (j.stealHint | 1) & m]) == null || |
1850 |
> |
if ((v = ws[h = (j.hint | 1) & m]) == null || |
1851 |
|
v.currentSteal != subtask) { |
1852 |
|
for (int origin = h;;) { // find stealer |
1853 |
|
if (((h = (h + 2) & m) & 15) == 1 && |
1855 |
|
continue restart; // occasional staleness check |
1856 |
|
if ((v = ws[h]) != null && |
1857 |
|
v.currentSteal == subtask) { |
1858 |
< |
j.stealHint = h; // save hint |
1858 |
> |
j.hint = h; // save hint |
1859 |
|
break; |
1860 |
|
} |
1861 |
|
if (h == origin) |
1904 |
|
|
1905 |
|
/** |
1906 |
|
* Analog of tryHelpStealer for CountedCompleters. Tries to steal |
1907 |
< |
* and run tasks within the target's computation |
1907 |
> |
* and run tasks within the target's computation. |
1908 |
|
* |
1909 |
|
* @param task the task to join |
1910 |
|
* @param mode if shared, exit upon completing any task |
1912 |
|
* |
1913 |
|
*/ |
1914 |
|
private int helpComplete(ForkJoinTask<?> task, int mode) { |
1915 |
< |
WorkQueue[] ws; WorkQueue q; int m, n, s; |
1915 |
> |
WorkQueue[] ws; WorkQueue q; int m, n, s, u; |
1916 |
|
if (task != null && (ws = workQueues) != null && |
1917 |
|
(m = ws.length - 1) >= 0) { |
1918 |
|
for (int j = 1, origin = j;;) { |
1920 |
|
return s; |
1921 |
|
if ((q = ws[j & m]) != null && q.pollAndExecCC(task)) { |
1922 |
|
origin = j; |
1923 |
< |
if (mode == SHARED_QUEUE && (int)(ctl >> AC_SHIFT) >= 0) |
1923 |
> |
if (mode == SHARED_QUEUE && |
1924 |
> |
((u = (int)(ctl >>> 32)) >= 0 || (u >> UAC_SHIFT) >= 0)) |
1925 |
|
break; |
1926 |
|
} |
1927 |
|
else if ((j = (j + 2) & m) == origin) |
1939 |
|
* may become starved. |
1940 |
|
*/ |
1941 |
|
final boolean tryCompensate() { |
1942 |
< |
int pc = parallelism, e, u, i, tc; long c; |
1942 |
> |
int pc = config & SMASK, e, i, tc; long c; |
1943 |
|
WorkQueue[] ws; WorkQueue w; Thread p; |
1944 |
< |
if ((e = (int)(c = ctl)) >= 0 && (ws = workQueues) != null) { |
1944 |
> |
if ((ws = workQueues) != null && (e = (int)(c = ctl)) >= 0) { |
1945 |
|
if (e != 0 && (i = e & SMASK) < ws.length && |
1946 |
|
(w = ws[i]) != null && w.eventCount == (e | INT_SIGN)) { |
1947 |
|
long nc = ((long)(w.nextWait & E_MASK) | |
1953 |
|
return true; // replace with idle worker |
1954 |
|
} |
1955 |
|
} |
1956 |
< |
else if ((short)((u = (int)(c >>> 32)) >>> UTC_SHIFT) >= 0 && |
1957 |
< |
(u >> UAC_SHIFT) + pc > 1) { |
1956 |
> |
else if ((tc = (short)(c >>> TC_SHIFT)) >= 0 && |
1957 |
> |
(int)(c >> AC_SHIFT) + pc > 1) { |
1958 |
|
long nc = ((c - AC_UNIT) & AC_MASK) | (c & ~AC_MASK); |
1959 |
|
if (U.compareAndSwapLong(this, CTL, c, nc)) |
1960 |
< |
return true; // no compensation |
1960 |
> |
return true; // no compensation |
1961 |
|
} |
1962 |
< |
else if ((tc = u + pc) < MAX_CAP) { |
1962 |
> |
else if (tc + pc < MAX_CAP) { |
1963 |
|
long nc = ((c + TC_UNIT) & TC_MASK) | (c & ~TC_MASK); |
1964 |
|
if (U.compareAndSwapLong(this, CTL, c, nc)) { |
1965 |
+ |
ForkJoinWorkerThreadFactory fac; |
1966 |
|
Throwable ex = null; |
1967 |
|
ForkJoinWorkerThread wt = null; |
1968 |
|
try { |
1913 |
– |
ForkJoinWorkerThreadFactory fac; |
1969 |
|
if ((fac = factory) != null && |
1970 |
|
(wt = fac.newThread(this)) != null) { |
1971 |
|
wt.start(); |
1974 |
|
} catch (Throwable rex) { |
1975 |
|
ex = rex; |
1976 |
|
} |
1977 |
< |
deregisterWorker(wt, ex); // adjust counts etc |
1977 |
> |
deregisterWorker(wt, ex); // clean up and return false |
1978 |
|
} |
1979 |
|
} |
1980 |
|
} |
1993 |
|
if (joiner != null && task != null && (s = task.status) >= 0) { |
1994 |
|
ForkJoinTask<?> prevJoin = joiner.currentJoin; |
1995 |
|
joiner.currentJoin = task; |
1996 |
< |
do {} while ((s = task.status) >= 0 && |
1942 |
< |
joiner.queueSize() > 0 && |
1996 |
> |
do {} while ((s = task.status) >= 0 && !joiner.isEmpty() && |
1997 |
|
joiner.tryRemoveAndExec(task)); // process local tasks |
1998 |
< |
if (s >= 0 && (s = task.status) >= 0 && |
1999 |
< |
(s = helpSignal(task, joiner.poolIndex)) >= 0 && |
2000 |
< |
(task instanceof CountedCompleter)) |
2001 |
< |
s = helpComplete(task, LIFO_QUEUE); |
1998 |
> |
if (s >= 0 && (s = task.status) >= 0) { |
1999 |
> |
helpSignal(task, joiner.poolIndex, false); |
2000 |
> |
if ((s = task.status) >= 0 && |
2001 |
> |
(task instanceof CountedCompleter)) |
2002 |
> |
s = helpComplete(task, LIFO_QUEUE); |
2003 |
> |
} |
2004 |
|
while (s >= 0 && (s = task.status) >= 0) { |
2005 |
< |
if ((joiner.queueSize() > 0 || // try helping |
2005 |
> |
if ((!joiner.isEmpty() || // try helping |
2006 |
|
(s = tryHelpStealer(joiner, task)) == 0) && |
2007 |
< |
(s = task.status) >= 0 && tryCompensate()) { |
2008 |
< |
if (task.trySetSignal() && (s = task.status) >= 0) { |
2009 |
< |
synchronized (task) { |
2010 |
< |
if (task.status >= 0) { |
2011 |
< |
try { // see ForkJoinTask |
2012 |
< |
task.wait(); // for explanation |
2013 |
< |
} catch (InterruptedException ie) { |
2007 |
> |
(s = task.status) >= 0) { |
2008 |
> |
helpSignal(task, joiner.poolIndex, false); |
2009 |
> |
if ((s = task.status) >= 0 && tryCompensate()) { |
2010 |
> |
if (task.trySetSignal() && (s = task.status) >= 0) { |
2011 |
> |
synchronized (task) { |
2012 |
> |
if (task.status >= 0) { |
2013 |
> |
try { // see ForkJoinTask |
2014 |
> |
task.wait(); // for explanation |
2015 |
> |
} catch (InterruptedException ie) { |
2016 |
> |
} |
2017 |
|
} |
2018 |
+ |
else |
2019 |
+ |
task.notifyAll(); |
2020 |
|
} |
1960 |
– |
else |
1961 |
– |
task.notifyAll(); |
2021 |
|
} |
2022 |
+ |
long c; // re-activate |
2023 |
+ |
do {} while (!U.compareAndSwapLong |
2024 |
+ |
(this, CTL, c = ctl, c + AC_UNIT)); |
2025 |
|
} |
1964 |
– |
long c; // re-activate |
1965 |
– |
do {} while (!U.compareAndSwapLong |
1966 |
– |
(this, CTL, c = ctl, c + AC_UNIT)); |
2026 |
|
} |
2027 |
|
} |
2028 |
|
joiner.currentJoin = prevJoin; |
2043 |
|
if (joiner != null && task != null && (s = task.status) >= 0) { |
2044 |
|
ForkJoinTask<?> prevJoin = joiner.currentJoin; |
2045 |
|
joiner.currentJoin = task; |
2046 |
< |
do {} while ((s = task.status) >= 0 && |
1988 |
< |
joiner.queueSize() > 0 && |
2046 |
> |
do {} while ((s = task.status) >= 0 && !joiner.isEmpty() && |
2047 |
|
joiner.tryRemoveAndExec(task)); |
2048 |
< |
if (s >= 0 && (s = task.status) >= 0 && |
2049 |
< |
(s = helpSignal(task, joiner.poolIndex)) >= 0 && |
2050 |
< |
(task instanceof CountedCompleter)) |
2051 |
< |
s = helpComplete(task, LIFO_QUEUE); |
2052 |
< |
if (s >= 0 && joiner.queueSize() == 0) { |
2048 |
> |
if (s >= 0 && (s = task.status) >= 0) { |
2049 |
> |
helpSignal(task, joiner.poolIndex, false); |
2050 |
> |
if ((s = task.status) >= 0 && |
2051 |
> |
(task instanceof CountedCompleter)) |
2052 |
> |
s = helpComplete(task, LIFO_QUEUE); |
2053 |
> |
} |
2054 |
> |
if (s >= 0 && joiner.isEmpty()) { |
2055 |
|
do {} while (task.status >= 0 && |
2056 |
|
tryHelpStealer(joiner, task) > 0); |
2057 |
|
} |
2067 |
|
* @param r a (random) seed for scanning |
2068 |
|
*/ |
2069 |
|
private WorkQueue findNonEmptyStealQueue(int r) { |
2010 |
– |
int step = (r >>> 16) | 1; |
2070 |
|
for (WorkQueue[] ws;;) { |
2071 |
< |
int ps = plock, m; |
2071 |
> |
int ps = plock, m, n; |
2072 |
|
if ((ws = workQueues) == null || (m = ws.length - 1) < 1) |
2073 |
|
return null; |
2074 |
< |
for (int j = (m + 1) << 2; ; r += step) { |
2075 |
< |
WorkQueue q = ws[((r << 1) | 1) & m]; |
2076 |
< |
if (q != null && q.queueSize() > 0) |
2074 |
> |
for (int j = (m + 1) << 2; ;) { |
2075 |
> |
WorkQueue q = ws[(((r + j) << 1) | 1) & m]; |
2076 |
> |
if (q != null && (n = q.base - q.top) < 0) { |
2077 |
> |
if (n < -1) |
2078 |
> |
signalWork(q); |
2079 |
|
return q; |
2080 |
+ |
} |
2081 |
|
else if (--j < 0) { |
2082 |
|
if (plock == ps) |
2083 |
|
return null; |
2120 |
|
} |
2121 |
|
else |
2122 |
|
c = ctl; // re-increment on exit |
2123 |
< |
if ((int)(c >> AC_SHIFT) + parallelism == 0) { |
2123 |
> |
if ((int)(c >> AC_SHIFT) + (config & SMASK) == 0) { |
2124 |
|
do {} while (!U.compareAndSwapLong |
2125 |
|
(this, CTL, c = ctl, c + AC_UNIT)); |
2126 |
|
break; |
2195 |
|
static int getSurplusQueuedTaskCount() { |
2196 |
|
Thread t; ForkJoinWorkerThread wt; ForkJoinPool pool; WorkQueue q; |
2197 |
|
if (((t = Thread.currentThread()) instanceof ForkJoinWorkerThread)) { |
2198 |
< |
int b = (q = (wt = (ForkJoinWorkerThread)t).workQueue).base; |
2199 |
< |
int p = (pool = wt.pool).parallelism; |
2198 |
> |
int p = (pool = (wt = (ForkJoinWorkerThread)t).pool).config & SMASK; |
2199 |
> |
int n = (q = wt.workQueue).top - q.base; |
2200 |
|
int a = (int)(pool.ctl >> AC_SHIFT) + p; |
2201 |
< |
return q.top - b - (a > (p >>>= 1) ? 0 : |
2202 |
< |
a > (p >>>= 1) ? 1 : |
2203 |
< |
a > (p >>>= 1) ? 2 : |
2204 |
< |
a > (p >>>= 1) ? 4 : |
2205 |
< |
8); |
2201 |
> |
return n - (a > (p >>>= 1) ? 0 : |
2202 |
> |
a > (p >>>= 1) ? 1 : |
2203 |
> |
a > (p >>>= 1) ? 2 : |
2204 |
> |
a > (p >>>= 1) ? 4 : |
2205 |
> |
8); |
2206 |
|
} |
2207 |
|
return 0; |
2208 |
|
} |
2228 |
|
return false; |
2229 |
|
for (long c;;) { |
2230 |
|
if (((c = ctl) & STOP_BIT) != 0) { // already terminating |
2231 |
< |
if ((short)(c >>> TC_SHIFT) == -parallelism) { |
2231 |
> |
if ((short)(c >>> TC_SHIFT) == -(config & SMASK)) { |
2232 |
|
synchronized (this) { |
2233 |
|
notifyAll(); // signal when 0 workers |
2234 |
|
} |
2247 |
|
releasePlock(nps); |
2248 |
|
} |
2249 |
|
if (!now) { // check if idle & no tasks |
2250 |
< |
if ((int)(c >> AC_SHIFT) != -parallelism || |
2250 |
> |
if ((int)(c >> AC_SHIFT) != -(config & SMASK) || |
2251 |
|
hasQueuedSubmissions()) |
2252 |
|
return false; |
2253 |
|
// Check for unqueued inactive workers. One pass suffices. |
2317 |
|
*/ |
2318 |
|
static boolean tryExternalUnpush(ForkJoinTask<?> t) { |
2319 |
|
ForkJoinPool p; WorkQueue[] ws; WorkQueue q; Submitter z; |
2320 |
< |
ForkJoinTask<?>[] a; int m, s; long j; |
2321 |
< |
if ((z = submitters.get()) != null && |
2320 |
> |
ForkJoinTask<?>[] a; int m, s; |
2321 |
> |
if (t != null && |
2322 |
> |
(z = submitters.get()) != null && |
2323 |
|
(p = commonPool) != null && |
2324 |
|
(ws = p.workQueues) != null && |
2325 |
|
(m = ws.length - 1) >= 0 && |
2326 |
|
(q = ws[m & z.seed & SQMASK]) != null && |
2327 |
|
(s = q.top) != q.base && |
2328 |
< |
(a = q.array) != null && |
2329 |
< |
U.getObjectVolatile |
2330 |
< |
(a, j = (((a.length - 1) & (s - 1)) << ASHIFT) + ABASE) == t && |
2331 |
< |
U.compareAndSwapInt(q, QLOCK, 0, 1)) { |
2332 |
< |
if (q.array == a && q.top == s && // recheck |
2333 |
< |
U.compareAndSwapObject(a, j, t, null)) { |
2334 |
< |
q.top = s - 1; |
2328 |
> |
(a = q.array) != null) { |
2329 |
> |
long j = (((a.length - 1) & (s - 1)) << ASHIFT) + ABASE; |
2330 |
> |
if (U.getObject(a, j) == t && |
2331 |
> |
U.compareAndSwapInt(q, QLOCK, 0, 1)) { |
2332 |
> |
if (q.array == a && q.top == s && // recheck |
2333 |
> |
U.compareAndSwapObject(a, j, t, null)) { |
2334 |
> |
q.top = s - 1; |
2335 |
> |
q.qlock = 0; |
2336 |
> |
return true; |
2337 |
> |
} |
2338 |
|
q.qlock = 0; |
2273 |
– |
return true; |
2339 |
|
} |
2275 |
– |
q.qlock = 0; |
2340 |
|
} |
2341 |
|
return false; |
2342 |
|
} |
2351 |
|
if (q != null && (a = q.array) != null && (m = (a.length - 1)) >= 0 && |
2352 |
|
root != null && root.status >= 0) { |
2353 |
|
for (;;) { |
2354 |
< |
int s; Object o; CountedCompleter<?> task = null; |
2354 |
> |
int s, u; Object o; CountedCompleter<?> task = null; |
2355 |
|
if ((s = q.top) - q.base > 0) { |
2356 |
|
long j = ((m & (s - 1)) << ASHIFT) + ABASE; |
2357 |
|
if ((o = U.getObject(a, j)) != null && |
2374 |
|
} |
2375 |
|
if (task != null) |
2376 |
|
task.doExec(); |
2377 |
< |
if (root.status < 0 || (int)(ctl >> AC_SHIFT) >= 0) |
2377 |
> |
if (root.status < 0 || |
2378 |
> |
(u = (int)(ctl >>> 32)) >= 0 || (u >> UAC_SHIFT) >= 0) |
2379 |
|
break; |
2380 |
|
if (task == null) { |
2381 |
< |
if (helpSignal(root, q.poolIndex) >= 0) |
2381 |
> |
helpSignal(root, q.poolIndex, false); |
2382 |
> |
if (root.status >= 0) |
2383 |
|
helpComplete(root, SHARED_QUEUE); |
2384 |
|
break; |
2385 |
|
} |
2394 |
|
static void externalHelpJoin(ForkJoinTask<?> t) { |
2395 |
|
// Some hard-to-avoid overlap with tryExternalUnpush |
2396 |
|
ForkJoinPool p; WorkQueue[] ws; WorkQueue q, w; Submitter z; |
2397 |
< |
ForkJoinTask<?>[] a; int m, s, n; long j; |
2398 |
< |
if (t != null && t.status >= 0 && |
2397 |
> |
ForkJoinTask<?>[] a; int m, s, n; |
2398 |
> |
if (t != null && |
2399 |
|
(z = submitters.get()) != null && |
2400 |
|
(p = commonPool) != null && |
2401 |
|
(ws = p.workQueues) != null && |
2402 |
|
(m = ws.length - 1) >= 0 && |
2403 |
|
(q = ws[m & z.seed & SQMASK]) != null && |
2404 |
|
(a = q.array) != null) { |
2405 |
< |
if ((s = q.top) != q.base && |
2406 |
< |
U.getObjectVolatile |
2407 |
< |
(a, j = (((a.length - 1) & (s - 1)) << ASHIFT) + ABASE) == t && |
2408 |
< |
U.compareAndSwapInt(q, QLOCK, 0, 1)) { |
2409 |
< |
if (q.array == a && q.top == s && |
2410 |
< |
U.compareAndSwapObject(a, j, t, null)) { |
2411 |
< |
q.top = s - 1; |
2412 |
< |
q.qlock = 0; |
2413 |
< |
t.doExec(); |
2405 |
> |
int am = a.length - 1; |
2406 |
> |
if ((s = q.top) != q.base) { |
2407 |
> |
long j = ((am & (s - 1)) << ASHIFT) + ABASE; |
2408 |
> |
if (U.getObject(a, j) == t && |
2409 |
> |
U.compareAndSwapInt(q, QLOCK, 0, 1)) { |
2410 |
> |
if (q.array == a && q.top == s && |
2411 |
> |
U.compareAndSwapObject(a, j, t, null)) { |
2412 |
> |
q.top = s - 1; |
2413 |
> |
q.qlock = 0; |
2414 |
> |
t.doExec(); |
2415 |
> |
} |
2416 |
> |
else |
2417 |
> |
q.qlock = 0; |
2418 |
|
} |
2349 |
– |
else |
2350 |
– |
q.qlock = 0; |
2419 |
|
} |
2420 |
|
if (t.status >= 0) { |
2421 |
|
if (t instanceof CountedCompleter) |
2422 |
|
p.externalHelpComplete(q, t); |
2423 |
|
else |
2424 |
< |
p.helpSignal(t, q.poolIndex); |
2424 |
> |
p.helpSignal(t, q.poolIndex, false); |
2425 |
|
} |
2426 |
|
} |
2427 |
|
} |
2431 |
|
*/ |
2432 |
|
static void externalHelpQuiescePool() { |
2433 |
|
ForkJoinPool p; ForkJoinTask<?> t; WorkQueue q; int b; |
2366 |
– |
int r = ThreadLocalRandom.current().nextInt(); |
2434 |
|
if ((p = commonPool) != null && |
2435 |
< |
(q = p.findNonEmptyStealQueue(r)) != null && |
2435 |
> |
(q = p.findNonEmptyStealQueue(1)) != null && |
2436 |
|
(b = q.base) - q.top < 0 && |
2437 |
|
(t = q.pollAt(b)) != null) |
2438 |
|
t.doExec(); |
2509 |
|
throw new NullPointerException(); |
2510 |
|
if (parallelism <= 0 || parallelism > MAX_CAP) |
2511 |
|
throw new IllegalArgumentException(); |
2445 |
– |
this.parallelism = parallelism; |
2512 |
|
this.factory = factory; |
2513 |
|
this.ueh = handler; |
2514 |
< |
this.localMode = asyncMode ? FIFO_QUEUE : LIFO_QUEUE; |
2514 |
> |
this.config = parallelism | (asyncMode ? (FIFO_QUEUE << 16) : 0); |
2515 |
|
long np = (long)(-parallelism); // offset ctl counts |
2516 |
|
this.ctl = ((np << AC_SHIFT) & AC_MASK) | ((np << TC_SHIFT) & TC_MASK); |
2517 |
|
int pn = nextPoolId(); |
2528 |
|
ForkJoinPool(int parallelism, long ctl, |
2529 |
|
ForkJoinWorkerThreadFactory factory, |
2530 |
|
Thread.UncaughtExceptionHandler handler) { |
2531 |
< |
this.parallelism = parallelism; |
2531 |
> |
this.config = parallelism; |
2532 |
|
this.ctl = ctl; |
2533 |
|
this.factory = factory; |
2534 |
|
this.ueh = handler; |
2469 |
– |
this.localMode = LIFO_QUEUE; |
2535 |
|
this.workerNamePrefix = "ForkJoinPool.commonPool-worker-"; |
2536 |
|
} |
2537 |
|
|
2541 |
|
* @return the common pool instance |
2542 |
|
*/ |
2543 |
|
public static ForkJoinPool commonPool() { |
2544 |
< |
return commonPool; // cannot be null (if so, a static init error) |
2544 |
> |
// assert commonPool != null : "static init error"; |
2545 |
> |
return commonPool; |
2546 |
|
} |
2547 |
|
|
2548 |
|
// Execution methods |
2714 |
|
* @return the targeted parallelism level of this pool |
2715 |
|
*/ |
2716 |
|
public int getParallelism() { |
2717 |
< |
return parallelism; |
2717 |
> |
return config & SMASK; |
2718 |
|
} |
2719 |
|
|
2720 |
|
/** |
2735 |
|
* @return the number of worker threads |
2736 |
|
*/ |
2737 |
|
public int getPoolSize() { |
2738 |
< |
return parallelism + (short)(ctl >>> TC_SHIFT); |
2738 |
> |
return (config & SMASK) + (short)(ctl >>> TC_SHIFT); |
2739 |
|
} |
2740 |
|
|
2741 |
|
/** |
2745 |
|
* @return {@code true} if this pool uses async mode |
2746 |
|
*/ |
2747 |
|
public boolean getAsyncMode() { |
2748 |
< |
return localMode != 0; |
2748 |
> |
return (config >>> 16) == FIFO_QUEUE; |
2749 |
|
} |
2750 |
|
|
2751 |
|
/** |
2776 |
|
* @return the number of active threads |
2777 |
|
*/ |
2778 |
|
public int getActiveThreadCount() { |
2779 |
< |
int r = parallelism + (int)(ctl >> AC_SHIFT); |
2779 |
> |
int r = (config & SMASK) + (int)(ctl >> AC_SHIFT); |
2780 |
|
return (r <= 0) ? 0 : r; // suppress momentarily negative values |
2781 |
|
} |
2782 |
|
|
2792 |
|
* @return {@code true} if all threads are currently idle |
2793 |
|
*/ |
2794 |
|
public boolean isQuiescent() { |
2795 |
< |
return (int)(ctl >> AC_SHIFT) + parallelism == 0; |
2795 |
> |
return (int)(ctl >> AC_SHIFT) + (config & SMASK) == 0; |
2796 |
|
} |
2797 |
|
|
2798 |
|
/** |
2869 |
|
WorkQueue[] ws; WorkQueue w; |
2870 |
|
if ((ws = workQueues) != null) { |
2871 |
|
for (int i = 0; i < ws.length; i += 2) { |
2872 |
< |
if ((w = ws[i]) != null && w.queueSize() != 0) |
2872 |
> |
if ((w = ws[i]) != null && !w.isEmpty()) |
2873 |
|
return true; |
2874 |
|
} |
2875 |
|
} |
2955 |
|
} |
2956 |
|
} |
2957 |
|
} |
2958 |
< |
int pc = parallelism; |
2958 |
> |
int pc = (config & SMASK); |
2959 |
|
int tc = pc + (short)(c >>> TC_SHIFT); |
2960 |
|
int ac = pc + (int)(c >> AC_SHIFT); |
2961 |
|
if (ac < 0) // ignore transient negative |
3028 |
|
public boolean isTerminated() { |
3029 |
|
long c = ctl; |
3030 |
|
return ((c & STOP_BIT) != 0L && |
3031 |
< |
(short)(c >>> TC_SHIFT) == -parallelism); |
3031 |
> |
(short)(c >>> TC_SHIFT) == -(config & SMASK)); |
3032 |
|
} |
3033 |
|
|
3034 |
|
/** |
3047 |
|
public boolean isTerminating() { |
3048 |
|
long c = ctl; |
3049 |
|
return ((c & STOP_BIT) != 0L && |
3050 |
< |
(short)(c >>> TC_SHIFT) != -parallelism); |
3050 |
> |
(short)(c >>> TC_SHIFT) != -(config & SMASK)); |
3051 |
|
} |
3052 |
|
|
3053 |
|
/** |
3191 |
|
if (t instanceof ForkJoinWorkerThread) { |
3192 |
|
ForkJoinPool p = ((ForkJoinWorkerThread)t).pool; |
3193 |
|
while (!blocker.isReleasable()) { // variant of helpSignal |
3194 |
< |
WorkQueue[] ws; WorkQueue q; int m, n; |
3194 |
> |
WorkQueue[] ws; WorkQueue q; int m, u; |
3195 |
|
if ((ws = p.workQueues) != null && (m = ws.length - 1) >= 0) { |
3196 |
|
for (int i = 0; i <= m; ++i) { |
3197 |
|
if (blocker.isReleasable()) |
3198 |
|
return; |
3199 |
< |
if ((q = ws[i]) != null && (n = q.queueSize()) > 0) { |
3200 |
< |
p.signalWork(q, n); |
3201 |
< |
if ((int)(p.ctl >> AC_SHIFT) >= 0) |
3199 |
> |
if ((q = ws[i]) != null && q.base - q.top < 0) { |
3200 |
> |
p.signalWork(q); |
3201 |
> |
if ((u = (int)(p.ctl >>> 32)) >= 0 || |
3202 |
> |
(u >> UAC_SHIFT) >= 0) |
3203 |
|
break; |
3204 |
|
} |
3205 |
|
} |
3245 |
|
private static final long QLOCK; |
3246 |
|
|
3247 |
|
static { |
3181 |
– |
// Establish common pool parameters |
3182 |
– |
// TBD: limit or report ignored exceptions? |
3183 |
– |
|
3184 |
– |
int par = 0; |
3185 |
– |
ForkJoinWorkerThreadFactory fac = null; |
3186 |
– |
Thread.UncaughtExceptionHandler handler = null; |
3187 |
– |
try { |
3188 |
– |
String pp = System.getProperty(propPrefix + "parallelism"); |
3189 |
– |
String hp = System.getProperty(propPrefix + "exceptionHandler"); |
3190 |
– |
String fp = System.getProperty(propPrefix + "threadFactory"); |
3191 |
– |
if (fp != null) |
3192 |
– |
fac = ((ForkJoinWorkerThreadFactory)ClassLoader. |
3193 |
– |
getSystemClassLoader().loadClass(fp).newInstance()); |
3194 |
– |
if (hp != null) |
3195 |
– |
handler = ((Thread.UncaughtExceptionHandler)ClassLoader. |
3196 |
– |
getSystemClassLoader().loadClass(hp).newInstance()); |
3197 |
– |
if (pp != null) |
3198 |
– |
par = Integer.parseInt(pp); |
3199 |
– |
} catch (Exception ignore) { |
3200 |
– |
} |
3201 |
– |
|
3248 |
|
int s; // initialize field offsets for CAS etc |
3249 |
|
try { |
3250 |
|
U = getUnsafe(); |
3273 |
|
if ((s & (s-1)) != 0) |
3274 |
|
throw new Error("data type scale not a power of two"); |
3275 |
|
|
3276 |
+ |
submitters = new ThreadLocal<Submitter>(); |
3277 |
+ |
ForkJoinWorkerThreadFactory fac = defaultForkJoinWorkerThreadFactory = |
3278 |
+ |
new DefaultForkJoinWorkerThreadFactory(); |
3279 |
+ |
modifyThreadPermission = new RuntimePermission("modifyThread"); |
3280 |
+ |
|
3281 |
|
/* |
3282 |
< |
* For extra caution, computations to set up pool state are |
3283 |
< |
* here; the constructor just assigns these values to fields. |
3282 |
> |
* Establish common pool parameters. For extra caution, |
3283 |
> |
* computations to set up common pool state are here; the |
3284 |
> |
* constructor just assigns these values to fields. |
3285 |
|
*/ |
3286 |
< |
ForkJoinWorkerThreadFactory defaultFac = |
3287 |
< |
defaultForkJoinWorkerThreadFactory = |
3288 |
< |
new DefaultForkJoinWorkerThreadFactory(); |
3289 |
< |
if (fac == null) |
3290 |
< |
fac = defaultFac; |
3286 |
> |
|
3287 |
> |
int par = 0; |
3288 |
> |
Thread.UncaughtExceptionHandler handler = null; |
3289 |
> |
try { // TBD: limit or report ignored exceptions? |
3290 |
> |
String pp = System.getProperty |
3291 |
> |
("java.util.concurrent.ForkJoinPool.common.parallelism"); |
3292 |
> |
String hp = System.getProperty |
3293 |
> |
("java.util.concurrent.ForkJoinPool.common.exceptionHandler"); |
3294 |
> |
String fp = System.getProperty |
3295 |
> |
("java.util.concurrent.ForkJoinPool.common.threadFactory"); |
3296 |
> |
if (fp != null) |
3297 |
> |
fac = ((ForkJoinWorkerThreadFactory)ClassLoader. |
3298 |
> |
getSystemClassLoader().loadClass(fp).newInstance()); |
3299 |
> |
if (hp != null) |
3300 |
> |
handler = ((Thread.UncaughtExceptionHandler)ClassLoader. |
3301 |
> |
getSystemClassLoader().loadClass(hp).newInstance()); |
3302 |
> |
if (pp != null) |
3303 |
> |
par = Integer.parseInt(pp); |
3304 |
> |
} catch (Exception ignore) { |
3305 |
> |
} |
3306 |
> |
|
3307 |
|
if (par <= 0) |
3308 |
|
par = Runtime.getRuntime().availableProcessors(); |
3309 |
|
if (par > MAX_CAP) |
3310 |
|
par = MAX_CAP; |
3311 |
+ |
commonPoolParallelism = par; |
3312 |
|
long np = (long)(-par); // precompute initial ctl value |
3313 |
|
long ct = ((np << AC_SHIFT) & AC_MASK) | ((np << TC_SHIFT) & TC_MASK); |
3314 |
|
|
3246 |
– |
commonPoolParallelism = par; |
3315 |
|
commonPool = new ForkJoinPool(par, ct, fac, handler); |
3248 |
– |
modifyThreadPermission = new RuntimePermission("modifyThread"); |
3249 |
– |
submitters = new ThreadLocal<Submitter>(); |
3316 |
|
} |
3317 |
|
|
3318 |
|
/** |