8338383: Implement JEP 491: Synchronize Virtual Threads without Pinning

Co-authored-by: Patricio Chilano Mateo <pchilanomate@openjdk.org>
Co-authored-by: Alan Bateman <alanb@openjdk.org>
Co-authored-by: Andrew Haley <aph@openjdk.org>
Co-authored-by: Fei Yang <fyang@openjdk.org>
Co-authored-by: Coleen Phillimore <coleenp@openjdk.org>
Co-authored-by: Richard Reingruber <rrich@openjdk.org>
Co-authored-by: Martin Doerr <mdoerr@openjdk.org>
Reviewed-by: aboldtch, dholmes, coleenp, fbredberg, dlong, sspitsyn
This commit is contained in:
Patricio Chilano Mateo 2024-11-12 15:23:48 +00:00
parent 8a2a75e56d
commit 78b80150e0
246 changed files with 8295 additions and 2755 deletions

View file

@ -26,7 +26,9 @@ package sun.nio.ch;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
@ -40,7 +42,7 @@ import sun.security.action.GetPropertyAction;
* Polls file descriptors. Virtual threads invoke the poll method to park
* until a given file descriptor is ready for I/O.
*/
abstract class Poller {
public abstract class Poller {
private static final Pollers POLLERS;
static {
try {
@ -142,6 +144,20 @@ abstract class Poller {
}
}
/**
* Parks the current thread until a Selector's file descriptor is ready.
* @param fdVal the Selector's file descriptor
* @param nanos the waiting time or 0 to wait indefinitely
*/
static void pollSelector(int fdVal, long nanos) throws IOException {
assert nanos >= 0L;
Poller poller = POLLERS.masterPoller();
if (poller == null) {
poller = POLLERS.readPoller(fdVal);
}
poller.poll(fdVal, nanos, () -> true);
}
/**
* If there is a thread polling the given file descriptor for the given event then
* the thread is unparked.
@ -258,6 +274,18 @@ abstract class Poller {
}
}
/**
* Returns the number I/O operations currently registered with this poller.
*/
public int registered() {
return map.size();
}
@Override
public String toString() {
return Objects.toIdentityString(this) + " [registered = " + registered() + "]";
}
/**
* The Pollers used for read and write events.
*/
@ -344,6 +372,13 @@ abstract class Poller {
}
}
/**
* Returns the master poller, or null if there is no master poller.
*/
Poller masterPoller() {
return masterPoller;
}
/**
* Returns the read poller for the given file descriptor.
*/
@ -360,6 +395,21 @@ abstract class Poller {
return writePollers[index];
}
/**
* Return the list of read pollers.
*/
List<Poller> readPollers() {
return List.of(readPollers);
}
/**
* Return the list of write pollers.
*/
List<Poller> writePollers() {
return List.of(writePollers);
}
/**
* Reads the given property name to get the poller count. If the property is
* set then the value must be a power of 2. Returns 1 if the property is not

View file

@ -25,6 +25,7 @@
package sun.security.ssl;
import java.lang.invoke.MethodHandles;
import java.net.Socket;
import java.security.*;
import java.security.cert.*;
@ -51,6 +52,15 @@ import sun.security.validator.*;
final class X509TrustManagerImpl extends X509ExtendedTrustManager
implements X509TrustManager {
static {
// eagerly initialize to avoid pinning virtual thread during TLS handshake
try {
MethodHandles.lookup().ensureInitialized(AnchorCertificates.class);
} catch (IllegalAccessException e) {
throw new ExceptionInInitializerError(e);
}
}
private final String validatorType;
/**