RFR: 8279339: (ch) Input/Output streams returned by Channels factory methods don't support concurrent read/write ops
Alan Bateman
alanb at openjdk.java.net
Tue Jan 4 09:56:26 UTC 2022
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.
-------------
Commit messages:
- Cleanup
- Merge
- Initial commit
Changes: https://git.openjdk.java.net/jdk/pull/6953/files
Webrev: https://webrevs.openjdk.java.net/?repo=jdk&pr=6953&range=00
Issue: https://bugs.openjdk.java.net/browse/JDK-8279339
Stats: 911 lines in 8 files changed: 755 ins; 116 del; 40 mod
Patch: https://git.openjdk.java.net/jdk/pull/6953.diff
Fetch: git fetch https://git.openjdk.java.net/jdk pull/6953/head:pull/6953
PR: https://git.openjdk.java.net/jdk/pull/6953
More information about the nio-dev
mailing list