Skip to content

Commit 5e18016

Browse files
committed
Backport 0de2cddf3a7be23f67af93972875af1235f3107e
Signed-off-by: Shruthi <[email protected]>
1 parent 3daec25 commit 5e18016

File tree

12 files changed

+81
-120
lines changed

12 files changed

+81
-120
lines changed

src/java.base/share/classes/sun/nio/ch/DatagramChannelImpl.java

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2001, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2001, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -1864,22 +1864,7 @@ private void implCloseBlockingMode() throws IOException {
18641864
registry.invalidateAll();
18651865

18661866
if (!tryClose()) {
1867-
long reader = readerThread;
1868-
long writer = writerThread;
1869-
if (reader != 0 || writer != 0) {
1870-
if (NativeThread.isVirtualThread(reader)
1871-
|| NativeThread.isVirtualThread(writer)) {
1872-
Poller.stopPoll(fdVal);
1873-
}
1874-
if (NativeThread.isNativeThread(reader)
1875-
|| NativeThread.isNativeThread(writer)) {
1876-
nd.preClose(fd);
1877-
if (NativeThread.isNativeThread(reader))
1878-
NativeThread.signal(reader);
1879-
if (NativeThread.isNativeThread(writer))
1880-
NativeThread.signal(writer);
1881-
}
1882-
}
1867+
nd.preClose(fd, readerThread, writerThread);
18831868
}
18841869
}
18851870
}

src/java.base/share/classes/sun/nio/ch/NativeDispatcher.java

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -27,13 +27,16 @@
2727

2828
import java.io.FileDescriptor;
2929
import java.io.IOException;
30+
import jdk.internal.access.JavaIOFileDescriptorAccess;
31+
import jdk.internal.access.SharedSecrets;
3032

3133
/**
3234
* Allows different platforms to call different native methods
3335
* for read and write operations.
3436
*/
3537

3638
abstract class NativeDispatcher {
39+
private static final JavaIOFileDescriptorAccess JIOFDA = SharedSecrets.getJavaIOFileDescriptorAccess();
3740

3841
abstract int read(FileDescriptor fd, long address, int len)
3942
throws IOException;
@@ -69,11 +72,28 @@ abstract long writev(FileDescriptor fd, long address, int len)
6972

7073
abstract void close(FileDescriptor fd) throws IOException;
7174

72-
// Prepare the given fd for closing by duping it to a known internal fd
73-
// that's already closed. This is necessary on some operating systems
74-
// (Solaris and Linux) to prevent fd recycling.
75-
//
76-
void preClose(FileDescriptor fd) throws IOException {
75+
/**
76+
* Prepare the given file descriptor for closing. If a virtual thread is blocked
77+
* on the file descriptor then it is unparked so that it stops polling. On Unix systems,
78+
* if a platform thread is blocked on the file descriptor then the file descriptor is
79+
* dup'ed to a special fd and the thread signalled so that the syscall fails with EINTR.
80+
*/
81+
final void preClose(FileDescriptor fd, long reader, long writer) throws IOException {
82+
if (NativeThread.isVirtualThread(reader) || NativeThread.isVirtualThread(writer)) {
83+
int fdVal = JIOFDA.get(fd);
84+
Poller.stopPoll(fdVal);
85+
}
86+
if (NativeThread.isNativeThread(reader) || NativeThread.isNativeThread(writer)) {
87+
implPreClose(fd, reader, writer);
88+
}
89+
}
90+
91+
/**
92+
* This method does nothing by default. On Unix systems the file descriptor is dup'ed
93+
* to a special fd and native threads signalled.
94+
*/
95+
96+
void implPreClose(FileDescriptor fd, long reader, long writer) throws IOException {
7797
// Do nothing by default; this is only needed on Unix
7898
}
7999

src/java.base/share/classes/sun/nio/ch/NioSocketImpl.java

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -900,20 +900,7 @@ protected void close() throws IOException {
900900
// then the socket is pre-closed and the thread(s) signalled. The
901901
// last thread will close the file descriptor.
902902
if (!tryClose()) {
903-
long reader = readerThread;
904-
long writer = writerThread;
905-
if (NativeThread.isVirtualThread(reader)
906-
|| NativeThread.isVirtualThread(writer)) {
907-
Poller.stopPoll(fdVal(fd));
908-
}
909-
if (NativeThread.isNativeThread(reader)
910-
|| NativeThread.isNativeThread(writer)) {
911-
nd.preClose(fd);
912-
if (NativeThread.isNativeThread(reader))
913-
NativeThread.signal(reader);
914-
if (NativeThread.isNativeThread(writer))
915-
NativeThread.signal(writer);
916-
}
903+
nd.preClose(fd, readerThread, writerThread);
917904
}
918905
}
919906
}

src/java.base/share/classes/sun/nio/ch/ServerSocketChannelImpl.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -604,15 +604,7 @@ private void implCloseBlockingMode() throws IOException {
604604
assert state < ST_CLOSING;
605605
state = ST_CLOSING;
606606
if (!tryClose()) {
607-
long th = thread;
608-
if (th != 0) {
609-
if (NativeThread.isVirtualThread(th)) {
610-
Poller.stopPoll(fdVal);
611-
} else {
612-
nd.preClose(fd);
613-
NativeThread.signal(th);
614-
}
615-
}
607+
nd.preClose(fd, thread, 0);
616608
}
617609
}
618610
}

