8338142: (dc) DatagramChannelImpl.blockingReceive can use untimed-park when no timeout set

Reviewed-by: dfuchs
This commit is contained in:
Alan Bateman 2024-08-13 16:05:38 +00:00
parent 6af1d6ff21
commit 58b9570544

View file

@ -674,7 +674,6 @@ class DatagramChannelImpl
configureSocketNonBlocking(); configureSocketNonBlocking();
} else { } else {
configureSocketNonBlockingIfVirtualThread(); configureSocketNonBlockingIfVirtualThread();
nanos = Long.MAX_VALUE;
} }
// p.bufLength is the maximum size of the datagram that can be received // p.bufLength is the maximum size of the datagram that can be received
@ -689,7 +688,9 @@ class DatagramChannelImpl
SocketAddress remote = beginRead(true, false); SocketAddress remote = beginRead(true, false);
boolean connected = (remote != null); boolean connected = (remote != null);
do { do {
long remainingNanos = nanos - (System.nanoTime() - startNanos); long remainingNanos = (nanos > 0)
? nanos - (System.nanoTime() - startNanos)
: 0;
ByteBuffer dst = tryBlockingReceive(connected, bufLength, remainingNanos); ByteBuffer dst = tryBlockingReceive(connected, bufLength, remainingNanos);
// if datagram received then get sender and copy to DatagramPacket // if datagram received then get sender and copy to DatagramPacket
@ -756,11 +757,15 @@ class DatagramChannelImpl
Util.offerFirstTemporaryDirectBuffer(dst); Util.offerFirstTemporaryDirectBuffer(dst);
dst = null; dst = null;
} }
if (nanos > 0) {
long remainingNanos = nanos - (System.nanoTime() - startNanos); long remainingNanos = nanos - (System.nanoTime() - startNanos);
if (remainingNanos <= 0) { if (remainingNanos <= 0) {
throw new SocketTimeoutException("Receive timed out"); throw new SocketTimeoutException("Receive timed out");
} }
park(Net.POLLIN, remainingNanos); park(Net.POLLIN, remainingNanos);
} else {
park(Net.POLLIN);
}
// virtual thread needs to re-allocate temporary direct buffer after parking // virtual thread needs to re-allocate temporary direct buffer after parking
if (Thread.currentThread().isVirtual()) { if (Thread.currentThread().isVirtual()) {
dst = Util.getTemporaryDirectBuffer(len); dst = Util.getTemporaryDirectBuffer(len);