mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-28 15:24:43 +02:00
8236246: SelectorProvider support for creating a DatagramChannel that is not interruptible
Reviewed-by: chegar
This commit is contained in:
parent
d1ad0eaf8f
commit
c6a4cea7a0
11 changed files with 350 additions and 109 deletions
|
@ -26,15 +26,18 @@
|
|||
package java.nio.channels.spi;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.net.ProtocolFamily;
|
||||
import java.nio.channels.*;
|
||||
import java.nio.channels.Channel;
|
||||
import java.nio.channels.DatagramChannel;
|
||||
import java.nio.channels.Pipe;
|
||||
import java.nio.channels.ServerSocketChannel;
|
||||
import java.nio.channels.SocketChannel;
|
||||
import java.security.AccessController;
|
||||
import java.security.PrivilegedAction;
|
||||
import java.util.Iterator;
|
||||
import java.util.ServiceLoader;
|
||||
import java.util.ServiceConfigurationError;
|
||||
import sun.security.action.GetPropertyAction;
|
||||
|
||||
|
||||
/**
|
||||
* Service-provider class for selectors and selectable channels.
|
||||
|
@ -68,9 +71,6 @@ import sun.security.action.GetPropertyAction;
|
|||
|
||||
public abstract class SelectorProvider {
|
||||
|
||||
private static final Object lock = new Object();
|
||||
private static SelectorProvider provider = null;
|
||||
|
||||
private static Void checkPermission() {
|
||||
SecurityManager sm = System.getSecurityManager();
|
||||
if (sm != null)
|
||||
|
@ -90,45 +90,53 @@ public abstract class SelectorProvider {
|
|||
this(checkPermission());
|
||||
}
|
||||
|
||||
private static boolean loadProviderFromProperty() {
|
||||
String cn = System.getProperty("java.nio.channels.spi.SelectorProvider");
|
||||
if (cn == null)
|
||||
return false;
|
||||
try {
|
||||
@SuppressWarnings("deprecation")
|
||||
Object tmp = Class.forName(cn, true,
|
||||
ClassLoader.getSystemClassLoader()).newInstance();
|
||||
provider = (SelectorProvider)tmp;
|
||||
return true;
|
||||
} catch (ClassNotFoundException x) {
|
||||
throw new ServiceConfigurationError(null, x);
|
||||
} catch (IllegalAccessException x) {
|
||||
throw new ServiceConfigurationError(null, x);
|
||||
} catch (InstantiationException x) {
|
||||
throw new ServiceConfigurationError(null, x);
|
||||
} catch (SecurityException x) {
|
||||
throw new ServiceConfigurationError(null, x);
|
||||
private static class Holder {
|
||||
static final SelectorProvider INSTANCE = provider();
|
||||
|
||||
static SelectorProvider provider() {
|
||||
PrivilegedAction<SelectorProvider> pa = () -> {
|
||||
SelectorProvider sp;
|
||||
if ((sp = loadProviderFromProperty()) != null)
|
||||
return sp;
|
||||
if ((sp = loadProviderAsService()) != null)
|
||||
return sp;
|
||||
return sun.nio.ch.DefaultSelectorProvider.get();
|
||||
};
|
||||
return AccessController.doPrivileged(pa);
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean loadProviderAsService() {
|
||||
|
||||
ServiceLoader<SelectorProvider> sl =
|
||||
ServiceLoader.load(SelectorProvider.class,
|
||||
ClassLoader.getSystemClassLoader());
|
||||
Iterator<SelectorProvider> i = sl.iterator();
|
||||
for (;;) {
|
||||
private static SelectorProvider loadProviderFromProperty() {
|
||||
String cn = System.getProperty("java.nio.channels.spi.SelectorProvider");
|
||||
if (cn == null)
|
||||
return null;
|
||||
try {
|
||||
if (!i.hasNext())
|
||||
return false;
|
||||
provider = i.next();
|
||||
return true;
|
||||
} catch (ServiceConfigurationError sce) {
|
||||
if (sce.getCause() instanceof SecurityException) {
|
||||
// Ignore the security exception, try the next provider
|
||||
continue;
|
||||
Class<?> clazz = Class.forName(cn, true, ClassLoader.getSystemClassLoader());
|
||||
return (SelectorProvider) clazz.getConstructor().newInstance();
|
||||
} catch (ClassNotFoundException |
|
||||
NoSuchMethodException |
|
||||
IllegalAccessException |
|
||||
InvocationTargetException |
|
||||
InstantiationException |
|
||||
SecurityException x) {
|
||||
throw new ServiceConfigurationError(null, x);
|
||||
}
|
||||
}
|
||||
|
||||
private static SelectorProvider loadProviderAsService() {
|
||||
ServiceLoader<SelectorProvider> sl =
|
||||
ServiceLoader.load(SelectorProvider.class,
|
||||
ClassLoader.getSystemClassLoader());
|
||||
Iterator<SelectorProvider> i = sl.iterator();
|
||||
for (;;) {
|
||||
try {
|
||||
return i.hasNext() ? i.next() : null;
|
||||
} catch (ServiceConfigurationError sce) {
|
||||
if (sce.getCause() instanceof SecurityException) {
|
||||
// Ignore the security exception, try the next provider
|
||||
continue;
|
||||
}
|
||||
throw sce;
|
||||
}
|
||||
throw sce;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -169,21 +177,7 @@ public abstract class SelectorProvider {
|
|||
* @return The system-wide default selector provider
|
||||
*/
|
||||
public static SelectorProvider provider() {
|
||||
synchronized (lock) {
|
||||
if (provider != null)
|
||||
return provider;
|
||||
return AccessController.doPrivileged(
|
||||
new PrivilegedAction<>() {
|
||||
public SelectorProvider run() {
|
||||
if (loadProviderFromProperty())
|
||||
return provider;
|
||||
if (loadProviderAsService())
|
||||
return provider;
|
||||
provider = sun.nio.ch.DefaultSelectorProvider.create();
|
||||
return provider;
|
||||
}
|
||||
});
|
||||
}
|
||||
return Holder.INSTANCE;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -317,8 +311,8 @@ public abstract class SelectorProvider {
|
|||
*
|
||||
* @since 1.5
|
||||
*/
|
||||
public Channel inheritedChannel() throws IOException {
|
||||
public Channel inheritedChannel() throws IOException {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -84,6 +84,9 @@ class DatagramChannelImpl
|
|||
// Used to make native read and write calls
|
||||
private static final NativeDispatcher nd = new DatagramDispatcher();
|
||||
|
||||
// true if interruptible (can be false to emulate legacy DatagramSocket)
|
||||
private final boolean interruptible;
|
||||
|
||||
// The protocol family of the socket
|
||||
private final ProtocolFamily family;
|
||||
|
||||
|
@ -158,13 +161,14 @@ class DatagramChannelImpl
|
|||
// -- End of fields protected by stateLock
|
||||
|
||||
|
||||
public DatagramChannelImpl(SelectorProvider sp) throws IOException {
|
||||
DatagramChannelImpl(SelectorProvider sp, boolean interruptible) throws IOException {
|
||||
this(sp, (Net.isIPv6Available()
|
||||
? StandardProtocolFamily.INET6
|
||||
: StandardProtocolFamily.INET));
|
||||
: StandardProtocolFamily.INET),
|
||||
interruptible);
|
||||
}
|
||||
|
||||
public DatagramChannelImpl(SelectorProvider sp, ProtocolFamily family)
|
||||
DatagramChannelImpl(SelectorProvider sp, ProtocolFamily family, boolean interruptible)
|
||||
throws IOException
|
||||
{
|
||||
super(sp);
|
||||
|
@ -184,6 +188,7 @@ class DatagramChannelImpl
|
|||
ResourceManager.beforeUdpCreate();
|
||||
boolean initialized = false;
|
||||
try {
|
||||
this.interruptible = interruptible;
|
||||
this.family = family;
|
||||
this.fd = fd = Net.socket(family, false);
|
||||
this.fdVal = IOUtil.fdVal(fd);
|
||||
|
@ -211,7 +216,7 @@ class DatagramChannelImpl
|
|||
this.cleaner = CleanerFactory.cleaner().register(this, releaser);
|
||||
}
|
||||
|
||||
public DatagramChannelImpl(SelectorProvider sp, FileDescriptor fd)
|
||||
DatagramChannelImpl(SelectorProvider sp, FileDescriptor fd)
|
||||
throws IOException
|
||||
{
|
||||
super(sp);
|
||||
|
@ -221,6 +226,7 @@ class DatagramChannelImpl
|
|||
ResourceManager.beforeUdpCreate();
|
||||
boolean initialized = false;
|
||||
try {
|
||||
this.interruptible = true;
|
||||
this.family = Net.isIPv6Available()
|
||||
? StandardProtocolFamily.INET6
|
||||
: StandardProtocolFamily.INET;
|
||||
|
@ -476,7 +482,7 @@ class DatagramChannelImpl
|
|||
private SocketAddress beginRead(boolean blocking, boolean mustBeConnected)
|
||||
throws IOException
|
||||
{
|
||||
if (blocking) {
|
||||
if (blocking && interruptible) {
|
||||
// set hook for Thread.interrupt
|
||||
begin();
|
||||
}
|
||||
|
@ -509,8 +515,12 @@ class DatagramChannelImpl
|
|||
tryFinishClose();
|
||||
}
|
||||
}
|
||||
// remove hook for Thread.interrupt
|
||||
end(completed);
|
||||
if (interruptible) {
|
||||
// remove hook for Thread.interrupt (may throw AsynchronousCloseException)
|
||||
end(completed);
|
||||
} else if (!completed && !isOpen()) {
|
||||
throw new AsynchronousCloseException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -951,7 +961,7 @@ class DatagramChannelImpl
|
|||
beginRead(blocking, true);
|
||||
n = IOUtil.read(fd, dsts, offset, length, nd);
|
||||
if (blocking) {
|
||||
while (IOStatus.okayToRetry(n) && isOpen()) {
|
||||
while (IOStatus.okayToRetry(n) && isOpen()) {
|
||||
park(Net.POLLIN);
|
||||
n = IOUtil.read(fd, dsts, offset, length, nd);
|
||||
}
|
||||
|
@ -978,7 +988,7 @@ class DatagramChannelImpl
|
|||
private SocketAddress beginWrite(boolean blocking, boolean mustBeConnected)
|
||||
throws IOException
|
||||
{
|
||||
if (blocking) {
|
||||
if (blocking && interruptible) {
|
||||
// set hook for Thread.interrupt
|
||||
begin();
|
||||
}
|
||||
|
@ -1011,8 +1021,13 @@ class DatagramChannelImpl
|
|||
tryFinishClose();
|
||||
}
|
||||
}
|
||||
// remove hook for Thread.interrupt
|
||||
end(completed);
|
||||
|
||||
if (interruptible) {
|
||||
// remove hook for Thread.interrupt (may throw AsynchronousCloseException)
|
||||
end(completed);
|
||||
} else if (!completed && !isOpen()) {
|
||||
throw new AsynchronousCloseException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2000, 2008, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2000, 2019, 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,37 +25,50 @@
|
|||
|
||||
package sun.nio.ch;
|
||||
|
||||
import java.io.FileDescriptor;
|
||||
import java.io.IOException;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.net.ProtocolFamily;
|
||||
import java.nio.channels.*;
|
||||
import java.nio.channels.spi.*;
|
||||
|
||||
import java.nio.channels.DatagramChannel;
|
||||
import java.nio.channels.Pipe;
|
||||
import java.nio.channels.ServerSocketChannel;
|
||||
import java.nio.channels.SocketChannel;
|
||||
import java.nio.channels.spi.AbstractSelector;
|
||||
import java.nio.channels.spi.SelectorProvider;
|
||||
|
||||
public abstract class SelectorProviderImpl
|
||||
extends SelectorProvider
|
||||
{
|
||||
|
||||
@Override
|
||||
public DatagramChannel openDatagramChannel() throws IOException {
|
||||
return new DatagramChannelImpl(this);
|
||||
return new DatagramChannelImpl(this, /*interruptible*/true);
|
||||
}
|
||||
|
||||
/**
|
||||
* SelectorProviderImpl specific method to create a DatagramChannel that
|
||||
* is not interruptible.
|
||||
*/
|
||||
public DatagramChannel openUninterruptibleDatagramChannel() throws IOException {
|
||||
return new DatagramChannelImpl(this, /*interruptible*/false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public DatagramChannel openDatagramChannel(ProtocolFamily family) throws IOException {
|
||||
return new DatagramChannelImpl(this, family);
|
||||
return new DatagramChannelImpl(this, family, /*interruptible*/true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Pipe openPipe() throws IOException {
|
||||
return new PipeImpl(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public abstract AbstractSelector openSelector() throws IOException;
|
||||
|
||||
@Override
|
||||
public ServerSocketChannel openServerSocketChannel() throws IOException {
|
||||
return new ServerSocketChannelImpl(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SocketChannel openSocketChannel() throws IOException {
|
||||
return new SocketChannelImpl(this);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue