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; |
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 |
|
|
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. |
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 |
|
|
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; |
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 |
|
} |