Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8201493

(se) Allow SelectableChannel.register to be invoked while selection operation is in progress

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P4 P4
    • 11
    • core-libs
    • None
    • behavioral
    • low
    • Hide
      It's possible, but not very likely, that there is existing code that synchronizes on the key-set and will fail if keys are added and removed while it holds the lock. So far, I've been unable to locate any examples where code synchronizes on this set.

      The API is pluggable. It's possible that there are alternative SelectorProvider implementations in the ecosystem with their own Selector implementations. I have never come across such an implementation, all implementations that I have come across are partial or just wrap the JDK implementation.

      Unlike the key-set, it is normal to synchronize on the selected-key set while processing selected/updated keys. Synchronizing on the selected-key set is not impacted by the changes proposed in this CSR.

      We do not propose, at this time, to introduce a system property or other means to revert to the previously specified behavior. This could be re-examined in the future if needed.
      Show
      It's possible, but not very likely, that there is existing code that synchronizes on the key-set and will fail if keys are added and removed while it holds the lock. So far, I've been unable to locate any examples where code synchronizes on this set. The API is pluggable. It's possible that there are alternative SelectorProvider implementations in the ecosystem with their own Selector implementations. I have never come across such an implementation, all implementations that I have come across are partial or just wrap the JDK implementation. Unlike the key-set, it is normal to synchronize on the selected-key set while processing selected/updated keys. Synchronizing on the selected-key set is not impacted by the changes proposed in this CSR. We do not propose, at this time, to introduce a system property or other means to revert to the previously specified behavior. This could be re-examined in the future if needed.
    • Java API
    • SE

      Summary

      Change java.nio.channels.Selector so that selection operations (select and selectNow methods) do not synchronize on the selector's key set. Change the java.nio.channels.SelectableChannel register and java.nio.channels.SelectionKey interestOps methods so that they may be called at any time.

      Problem

      Selection operations are specified to synchronize on the selector, its key set, and on its selected-key set (in that order). New registrations are specified to synchronize on the selector's key set and block if a selection operation is in progress. Changes to the interest set of channels registered with a selector may or may not block (it's implementation specific, the JDK implementations have historically not blocked if there is a selection operation in progress, at least not in recent releases).

      Registering new channels or changing the interest set of existing registrations is problematic and a long standing bugbear for high performance libraries as it requires using a "gate" object and additional synchronization in order to coordinate with threads doing selection operations.

      Solution

      The proposed solution is to re-specify the Selector key-set to be safe for use by concurrent threads so that selection operations and new registrations do not synchronize on the key-set. Selection operations will continue to specify that they synchronize on the selector and its selected-key set (also its cancelled-key set during the cancellation phases of selection).

      Additionally, the SelectionKey interestOps() and interestOps(int) methods are re-specified so that they can be invoked at any time without concern that they may block indefinitely. This part of the solution aligns the spec with the behavior of recent releases.

      If the proposal is accepted, it means that new registrations, changes to interest operations, or cancellations can be coordinated with concurrent selection operations in a relatively simple and consistent manner (i.e. register followed by wakeup, interestOps(ops) followed by wakeup, cancel followed by wakeup).

      Specification

      The specdiffs with the proposed changes are attached.

      The change is significant and therefore worthy of a release note.

            alanb Alan Bateman
            alanb Alan Bateman
            Brian Burkhalter
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: