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

@ -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
@ -34,6 +34,7 @@ import java.util.Objects;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadFactory;
import jdk.internal.misc.Unsafe;
import jdk.internal.invoke.MhUtil;
import jdk.internal.vm.ContinuationSupport;
/**
@ -273,15 +274,9 @@ class ThreadBuilders {
* Base ThreadFactory implementation.
*/
private abstract static class BaseThreadFactory implements ThreadFactory {
private static final VarHandle COUNT;
static {
try {
MethodHandles.Lookup l = MethodHandles.lookup();
COUNT = l.findVarHandle(BaseThreadFactory.class, "count", long.class);
} catch (Exception e) {
throw new InternalError(e);
}
}
private static final VarHandle COUNT = MhUtil.findVarHandle(
MethodHandles.lookup(), "count", long.class);
private final String name;
private final int characteristics;
private final UncaughtExceptionHandler uhe;

View file

@ -25,6 +25,7 @@
package java.lang.invoke;
import jdk.internal.invoke.MhUtil;
import jdk.internal.vm.annotation.DontInline;
import jdk.internal.vm.annotation.ForceInline;
import jdk.internal.vm.annotation.Hidden;
@ -681,16 +682,9 @@ class Invokers {
}
private static class Lazy {
private static final MethodHandle MH_asSpreader;
static {
try {
MH_asSpreader = IMPL_LOOKUP.findVirtual(MethodHandle.class, "asSpreader",
private static final MethodHandle MH_asSpreader = MhUtil.findVirtual(
IMPL_LOOKUP, MethodHandle.class, "asSpreader",
MethodType.methodType(MethodHandle.class, Class.class, int.class));
} catch (ReflectiveOperationException ex) {
throw newInternalError(ex);
}
}
}
static {

View file

@ -27,6 +27,7 @@ package java.net;
import jdk.internal.event.SocketReadEvent;
import jdk.internal.event.SocketWriteEvent;
import jdk.internal.invoke.MhUtil;
import sun.security.util.SecurityConstants;
import java.io.InputStream;
@ -102,14 +103,10 @@ import java.util.Collections;
public class Socket implements java.io.Closeable {
private static final VarHandle STATE, IN, OUT;
static {
try {
MethodHandles.Lookup l = MethodHandles.lookup();
STATE = l.findVarHandle(Socket.class, "state", int.class);
IN = l.findVarHandle(Socket.class, "in", InputStream.class);
OUT = l.findVarHandle(Socket.class, "out", OutputStream.class);
} catch (Exception e) {
throw new InternalError(e);
}
STATE = MhUtil.findVarHandle(l, "state", int.class);
IN = MhUtil.findVarHandle(l, "in", InputStream.class);
OUT = MhUtil.findVarHandle(l, "out", OutputStream.class);
}
// the underlying SocketImpl, may be null, may be swapped when connecting

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 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
@ -25,6 +25,8 @@
package java.nio.channels;
import jdk.internal.invoke.MhUtil;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;
@ -429,15 +431,9 @@ public abstract class SelectionKey {
// -- Attachments --
private static final VarHandle ATTACHMENT;
static {
try {
MethodHandles.Lookup l = MethodHandles.lookup();
ATTACHMENT = l.findVarHandle(SelectionKey.class, "attachment", Object.class);
} catch (Exception e) {
throw new InternalError(e);
}
}
private static final VarHandle ATTACHMENT = MhUtil.findVarHandle(
MethodHandles.lookup(), "attachment", Object.class);
private volatile Object attachment;
/**

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 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
@ -30,6 +30,7 @@ import java.lang.invoke.VarHandle;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import jdk.internal.invoke.MhUtil;
import sun.nio.ch.SelectionKeyImpl;
import sun.nio.ch.SelectorImpl;
@ -46,15 +47,8 @@ import sun.nio.ch.SelectorImpl;
public abstract class AbstractSelectionKey
extends SelectionKey
{
private static final VarHandle INVALID;
static {
try {
MethodHandles.Lookup l = MethodHandles.lookup();
INVALID = l.findVarHandle(AbstractSelectionKey.class, "invalid", boolean.class);
} catch (Exception e) {
throw new InternalError(e);
}
}
private static final VarHandle INVALID = MhUtil.findVarHandle(
MethodHandles.lookup(), "invalid", boolean.class);
/**
* Initializes a new instance of this class.

View file

@ -32,6 +32,8 @@ import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.util.HashSet;
import java.util.Set;
import jdk.internal.invoke.MhUtil;
import sun.nio.ch.Interruptible;
import sun.nio.ch.SelectorImpl;
@ -72,15 +74,9 @@ import sun.nio.ch.SelectorImpl;
public abstract class AbstractSelector
extends Selector
{
private static final VarHandle CLOSED;
static {
try {
MethodHandles.Lookup l = MethodHandles.lookup();
CLOSED = l.findVarHandle(AbstractSelector.class, "closed", boolean.class);
} catch (Exception e) {
throw new InternalError(e);
}
}
private static final VarHandle CLOSED = MhUtil.findVarHandle(
MethodHandles.lookup(), "closed", boolean.class);
private volatile boolean closed;
// The provider that created this selector

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);
}
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);
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);
} catch (ReflectiveOperationException e) {
throw new ExceptionInInitializerError(e);
}
}
}

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);
}
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);
}
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);
CTL = MhUtil.findVarHandle(l, "ctl", int.class);
DEMAND = MhUtil.findVarHandle(l, "demand", long.class);
QA = MethodHandles.arrayElementVarHandle(Object[].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

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);
}
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,12 +376,10 @@ 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);
BASE = MhUtil.findVarHandle(l1, "base", long.class);
CELLSBUSY = MhUtil.findVarHandle(l1, "cellsBusy", int.class);
@SuppressWarnings("removal")
MethodHandles.Lookup l2 = java.security.AccessController.doPrivileged(
new java.security.PrivilegedAction<>() {
@ -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);

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, 2022, 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
@ -28,6 +28,7 @@ package jdk.internal.foreign;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;
import jdk.internal.invoke.MhUtil;
import jdk.internal.vm.annotation.ForceInline;
/**
@ -40,15 +41,8 @@ final class ConfinedSession extends MemorySessionImpl {
private int asyncReleaseCount = 0;
static final VarHandle ASYNC_RELEASE_COUNT;
static {
try {
ASYNC_RELEASE_COUNT = MethodHandles.lookup().findVarHandle(ConfinedSession.class, "asyncReleaseCount", int.class);
} catch (Throwable ex) {
throw new ExceptionInInitializerError(ex);
}
}
static final VarHandle ASYNC_RELEASE_COUNT= MhUtil.findVarHandle(
MethodHandles.lookup(), "asyncReleaseCount", int.class);
public ConfinedSession(Thread owner) {
super(owner, new ConfinedResourceList());

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
@ -36,6 +36,7 @@ import java.util.Objects;
import jdk.internal.foreign.GlobalSession.HeapSession;
import jdk.internal.misc.ScopedMemoryAccess;
import jdk.internal.invoke.MhUtil;
import jdk.internal.vm.annotation.ForceInline;
/**
@ -57,7 +58,9 @@ public abstract sealed class MemorySessionImpl
static final int OPEN = 0;
static final int CLOSED = -1;
static final VarHandle STATE;
static final VarHandle STATE = MhUtil.findVarHandle(
MethodHandles.lookup(), "state", int.class);
static final int MAX_FORKS = Integer.MAX_VALUE;
static final ScopedMemoryAccess.ScopedAccessError ALREADY_CLOSED = new ScopedMemoryAccess.ScopedAccessError(MemorySessionImpl::alreadyClosed);
@ -69,14 +72,6 @@ public abstract sealed class MemorySessionImpl
final Thread owner;
int state = OPEN;
static {
try {
STATE = MethodHandles.lookup().findVarHandle(MemorySessionImpl.class, "state", int.class);
} catch (Exception ex) {
throw new ExceptionInInitializerError(ex);
}
}
public Arena asArena() {
return new ArenaImpl(this);
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, 2022, 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
@ -28,6 +28,7 @@ package jdk.internal.foreign;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;
import jdk.internal.misc.ScopedMemoryAccess;
import jdk.internal.invoke.MhUtil;
import jdk.internal.vm.annotation.ForceInline;
/**
@ -91,15 +92,8 @@ sealed class SharedSession extends MemorySessionImpl permits ImplicitSession {
*/
static class SharedResourceList extends ResourceList {
static final VarHandle FST;
static {
try {
FST = MethodHandles.lookup().findVarHandle(ResourceList.class, "fst", ResourceCleanup.class);
} catch (Throwable ex) {
throw new ExceptionInInitializerError();
}
}
static final VarHandle FST = MhUtil.findVarHandle(
MethodHandles.lookup(), ResourceList.class, "fst", ResourceCleanup.class);
@Override
void add(ResourceCleanup cleanup) {

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 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
@ -26,6 +26,7 @@ package jdk.internal.foreign.abi;
import jdk.internal.access.JavaLangInvokeAccess;
import jdk.internal.access.SharedSecrets;
import jdk.internal.invoke.MhUtil;
import sun.security.action.GetPropertyAction;
import java.lang.foreign.AddressLayout;
@ -38,7 +39,6 @@ import java.lang.invoke.MethodType;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
import jdk.internal.foreign.AbstractMemorySegmentImpl;
@ -56,18 +56,11 @@ public class DowncallLinker {
private static final JavaLangInvokeAccess JLIA = SharedSecrets.getJavaLangInvokeAccess();
private static final MethodHandle MH_INVOKE_INTERP_BINDINGS;
private static final MethodHandle EMPTY_OBJECT_ARRAY_HANDLE = MethodHandles.constant(Object[].class, new Object[0]);
static {
try {
MethodHandles.Lookup lookup = MethodHandles.lookup();
MH_INVOKE_INTERP_BINDINGS = lookup.findVirtual(DowncallLinker.class, "invokeInterpBindings",
private static final MethodHandle MH_INVOKE_INTERP_BINDINGS = MhUtil.findVirtual(
MethodHandles.lookup(), DowncallLinker.class, "invokeInterpBindings",
methodType(Object.class, SegmentAllocator.class, Object[].class, InvocationData.class));
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}
private static final MethodHandle EMPTY_OBJECT_ARRAY_HANDLE = MethodHandles.constant(Object[].class, new Object[0]);
private final ABIDescriptor abi;
private final CallingSequence callingSequence;

View file

@ -27,6 +27,7 @@ package jdk.internal.foreign.layout;
import jdk.internal.foreign.LayoutPath;
import jdk.internal.foreign.Utils;
import jdk.internal.invoke.MhUtil;
import java.lang.foreign.GroupLayout;
import java.lang.foreign.MemoryLayout;
@ -157,15 +158,9 @@ public abstract sealed class AbstractLayout<L extends AbstractLayout<L> & Memory
public MethodHandle scaleHandle() {
class Holder {
static final MethodHandle MH_SCALE;
static {
try {
MH_SCALE = MethodHandles.lookup().findVirtual(MemoryLayout.class, "scale",
static final MethodHandle MH_SCALE = MhUtil.findVirtual(
MethodHandles.lookup(), MemoryLayout.class, "scale",
MethodType.methodType(long.class, long.class, long.class));
} catch (ReflectiveOperationException e) {
throw new ExceptionInInitializerError(e);
}
}
}
return Holder.MH_SCALE.bindTo(this);
}

View file

@ -0,0 +1,78 @@
/*
* Copyright (c) 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.internal.invoke;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.invoke.VarHandle;
/**
* Static factories for certain VarHandle/MethodHandle variants.
* <p>
* The methods will throw an {@link InternalError} if the lookup fails.
* <p>
* Here is an example of how one of these methods could be used:
* {@snippet lang=java
* static MethodHandle BAR_HANDLE =
* MhUtil.findVirtual(MethodHandles.lookup(),
* Foo.class,"bar",MethodType.methodType(int.class));
* }
*/
public final class MhUtil {
private MhUtil() {}
public static VarHandle findVarHandle(MethodHandles.Lookup lookup,
String name,
Class<?> type) {
return findVarHandle(lookup, lookup.lookupClass(), name, type);
}
public static VarHandle findVarHandle(MethodHandles.Lookup lookup,
Class<?> recv,
String name,
Class<?> type) {
try {
return lookup.findVarHandle(recv, name, type);
} catch (ReflectiveOperationException e) {
throw new InternalError(e);
}
}
public static MethodHandle findVirtual(MethodHandles.Lookup lookup,
Class<?> refc,
String name,
MethodType type) {
try {
return lookup.findVirtual(refc, name, type);
} catch (ReflectiveOperationException e) {
throw new InternalError(e);
}
}
}

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
@ -36,6 +36,7 @@ import java.util.concurrent.locks.LockSupport;
import java.util.stream.Stream;
import jdk.internal.access.JavaLangAccess;
import jdk.internal.access.SharedSecrets;
import jdk.internal.invoke.MhUtil;
import jdk.internal.vm.ScopedValueContainer;
import jdk.internal.vm.ThreadContainer;
import jdk.internal.vm.ThreadContainers;
@ -84,13 +85,9 @@ public class ThreadFlock implements AutoCloseable {
private static final VarHandle THREAD_COUNT;
private static final VarHandle PERMIT;
static {
try {
MethodHandles.Lookup l = MethodHandles.lookup();
THREAD_COUNT = l.findVarHandle(ThreadFlock.class, "threadCount", int.class);
PERMIT = l.findVarHandle(ThreadFlock.class, "permit", boolean.class);
} catch (Exception e) {
throw new InternalError(e);
}
THREAD_COUNT = MhUtil.findVarHandle(l, "threadCount", int.class);
PERMIT = MhUtil.findVarHandle(l, "permit", boolean.class);
}
private final Set<Thread> threads = ConcurrentHashMap.newKeySet();

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
@ -27,6 +27,7 @@ package jdk.internal.reflect;
import jdk.internal.access.JavaLangInvokeAccess;
import jdk.internal.access.SharedSecrets;
import jdk.internal.invoke.MhUtil;
import jdk.internal.misc.VM;
import jdk.internal.vm.annotation.ForceInline;
import jdk.internal.vm.annotation.Hidden;
@ -310,17 +311,10 @@ class DirectMethodHandleAccessor extends MethodAccessorImpl {
}
}
static final JavaLangInvokeAccess JLIA;
static final MethodHandle NATIVE_ACCESSOR_INVOKE;
static {
try {
JLIA = SharedSecrets.getJavaLangInvokeAccess();
NATIVE_ACCESSOR_INVOKE = MethodHandles.lookup().findVirtual(NativeAccessor.class, "invoke",
static final JavaLangInvokeAccess JLIA = SharedSecrets.getJavaLangInvokeAccess();
static final MethodHandle NATIVE_ACCESSOR_INVOKE = MhUtil.findVirtual(
MethodHandles.lookup(), NativeAccessor.class, "invoke",
genericMethodType(1, true));
} catch (NoSuchMethodException|IllegalAccessException e) {
throw new InternalError(e);
}
}
}
}

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
@ -31,6 +31,7 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Stream;
import jdk.internal.access.JavaLangAccess;
import jdk.internal.access.SharedSecrets;
import jdk.internal.invoke.MhUtil;
/**
* A "shared" thread container. A shared thread container doesn't have an owner
@ -41,15 +42,9 @@ public class SharedThreadContainer extends ThreadContainer implements AutoClosea
private static final VarHandle CLOSED;
private static final VarHandle VIRTUAL_THREADS;
static {
try {
MethodHandles.Lookup l = MethodHandles.lookup();
CLOSED = l.findVarHandle(SharedThreadContainer.class,
"closed", boolean.class);
VIRTUAL_THREADS = l.findVarHandle(SharedThreadContainer.class,
"virtualThreads", Set.class);
} catch (Exception e) {
throw new ExceptionInInitializerError(e);
}
CLOSED = MhUtil.findVarHandle(l, "closed", boolean.class);
VIRTUAL_THREADS = MhUtil.findVarHandle(l, "virtualThreads", Set.class);
}
// name of container, used by toString

View file

@ -68,13 +68,13 @@ import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Consumer;
import jdk.internal.access.JavaNioAccess;
import jdk.internal.access.SharedSecrets;
import jdk.internal.ref.CleanerFactory;
import jdk.internal.invoke.MhUtil;
import sun.net.ResourceManager;
import sun.net.ext.ExtendedSocketOptions;
import sun.net.util.IPAddressUtil;
@ -149,15 +149,8 @@ class DatagramChannelImpl
private InetSocketAddress initialLocalAddress;
// Socket adaptor, created lazily
private static final VarHandle SOCKET;
static {
try {
MethodHandles.Lookup l = MethodHandles.lookup();
SOCKET = l.findVarHandle(DatagramChannelImpl.class, "socket", DatagramSocket.class);
} catch (Exception e) {
throw new InternalError(e);
}
}
private static final VarHandle SOCKET = MhUtil.findVarHandle(
MethodHandles.lookup(), "socket", DatagramSocket.class);
private volatile DatagramSocket socket;
// Multicast support