1 /*
   2  * Copyright (c) 2001, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  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.util.*;
  32 
  33 
  34 /**
  35  * An abstract selector impl.
  36  */
  37 
  38 abstract class AbstractPollSelectorImpl
  39     extends SelectorImpl
  40 {
  41 
  42     // The poll fd array
  43     PollArrayWrapper pollWrapper;
  44 
  45     // Initial capacity of the pollfd array
  46     protected final int INIT_CAP = 10;
  47 
  48     // The list of SelectableChannels serviced by this Selector
  49     protected SelectionKeyImpl[] channelArray;
  50 
  51     // In some impls the first entry of channelArray is bogus
  52     protected int channelOffset = 0;
  53 
  54     // The number of valid channels in this Selector's poll array
  55     protected int totalChannels;
  56 
  57     // True if this Selector has been closed
  58     private boolean closed = false;
  59 
  60     // Lock for close and cleanup
  61     private Object closeLock = new Object();
  62 
  63     AbstractPollSelectorImpl(SelectorProvider sp, int channels, int offset) {
  64         super(sp);
  65         this.totalChannels = channels;
  66         this.channelOffset = offset;
  67     }
  68 
  69     public void putEventOps(SelectionKeyImpl sk, int ops) {
  70         synchronized (closeLock) {
  71             if (closed)
  72                 throw new ClosedSelectorException();
  73             pollWrapper.putEventOps(sk.getIndex(), ops);
  74         }
  75     }
  76 
  77     public Selector wakeup() {
  78         pollWrapper.interrupt();
  79         return this;
  80     }
  81 
  82     protected abstract int doSelect(long timeout) throws IOException;
  83 
  84     protected void implClose() throws IOException {
  85         synchronized (closeLock) {
  86             if (closed)
  87                 return;
  88             closed = true;
  89             // Deregister channels
  90             for(int i=channelOffset; i<totalChannels; i++) {
  91                 SelectionKeyImpl ski = channelArray[i];
  92                 assert(ski.getIndex() != -1);
  93                 ski.setIndex(-1);
  94                 deregister(ski);
  95                 SelectableChannel selch = channelArray[i].channel();
  96                 if (!selch.isOpen() && !selch.isRegistered())
  97                     ((SelChImpl)selch).kill();
  98             }
  99             implCloseInterrupt();
 100             pollWrapper.free();
 101             pollWrapper = null;
 102             selectedKeys = null;
 103             channelArray = null;
 104             totalChannels = 0;
 105         }
 106     }
 107 
 108     protected abstract void implCloseInterrupt() throws IOException;
 109 
 110     /**
 111      * Copy the information in the pollfd structs into the opss
 112      * of the corresponding Channels. Add the ready keys to the
 113      * ready queue.
 114      */
 115     protected int updateSelectedKeys() {
 116         int numKeysUpdated = 0;
 117         // Skip zeroth entry; it is for interrupts only
 118         for (int i=channelOffset; i<totalChannels; i++) {
 119             int rOps = pollWrapper.getReventOps(i);
 120             if (rOps != 0) {
 121                 SelectionKeyImpl sk = channelArray[i];
 122                 pollWrapper.putReventOps(i, 0);
 123                 if (selectedKeys.contains(sk)) {
 124                     if (sk.channel.translateAndSetReadyOps(rOps, sk)) {
 125                         numKeysUpdated++;
 126                     }
 127                 } else {
 128                     sk.channel.translateAndSetReadyOps(rOps, sk);
 129                     if ((sk.nioReadyOps() & sk.nioInterestOps()) != 0) {
 130                         selectedKeys.add(sk);
 131                         numKeysUpdated++;
 132                     }
 133                 }
 134             }
 135         }
 136         return numKeysUpdated;
 137     }
 138 
 139     protected void implRegister(SelectionKeyImpl ski) {
 140         synchronized (closeLock) {
 141             if (closed)
 142                 throw new ClosedSelectorException();
 143 
 144             // Check to see if the array is large enough
 145             if (channelArray.length == totalChannels) {
 146                 // Make a larger array
 147                 int newSize = pollWrapper.totalChannels * 2;
 148                 SelectionKeyImpl temp[] = new SelectionKeyImpl[newSize];
 149                 // Copy over
 150                 for (int i=channelOffset; i<totalChannels; i++)
 151                     temp[i] = channelArray[i];
 152                 channelArray = temp;
 153                 // Grow the NativeObject poll array
 154                 pollWrapper.grow(newSize);
 155             }
 156             channelArray[totalChannels] = ski;
 157             ski.setIndex(totalChannels);
 158             pollWrapper.addEntry(ski.channel);
 159             totalChannels++;
 160             keys.add(ski);
 161         }
 162     }
 163 
 164     protected void implDereg(SelectionKeyImpl ski) throws IOException {
 165         // Algorithm: Copy the sc from the end of the list and put it into
 166         // the location of the sc to be removed (since order doesn't
 167         // matter). Decrement the sc count. Update the index of the sc
 168         // that is moved.
 169         int i = ski.getIndex();
 170         assert (i >= 0);
 171         if (i != totalChannels - 1) {
 172             // Copy end one over it
 173             SelectionKeyImpl endChannel = channelArray[totalChannels-1];
 174             channelArray[i] = endChannel;
 175             endChannel.setIndex(i);
 176             pollWrapper.release(i);
 177             PollArrayWrapper.replaceEntry(pollWrapper, totalChannels - 1,
 178                                           pollWrapper, i);
 179         } else {
 180             pollWrapper.release(i);
 181         }
 182         // Destroy the last one
 183         channelArray[totalChannels-1] = null;
 184         totalChannels--;
 185         pollWrapper.totalChannels--;
 186         ski.setIndex(-1);
 187         // Remove the key from keys and selectedKeys
 188         keys.remove(ski);
 189         selectedKeys.remove(ski);
 190         deregister((AbstractSelectionKey)ski);
 191         SelectableChannel selch = ski.channel();
 192         if (!selch.isOpen() && !selch.isRegistered())
 193             ((SelChImpl)selch).kill();
 194     }
 195 }