687 |
|
return (n >= 0) ? 0 : -n; // ignore transient negative |
688 |
|
} |
689 |
|
|
690 |
< |
/** |
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. |
708 |
|
* shared-queue version is embedded in method externalPush.) |
709 |
|
* |
710 |
|
* @param task the task. Caller must ensure non-null. |
711 |
< |
* @throw RejectedExecutionException if array cannot be resized |
711 |
> |
* @throws RejectedExecutionException if array cannot be resized |
712 |
|
*/ |
713 |
|
final void push(ForkJoinTask<?> task) { |
714 |
|
ForkJoinTask<?>[] a; ForkJoinPool p; |
725 |
|
} |
726 |
|
} |
727 |
|
|
728 |
< |
/** |
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. |
907 |
|
* or any other cancelled task. Returns (true) on any CAS |
908 |
|
* or consistency check failure so caller can retry. |
909 |
|
* |
910 |
< |
* @return false if no progress can be made, else true; |
910 |
> |
* @return false if no progress can be made, else true |
911 |
|
*/ |
912 |
|
final boolean tryRemoveAndExec(ForkJoinTask<?> task) { |
913 |
|
boolean stat = true, removed = false, empty = true; |
952 |
|
|
953 |
|
/** |
954 |
|
* Polls for and executes the given task or any other task in |
955 |
< |
* its CountedCompleter computation |
955 |
> |
* its CountedCompleter computation. |
956 |
|
*/ |
957 |
|
final boolean pollAndExecCC(ForkJoinTask<?> root) { |
958 |
|
ForkJoinTask<?>[] a; int b; Object o; |
1026 |
|
private static final int ABASE; |
1027 |
|
private static final int ASHIFT; |
1028 |
|
static { |
1029 |
– |
int s; |
1029 |
|
try { |
1030 |
|
U = getUnsafe(); |
1031 |
|
Class<?> k = WorkQueue.class; |
1033 |
|
QLOCK = U.objectFieldOffset |
1034 |
|
(k.getDeclaredField("qlock")); |
1035 |
|
ABASE = U.arrayBaseOffset(ak); |
1036 |
< |
s = U.arrayIndexScale(ak); |
1036 |
> |
int scale = U.arrayIndexScale(ak); |
1037 |
> |
if ((scale & (scale - 1)) != 0) |
1038 |
> |
throw new Error("data type scale not a power of two"); |
1039 |
> |
ASHIFT = 31 - Integer.numberOfLeadingZeros(scale); |
1040 |
|
} catch (Exception e) { |
1041 |
|
throw new Error(e); |
1042 |
|
} |
1041 |
– |
if ((s & (s-1)) != 0) |
1042 |
– |
throw new Error("data type scale not a power of two"); |
1043 |
– |
ASHIFT = 31 - Integer.numberOfLeadingZeros(s); |
1043 |
|
} |
1044 |
|
} |
1045 |
|
|
1131 |
|
*/ |
1132 |
|
private static final int SEED_INCREMENT = 0x61c88647; |
1133 |
|
|
1134 |
< |
/** |
1134 |
> |
/* |
1135 |
|
* Bits and masks for control variables |
1136 |
|
* |
1137 |
|
* Field ctl is a long packed with: |
1244 |
|
volatile Object pad10, pad11, pad12, pad13, pad14, pad15, pad16, pad17; |
1245 |
|
volatile Object pad18, pad19, pad1a, pad1b; |
1246 |
|
|
1247 |
< |
/* |
1247 |
> |
/** |
1248 |
|
* Acquires the plock lock to protect worker array and related |
1249 |
|
* updates. This method is called only if an initial CAS on plock |
1250 |
< |
* fails. This acts as a spinLock for normal cases, but falls back |
1250 |
> |
* fails. This acts as a spinlock for normal cases, but falls back |
1251 |
|
* to builtin monitor to block when (rarely) needed. This would be |
1252 |
|
* a terrible idea for a highly contended lock, but works fine as |
1253 |
|
* a more conservative alternative to a pure spinlock. |
1521 |
|
(m = ws.length - 1) < 0) { // initialize workQueues |
1522 |
|
int p = config & SMASK; // find power of two table size |
1523 |
|
int n = (p > 1) ? p - 1 : 1; // ensure at least 2 slots |
1524 |
< |
n |= n >>> 1; n |= n >>> 2; n |= n >>> 4; |
1525 |
< |
n |= n >>> 8; n |= n >>> 16; n = (n + 1) << 1; |
1524 |
> |
n |= n >>> 1; |
1525 |
> |
n |= n >>> 2; |
1526 |
> |
n |= n >>> 4; |
1527 |
> |
n |= n >>> 8; |
1528 |
> |
n |= n >>> 16; |
1529 |
> |
n = (n + 1) << 1; |
1530 |
|
WorkQueue[] nws = ((ws = workQueues) == null || ws.length == 0 ? |
1531 |
|
new WorkQueue[n] : null); |
1532 |
|
if (((ps = plock) & PL_LOCK) != 0 || |
2131 |
|
w.runSubtask(t); |
2132 |
|
} |
2133 |
|
} |
2134 |
< |
else if (active) { // decrement active count without queuing |
2134 |
> |
else if (active) { // decrement active count without queuing |
2135 |
|
long nc = (c = ctl) - AC_UNIT; |
2136 |
|
if ((int)(nc >> AC_SHIFT) + (config & SMASK) == 0) |
2137 |
< |
return; // bypass decrement-then-increment |
2137 |
> |
return; // bypass decrement-then-increment |
2138 |
|
if (U.compareAndSwapLong(this, CTL, c, nc)) |
2139 |
|
active = false; |
2140 |
|
} |
2189 |
|
* producing extra tasks amortizes the uncertainty of progress and |
2190 |
|
* diffusion assumptions. |
2191 |
|
* |
2192 |
< |
* So, users will want to use values larger, but not much larger |
2192 |
> |
* So, users will want to use values larger (but not much larger) |
2193 |
|
* than 1 to both smooth over transient shortages and hedge |
2194 |
|
* against uneven progress; as traded off against the cost of |
2195 |
|
* extra task overhead. We leave the user to pick a threshold |
2470 |
|
* java.lang.RuntimePermission}{@code ("modifyThread")} |
2471 |
|
*/ |
2472 |
|
public ForkJoinPool() { |
2473 |
< |
this(Runtime.getRuntime().availableProcessors(), |
2473 |
> |
this(Math.min(MAX_CAP, Runtime.getRuntime().availableProcessors()), |
2474 |
|
defaultForkJoinWorkerThreadFactory, null, false); |
2475 |
|
} |
2476 |
|
|
2562 |
|
* exit. |
2563 |
|
* |
2564 |
|
* @return the common pool instance |
2565 |
+ |
* @since 1.8 |
2566 |
|
*/ |
2567 |
|
public static ForkJoinPool commonPool() { |
2568 |
|
// assert common != null : "static init error"; |
2689 |
|
// In previous versions of this class, this method constructed |
2690 |
|
// a task to run ForkJoinTask.invokeAll, but now external |
2691 |
|
// invocation of multiple tasks is at least as efficient. |
2692 |
< |
List<ForkJoinTask<T>> fs = new ArrayList<ForkJoinTask<T>>(tasks.size()); |
2689 |
< |
// Workaround needed because method wasn't declared with |
2690 |
< |
// wildcards in return type but should have been. |
2691 |
< |
@SuppressWarnings({"unchecked", "rawtypes"}) |
2692 |
< |
List<Future<T>> futures = (List<Future<T>>) (List) fs; |
2692 |
> |
ArrayList<Future<T>> futures = new ArrayList<Future<T>>(tasks.size()); |
2693 |
|
|
2694 |
|
boolean done = false; |
2695 |
|
try { |
2696 |
|
for (Callable<T> t : tasks) { |
2697 |
|
ForkJoinTask<T> f = new ForkJoinTask.AdaptedCallable<T>(t); |
2698 |
+ |
futures.add(f); |
2699 |
|
externalPush(f); |
2699 |
– |
fs.add(f); |
2700 |
|
} |
2701 |
< |
for (ForkJoinTask<T> f : fs) |
2702 |
< |
f.quietlyJoin(); |
2701 |
> |
for (int i = 0, size = futures.size(); i < size; i++) |
2702 |
> |
((ForkJoinTask<?>)futures.get(i)).quietlyJoin(); |
2703 |
|
done = true; |
2704 |
|
return futures; |
2705 |
|
} finally { |
2706 |
|
if (!done) |
2707 |
< |
for (ForkJoinTask<T> f : fs) |
2708 |
< |
f.cancel(false); |
2707 |
> |
for (int i = 0, size = futures.size(); i < size; i++) |
2708 |
> |
futures.get(i).cancel(false); |
2709 |
|
} |
2710 |
|
} |
2711 |
|
|
2741 |
|
* Returns the targeted parallelism level of the common pool. |
2742 |
|
* |
2743 |
|
* @return the targeted parallelism level of the common pool |
2744 |
+ |
* @since 1.8 |
2745 |
|
*/ |
2746 |
|
public static int getCommonPoolParallelism() { |
2747 |
|
return commonParallelism; |
3170 |
|
|
3171 |
|
/** |
3172 |
|
* Waits and/or attempts to assist performing tasks indefinitely |
3173 |
< |
* until the {@link #commonPool()} {@link #isQuiescent} |
3173 |
> |
* until the {@link #commonPool()} {@link #isQuiescent}. |
3174 |
|
*/ |
3175 |
|
static void quiesceCommonPool() { |
3176 |
|
common.awaitQuiescence(Long.MAX_VALUE, TimeUnit.NANOSECONDS); |
3329 |
|
private static final long QLOCK; |
3330 |
|
|
3331 |
|
static { |
3332 |
< |
int s; // initialize field offsets for CAS etc |
3332 |
> |
// initialize field offsets for CAS etc |
3333 |
|
try { |
3334 |
|
U = getUnsafe(); |
3335 |
|
Class<?> k = ForkJoinPool.class; |
3349 |
|
(wk.getDeclaredField("qlock")); |
3350 |
|
Class<?> ak = ForkJoinTask[].class; |
3351 |
|
ABASE = U.arrayBaseOffset(ak); |
3352 |
< |
s = U.arrayIndexScale(ak); |
3353 |
< |
ASHIFT = 31 - Integer.numberOfLeadingZeros(s); |
3352 |
> |
int scale = U.arrayIndexScale(ak); |
3353 |
> |
if ((scale & (scale - 1)) != 0) |
3354 |
> |
throw new Error("data type scale not a power of two"); |
3355 |
> |
ASHIFT = 31 - Integer.numberOfLeadingZeros(scale); |
3356 |
|
} catch (Exception e) { |
3357 |
|
throw new Error(e); |
3358 |
|
} |
3356 |
– |
if ((s & (s-1)) != 0) |
3357 |
– |
throw new Error("data type scale not a power of two"); |
3359 |
|
|
3360 |
|
submitters = new ThreadLocal<Submitter>(); |
3361 |
|
ForkJoinWorkerThreadFactory fac = defaultForkJoinWorkerThreadFactory = |
3409 |
|
private static sun.misc.Unsafe getUnsafe() { |
3410 |
|
try { |
3411 |
|
return sun.misc.Unsafe.getUnsafe(); |
3412 |
< |
} catch (SecurityException se) { |
3413 |
< |
try { |
3414 |
< |
return java.security.AccessController.doPrivileged |
3415 |
< |
(new java.security |
3416 |
< |
.PrivilegedExceptionAction<sun.misc.Unsafe>() { |
3417 |
< |
public sun.misc.Unsafe run() throws Exception { |
3418 |
< |
java.lang.reflect.Field f = sun.misc |
3419 |
< |
.Unsafe.class.getDeclaredField("theUnsafe"); |
3420 |
< |
f.setAccessible(true); |
3421 |
< |
return (sun.misc.Unsafe) f.get(null); |
3422 |
< |
}}); |
3423 |
< |
} catch (java.security.PrivilegedActionException e) { |
3424 |
< |
throw new RuntimeException("Could not initialize intrinsics", |
3425 |
< |
e.getCause()); |
3426 |
< |
} |
3412 |
> |
} catch (SecurityException tryReflectionInstead) {} |
3413 |
> |
try { |
3414 |
> |
return java.security.AccessController.doPrivileged |
3415 |
> |
(new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() { |
3416 |
> |
public sun.misc.Unsafe run() throws Exception { |
3417 |
> |
Class<sun.misc.Unsafe> k = sun.misc.Unsafe.class; |
3418 |
> |
for (java.lang.reflect.Field f : k.getDeclaredFields()) { |
3419 |
> |
f.setAccessible(true); |
3420 |
> |
Object x = f.get(null); |
3421 |
> |
if (k.isInstance(x)) |
3422 |
> |
return k.cast(x); |
3423 |
> |
} |
3424 |
> |
throw new NoSuchFieldError("the Unsafe"); |
3425 |
> |
}}); |
3426 |
> |
} catch (java.security.PrivilegedActionException e) { |
3427 |
> |
throw new RuntimeException("Could not initialize intrinsics", |
3428 |
> |
e.getCause()); |
3429 |
|
} |
3430 |
|
} |
3428 |
– |
|
3431 |
|
} |