src/java.base/share/classes/sun/nio/ch/SocketChannelImpl.java

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -1050,20 +1050,8 @@ private void implCloseBlockingMode() throws IOException {
10501050
} catch (IOException ignore) { }
10511051
}
10521052

1053-
long reader = readerThread;
1054-
long writer = writerThread;
1055-
if (NativeThread.isVirtualThread(reader)
1056-
|| NativeThread.isVirtualThread(writer)) {
1057-
Poller.stopPoll(fdVal);
1058-
}
1059-
if (NativeThread.isNativeThread(reader)
1060-
|| NativeThread.isNativeThread(writer)) {
1061-
nd.preClose(fd);
1062-
if (NativeThread.isNativeThread(reader))
1063-
NativeThread.signal(reader);
1064-
if (NativeThread.isNativeThread(writer))
1065-
NativeThread.signal(writer);
1066-
}
1053+
// prepare file descriptor for closing
1054+
nd.preClose(fd, readerThread, writerThread);
10671055
}
10681056
}
10691057
}

src/java.base/unix/classes/sun/nio/ch/DatagramDispatcher.java

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2001, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2001, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -39,46 +39,43 @@ class DatagramDispatcher extends UnixDispatcher {
3939
IOUtil.load();
4040
}
4141

42+
@Override
4243
int read(FileDescriptor fd, long address, int len) throws IOException {
4344
return read0(fd, address, len);
4445
}
4546

47+
@Override
4648
long readv(FileDescriptor fd, long address, int len) throws IOException {
4749
return readv0(fd, address, len);
4850
}
4951

52+
@Override
5053
int write(FileDescriptor fd, long address, int len) throws IOException {
5154
return write0(fd, address, len);
5255
}
5356

57+
@Override
5458
long writev(FileDescriptor fd, long address, int len) throws IOException {
5559
return writev0(fd, address, len);
5660
}
5761

58-
void close(FileDescriptor fd) throws IOException {
59-
close0(fd);
60-
}
61-
62-
void preClose(FileDescriptor fd) throws IOException {
63-
preClose0(fd);
64-
}
65-
62+
@Override
6663
void dup(FileDescriptor fd1, FileDescriptor fd2) throws IOException {
6764
dup0(fd1, fd2);
6865
}
6966

70-
static native int read0(FileDescriptor fd, long address, int len)
67+
private static native int read0(FileDescriptor fd, long address, int len)
7168
throws IOException;
7269

73-
static native long readv0(FileDescriptor fd, long address, int len)
70+
private static native long readv0(FileDescriptor fd, long address, int len)
7471
throws IOException;
7572

76-
static native int write0(FileDescriptor fd, long address, int len)
73+
private static native int write0(FileDescriptor fd, long address, int len)
7774
throws IOException;
7875

79-
static native long writev0(FileDescriptor fd, long address, int len)
76+
private static native long writev0(FileDescriptor fd, long address, int len)
8077
throws IOException;
8178

82-
static native void dup0(FileDescriptor fd1, FileDescriptor fd2)
79+
private static native void dup0(FileDescriptor fd1, FileDescriptor fd2)
8380
throws IOException;
8481
}

src/java.base/unix/classes/sun/nio/ch/SinkChannelImpl.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -42,7 +42,7 @@ class SinkChannelImpl
4242
implements SelChImpl
4343
{
4444
// Used to make native read and write calls
45-
private static final NativeDispatcher nd = new FileDispatcherImpl();
45+
private static final NativeDispatcher nd = new SocketDispatcher();
4646

4747
// The file descriptor associated with this channel
4848
private final FileDescriptor fd;
@@ -152,15 +152,7 @@ private void implCloseBlockingMode() throws IOException {
152152
assert state < ST_CLOSING;
153153
state = ST_CLOSING;
154154
if (!tryClose()) {
155-
long th = thread;
156-
if (th != 0) {
157-
if (NativeThread.isVirtualThread(th)) {
158-
Poller.stopPoll(fdVal);
159-
} else {
160-
nd.preClose(fd);
161-
NativeThread.signal(th);
162-
}
163-
}
155+
nd.preClose(fd, thread, 0);
164156
}
165157
}
166158
}

