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

Brian Burkhalter bpb at openjdk.java.net
Wed Jan 5 20:10:11 UTC 2022


On Wed, 5 Jan 2022 20:05:53 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.
>
> Alan Bateman has updated the pull request with a new target base due to a merge or a rebase. The incremental webrev excludes the unrelated changes brought in by the merge/rebase. The pull request contains six additional commits since the last revision:
> 
>  - Fix typo in comment
>  - Remove redundant case
>  - Merge
>  - Cleanup
>  - Merge
>  - Initial commit

Marked as reviewed by bpb (Reviewer).

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

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


More information about the nio-dev mailing list