1 /*
   2  * Copyright (c) 2001, 2018, 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 implementation of Selector for Solaris.
  36  */
  37 class DevPollSelectorImpl
  38     extends SelectorImpl
  39 {
  40 
  41     // File descriptors used for interrupt
  42     protected int fd0;
  43     protected int fd1;
  44 
  45     // The poll object
  46     final DevPollArrayWrapper pollWrapper;
  47 
  48     // Maps from file descriptors to keys
  49     private final Map<Integer,SelectionKeyImpl> fdToKey;
  50 
  51     // True if this Selector has been closed
  52     private boolean closed = false;
  53 
  54     // Lock for interrupt triggering and clearing
  55     private final Object interruptLock = new Object();
  56     private boolean interruptTriggered = false;
  57 
  58     /**
  59      * Package private constructor called by factory method in
  60      * the abstract superclass Selector.
  61      */
  62     DevPollSelectorImpl(SelectorProvider sp) {
  63         super(sp);
  64         long pipeFds = IOUtil.makePipe(false);
  65         fd0 = (int) (pipeFds >>> 32);
  66         fd1 = (int) pipeFds;
  67         try {
  68             pollWrapper = new DevPollArrayWrapper();
  69             pollWrapper.initInterrupt(fd0, fd1);
  70             fdToKey = new HashMap<>();
  71         } catch (Throwable t) {
  72             try {
  73                 FileDispatcherImpl.closeIntFD(fd0);
  74             } catch (IOException ioe0) {
  75                 t.addSuppressed(ioe0);
  76             }
  77             try {
  78                 FileDispatcherImpl.closeIntFD(fd1);
  79             } catch (IOException ioe1) {
  80                 t.addSuppressed(ioe1);
  81             }
  82             throw t;
  83         }
  84     }
  85 
  86     protected int doSelect(long timeout)
  87         throws IOException
  88     {
  89         if (closed)
  90             throw new ClosedSelectorException();
  91         processDeregisterQueue();
  92         try {
  93             begin();
  94             pollWrapper.poll(timeout);
  95         } finally {
  96             end();
  97         }
  98         processDeregisterQueue();
  99         int numKeysUpdated = updateSelectedKeys();
 100         if (pollWrapper.interrupted()) {
 101             // Clear the wakeup pipe
 102             pollWrapper.putReventOps(pollWrapper.interruptedIndex(), 0);
 103             synchronized (interruptLock) {
 104                 pollWrapper.clearInterrupted();
 105                 IOUtil.drain(fd0);
 106                 interruptTriggered = false;
 107             }
 108         }
 109         return numKeysUpdated;
 110     }
 111 
 112     /**
 113      * Update the keys whose fd's have been selected by the devpoll
 114      * driver. Add the ready keys to the ready queue.
 115      */
 116     private int updateSelectedKeys() {
 117         int entries = pollWrapper.updated;
 118         int numKeysUpdated = 0;
 119         for (int i=0; i<entries; i++) {
 120             int nextFD = pollWrapper.getDescriptor(i);
 121             SelectionKeyImpl ski = fdToKey.get(Integer.valueOf(nextFD));
 122             // ski is null in the case of an interrupt
 123             if (ski != null) {
 124                 int rOps = pollWrapper.getReventOps(i);
 125                 if (selectedKeys.contains(ski)) {
 126                     if (ski.channel.translateAndSetReadyOps(rOps, ski)) {
 127                         numKeysUpdated++;
 128                     }
 129                 } else {
 130                     ski.channel.translateAndSetReadyOps(rOps, ski);
 131                     if ((ski.nioReadyOps() & ski.nioInterestOps()) != 0) {
 132                         selectedKeys.add(ski);
 133                         numKeysUpdated++;
 134                     }
 135                 }
 136             }
 137         }
 138         return numKeysUpdated;
 139     }
 140 
 141     protected void implClose() throws IOException {
 142         if (closed)
 143             return;
 144         closed = true;
 145 
 146         // prevent further wakeup
 147         synchronized (interruptLock) {
 148             interruptTriggered = true;
 149         }
 150 
 151         FileDispatcherImpl.closeIntFD(fd0);
 152         FileDispatcherImpl.closeIntFD(fd1);
 153 
 154         pollWrapper.release(fd0);
 155         pollWrapper.closeDevPollFD();
 156         selectedKeys.clear();
 157 
 158         // Deregister channels
 159         Iterator<SelectionKey> i = keys.iterator();
 160         while (i.hasNext()) {
 161             SelectionKeyImpl ski = (SelectionKeyImpl)i.next();
 162             deregister(ski);
 163             SelectableChannel selch = ski.channel();
 164             if (!selch.isOpen() && !selch.isRegistered())
 165                 ((SelChImpl)selch).kill();
 166             i.remove();
 167         }
 168         fd0 = -1;
 169         fd1 = -1;
 170     }
 171 
 172     protected void implRegister(SelectionKeyImpl ski) {
 173         int fd = IOUtil.fdVal(ski.channel.getFD());
 174         fdToKey.put(Integer.valueOf(fd), ski);
 175         keys.add(ski);
 176     }
 177 
 178     protected void implDereg(SelectionKeyImpl ski) throws IOException {
 179         int i = ski.getIndex();
 180         assert (i >= 0);
 181         int fd = ski.channel.getFDVal();
 182         fdToKey.remove(Integer.valueOf(fd));
 183         pollWrapper.release(fd);
 184         ski.setIndex(-1);
 185         keys.remove(ski);
 186         selectedKeys.remove(ski);
 187         deregister(ski);
 188         SelectableChannel selch = ski.channel();
 189         if (!selch.isOpen() && !selch.isRegistered())
 190             ((SelChImpl)selch).kill();
 191     }
 192 
 193     public void putEventOps(SelectionKeyImpl sk, int ops) {
 194         if (closed)
 195             throw new ClosedSelectorException();
 196         int fd = IOUtil.fdVal(sk.channel.getFD());
 197         pollWrapper.setInterest(fd, ops);
 198     }
 199 
 200     public Selector wakeup() {
 201         synchronized (interruptLock) {
 202             if (!interruptTriggered) {
 203                 pollWrapper.interrupt();
 204                 interruptTriggered = true;
 205             }
 206         }
 207         return this;
 208     }
 209 }