@@ -792,25 +792,25 @@ static final class DefaultForkJoinWorkerThreadFactory
792
792
private static final AccessControlContext ACC = contextWithPermissions (
793
793
new RuntimePermission ("getClassLoader" ),
794
794
new RuntimePermission ("setContextClassLoader" ));
795
+
795
796
public final ForkJoinWorkerThread newThread (ForkJoinPool pool ) {
796
797
return AccessController .doPrivileged (
797
798
new PrivilegedAction <>() {
798
799
public ForkJoinWorkerThread run () {
799
- return new ForkJoinWorkerThread (null , pool , true , false );
800
+ return new ForkJoinWorkerThread (null , pool , true , true );
800
801
}},
801
802
ACC );
802
803
}
803
804
}
804
805
805
806
/**
806
- * Factory for CommonPool unless overridded by System
807
- * property. Creates InnocuousForkJoinWorkerThreads if a security
808
- * manager is present at time of invocation. Support requires that
809
- * we break quite a lot of encapsulation (some via helper methods
810
- * in ThreadLocalRandom) to access and set Thread fields.
807
+ * Factory for InnocuousForkJoinWorkerThread. Support requires
808
+ * that we break quite a lot of encapsulation (some via helper
809
+ * methods in ThreadLocalRandom) to access and set Thread fields.
811
810
*/
812
- static final class DefaultCommonPoolForkJoinWorkerThreadFactory
811
+ static final class InnocuousForkJoinWorkerThreadFactory
813
812
implements ForkJoinWorkerThreadFactory {
813
+ // ACC for access to the factory
814
814
private static final AccessControlContext ACC = contextWithPermissions (
815
815
modifyThreadPermission ,
816
816
new RuntimePermission ("enableContextClassLoaderOverride" ),
@@ -820,13 +820,11 @@ static final class DefaultCommonPoolForkJoinWorkerThreadFactory
820
820
821
821
public final ForkJoinWorkerThread newThread (ForkJoinPool pool ) {
822
822
return AccessController .doPrivileged (
823
- new PrivilegedAction <>() {
824
- public ForkJoinWorkerThread run () {
825
- return System .getSecurityManager () == null ?
826
- new ForkJoinWorkerThread (null , pool , true , true ):
827
- new ForkJoinWorkerThread .
828
- InnocuousForkJoinWorkerThread (pool ); }},
829
- ACC );
823
+ new PrivilegedAction <>() {
824
+ public ForkJoinWorkerThread run () {
825
+ return new ForkJoinWorkerThread .
826
+ InnocuousForkJoinWorkerThread (pool ); }},
827
+ ACC );
830
828
}
831
829
}
832
830
@@ -1712,19 +1710,11 @@ else if (deadline != 0L)
1712
1710
// Utilities used by ForkJoinTask
1713
1711
1714
1712
/**
1715
- * Returns true if all workers are busy, possibly creating one if allowed
1713
+ * Returns true if all workers are busy
1716
1714
*/
1717
1715
final boolean isSaturated () {
1718
- int maxTotal = bounds >>> SWIDTH ;
1719
- for (long c ;;) {
1720
- if (((int )(c = ctl ) & ~UNSIGNALLED ) != 0 )
1721
- return false ;
1722
- if ((short )(c >>> TC_SHIFT ) >= maxTotal )
1723
- return true ;
1724
- long nc = ((c + TC_UNIT ) & TC_MASK ) | (c & ~TC_MASK );
1725
- if (compareAndSetCtl (c , nc ))
1726
- return !createWorker ();
1727
- }
1716
+ long c ;
1717
+ return (int )((c = ctl ) >> RC_SHIFT ) >= 0 && ((int )c & ~UNSIGNALLED ) == 0 ;
1728
1718
}
1729
1719
1730
1720
/**
@@ -1787,7 +1777,7 @@ private int tryCompensate(long c) {
1787
1777
}
1788
1778
return -1 ; // retry
1789
1779
}
1790
- else if (active - minActive > 1 ) { // reduce parallelism
1780
+ else if (active > minActive ) { // reduce parallelism
1791
1781
long nc = ((RC_MASK & (c - RC_UNIT )) | (~RC_MASK & c ));
1792
1782
return compareAndSetCtl (c , nc ) ? UNCOMPENSATE : -1 ;
1793
1783
}
@@ -2544,7 +2534,9 @@ private ForkJoinPool(byte forCommonPoolOnly) {
2544
2534
int p = this .mode = Math .min (Math .max (parallelism , 0 ), MAX_CAP );
2545
2535
int size = 1 << (33 - Integer .numberOfLeadingZeros (p > 0 ? p - 1 : 1 ));
2546
2536
this .factory = (fac != null ) ? fac :
2547
- new DefaultCommonPoolForkJoinWorkerThreadFactory ();
2537
+ (System .getSecurityManager () == null ?
2538
+ defaultForkJoinWorkerThreadFactory :
2539
+ new InnocuousForkJoinWorkerThreadFactory ());
2548
2540
this .ueh = handler ;
2549
2541
this .keepAlive = DEFAULT_KEEPALIVE ;
2550
2542
this .saturate = null ;
@@ -2684,10 +2676,7 @@ public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) {
2684
2676
ForkJoinTask <T > f =
2685
2677
new ForkJoinTask .AdaptedInterruptibleCallable <T >(t );
2686
2678
futures .add (f );
2687
- if (isSaturated ())
2688
- f .doExec ();
2689
- else
2690
- externalSubmit (f );
2679
+ externalSubmit (f );
2691
2680
}
2692
2681
for (int i = futures .size () - 1 ; i >= 0 ; --i )
2693
2682
((ForkJoinTask <?>)futures .get (i )).quietlyJoin ();
@@ -2710,10 +2699,7 @@ public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks,
2710
2699
ForkJoinTask <T > f =
2711
2700
new ForkJoinTask .AdaptedInterruptibleCallable <T >(t );
2712
2701
futures .add (f );
2713
- if (isSaturated ())
2714
- f .doExec ();
2715
- else
2716
- externalSubmit (f );
2702
+ externalSubmit (f );
2717
2703
}
2718
2704
long startTime = System .nanoTime (), ns = nanos ;
2719
2705
boolean timedOut = (ns < 0L );
@@ -2749,22 +2735,14 @@ static final class InvokeAnyRoot<E> extends ForkJoinTask<E> {
2749
2735
final AtomicInteger count ; // in case all throw
2750
2736
InvokeAnyRoot (int n ) { count = new AtomicInteger (n ); }
2751
2737
final void tryComplete (Callable <E > c ) { // called by InvokeAnyTasks
2752
- Throwable ex = null ;
2753
- boolean failed = false ;
2754
- if (c != null ) { // raciness OK
2755
- if (isCancelled ())
2756
- failed = true ;
2757
- else if (!isDone ()) {
2758
- try {
2759
- complete (c .call ());
2760
- } catch (Throwable tx ) {
2761
- ex = tx ;
2762
- failed = true ;
2763
- }
2738
+ if (c != null && !isDone ()) { // raciness OK
2739
+ try {
2740
+ complete (c .call ());
2741
+ } catch (Throwable ex ) {
2742
+ if (count .getAndDecrement () <= 1 )
2743
+ trySetThrown (ex );
2764
2744
}
2765
2745
}
2766
- if (failed && count .getAndDecrement () <= 1 )
2767
- trySetThrown (ex != null ? ex : new CancellationException ());
2768
2746
}
2769
2747
public final boolean exec () { return false ; } // never forked
2770
2748
public final E getRawResult () { return result ; }
0 commit comments