Skip to content

Commit cd77609

Browse files
author
Alan Bateman
committed
8294278: ForkJoinPool.getAndAddPoolIds should use Unsafe.staticFieldBase
Reviewed-by: burban, chegar, martin
1 parent eea1a8a commit cd77609

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

src/java.base/share/classes/java/lang/Thread.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
package java.lang;
2727

28+
import java.lang.reflect.Field;
2829
import java.security.AccessController;
2930
import java.security.AccessControlContext;
3031
import java.security.Permission;
@@ -1107,14 +1108,21 @@ protected Object clone() throws CloneNotSupportedException {
11071108
*/
11081109
private static class ThreadNumbering {
11091110
private static final Unsafe U;
1110-
private static final long NEXT;
1111+
private static final Object NEXT_BASE;
1112+
private static final long NEXT_OFFSET;
11111113
static {
11121114
U = Unsafe.getUnsafe();
1113-
NEXT = U.objectFieldOffset(ThreadNumbering.class, "next");
1115+
try {
1116+
Field nextField = ThreadNumbering.class.getDeclaredField("next");
1117+
NEXT_BASE = U.staticFieldBase(nextField);
1118+
NEXT_OFFSET = U.staticFieldOffset(nextField);
1119+
} catch (NoSuchFieldException e) {
1120+
throw new ExceptionInInitializerError(e);
1121+
}
11141122
}
11151123
private static volatile int next;
11161124
static int next() {
1117-
return U.getAndAddInt(ThreadNumbering.class, NEXT, 1);
1125+
return U.getAndAddInt(NEXT_BASE, NEXT_OFFSET, 1);
11181126
}
11191127
}
11201128

src/java.base/share/classes/java/util/concurrent/ForkJoinPool.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
package java.util.concurrent;
3737

3838
import java.lang.Thread.UncaughtExceptionHandler;
39+
import java.lang.reflect.Field;
3940
import java.security.AccessController;
4041
import java.security.AccessControlContext;
4142
import java.security.Permission;
@@ -1527,6 +1528,7 @@ final void setClearThreadLocals() {
15271528
private static final long RUNSTATE;
15281529
private static final long PARALLELISM;
15291530
private static final long THREADIDS;
1531+
private static final Object POOLIDS_BASE;
15301532
private static final long POOLIDS;
15311533

15321534
private boolean compareAndSetCtl(long c, long v) {
@@ -1545,7 +1547,7 @@ private long incrementThreadIds() {
15451547
return U.getAndAddLong(this, THREADIDS, 1L);
15461548
}
15471549
private static int getAndAddPoolIds(int x) {
1548-
return U.getAndAddInt(ForkJoinPool.class, POOLIDS, x);
1550+
return U.getAndAddInt(POOLIDS_BASE, POOLIDS, x);
15491551
}
15501552
private int getAndSetParallelism(int v) {
15511553
return U.getAndSetInt(this, PARALLELISM, v);
@@ -3763,7 +3765,9 @@ protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) {
37633765
U = Unsafe.getUnsafe();
37643766
Class<ForkJoinPool> klass = ForkJoinPool.class;
37653767
try {
3766-
POOLIDS = U.staticFieldOffset(klass.getDeclaredField("poolIds"));
3768+
Field poolIdsField = klass.getDeclaredField("poolIds");
3769+
POOLIDS_BASE = U.staticFieldBase(poolIdsField);
3770+
POOLIDS = U.staticFieldOffset(poolIdsField);
37673771
} catch (NoSuchFieldException e) {
37683772
throw new ExceptionInInitializerError(e);
37693773
}

0 commit comments

Comments
 (0)