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; |
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. |
2185 |
|
* producing extra tasks amortizes the uncertainty of progress and |
2186 |
|
* diffusion assumptions. |
2187 |
|
* |
2188 |
< |
* So, users will want to use values larger, but not much larger |
2188 |
> |
* So, users will want to use values larger (but not much larger) |
2189 |
|
* than 1 to both smooth over transient shortages and hedge |
2190 |
|
* against uneven progress; as traded off against the cost of |
2191 |
|
* extra task overhead. We leave the user to pick a threshold |
2466 |
|
* java.lang.RuntimePermission}{@code ("modifyThread")} |
2467 |
|
*/ |
2468 |
|
public ForkJoinPool() { |
2469 |
< |
this(Runtime.getRuntime().availableProcessors(), |
2469 |
> |
this(Math.min(MAX_CAP, Runtime.getRuntime().availableProcessors()), |
2470 |
|
defaultForkJoinWorkerThreadFactory, null, false); |
2471 |
|
} |
2472 |
|
|
2554 |
|
* ongoing processing are automatically terminated upon program |
2555 |
|
* {@link System#exit}. Any program that relies on asynchronous |
2556 |
|
* task processing to complete before program termination should |
2557 |
< |
* invoke {@link #quiesceCommonPool}, or the timeout-based {@code |
2558 |
< |
* commonPool().}{@link #awaitQuiescence}, before exit. |
2557 |
> |
* invoke {@code commonPool().}{@link #awaitQuiescence}, before |
2558 |
> |
* exit. |
2559 |
|
* |
2560 |
|
* @return the common pool instance |
2561 |
+ |
* @since 1.8 |
2562 |
|
*/ |
2563 |
|
public static ForkJoinPool commonPool() { |
2564 |
|
// assert common != null : "static init error"; |
2685 |
|
// In previous versions of this class, this method constructed |
2686 |
|
// a task to run ForkJoinTask.invokeAll, but now external |
2687 |
|
// invocation of multiple tasks is at least as efficient. |
2688 |
< |
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; |
2688 |
> |
ArrayList<Future<T>> futures = new ArrayList<Future<T>>(tasks.size()); |
2689 |
|
|
2690 |
|
boolean done = false; |
2691 |
|
try { |
2692 |
|
for (Callable<T> t : tasks) { |
2693 |
|
ForkJoinTask<T> f = new ForkJoinTask.AdaptedCallable<T>(t); |
2694 |
+ |
futures.add(f); |
2695 |
|
externalPush(f); |
2699 |
– |
fs.add(f); |
2696 |
|
} |
2697 |
< |
for (ForkJoinTask<T> f : fs) |
2698 |
< |
f.quietlyJoin(); |
2697 |
> |
for (int i = 0, size = futures.size(); i < size; i++) |
2698 |
> |
((ForkJoinTask<?>)futures.get(i)).quietlyJoin(); |
2699 |
|
done = true; |
2700 |
|
return futures; |
2701 |
|
} finally { |
2702 |
|
if (!done) |
2703 |
< |
for (ForkJoinTask<T> f : fs) |
2704 |
< |
f.cancel(false); |
2703 |
> |
for (int i = 0, size = futures.size(); i < size; i++) |
2704 |
> |
futures.get(i).cancel(false); |
2705 |
|
} |
2706 |
|
} |
2707 |
|
|
2737 |
|
* Returns the targeted parallelism level of the common pool. |
2738 |
|
* |
2739 |
|
* @return the targeted parallelism level of the common pool |
2740 |
+ |
* @since 1.8 |
2741 |
|
*/ |
2742 |
|
public static int getCommonPoolParallelism() { |
2743 |
|
return commonParallelism; |
2998 |
|
* Possibly initiates an orderly shutdown in which previously |
2999 |
|
* submitted tasks are executed, but no new tasks will be |
3000 |
|
* accepted. Invocation has no effect on execution state if this |
3001 |
< |
* is the {@link #commonPool}, and no additional effect if |
3001 |
> |
* is the {@link #commonPool()}, and no additional effect if |
3002 |
|
* already shut down. Tasks that are in the process of being |
3003 |
|
* submitted concurrently during the course of this method may or |
3004 |
|
* may not be rejected. |
3016 |
|
/** |
3017 |
|
* Possibly attempts to cancel and/or stop all tasks, and reject |
3018 |
|
* all subsequently submitted tasks. Invocation has no effect on |
3019 |
< |
* execution state if this is the {@link #commonPool}, and no |
3019 |
> |
* execution state if this is the {@link #commonPool()}, and no |
3020 |
|
* additional effect if already shut down. Otherwise, tasks that |
3021 |
|
* are in the process of being submitted or executed concurrently |
3022 |
|
* during the course of this method may or may not be |
3166 |
|
|
3167 |
|
/** |
3168 |
|
* Waits and/or attempts to assist performing tasks indefinitely |
3169 |
< |
* until the {@link #commonPool()} {@link #isQuiescent} |
3169 |
> |
* until the {@link #commonPool()} {@link #isQuiescent}. |
3170 |
|
*/ |
3171 |
< |
public static void quiesceCommonPool() { |
3171 |
> |
static void quiesceCommonPool() { |
3172 |
|
common.awaitQuiescence(Long.MAX_VALUE, TimeUnit.NANOSECONDS); |
3173 |
|
} |
3174 |
|
|
3325 |
|
private static final long QLOCK; |
3326 |
|
|
3327 |
|
static { |
3328 |
< |
int s; // initialize field offsets for CAS etc |
3328 |
> |
// initialize field offsets for CAS etc |
3329 |
|
try { |
3330 |
|
U = getUnsafe(); |
3331 |
|
Class<?> k = ForkJoinPool.class; |
3345 |
|
(wk.getDeclaredField("qlock")); |
3346 |
|
Class<?> ak = ForkJoinTask[].class; |
3347 |
|
ABASE = U.arrayBaseOffset(ak); |
3348 |
< |
s = U.arrayIndexScale(ak); |
3349 |
< |
ASHIFT = 31 - Integer.numberOfLeadingZeros(s); |
3348 |
> |
int scale = U.arrayIndexScale(ak); |
3349 |
> |
if ((scale & (scale - 1)) != 0) |
3350 |
> |
throw new Error("data type scale not a power of two"); |
3351 |
> |
ASHIFT = 31 - Integer.numberOfLeadingZeros(scale); |
3352 |
|
} catch (Exception e) { |
3353 |
|
throw new Error(e); |
3354 |
|
} |
3356 |
– |
if ((s & (s-1)) != 0) |
3357 |
– |
throw new Error("data type scale not a power of two"); |
3355 |
|
|
3356 |
|
submitters = new ThreadLocal<Submitter>(); |
3357 |
|
ForkJoinWorkerThreadFactory fac = defaultForkJoinWorkerThreadFactory = |
3405 |
|
private static sun.misc.Unsafe getUnsafe() { |
3406 |
|
try { |
3407 |
|
return sun.misc.Unsafe.getUnsafe(); |
3408 |
< |
} catch (SecurityException se) { |
3409 |
< |
try { |
3410 |
< |
return java.security.AccessController.doPrivileged |
3411 |
< |
(new java.security |
3412 |
< |
.PrivilegedExceptionAction<sun.misc.Unsafe>() { |
3413 |
< |
public sun.misc.Unsafe run() throws Exception { |
3414 |
< |
java.lang.reflect.Field f = sun.misc |
3415 |
< |
.Unsafe.class.getDeclaredField("theUnsafe"); |
3416 |
< |
f.setAccessible(true); |
3417 |
< |
return (sun.misc.Unsafe) f.get(null); |
3418 |
< |
}}); |
3419 |
< |
} catch (java.security.PrivilegedActionException e) { |
3420 |
< |
throw new RuntimeException("Could not initialize intrinsics", |
3421 |
< |
e.getCause()); |
3422 |
< |
} |
3408 |
> |
} catch (SecurityException tryReflectionInstead) {} |
3409 |
> |
try { |
3410 |
> |
return java.security.AccessController.doPrivileged |
3411 |
> |
(new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() { |
3412 |
> |
public sun.misc.Unsafe run() throws Exception { |
3413 |
> |
Class<sun.misc.Unsafe> k = sun.misc.Unsafe.class; |
3414 |
> |
for (java.lang.reflect.Field f : k.getDeclaredFields()) { |
3415 |
> |
f.setAccessible(true); |
3416 |
> |
Object x = f.get(null); |
3417 |
> |
if (k.isInstance(x)) |
3418 |
> |
return k.cast(x); |
3419 |
> |
} |
3420 |
> |
throw new NoSuchFieldError("the Unsafe"); |
3421 |
> |
}}); |
3422 |
> |
} catch (java.security.PrivilegedActionException e) { |
3423 |
> |
throw new RuntimeException("Could not initialize intrinsics", |
3424 |
> |
e.getCause()); |
3425 |
|
} |
3426 |
|
} |
3428 |
– |
|
3427 |
|
} |