8236246: SelectorProvider support for creating a DatagramChannel that is not interruptible

Reviewed-by: chegar
This commit is contained in:
Alan Bateman 2019-12-20 09:28:57 +00:00
parent d1ad0eaf8f
commit c6a4cea7a0
11 changed files with 350 additions and 109 deletions

View file

@ -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();
}
}
}

View file

@ -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);
}