Integrated: 8279339: (ch) Input/Output streams returned by Channels factory methods don't support concurrent read/write ops

Alan Bateman alanb at openjdk.java.net
Thu Jan 6 08:16:19 UTC 2022


On Tue, 4 Jan 2022 09:13:34 GMT, Alan Bateman <alanb at openjdk.org> wrote:

> If someone has a SocketChannel sc then one way to obtain input/output streams is via the Socket adaptor, i.e. sc.socket().getXXXStream. Another way is with Channels.newXXXStream(sc) factory methods. A long standing limitation is that the input/output streams returned by the Channels.newXXXStream methods must synchronize on the channel's blockingLock() to prevent the channel from being configured non-blocking. There is one "blocking lock" per SelectableChannel so problematic when the SelectableChannel is also a ByteChannel as it prevents a thread from writing while another is blocked in read (and vice versa).
> 
> The change proposed here is to have Channels.newXXXStream special case the SocketChannel implementation so that it returns the same input/output stream as the socket adaptors. These socket streams make use of non-exposed blocking read/write methods that coordinate with configureBlocking so don't need to synchronize on the blockingLock. The core part of the change is that the inner classes that were in SocketAdaptor are moved to be top-level SocketInputStream and SocketOutputStream classes.
> 
> There is some minor cleanup to ChannelInputStream and ChannelOutputStream in this patch. Once pattern matching for switch is permanent, and Markus Karg is done with transferTo, then we can see if we want to refactor that method and/or introduce a sub-class for FileChannel.
> 
> I've added a new test to exercise the the read/write methods, with focus on async close and interrupt. It includes tests that do concurrent read + write operations.

This pull request has now been integrated.

Changeset: 2dbb936d
Author:    Alan Bateman <alanb at openjdk.org>
URL:       https://git.openjdk.java.net/jdk/commit/2dbb936da91a875a5a88da5d4c0b4956b9c6368f
Stats:     907 lines in 8 files changed: 751 ins; 116 del; 40 mod

8279339: (ch) Input/Output streams returned by Channels factory methods don't support concurrent read/write ops

Reviewed-by: lancea, bpb

-------------

PR: https://git.openjdk.java.net/jdk/pull/6953


More information about the nio-dev mailing list