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

Reviewed-by: dfuchs, alanb
  • Loading branch information
Michael-Mc-Mahon committed Feb 2, 2022
1 parent ce71e8b commit 87ab099
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

3 comments on commit 87ab099

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

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

@RealCLanger
Copy link
Contributor

Choose a reason for hiding this comment

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

/backport jdk17u-dev

@openjdk
Copy link

@openjdk openjdk bot commented on 87ab099 Aug 2, 2022

Choose a reason for hiding this comment

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

@RealCLanger the backport was successfully created on the branch RealCLanger-backport-87ab0994 in my personal fork of openjdk/jdk17u-dev. To create a pull request with this backport targeting openjdk/jdk17u-dev:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit 87ab0994 from the openjdk/jdk repository.

The commit being backported was authored by Michael McMahon on 2 Feb 2022 and was reviewed by Daniel Fuchs and Alan Bateman.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk17u-dev:

$ git fetch https://github.com/openjdk-bots/jdk17u-dev RealCLanger-backport-87ab0994:RealCLanger-backport-87ab0994
$ git checkout RealCLanger-backport-87ab0994
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk17u-dev RealCLanger-backport-87ab0994

Please sign in to comment.