Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions src/java.base/share/classes/sun/nio/ch/DatagramChannelImpl.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2001, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2001, 2026, 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
Expand Down Expand Up @@ -1729,15 +1729,7 @@ private void implCloseBlockingMode() throws IOException {
registry.invalidateAll();

if (!tryClose()) {
long reader = readerThread;
long writer = writerThread;
if (reader != 0 || writer != 0) {
nd.preClose(fd);
if (reader != 0)
NativeThread.signal(reader);
if (writer != 0)
NativeThread.signal(writer);
}
nd.preClose(fd, readerThread, writerThread);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/java.base/share/classes/sun/nio/ch/NativeDispatcher.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2026, 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
Expand Down Expand Up @@ -73,7 +73,7 @@ abstract long writev(FileDescriptor fd, long address, int len)
// that's already closed. This is necessary on some operating systems
// (Solaris and Linux) to prevent fd recycling.
//
void preClose(FileDescriptor fd) throws IOException {
void preClose(FileDescriptor fd, long reader, long writer) throws IOException {
// Do nothing by default; this is only needed on Unix
}

Expand Down
10 changes: 2 additions & 8 deletions src/java.base/share/classes/sun/nio/ch/NioSocketImpl.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2026, 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
Expand Down Expand Up @@ -909,13 +909,7 @@ protected void close() throws IOException {
// then the socket is pre-closed and the thread(s) signalled. The
// last thread will close the file descriptor.
if (!tryClose()) {
nd.preClose(fd);
long reader = readerThread;
if (reader != 0)
NativeThread.signal(reader);
long writer = writerThread;
if (writer != 0)
NativeThread.signal(writer);
nd.preClose(fd, readerThread, writerThread);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2026, 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
Expand Down Expand Up @@ -581,11 +581,7 @@ private void implCloseBlockingMode() throws IOException {
assert state < ST_CLOSING;
state = ST_CLOSING;
if (!tryClose()) {
long th = thread;
if (th != 0) {
nd.preClose(fd);
NativeThread.signal(th);
}
nd.preClose(fd, thread, 0);
}
}
}
Expand Down
12 changes: 2 additions & 10 deletions src/java.base/share/classes/sun/nio/ch/SocketChannelImpl.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2026, 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
Expand Down Expand Up @@ -1028,15 +1028,7 @@ private void implCloseBlockingMode() throws IOException {
}

if (!tryClose()) {
long reader = readerThread;
long writer = writerThread;
if (reader != 0 || writer != 0) {
nd.preClose(fd);
if (reader != 0)
NativeThread.signal(reader);
if (writer != 0)
NativeThread.signal(writer);
}
nd.preClose(fd, readerThread, writerThread);
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/java.base/unix/classes/sun/nio/ch/DatagramDispatcher.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2001, 2026, 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
Expand Down Expand Up @@ -59,8 +59,9 @@ void close(FileDescriptor fd) throws IOException {
FileDispatcherImpl.close0(fd);
}

void preClose(FileDescriptor fd) throws IOException {
FileDispatcherImpl.preClose0(fd);
void preClose(FileDescriptor fd, long reader, long writer) throws IOException {
FileDispatcherImpl dispatcher = new FileDispatcherImpl();
dispatcher.preClose(fd, reader, writer);
Comment on lines +63 to +64
Copy link
Contributor

Choose a reason for hiding this comment

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

This should just delegate to FileDispatcherImpl as before:

Suggested change
FileDispatcherImpl dispatcher = new FileDispatcherImpl();
dispatcher.preClose(fd, reader, writer);
FileDispatcherImpl.preClose(fd, reader, writer);

}

void dup(FileDescriptor fd1, FileDescriptor fd2) throws IOException {
Expand Down
30 changes: 28 additions & 2 deletions src/java.base/unix/classes/sun/nio/ch/FileDispatcherImpl.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2026, 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
Expand Down Expand Up @@ -32,6 +32,7 @@
import jdk.internal.access.SharedSecrets;

class FileDispatcherImpl extends FileDispatcher {
private static final boolean SUPPORTS_PENDING_SIGNALS = NativeThread.supportPendingSignals();

static {
IOUtil.load();
Expand Down Expand Up @@ -104,8 +105,33 @@ void close(FileDescriptor fd) throws IOException {
fdAccess.close(fd);
}

void preClose(FileDescriptor fd) throws IOException {
/**
* Prepare the given file descriptor for closing. If a virtual thread is blocked
* on the file descriptor then it is unparked so that it stops polling. On Unix systems,
* if a platform thread is blocked on the file descriptor then the file descriptor is
* dup'ed to a special fd and the thread signalled so that the syscall fails with EINTR.
*/
Comment on lines +109 to +113
Copy link
Contributor

Choose a reason for hiding this comment

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

This comment is misleading and should be changed. There are no virtual threads in JDK 17. I suggest to drop the second sentence.

final void preClose(FileDescriptor fd, long reader, long writer) throws IOException {
if (reader != 0 || writer != 0) {
implPreClose(fd, reader, writer);
}
}

private void signalThreads(long reader, long writer) {
if (reader != 0)
NativeThread.signal(reader);
if (writer != 0)
NativeThread.signal(writer);
}

void implPreClose(FileDescriptor fd, long reader, long writer) throws IOException {
if (SUPPORTS_PENDING_SIGNALS) {
signalThreads(reader, writer);
}
preClose0(fd);
if (!SUPPORTS_PENDING_SIGNALS) {
signalThreads(reader, writer);
}
Comment on lines +114 to +134
Copy link
Contributor

Choose a reason for hiding this comment

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

It should be fine to collapse those as shared static methods, used by DatagramChannelImpl etc.

}

void dup(FileDescriptor fd1, FileDescriptor fd2) throws IOException {
Expand Down
13 changes: 12 additions & 1 deletion src/java.base/unix/classes/sun/nio/ch/NativeThread.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2002, 2026, 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
Expand Down Expand Up @@ -39,6 +39,17 @@

public class NativeThread {

/**
* Return true if the operating system supports pending signals. If a signal is sent
* to a thread but cannot be delivered immediately then it will be delivered when the
* thread is in the appropriate state.
*/
static boolean supportPendingSignals() {
return supportPendingSignals0();
}

private static native boolean supportPendingSignals0();

// Returns an opaque token representing the native thread underlying the
// invoking Java thread. On systems that do not require signalling, this
// method always returns -1.
Expand Down
8 changes: 2 additions & 6 deletions src/java.base/unix/classes/sun/nio/ch/SinkChannelImpl.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2026, 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
Expand Down Expand Up @@ -121,11 +121,7 @@ private void implCloseBlockingMode() throws IOException {
assert state < ST_CLOSING;
state = ST_CLOSING;
if (!tryClose()) {
long th = thread;
if (th != 0) {
nd.preClose(fd);
NativeThread.signal(th);
}
nd.preClose(fd, thread, 0);
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/java.base/unix/classes/sun/nio/ch/SocketDispatcher.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2026, 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
Expand Down Expand Up @@ -70,8 +70,9 @@ void close(FileDescriptor fd) throws IOException {
FileDispatcherImpl.close0(fd);
}

void preClose(FileDescriptor fd) throws IOException {
FileDispatcherImpl.preClose0(fd);
void preClose(FileDescriptor fd, long reader, long writer) throws IOException {
FileDispatcherImpl dispatcher = new FileDispatcherImpl();
dispatcher.preClose(fd, reader, writer);
Comment on lines +73 to +75
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
void preClose(FileDescriptor fd, long reader, long writer) throws IOException {
FileDispatcherImpl dispatcher = new FileDispatcherImpl();
dispatcher.preClose(fd, reader, writer);
void preClose(FileDescriptor fd, long reader, long writer) throws IOException {
FileDispatcherImpl.preClose(fd, reader, writer);

}

// -- Native methods --
Expand Down
8 changes: 2 additions & 6 deletions src/java.base/unix/classes/sun/nio/ch/SourceChannelImpl.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2026, 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
Expand Down Expand Up @@ -121,11 +121,7 @@ private void implCloseBlockingMode() throws IOException {
assert state < ST_CLOSING;
state = ST_CLOSING;
if (!tryClose()) {
long th = thread;
if (th != 0) {
nd.preClose(fd);
NativeThread.signal(th);
}
nd.preClose(fd, thread, 0);
}
}
}
Expand Down
11 changes: 10 additions & 1 deletion src/java.base/unix/native/libnio/ch/NativeThread.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2002, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2002, 2026, 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
Expand Down Expand Up @@ -89,3 +89,12 @@ Java_sun_nio_ch_NativeThread_signal(JNIEnv *env, jclass cl, jlong thread)
#endif
JNU_ThrowIOExceptionWithLastError(env, "Thread signal failed");
}

JNIEXPORT jboolean JNICALL
Java_sun_nio_ch_NativeThread_supportPendingSignals0(JNIEnv *env, jclass cl) {
#if defined(_AIX)
return JNI_TRUE;
#else
return JNI_FALSE;
#endif
}