Skip to content

Commit

Permalink
8280944: Enable Unix domain sockets in Windows Selector notification …
Browse files Browse the repository at this point in the history
…mechanism

Backport-of: 87ab0994ded3b535a160bb87b6540bd072042c44
  • Loading branch information
RealCLanger committed Aug 4, 2022
1 parent 1fe8777 commit b284851
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 13 deletions.
43 changes: 32 additions & 11 deletions src/java.base/windows/classes/sun/nio/ch/PipeImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.StandardSocketOptions;
import java.net.UnixDomainSocketAddress;
import java.nio.*;
import java.nio.channels.*;
import java.nio.file.Files;
Expand All @@ -44,6 +45,7 @@
import java.security.SecureRandom;
import java.util.Random;

import static java.net.StandardProtocolFamily.UNIX;

/**
* A simple Pipe implementation based on a socket connection.
Expand All @@ -67,12 +69,14 @@ private static class Initializer
{

private final SelectorProvider sp;
private final boolean preferUnixDomain;
private IOException ioe;
SourceChannelImpl source;
SinkChannelImpl sink;

private Initializer(SelectorProvider sp) {
private Initializer(SelectorProvider sp, boolean preferUnixDomain) {
this.sp = sp;
this.preferUnixDomain = preferUnixDomain;
}

@Override
Expand Down Expand Up @@ -120,7 +124,7 @@ public void run() {
// Bind ServerSocketChannel to a port on the loopback
// address
if (ssc == null || !ssc.isOpen()) {
ssc = createListener();
ssc = createListener(preferUnixDomain);
sa = ssc.getLocalAddress();
}

Expand Down Expand Up @@ -162,28 +166,34 @@ public void run() {
try {
if (ssc != null)
ssc.close();
if (sa instanceof UnixDomainSocketAddress uaddr) {
Files.deleteIfExists(uaddr.getPath());
}
} catch (IOException e2) {}
}
}
}
}

/**
* Creates a Pipe implementation that supports buffering.
* Creates a (TCP) Pipe implementation that supports buffering.
*/
PipeImpl(SelectorProvider sp) throws IOException {
this(sp, true);
this(sp, true, false);
}

/**
* Creates Pipe implementation that supports optionally buffering.
* Creates Pipe implementation that supports optionally buffering
* and is TCP by default, but if Unix domain is supported and
* preferAfUnix is true, then Unix domain sockets are used.
*
* @param preferAfUnix use Unix domain sockets if supported
*
* @implNote Uses a loopback connection. When buffering is
* disabled then it sets TCP_NODELAY on the sink channel.
* @param buffering if false set TCP_NODELAY on TCP sockets
*/
@SuppressWarnings("removal")
PipeImpl(SelectorProvider sp, boolean buffering) throws IOException {
Initializer initializer = new Initializer(sp);
PipeImpl(SelectorProvider sp, boolean preferAfUnix, boolean buffering) throws IOException {
Initializer initializer = new Initializer(sp, preferAfUnix);
try {
AccessController.doPrivileged(initializer);
SinkChannelImpl sink = initializer.sink;
Expand All @@ -205,8 +215,19 @@ public SinkChannelImpl sink() {
return sink;
}

private static ServerSocketChannel createListener() throws IOException {
ServerSocketChannel listener = ServerSocketChannel.open();
private static ServerSocketChannel createListener(boolean preferUnixDomain) throws IOException {
ServerSocketChannel listener = null;
if (preferUnixDomain && UnixDomainSockets.isSupported()) {
try {
listener = ServerSocketChannel.open(UNIX);
listener.bind(null);
return listener;
} catch (IOException | UnsupportedOperationException e) {
if (listener != null)
listener.close();
}
}
listener = ServerSocketChannel.open();
InetAddress lb = InetAddress.getLoopbackAddress();
listener.bind(new InetSocketAddress(lb, 0));
return listener;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class WEPollSelectorImpl extends SelectorImpl {

// wakeup support
try {
this.pipe = new PipeImpl(sp, /*buffering*/ false);
this.pipe = new PipeImpl(sp, /* AF_UNIX */ true, /*buffering*/ false);
} catch (IOException ioe) {
WEPoll.freePollArray(pollArrayAddress);
WEPoll.close(eph);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ private static final class MapEntry {
WindowsSelectorImpl(SelectorProvider sp) throws IOException {
super(sp);
pollWrapper = new PollArrayWrapper(INIT_CAP);
wakeupPipe = new PipeImpl(sp, false);
wakeupPipe = new PipeImpl(sp, /* AF_UNIX */ true, /*buffering*/ false);
wakeupSourceFd = ((SelChImpl)wakeupPipe.source()).getFDVal();
wakeupSinkFd = ((SelChImpl)wakeupPipe.sink()).getFDVal();
pollWrapper.addWakeupSocket(wakeupSourceFd, 0);
Expand Down

1 comment on commit b284851

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.