Callback Based Selectors

David Lloyd david.lloyd at redhat.com
Wed Mar 28 14:45:04 UTC 2018


On Wed, Mar 28, 2018 at 9:32 AM, Alan Bateman <Alan.Bateman at oracle.com> wrote:
> I looked into the API/spec implications of changing this and it's not too
> bad. The recent changes to the Selector implementations means it has little
> impact as all the implementations in the JDK now handle new registrations
> and changes to interest ops as part of the selection operation.
>
> In terms of compatibility then the only concern is code that synchronizes on
> the key set or selected-key to prevent keys being added by concurrent
> registrations or selection operations (e.g. code processes and removes all
> keys from the selected-key set and then asserts that the selected-key is
> empty). If this is an issue then registration could continue to synchronize,
> and the selection operation could limit is synchronization to when it needs
> to add to the selected-key set. The other aspect to compatibility is other
> SelectorProvider (and Selector) implementations although I've never come
> across complete implementations.
>
> Here's prototype spec changes to see how it might look:
> http://cr.openjdk.java.net/~alanb/concurrent-selector-register/webrev/
>
> New registrations would be register+wakeup, changes to interest ops can be
> interestOps+wakeup.

Great!  This seems much cleaner to me; I hope it works out.

I wonder, since we're moving these APIs to be more
concurrency-friendly, if you could consider an additional small
enhancement to add a couple atomic operations to SelectionKey for
interest ops like getAndSetBits/getAndClearBits?  They would only have
to be atomic with respect to each other, so they could be emulated
with locks for compatibility.  At present, coordinating adding
interest ops across threads means adding a lock, which is super
heavyweight considering that interest ops are backed a volatile int
field in most if not all cases.

I could work up a patch if you want; since you've been so busy in the
NIO area lately I've been hesitant to try anything in that area, at
least until things settle down a little.
-- 
- DML


More information about the nio-dev mailing list