src/share/classes/sun/nio/ch/SelectorImpl.java

Print this page




  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.nio.ch;
  27 
  28 import java.io.IOException;
  29 import java.nio.channels.*;
  30 import java.nio.channels.spi.*;
  31 import java.net.SocketException;
  32 import java.util.*;
  33 
  34 
  35 /**
  36  * Base Selector implementation class.
  37  */
  38 
  39 abstract class SelectorImpl
  40     extends AbstractSelector
  41 {
  42 
  43     // The set of keys with data ready for an operation
  44     protected Set<SelectionKey> selectedKeys;
  45 
  46     // The set of keys registered with this Selector
  47     protected HashSet<SelectionKey> keys;
  48 
  49     // Public views of the key sets
  50     private Set<SelectionKey> publicKeys;             // Immutable
  51     private Set<SelectionKey> publicSelectedKeys;     // Removal allowed, but not addition
  52 
  53     protected SelectorImpl(SelectorProvider sp) {
  54         super(sp);
  55         keys = new HashSet<SelectionKey>();
  56         selectedKeys = new HashSet<SelectionKey>();
  57         if (Util.atBugLevel("1.4")) {
  58             publicKeys = keys;
  59             publicSelectedKeys = selectedKeys;


 101         return select(0);
 102     }
 103 
 104     public int selectNow() throws IOException {
 105         return lockAndDoSelect(0);
 106     }
 107 
 108     public void implCloseSelector() throws IOException {
 109         wakeup();
 110         synchronized (this) {
 111             synchronized (publicKeys) {
 112                 synchronized (publicSelectedKeys) {
 113                     implClose();
 114                 }
 115             }
 116         }
 117     }
 118 
 119     protected abstract void implClose() throws IOException;
 120 
 121     void putEventOps(SelectionKeyImpl sk, int ops) { }
 122 
 123     protected final SelectionKey register(AbstractSelectableChannel ch,
 124                                           int ops,
 125                                           Object attachment)
 126     {
 127         if (!(ch instanceof SelChImpl))
 128             throw new IllegalSelectorException();
 129         SelectionKeyImpl k = new SelectionKeyImpl((SelChImpl)ch, this);
 130         k.attach(attachment);
 131         synchronized (publicKeys) {
 132             implRegister(k);
 133         }
 134         k.interestOps(ops);
 135         return k;
 136     }
 137 
 138     protected abstract void implRegister(SelectionKeyImpl ski);
 139 
 140     void processDeregisterQueue() throws IOException {
 141         // Precondition: Synchronized on this, keys, and selectedKeys




  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.nio.ch;
  27 
  28 import java.io.IOException;
  29 import java.nio.channels.*;
  30 import java.nio.channels.spi.*;
  31 import java.net.SocketException;
  32 import java.util.*;
  33 
  34 
  35 /**
  36  * Base Selector implementation class.
  37  */
  38 
  39 public abstract class SelectorImpl
  40     extends AbstractSelector
  41 {
  42 
  43     // The set of keys with data ready for an operation
  44     protected Set<SelectionKey> selectedKeys;
  45 
  46     // The set of keys registered with this Selector
  47     protected HashSet<SelectionKey> keys;
  48 
  49     // Public views of the key sets
  50     private Set<SelectionKey> publicKeys;             // Immutable
  51     private Set<SelectionKey> publicSelectedKeys;     // Removal allowed, but not addition
  52 
  53     protected SelectorImpl(SelectorProvider sp) {
  54         super(sp);
  55         keys = new HashSet<SelectionKey>();
  56         selectedKeys = new HashSet<SelectionKey>();
  57         if (Util.atBugLevel("1.4")) {
  58             publicKeys = keys;
  59             publicSelectedKeys = selectedKeys;


 101         return select(0);
 102     }
 103 
 104     public int selectNow() throws IOException {
 105         return lockAndDoSelect(0);
 106     }
 107 
 108     public void implCloseSelector() throws IOException {
 109         wakeup();
 110         synchronized (this) {
 111             synchronized (publicKeys) {
 112                 synchronized (publicSelectedKeys) {
 113                     implClose();
 114                 }
 115             }
 116         }
 117     }
 118 
 119     protected abstract void implClose() throws IOException;
 120 
 121     public void putEventOps(SelectionKeyImpl sk, int ops) { }
 122 
 123     protected final SelectionKey register(AbstractSelectableChannel ch,
 124                                           int ops,
 125                                           Object attachment)
 126     {
 127         if (!(ch instanceof SelChImpl))
 128             throw new IllegalSelectorException();
 129         SelectionKeyImpl k = new SelectionKeyImpl((SelChImpl)ch, this);
 130         k.attach(attachment);
 131         synchronized (publicKeys) {
 132             implRegister(k);
 133         }
 134         k.interestOps(ops);
 135         return k;
 136     }
 137 
 138     protected abstract void implRegister(SelectionKeyImpl ski);
 139 
 140     void processDeregisterQueue() throws IOException {
 141         // Precondition: Synchronized on this, keys, and selectedKeys