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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * 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.Executor;
import java.util.concurrent.ThreadFactory; import java.util.concurrent.ThreadFactory;
import jdk.internal.misc.Unsafe; import jdk.internal.misc.Unsafe;
import jdk.internal.invoke.MhUtil;
import jdk.internal.vm.ContinuationSupport; import jdk.internal.vm.ContinuationSupport;
/** /**
@ -273,15 +274,9 @@ class ThreadBuilders {
* Base ThreadFactory implementation. * Base ThreadFactory implementation.
*/ */
private abstract static class BaseThreadFactory implements ThreadFactory { private abstract static class BaseThreadFactory implements ThreadFactory {
private static final VarHandle COUNT; private static final VarHandle COUNT = MhUtil.findVarHandle(
static { MethodHandles.lookup(), "count", long.class);
try {
MethodHandles.Lookup l = MethodHandles.lookup();
COUNT = l.findVarHandle(BaseThreadFactory.class, "count", long.class);
} catch (Exception e) {
throw new InternalError(e);
}
}
private final String name; private final String name;
private final int characteristics; private final int characteristics;
private final UncaughtExceptionHandler uhe; private final UncaughtExceptionHandler uhe;

View file

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

View file

@ -27,6 +27,7 @@ package java.net;
import jdk.internal.event.SocketReadEvent; import jdk.internal.event.SocketReadEvent;
import jdk.internal.event.SocketWriteEvent; import jdk.internal.event.SocketWriteEvent;
import jdk.internal.invoke.MhUtil;
import sun.security.util.SecurityConstants; import sun.security.util.SecurityConstants;
import java.io.InputStream; import java.io.InputStream;
@ -102,14 +103,10 @@ import java.util.Collections;
public class Socket implements java.io.Closeable { public class Socket implements java.io.Closeable {
private static final VarHandle STATE, IN, OUT; private static final VarHandle STATE, IN, OUT;
static { static {
try { MethodHandles.Lookup l = MethodHandles.lookup();
MethodHandles.Lookup l = MethodHandles.lookup(); STATE = MhUtil.findVarHandle(l, "state", int.class);
STATE = l.findVarHandle(Socket.class, "state", int.class); IN = MhUtil.findVarHandle(l, "in", InputStream.class);
IN = l.findVarHandle(Socket.class, "in", InputStream.class); OUT = MhUtil.findVarHandle(l, "out", OutputStream.class);
OUT = l.findVarHandle(Socket.class, "out", OutputStream.class);
} catch (Exception e) {
throw new InternalError(e);
}
} }
// the underlying SocketImpl, may be null, may be swapped when connecting // 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -25,6 +25,8 @@
package java.nio.channels; package java.nio.channels;
import jdk.internal.invoke.MhUtil;
import java.lang.invoke.MethodHandles; import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle; import java.lang.invoke.VarHandle;
@ -429,15 +431,9 @@ public abstract class SelectionKey {
// -- Attachments -- // -- Attachments --
private static final VarHandle ATTACHMENT; private static final VarHandle ATTACHMENT = MhUtil.findVarHandle(
static { MethodHandles.lookup(), "attachment", Object.class);
try {
MethodHandles.Lookup l = MethodHandles.lookup();
ATTACHMENT = l.findVarHandle(SelectionKey.class, "attachment", Object.class);
} catch (Exception e) {
throw new InternalError(e);
}
}
private volatile Object attachment; 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * 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.SelectionKey;
import java.nio.channels.Selector; import java.nio.channels.Selector;
import jdk.internal.invoke.MhUtil;
import sun.nio.ch.SelectionKeyImpl; import sun.nio.ch.SelectionKeyImpl;
import sun.nio.ch.SelectorImpl; import sun.nio.ch.SelectorImpl;
@ -46,15 +47,8 @@ import sun.nio.ch.SelectorImpl;
public abstract class AbstractSelectionKey public abstract class AbstractSelectionKey
extends SelectionKey extends SelectionKey
{ {
private static final VarHandle INVALID; private static final VarHandle INVALID = MhUtil.findVarHandle(
static { MethodHandles.lookup(), "invalid", boolean.class);
try {
MethodHandles.Lookup l = MethodHandles.lookup();
INVALID = l.findVarHandle(AbstractSelectionKey.class, "invalid", boolean.class);
} catch (Exception e) {
throw new InternalError(e);
}
}
/** /**
* Initializes a new instance of this 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.nio.channels.Selector;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import jdk.internal.invoke.MhUtil;
import sun.nio.ch.Interruptible; import sun.nio.ch.Interruptible;
import sun.nio.ch.SelectorImpl; import sun.nio.ch.SelectorImpl;
@ -72,15 +74,9 @@ import sun.nio.ch.SelectorImpl;
public abstract class AbstractSelector public abstract class AbstractSelector
extends Selector extends Selector
{ {
private static final VarHandle CLOSED; private static final VarHandle CLOSED = MhUtil.findVarHandle(
static { MethodHandles.lookup(), "closed", boolean.class);
try {
MethodHandles.Lookup l = MethodHandles.lookup();
CLOSED = l.findVarHandle(AbstractSelector.class, "closed", boolean.class);
} catch (Exception e) {
throw new InternalError(e);
}
}
private volatile boolean closed; private volatile boolean closed;
// The provider that created this selector // The provider that created this selector

View file

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

View file

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

View file

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

View file

@ -35,6 +35,8 @@
package java.util.concurrent; package java.util.concurrent;
import jdk.internal.invoke.MhUtil;
import java.lang.invoke.MethodHandles; import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle; import java.lang.invoke.VarHandle;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
@ -1137,15 +1139,9 @@ public class Phaser {
} }
// VarHandle mechanics // VarHandle mechanics
private static final VarHandle STATE; private static final VarHandle STATE = MhUtil.findVarHandle(
MethodHandles.lookup(), "state", long.class);
static { 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 // Reduce the risk of rare disastrous classloading in first call to
// LockSupport.park: https://bugs.openjdk.org/browse/JDK-8074773 // LockSupport.park: https://bugs.openjdk.org/browse/JDK-8074773
Class<?> ensureLoaded = LockSupport.class; 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.Consumer;
import java.util.function.Predicate; import java.util.function.Predicate;
import jdk.internal.access.SharedSecrets; import jdk.internal.access.SharedSecrets;
import jdk.internal.invoke.MhUtil;
import jdk.internal.util.ArraysSupport; import jdk.internal.util.ArraysSupport;
/** /**
@ -1093,15 +1094,8 @@ public class PriorityBlockingQueue<E> extends AbstractQueue<E>
} }
// VarHandle mechanics // VarHandle mechanics
private static final VarHandle ALLOCATIONSPINLOCK; private static final VarHandle ALLOCATIONSPINLOCK =
static { MhUtil.findVarHandle(
try { MethodHandles.lookup(), "allocationSpinLock", int.class);
MethodHandles.Lookup l = MethodHandles.lookup();
ALLOCATIONSPINLOCK = l.findVarHandle(PriorityBlockingQueue.class,
"allocationSpinLock",
int.class);
} catch (ReflectiveOperationException e) {
throw new ExceptionInInitializerError(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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * 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 java.util.function.Supplier;
import jdk.internal.javac.PreviewFeature; import jdk.internal.javac.PreviewFeature;
import jdk.internal.misc.ThreadFlock; import jdk.internal.misc.ThreadFlock;
import jdk.internal.invoke.MhUtil;
/** /**
* A basic API for <em>structured concurrency</em>. {@code StructuredTaskScope} supports * 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_RESULT;
private static final VarHandle FIRST_EXCEPTION; private static final VarHandle FIRST_EXCEPTION;
static { static {
try { MethodHandles.Lookup l = MethodHandles.lookup();
MethodHandles.Lookup l = MethodHandles.lookup(); FIRST_RESULT = MhUtil.findVarHandle(l, "firstResult", Object.class);
FIRST_RESULT = l.findVarHandle(ShutdownOnSuccess.class, "firstResult", Object.class); FIRST_EXCEPTION = MhUtil.findVarHandle(l, "firstException", Throwable.class);
FIRST_EXCEPTION = l.findVarHandle(ShutdownOnSuccess.class, "firstException", Throwable.class);
} catch (Exception e) {
throw new ExceptionInInitializerError(e);
}
} }
private volatile Object firstResult; private volatile Object firstResult;
private volatile Throwable firstException; private volatile Throwable firstException;
@ -1177,15 +1174,8 @@ public class StructuredTaskScope<T> implements AutoCloseable {
*/ */
@PreviewFeature(feature = PreviewFeature.Feature.STRUCTURED_CONCURRENCY) @PreviewFeature(feature = PreviewFeature.Feature.STRUCTURED_CONCURRENCY)
public static final class ShutdownOnFailure extends StructuredTaskScope<Object> { public static final class ShutdownOnFailure extends StructuredTaskScope<Object> {
private static final VarHandle FIRST_EXCEPTION; private static final VarHandle FIRST_EXCEPTION =
static { MhUtil.findVarHandle(MethodHandles.lookup(), "firstException", Throwable.class);
try {
MethodHandles.Lookup l = MethodHandles.lookup();
FIRST_EXCEPTION = l.findVarHandle(ShutdownOnFailure.class, "firstException", Throwable.class);
} catch (Exception e) {
throw new ExceptionInInitializerError(e);
}
}
private volatile Throwable firstException; private volatile Throwable firstException;
/** /**

View file

@ -35,6 +35,8 @@
package java.util.concurrent; package java.util.concurrent;
import jdk.internal.invoke.MhUtil;
import java.lang.invoke.MethodHandles; import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle; import java.lang.invoke.VarHandle;
import java.util.ArrayList; import java.util.ArrayList;
@ -1505,16 +1507,10 @@ public class SubmissionPublisher<T> implements Publisher<T>,
static final VarHandle QA; static final VarHandle QA;
static { static {
try { MethodHandles.Lookup l = MethodHandles.lookup();
MethodHandles.Lookup l = MethodHandles.lookup(); CTL = MhUtil.findVarHandle(l, "ctl", int.class);
CTL = l.findVarHandle(BufferedSubscription.class, "ctl", DEMAND = MhUtil.findVarHandle(l, "demand", long.class);
int.class); QA = MethodHandles.arrayElementVarHandle(Object[].class);
DEMAND = l.findVarHandle(BufferedSubscription.class, "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 // Reduce the risk of rare disastrous classloading in first call to
// LockSupport.park: https://bugs.openjdk.org/browse/JDK-8074773 // 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * 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 static java.util.concurrent.TimeUnit.NANOSECONDS;
import jdk.internal.access.JavaLangAccess; import jdk.internal.access.JavaLangAccess;
import jdk.internal.access.SharedSecrets; import jdk.internal.access.SharedSecrets;
import jdk.internal.invoke.MhUtil;
import jdk.internal.vm.ThreadContainer; import jdk.internal.vm.ThreadContainer;
import jdk.internal.vm.ThreadContainers; import jdk.internal.vm.ThreadContainers;
@ -48,15 +49,8 @@ import jdk.internal.vm.ThreadContainers;
class ThreadPerTaskExecutor extends ThreadContainer implements ExecutorService { class ThreadPerTaskExecutor extends ThreadContainer implements ExecutorService {
private static final JavaLangAccess JLA = SharedSecrets.getJavaLangAccess(); private static final JavaLangAccess JLA = SharedSecrets.getJavaLangAccess();
private static final Permission MODIFY_THREAD = new RuntimePermission("modifyThread"); private static final Permission MODIFY_THREAD = new RuntimePermission("modifyThread");
private static final VarHandle STATE; private static final VarHandle STATE = MhUtil.findVarHandle(
static { MethodHandles.lookup(), "state", int.class);
try {
MethodHandles.Lookup l = MethodHandles.lookup();
STATE = l.findVarHandle(ThreadPerTaskExecutor.class, "state", int.class);
} catch (Exception e) {
throw new ExceptionInInitializerError(e);
}
}
private final ThreadFactory factory; private final ThreadFactory factory;
private final Set<Thread> threads = ConcurrentHashMap.newKeySet(); 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;
private static final VarHandle EXCEPTION_COUNT; private static final VarHandle EXCEPTION_COUNT;
static { static {
try { MethodHandles.Lookup l = MethodHandles.lookup();
MethodHandles.Lookup l = MethodHandles.lookup(); RESULT = MhUtil.findVarHandle(l, "result", Object.class);
RESULT = l.findVarHandle(AnyResultHolder.class, "result", Object.class); EXCEPTION = MhUtil.findVarHandle(l, "exception", Throwable.class);
EXCEPTION = l.findVarHandle(AnyResultHolder.class, "exception", Throwable.class); EXCEPTION_COUNT = MhUtil.findVarHandle(l, "exceptionCount", int.class);
EXCEPTION_COUNT = l.findVarHandle(AnyResultHolder.class, "exceptionCount", int.class);
} catch (Exception e) {
throw new InternalError(e);
}
} }
private static final Object NULL = new Object(); private static final Object NULL = new Object();

View file

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

View file

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

View file

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

View file

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

View file

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

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

View file

@ -24,6 +24,7 @@
*/ */
package java.util.stream; package java.util.stream;
import jdk.internal.invoke.MhUtil;
import jdk.internal.vm.annotation.ForceInline; import jdk.internal.vm.annotation.ForceInline;
import java.lang.invoke.MethodHandles; import java.lang.invoke.MethodHandles;
@ -453,16 +454,8 @@ final class GathererOp<T, A, R> extends ReferencePipeline<T, R> {
private Spliterator<T> spliterator; private Spliterator<T> spliterator;
private Hybrid next; private Hybrid next;
private static final VarHandle NEXT; private static final VarHandle NEXT = MhUtil.findVarHandle(
MethodHandles.lookup(), "next", Hybrid.class);
static {
try {
MethodHandles.Lookup l = MethodHandles.lookup();
NEXT = l.findVarHandle(Hybrid.class, "next", Hybrid.class);
} catch (Exception e) {
throw new InternalError(e);
}
}
protected Hybrid(Spliterator<T> spliterator) { protected Hybrid(Spliterator<T> spliterator) {
super(null); 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * 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.MethodHandles;
import java.lang.invoke.VarHandle; import java.lang.invoke.VarHandle;
import jdk.internal.invoke.MhUtil;
import jdk.internal.vm.annotation.ForceInline; import jdk.internal.vm.annotation.ForceInline;
/** /**
@ -40,15 +41,8 @@ final class ConfinedSession extends MemorySessionImpl {
private int asyncReleaseCount = 0; private int asyncReleaseCount = 0;
static final VarHandle ASYNC_RELEASE_COUNT; static final VarHandle ASYNC_RELEASE_COUNT= MhUtil.findVarHandle(
MethodHandles.lookup(), "asyncReleaseCount", int.class);
static {
try {
ASYNC_RELEASE_COUNT = MethodHandles.lookup().findVarHandle(ConfinedSession.class, "asyncReleaseCount", int.class);
} catch (Throwable ex) {
throw new ExceptionInInitializerError(ex);
}
}
public ConfinedSession(Thread owner) { public ConfinedSession(Thread owner) {
super(owner, new ConfinedResourceList()); 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * 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.foreign.GlobalSession.HeapSession;
import jdk.internal.misc.ScopedMemoryAccess; import jdk.internal.misc.ScopedMemoryAccess;
import jdk.internal.invoke.MhUtil;
import jdk.internal.vm.annotation.ForceInline; import jdk.internal.vm.annotation.ForceInline;
/** /**
@ -57,7 +58,9 @@ public abstract sealed class MemorySessionImpl
static final int OPEN = 0; static final int OPEN = 0;
static final int CLOSED = -1; 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 int MAX_FORKS = Integer.MAX_VALUE;
static final ScopedMemoryAccess.ScopedAccessError ALREADY_CLOSED = new ScopedMemoryAccess.ScopedAccessError(MemorySessionImpl::alreadyClosed); static final ScopedMemoryAccess.ScopedAccessError ALREADY_CLOSED = new ScopedMemoryAccess.ScopedAccessError(MemorySessionImpl::alreadyClosed);
@ -69,14 +72,6 @@ public abstract sealed class MemorySessionImpl
final Thread owner; final Thread owner;
int state = OPEN; int state = OPEN;
static {
try {
STATE = MethodHandles.lookup().findVarHandle(MemorySessionImpl.class, "state", int.class);
} catch (Exception ex) {
throw new ExceptionInInitializerError(ex);
}
}
public Arena asArena() { public Arena asArena() {
return new ArenaImpl(this); 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * 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.MethodHandles;
import java.lang.invoke.VarHandle; import java.lang.invoke.VarHandle;
import jdk.internal.misc.ScopedMemoryAccess; import jdk.internal.misc.ScopedMemoryAccess;
import jdk.internal.invoke.MhUtil;
import jdk.internal.vm.annotation.ForceInline; import jdk.internal.vm.annotation.ForceInline;
/** /**
@ -91,15 +92,8 @@ sealed class SharedSession extends MemorySessionImpl permits ImplicitSession {
*/ */
static class SharedResourceList extends ResourceList { static class SharedResourceList extends ResourceList {
static final VarHandle FST; static final VarHandle FST = MhUtil.findVarHandle(
MethodHandles.lookup(), ResourceList.class, "fst", ResourceCleanup.class);
static {
try {
FST = MethodHandles.lookup().findVarHandle(ResourceList.class, "fst", ResourceCleanup.class);
} catch (Throwable ex) {
throw new ExceptionInInitializerError();
}
}
@Override @Override
void add(ResourceCleanup cleanup) { 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * 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.JavaLangInvokeAccess;
import jdk.internal.access.SharedSecrets; import jdk.internal.access.SharedSecrets;
import jdk.internal.invoke.MhUtil;
import sun.security.action.GetPropertyAction; import sun.security.action.GetPropertyAction;
import java.lang.foreign.AddressLayout; import java.lang.foreign.AddressLayout;
@ -38,7 +39,6 @@ import java.lang.invoke.MethodType;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.stream.Stream; import java.util.stream.Stream;
import jdk.internal.foreign.AbstractMemorySegmentImpl; import jdk.internal.foreign.AbstractMemorySegmentImpl;
@ -56,18 +56,11 @@ public class DowncallLinker {
private static final JavaLangInvokeAccess JLIA = SharedSecrets.getJavaLangInvokeAccess(); private static final JavaLangInvokeAccess JLIA = SharedSecrets.getJavaLangInvokeAccess();
private static final MethodHandle MH_INVOKE_INTERP_BINDINGS; private static final MethodHandle MH_INVOKE_INTERP_BINDINGS = MhUtil.findVirtual(
private static final MethodHandle EMPTY_OBJECT_ARRAY_HANDLE = MethodHandles.constant(Object[].class, new Object[0]); MethodHandles.lookup(), DowncallLinker.class, "invokeInterpBindings",
methodType(Object.class, SegmentAllocator.class, Object[].class, InvocationData.class));
static { private static final MethodHandle EMPTY_OBJECT_ARRAY_HANDLE = MethodHandles.constant(Object[].class, new Object[0]);
try {
MethodHandles.Lookup lookup = MethodHandles.lookup();
MH_INVOKE_INTERP_BINDINGS = lookup.findVirtual(DowncallLinker.class, "invokeInterpBindings",
methodType(Object.class, SegmentAllocator.class, Object[].class, InvocationData.class));
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}
private final ABIDescriptor abi; private final ABIDescriptor abi;
private final CallingSequence callingSequence; private final CallingSequence callingSequence;

View file

@ -27,6 +27,7 @@ package jdk.internal.foreign.layout;
import jdk.internal.foreign.LayoutPath; import jdk.internal.foreign.LayoutPath;
import jdk.internal.foreign.Utils; import jdk.internal.foreign.Utils;
import jdk.internal.invoke.MhUtil;
import java.lang.foreign.GroupLayout; import java.lang.foreign.GroupLayout;
import java.lang.foreign.MemoryLayout; import java.lang.foreign.MemoryLayout;
@ -157,15 +158,9 @@ public abstract sealed class AbstractLayout<L extends AbstractLayout<L> & Memory
public MethodHandle scaleHandle() { public MethodHandle scaleHandle() {
class Holder { class Holder {
static final MethodHandle MH_SCALE; static final MethodHandle MH_SCALE = MhUtil.findVirtual(
static { MethodHandles.lookup(), MemoryLayout.class, "scale",
try { MethodType.methodType(long.class, long.class, long.class));
MH_SCALE = MethodHandles.lookup().findVirtual(MemoryLayout.class, "scale",
MethodType.methodType(long.class, long.class, long.class));
} catch (ReflectiveOperationException e) {
throw new ExceptionInInitializerError(e);
}
}
} }
return Holder.MH_SCALE.bindTo(this); 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * 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 java.util.stream.Stream;
import jdk.internal.access.JavaLangAccess; import jdk.internal.access.JavaLangAccess;
import jdk.internal.access.SharedSecrets; import jdk.internal.access.SharedSecrets;
import jdk.internal.invoke.MhUtil;
import jdk.internal.vm.ScopedValueContainer; import jdk.internal.vm.ScopedValueContainer;
import jdk.internal.vm.ThreadContainer; import jdk.internal.vm.ThreadContainer;
import jdk.internal.vm.ThreadContainers; import jdk.internal.vm.ThreadContainers;
@ -84,13 +85,9 @@ public class ThreadFlock implements AutoCloseable {
private static final VarHandle THREAD_COUNT; private static final VarHandle THREAD_COUNT;
private static final VarHandle PERMIT; private static final VarHandle PERMIT;
static { static {
try { MethodHandles.Lookup l = MethodHandles.lookup();
MethodHandles.Lookup l = MethodHandles.lookup(); THREAD_COUNT = MhUtil.findVarHandle(l, "threadCount", int.class);
THREAD_COUNT = l.findVarHandle(ThreadFlock.class, "threadCount", int.class); PERMIT = MhUtil.findVarHandle(l, "permit", boolean.class);
PERMIT = l.findVarHandle(ThreadFlock.class, "permit", boolean.class);
} catch (Exception e) {
throw new InternalError(e);
}
} }
private final Set<Thread> threads = ConcurrentHashMap.newKeySet(); 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * 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.JavaLangInvokeAccess;
import jdk.internal.access.SharedSecrets; import jdk.internal.access.SharedSecrets;
import jdk.internal.invoke.MhUtil;
import jdk.internal.misc.VM; import jdk.internal.misc.VM;
import jdk.internal.vm.annotation.ForceInline; import jdk.internal.vm.annotation.ForceInline;
import jdk.internal.vm.annotation.Hidden; import jdk.internal.vm.annotation.Hidden;
@ -310,17 +311,10 @@ class DirectMethodHandleAccessor extends MethodAccessorImpl {
} }
} }
static final JavaLangInvokeAccess JLIA; static final JavaLangInvokeAccess JLIA = SharedSecrets.getJavaLangInvokeAccess();
static final MethodHandle NATIVE_ACCESSOR_INVOKE; static final MethodHandle NATIVE_ACCESSOR_INVOKE = MhUtil.findVirtual(
static { MethodHandles.lookup(), NativeAccessor.class, "invoke",
try { genericMethodType(1, true));
JLIA = SharedSecrets.getJavaLangInvokeAccess();
NATIVE_ACCESSOR_INVOKE = MethodHandles.lookup().findVirtual(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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * 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 java.util.stream.Stream;
import jdk.internal.access.JavaLangAccess; import jdk.internal.access.JavaLangAccess;
import jdk.internal.access.SharedSecrets; import jdk.internal.access.SharedSecrets;
import jdk.internal.invoke.MhUtil;
/** /**
* A "shared" thread container. A shared thread container doesn't have an owner * 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 CLOSED;
private static final VarHandle VIRTUAL_THREADS; private static final VarHandle VIRTUAL_THREADS;
static { static {
try { MethodHandles.Lookup l = MethodHandles.lookup();
MethodHandles.Lookup l = MethodHandles.lookup(); CLOSED = MhUtil.findVarHandle(l, "closed", boolean.class);
CLOSED = l.findVarHandle(SharedThreadContainer.class, VIRTUAL_THREADS = MhUtil.findVarHandle(l, "virtualThreads", Set.class);
"closed", boolean.class);
VIRTUAL_THREADS = l.findVarHandle(SharedThreadContainer.class,
"virtualThreads", Set.class);
} catch (Exception e) {
throw new ExceptionInInitializerError(e);
}
} }
// name of container, used by toString // name of container, used by toString

View file

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