8325949: Create an internal utility method for creating VarHandle instances

Reviewed-by: rriggs
This commit is contained in:
Per Minborg 2024-09-23 10:57:43 +00:00
parent 67448b0eb2
commit 384deda65f
31 changed files with 232 additions and 317 deletions

View file

@ -35,6 +35,8 @@
package java.util.concurrent;
import jdk.internal.invoke.MhUtil;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;
import java.util.concurrent.locks.LockSupport;
@ -3079,14 +3081,10 @@ public class CompletableFuture<T> implements Future<T>, CompletionStage<T> {
private static final VarHandle STACK;
private static final VarHandle NEXT;
static {
try {
MethodHandles.Lookup l = MethodHandles.lookup();
RESULT = l.findVarHandle(CompletableFuture.class, "result", Object.class);
STACK = l.findVarHandle(CompletableFuture.class, "stack", Completion.class);
NEXT = l.findVarHandle(Completion.class, "next", Completion.class);
} catch (ReflectiveOperationException e) {
throw new ExceptionInInitializerError(e);
}
MethodHandles.Lookup l = MethodHandles.lookup();
RESULT = MhUtil.findVarHandle(l, "result", Object.class);
STACK = MhUtil.findVarHandle(l, "stack", Completion.class);
NEXT = MhUtil.findVarHandle(l, Completion.class, "next", Completion.class);
// Reduce the risk of rare disastrous classloading in first call to
// LockSupport.park: https://bugs.openjdk.org/browse/JDK-8074773

View file

@ -36,6 +36,8 @@
package java.util.concurrent;
import jdk.internal.invoke.MhUtil;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;
import java.util.concurrent.locks.LockSupport;
@ -530,15 +532,11 @@ public class Exchanger<V> {
private static final VarHandle ENTRY;
private static final VarHandle AA;
static {
try {
MethodHandles.Lookup l = MethodHandles.lookup();
BOUND = l.findVarHandle(Exchanger.class, "bound", int.class);
MATCH = l.findVarHandle(Node.class, "match", Object.class);
ENTRY = l.findVarHandle(Slot.class, "entry", Node.class);
AA = MethodHandles.arrayElementVarHandle(Slot[].class);
} catch (ReflectiveOperationException e) {
throw new ExceptionInInitializerError(e);
}
MethodHandles.Lookup l = MethodHandles.lookup();
BOUND = MhUtil.findVarHandle(l, "bound", int.class);
MATCH = MhUtil.findVarHandle(l, Node.class, "match", Object.class);
ENTRY = MhUtil.findVarHandle(l, Slot.class, "entry", Node.class);
AA = MethodHandles.arrayElementVarHandle(Slot[].class);
}
}

View file

@ -35,6 +35,8 @@
package java.util.concurrent;
import jdk.internal.invoke.MhUtil;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;
import java.util.concurrent.locks.LockSupport;
@ -582,14 +584,10 @@ public class FutureTask<V> implements RunnableFuture<V> {
private static final VarHandle RUNNER;
private static final VarHandle WAITERS;
static {
try {
MethodHandles.Lookup l = MethodHandles.lookup();
STATE = l.findVarHandle(FutureTask.class, "state", int.class);
RUNNER = l.findVarHandle(FutureTask.class, "runner", Thread.class);
WAITERS = l.findVarHandle(FutureTask.class, "waiters", WaitNode.class);
} catch (ReflectiveOperationException e) {
throw new ExceptionInInitializerError(e);
}
MethodHandles.Lookup l = MethodHandles.lookup();
STATE = MhUtil.findVarHandle(l, "state", int.class);
RUNNER = MhUtil.findVarHandle(l, "runner", Thread.class);
WAITERS = MhUtil.findVarHandle(l, "waiters", WaitNode.class);
// Reduce the risk of rare disastrous classloading in first call to
// LockSupport.park: https://bugs.openjdk.org/browse/JDK-8074773

View file

@ -35,6 +35,8 @@
package java.util.concurrent;
import jdk.internal.invoke.MhUtil;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;
import java.util.concurrent.atomic.AtomicReference;
@ -1137,15 +1139,9 @@ public class Phaser {
}
// VarHandle mechanics
private static final VarHandle STATE;
private static final VarHandle STATE = MhUtil.findVarHandle(
MethodHandles.lookup(), "state", long.class);
static {
try {
MethodHandles.Lookup l = MethodHandles.lookup();
STATE = l.findVarHandle(Phaser.class, "state", long.class);
} catch (ReflectiveOperationException e) {
throw new ExceptionInInitializerError(e);
}
// Reduce the risk of rare disastrous classloading in first call to
// LockSupport.park: https://bugs.openjdk.org/browse/JDK-8074773
Class<?> ensureLoaded = LockSupport.class;

View file

@ -53,6 +53,7 @@ import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Consumer;
import java.util.function.Predicate;
import jdk.internal.access.SharedSecrets;
import jdk.internal.invoke.MhUtil;
import jdk.internal.util.ArraysSupport;
/**
@ -1093,15 +1094,8 @@ public class PriorityBlockingQueue<E> extends AbstractQueue<E>
}
// VarHandle mechanics
private static final VarHandle ALLOCATIONSPINLOCK;
static {
try {
MethodHandles.Lookup l = MethodHandles.lookup();
ALLOCATIONSPINLOCK = l.findVarHandle(PriorityBlockingQueue.class,
"allocationSpinLock",
int.class);
} catch (ReflectiveOperationException e) {
throw new ExceptionInInitializerError(e);
}
}
private static final VarHandle ALLOCATIONSPINLOCK =
MhUtil.findVarHandle(
MethodHandles.lookup(), "allocationSpinLock", int.class);
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -37,6 +37,7 @@ import java.util.function.Function;
import java.util.function.Supplier;
import jdk.internal.javac.PreviewFeature;
import jdk.internal.misc.ThreadFlock;
import jdk.internal.invoke.MhUtil;
/**
* A basic API for <em>structured concurrency</em>. {@code StructuredTaskScope} supports
@ -990,13 +991,9 @@ public class StructuredTaskScope<T> implements AutoCloseable {
private static final VarHandle FIRST_RESULT;
private static final VarHandle FIRST_EXCEPTION;
static {
try {
MethodHandles.Lookup l = MethodHandles.lookup();
FIRST_RESULT = l.findVarHandle(ShutdownOnSuccess.class, "firstResult", Object.class);
FIRST_EXCEPTION = l.findVarHandle(ShutdownOnSuccess.class, "firstException", Throwable.class);
} catch (Exception e) {
throw new ExceptionInInitializerError(e);
}
MethodHandles.Lookup l = MethodHandles.lookup();
FIRST_RESULT = MhUtil.findVarHandle(l, "firstResult", Object.class);
FIRST_EXCEPTION = MhUtil.findVarHandle(l, "firstException", Throwable.class);
}
private volatile Object firstResult;
private volatile Throwable firstException;
@ -1177,15 +1174,8 @@ public class StructuredTaskScope<T> implements AutoCloseable {
*/
@PreviewFeature(feature = PreviewFeature.Feature.STRUCTURED_CONCURRENCY)
public static final class ShutdownOnFailure extends StructuredTaskScope<Object> {
private static final VarHandle FIRST_EXCEPTION;
static {
try {
MethodHandles.Lookup l = MethodHandles.lookup();
FIRST_EXCEPTION = l.findVarHandle(ShutdownOnFailure.class, "firstException", Throwable.class);
} catch (Exception e) {
throw new ExceptionInInitializerError(e);
}
}
private static final VarHandle FIRST_EXCEPTION =
MhUtil.findVarHandle(MethodHandles.lookup(), "firstException", Throwable.class);
private volatile Throwable firstException;
/**

View file

@ -35,6 +35,8 @@
package java.util.concurrent;
import jdk.internal.invoke.MhUtil;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;
import java.util.ArrayList;
@ -1505,16 +1507,10 @@ public class SubmissionPublisher<T> implements Publisher<T>,
static final VarHandle QA;
static {
try {
MethodHandles.Lookup l = MethodHandles.lookup();
CTL = l.findVarHandle(BufferedSubscription.class, "ctl",
int.class);
DEMAND = l.findVarHandle(BufferedSubscription.class, "demand",
long.class);
QA = MethodHandles.arrayElementVarHandle(Object[].class);
} catch (ReflectiveOperationException e) {
throw new ExceptionInInitializerError(e);
}
MethodHandles.Lookup l = MethodHandles.lookup();
CTL = MhUtil.findVarHandle(l, "ctl", int.class);
DEMAND = MhUtil.findVarHandle(l, "demand", long.class);
QA = MethodHandles.arrayElementVarHandle(Object[].class);
// Reduce the risk of rare disastrous classloading in first call to
// LockSupport.park: https://bugs.openjdk.org/browse/JDK-8074773

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -38,6 +38,7 @@ import java.util.stream.Stream;
import static java.util.concurrent.TimeUnit.NANOSECONDS;
import jdk.internal.access.JavaLangAccess;
import jdk.internal.access.SharedSecrets;
import jdk.internal.invoke.MhUtil;
import jdk.internal.vm.ThreadContainer;
import jdk.internal.vm.ThreadContainers;
@ -48,15 +49,8 @@ import jdk.internal.vm.ThreadContainers;
class ThreadPerTaskExecutor extends ThreadContainer implements ExecutorService {
private static final JavaLangAccess JLA = SharedSecrets.getJavaLangAccess();
private static final Permission MODIFY_THREAD = new RuntimePermission("modifyThread");
private static final VarHandle STATE;
static {
try {
MethodHandles.Lookup l = MethodHandles.lookup();
STATE = l.findVarHandle(ThreadPerTaskExecutor.class, "state", int.class);
} catch (Exception e) {
throw new ExceptionInInitializerError(e);
}
}
private static final VarHandle STATE = MhUtil.findVarHandle(
MethodHandles.lookup(), "state", int.class);
private final ThreadFactory factory;
private final Set<Thread> threads = ConcurrentHashMap.newKeySet();
@ -506,14 +500,10 @@ class ThreadPerTaskExecutor extends ThreadContainer implements ExecutorService {
private static final VarHandle EXCEPTION;
private static final VarHandle EXCEPTION_COUNT;
static {
try {
MethodHandles.Lookup l = MethodHandles.lookup();
RESULT = l.findVarHandle(AnyResultHolder.class, "result", Object.class);
EXCEPTION = l.findVarHandle(AnyResultHolder.class, "exception", Throwable.class);
EXCEPTION_COUNT = l.findVarHandle(AnyResultHolder.class, "exceptionCount", int.class);
} catch (Exception e) {
throw new InternalError(e);
}
MethodHandles.Lookup l = MethodHandles.lookup();
RESULT = MhUtil.findVarHandle(l, "result", Object.class);
EXCEPTION = MhUtil.findVarHandle(l, "exception", Throwable.class);
EXCEPTION_COUNT = MhUtil.findVarHandle(l, "exceptionCount", int.class);
}
private static final Object NULL = new Object();

View file

@ -35,6 +35,8 @@
package java.util.concurrent.atomic;
import jdk.internal.invoke.MhUtil;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;
@ -50,15 +52,8 @@ import java.lang.invoke.VarHandle;
*/
public class AtomicBoolean implements java.io.Serializable {
private static final long serialVersionUID = 4654671469794556979L;
private static final VarHandle VALUE;
static {
try {
MethodHandles.Lookup l = MethodHandles.lookup();
VALUE = l.findVarHandle(AtomicBoolean.class, "value", int.class);
} catch (ReflectiveOperationException e) {
throw new ExceptionInInitializerError(e);
}
}
private static final VarHandle VALUE = MhUtil.findVarHandle(
MethodHandles.lookup(), "value", int.class);
private volatile int value;

View file

@ -35,6 +35,8 @@
package java.util.concurrent.atomic;
import jdk.internal.invoke.MhUtil;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;
@ -190,16 +192,8 @@ public class AtomicMarkableReference<V> {
}
// VarHandle mechanics
private static final VarHandle PAIR;
static {
try {
MethodHandles.Lookup l = MethodHandles.lookup();
PAIR = l.findVarHandle(AtomicMarkableReference.class, "pair",
Pair.class);
} catch (ReflectiveOperationException e) {
throw new ExceptionInInitializerError(e);
}
}
private static final VarHandle PAIR = MhUtil.findVarHandle(
MethodHandles.lookup(), "pair", Pair.class);
private boolean casPair(Pair<V> cmp, Pair<V> val) {
return PAIR.compareAndSet(this, cmp, val);

View file

@ -35,6 +35,8 @@
package java.util.concurrent.atomic;
import jdk.internal.invoke.MhUtil;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;
import java.util.function.BinaryOperator;
@ -50,15 +52,8 @@ import java.util.function.UnaryOperator;
*/
public class AtomicReference<V> implements java.io.Serializable {
private static final long serialVersionUID = -1848883965231344442L;
private static final VarHandle VALUE;
static {
try {
MethodHandles.Lookup l = MethodHandles.lookup();
VALUE = l.findVarHandle(AtomicReference.class, "value", Object.class);
} catch (ReflectiveOperationException e) {
throw new ExceptionInInitializerError(e);
}
}
private static final VarHandle VALUE = MhUtil.findVarHandle(
MethodHandles.lookup(), "value", Object.class);
@SuppressWarnings("serial") // Conditionally serializable
private volatile V value;

View file

@ -35,6 +35,8 @@
package java.util.concurrent.atomic;
import jdk.internal.invoke.MhUtil;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;
@ -190,16 +192,8 @@ public class AtomicStampedReference<V> {
}
// VarHandle mechanics
private static final VarHandle PAIR;
static {
try {
MethodHandles.Lookup l = MethodHandles.lookup();
PAIR = l.findVarHandle(AtomicStampedReference.class, "pair",
Pair.class);
} catch (ReflectiveOperationException e) {
throw new ExceptionInInitializerError(e);
}
}
private static final VarHandle PAIR = MhUtil.findVarHandle(
MethodHandles.lookup(), "pair", Pair.class);
private boolean casPair(Pair<V> cmp, Pair<V> val) {
return PAIR.compareAndSet(this, cmp, val);

View file

@ -35,6 +35,8 @@
package java.util.concurrent.atomic;
import jdk.internal.invoke.MhUtil;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;
import java.util.Arrays;
@ -138,15 +140,8 @@ abstract class Striped64 extends Number {
}
// VarHandle mechanics
private static final VarHandle VALUE;
static {
try {
MethodHandles.Lookup l = MethodHandles.lookup();
VALUE = l.findVarHandle(Cell.class, "value", long.class);
} catch (ReflectiveOperationException e) {
throw new ExceptionInInitializerError(e);
}
}
private static final VarHandle VALUE = MhUtil.findVarHandle(
MethodHandles.lookup(), "value", long.class);
}
/** Number of CPUS, to place bound on table size */
@ -381,14 +376,12 @@ abstract class Striped64 extends Number {
private static final VarHandle CELLSBUSY;
private static final VarHandle THREAD_PROBE;
static {
try {
MethodHandles.Lookup l1 = MethodHandles.lookup();
BASE = l1.findVarHandle(Striped64.class,
"base", long.class);
CELLSBUSY = l1.findVarHandle(Striped64.class,
"cellsBusy", int.class);
MethodHandles.Lookup l1 = MethodHandles.lookup();
BASE = MhUtil.findVarHandle(l1, "base", long.class);
CELLSBUSY = MhUtil.findVarHandle(l1, "cellsBusy", int.class);
@SuppressWarnings("removal")
MethodHandles.Lookup l2 = java.security.AccessController.doPrivileged(
MethodHandles.Lookup l2 = java.security.AccessController.doPrivileged(
new java.security.PrivilegedAction<>() {
public MethodHandles.Lookup run() {
try {
@ -397,11 +390,7 @@ abstract class Striped64 extends Number {
throw new ExceptionInInitializerError(e);
}
}});
THREAD_PROBE = l2.findVarHandle(Thread.class,
"threadLocalRandomProbe", int.class);
} catch (ReflectiveOperationException e) {
throw new ExceptionInInitializerError(e);
}
THREAD_PROBE = MhUtil.findVarHandle(l2, "threadLocalRandomProbe", int.class);
}
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -24,6 +24,8 @@
*/
package java.util.stream;
import jdk.internal.invoke.MhUtil;
import java.util.Objects;
import java.util.Spliterator;
import java.util.concurrent.CountedCompleter;
@ -370,15 +372,8 @@ final class ForEachOps {
private Node<T> node;
private ForEachOrderedTask<S, T> next;
private static final VarHandle NEXT;
static {
try {
MethodHandles.Lookup l = MethodHandles.lookup();
NEXT = l.findVarHandle(ForEachOrderedTask.class, "next", ForEachOrderedTask.class);
} catch (Exception e) {
throw new InternalError(e);
}
}
private static final VarHandle NEXT = MhUtil.findVarHandle(
MethodHandles.lookup(), "next", ForEachOrderedTask.class);
protected ForEachOrderedTask(PipelineHelper<T> helper,
Spliterator<S> spliterator,

View file

@ -24,6 +24,7 @@
*/
package java.util.stream;
import jdk.internal.invoke.MhUtil;
import jdk.internal.vm.annotation.ForceInline;
import java.lang.invoke.MethodHandles;
@ -453,16 +454,8 @@ final class GathererOp<T, A, R> extends ReferencePipeline<T, R> {
private Spliterator<T> spliterator;
private Hybrid next;
private static final VarHandle NEXT;
static {
try {
MethodHandles.Lookup l = MethodHandles.lookup();
NEXT = l.findVarHandle(Hybrid.class, "next", Hybrid.class);
} catch (Exception e) {
throw new InternalError(e);
}
}
private static final VarHandle NEXT = MhUtil.findVarHandle(
MethodHandles.lookup(), "next", Hybrid.class);
protected Hybrid(Spliterator<T> spliterator) {
super(null);