src/java.base/unix/classes/sun/nio/ch/SocketDispatcher.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -43,6 +43,7 @@ class SocketDispatcher extends UnixDispatcher {
4343
* @throws sun.net.ConnectionResetException if connection reset is detected
4444
* @throws IOException if another I/O error occurs
4545
*/
46+
@Override
4647
int read(FileDescriptor fd, long address, int len) throws IOException {
4748
return read0(fd, address, len);
4849
}
@@ -54,26 +55,21 @@ int read(FileDescriptor fd, long address, int len) throws IOException {
5455
* @throws sun.net.ConnectionResetException if connection reset is detected
5556
* @throws IOException if another I/O error occurs
5657
*/
58+
@Override
5759
long readv(FileDescriptor fd, long address, int len) throws IOException {
5860
return readv0(fd, address, len);
5961
}
6062

63+
@Override
6164
int write(FileDescriptor fd, long address, int len) throws IOException {
6265
return write0(fd, address, len);
6366
}
6467

68+
@Override
6569
long writev(FileDescriptor fd, long address, int len) throws IOException {
6670
return writev0(fd, address, len);
6771
}
6872

69-
void close(FileDescriptor fd) throws IOException {
70-
close0(fd);
71-
}
72-
73-
void preClose(FileDescriptor fd) throws IOException {
74-
preClose0(fd);
75-
}
76-
7773
// -- Native methods --
7874

7975
private static native int read0(FileDescriptor fd, long address, int len)
@@ -82,10 +78,10 @@ private static native int read0(FileDescriptor fd, long address, int len)
8278
private static native long readv0(FileDescriptor fd, long address, int len)
8379
throws IOException;
8480

85-
static native int write0(FileDescriptor fd, long address, int len)
81+
private static native int write0(FileDescriptor fd, long address, int len)
8682
throws IOException;
8783

88-
static native long writev0(FileDescriptor fd, long address, int len)
84+
private static native long writev0(FileDescriptor fd, long address, int len)
8985
throws IOException;
9086

9187
static {

src/java.base/unix/classes/sun/nio/ch/SourceChannelImpl.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -42,7 +42,7 @@ class SourceChannelImpl
4242
implements SelChImpl
4343
{
4444
// Used to make native read and write calls
45-
private static final NativeDispatcher nd = new FileDispatcherImpl();
45+
private static final NativeDispatcher nd = new SocketDispatcher();
4646

4747
// The file descriptor associated with this channel
4848
private final FileDescriptor fd;
@@ -152,15 +152,7 @@ private void implCloseBlockingMode() throws IOException {
152152
assert state < ST_CLOSING;
153153
state = ST_CLOSING;
154154
if (!tryClose()) {
155-
long th = thread;
156-
if (th != 0) {
157-
if (NativeThread.isVirtualThread(th)) {
158-
Poller.stopPoll(fdVal);
159-
} else {
160-
nd.preClose(fd);
161-
NativeThread.signal(th);
162-
}
163-
}
155+
nd.preClose(fd, thread, 0);
164156
}
165157
}
166158
}

src/java.base/unix/classes/sun/nio/ch/UnixDispatcher.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2022, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -30,17 +30,23 @@
3030

3131
abstract class UnixDispatcher extends NativeDispatcher {
3232

33+
@Override
3334
void close(FileDescriptor fd) throws IOException {
3435
close0(fd);
3536
}
3637

37-
void preClose(FileDescriptor fd) throws IOException {
38+
@Override
39+
void implPreClose(FileDescriptor fd, long reader, long writer) throws IOException {
3840
preClose0(fd);
41+
if (NativeThread.isNativeThread(reader))
42+
NativeThread.signal(reader);
43+
if (NativeThread.isNativeThread(writer))
44+
NativeThread.signal(writer);
3945
}
4046

41-
static native void close0(FileDescriptor fd) throws IOException;
47+
private static native void close0(FileDescriptor fd) throws IOException;
4248

43-
static native void preClose0(FileDescriptor fd) throws IOException;
49+
private static native void preClose0(FileDescriptor fd) throws IOException;
4450

4551
static native void init();
4652

0 commit comments

Comments
 (0)