< prev index next >

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

Print this page
rev 49271 : [mq]: selector-cleanup


  61         keys = new HashSet<>();
  62         selectedKeys = new HashSet<>();
  63         publicKeys = Collections.unmodifiableSet(keys);
  64         publicSelectedKeys = Util.ungrowableSet(selectedKeys);
  65     }
  66 
  67     @Override
  68     public final Set<SelectionKey> keys() {
  69         if (!isOpen())
  70             throw new ClosedSelectorException();
  71         return publicKeys;
  72     }
  73 
  74     @Override
  75     public final Set<SelectionKey> selectedKeys() {
  76         if (!isOpen())
  77             throw new ClosedSelectorException();
  78         return publicSelectedKeys;
  79     }
  80 










  81     protected abstract int doSelect(long timeout) throws IOException;
  82 
  83     private int lockAndDoSelect(long timeout) throws IOException {
  84         synchronized (this) {
  85             if (!isOpen())
  86                 throw new ClosedSelectorException();
  87             synchronized (publicKeys) {
  88                 synchronized (publicSelectedKeys) {
  89                     return doSelect(timeout);
  90                 }
  91             }
  92         }
  93     }
  94 
  95     @Override
  96     public final int select(long timeout)
  97         throws IOException
  98     {
  99         if (timeout < 0)
 100             throw new IllegalArgumentException("Negative timeout");


 108 
 109     @Override
 110     public final int selectNow() throws IOException {
 111         return lockAndDoSelect(0);
 112     }
 113 
 114     @Override
 115     public final void implCloseSelector() throws IOException {
 116         wakeup();
 117         synchronized (this) {
 118             synchronized (publicKeys) {
 119                 synchronized (publicSelectedKeys) {
 120                     implClose();
 121                 }
 122             }
 123         }
 124     }
 125 
 126     protected abstract void implClose() throws IOException;
 127 
 128     public abstract void putEventOps(SelectionKeyImpl sk, int ops);
 129 
 130     @Override
 131     protected final SelectionKey register(AbstractSelectableChannel ch,
 132                                           int ops,
 133                                           Object attachment)
 134     {
 135         if (!(ch instanceof SelChImpl))
 136             throw new IllegalSelectorException();
 137         SelectionKeyImpl k = new SelectionKeyImpl((SelChImpl)ch, this);
 138         k.attach(attachment);
 139         synchronized (publicKeys) {
 140             implRegister(k);
 141         }
 142         k.interestOps(ops);
 143         return k;
 144     }
 145 
 146     protected abstract void implRegister(SelectionKeyImpl ski);
 147 
 148     protected abstract void implDereg(SelectionKeyImpl ski) throws IOException;
 149 
 150     protected final void processDeregisterQueue() throws IOException {
 151         // Precondition: Synchronized on this, keys, and selectedKeys
 152         Set<SelectionKey> cks = cancelledKeys();
 153         synchronized (cks) {
 154             if (!cks.isEmpty()) {
 155                 Iterator<SelectionKey> i = cks.iterator();
 156                 while (i.hasNext()) {
 157                     SelectionKeyImpl ski = (SelectionKeyImpl)i.next();
 158                     try {
 159                         implDereg(ski);
 160                     } catch (SocketException se) {
 161                         throw new IOException("Error deregistering key", se);
 162                     } finally {
 163                         i.remove();
 164                     }
 165                 }
 166             }
 167         }
 168     }





 169 }


  61         keys = new HashSet<>();
  62         selectedKeys = new HashSet<>();
  63         publicKeys = Collections.unmodifiableSet(keys);
  64         publicSelectedKeys = Util.ungrowableSet(selectedKeys);
  65     }
  66 
  67     @Override
  68     public final Set<SelectionKey> keys() {
  69         if (!isOpen())
  70             throw new ClosedSelectorException();
  71         return publicKeys;
  72     }
  73 
  74     @Override
  75     public final Set<SelectionKey> selectedKeys() {
  76         if (!isOpen())
  77             throw new ClosedSelectorException();
  78         return publicSelectedKeys;
  79     }
  80 
  81     /**
  82      * Returns the public view of the key sets
  83      */
  84     protected final Set<SelectionKey> nioKeys() {
  85         return publicKeys;
  86     }
  87     protected final Set<SelectionKey> nioSelectedKeys() {
  88         return publicSelectedKeys;
  89     }
  90 
  91     protected abstract int doSelect(long timeout) throws IOException;
  92 
  93     private int lockAndDoSelect(long timeout) throws IOException {
  94         synchronized (this) {
  95             if (!isOpen())
  96                 throw new ClosedSelectorException();
  97             synchronized (publicKeys) {
  98                 synchronized (publicSelectedKeys) {
  99                     return doSelect(timeout);
 100                 }
 101             }
 102         }
 103     }
 104 
 105     @Override
 106     public final int select(long timeout)
 107         throws IOException
 108     {
 109         if (timeout < 0)
 110             throw new IllegalArgumentException("Negative timeout");


 118 
 119     @Override
 120     public final int selectNow() throws IOException {
 121         return lockAndDoSelect(0);
 122     }
 123 
 124     @Override
 125     public final void implCloseSelector() throws IOException {
 126         wakeup();
 127         synchronized (this) {
 128             synchronized (publicKeys) {
 129                 synchronized (publicSelectedKeys) {
 130                     implClose();
 131                 }
 132             }
 133         }
 134     }
 135 
 136     protected abstract void implClose() throws IOException;
 137 


 138     @Override
 139     protected final SelectionKey register(AbstractSelectableChannel ch,
 140                                           int ops,
 141                                           Object attachment)
 142     {
 143         if (!(ch instanceof SelChImpl))
 144             throw new IllegalSelectorException();
 145         SelectionKeyImpl k = new SelectionKeyImpl((SelChImpl)ch, this);
 146         k.attach(attachment);
 147         synchronized (publicKeys) {
 148             implRegister(k);
 149         }
 150         k.interestOps(ops);
 151         return k;
 152     }
 153 
 154     protected abstract void implRegister(SelectionKeyImpl ski);
 155 
 156     protected abstract void implDereg(SelectionKeyImpl ski) throws IOException;
 157 
 158     protected final void processDeregisterQueue() throws IOException {
 159         // Precondition: Synchronized on this, keys, and selectedKeys
 160         Set<SelectionKey> cks = cancelledKeys();
 161         synchronized (cks) {
 162             if (!cks.isEmpty()) {
 163                 Iterator<SelectionKey> i = cks.iterator();
 164                 while (i.hasNext()) {
 165                     SelectionKeyImpl ski = (SelectionKeyImpl)i.next();
 166                     try {
 167                         implDereg(ski);
 168                     } catch (SocketException se) {
 169                         throw new IOException("Error deregistering key", se);
 170                     } finally {
 171                         i.remove();
 172                     }
 173                 }
 174             }
 175         }
 176     }
 177 
 178     /**
 179      * Invoked to change the key's interest set
 180      */
 181     public abstract void putEventOps(SelectionKeyImpl ski, int ops);
 182 }
< prev index next >