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 
  32 
  33 /**
  34  * An implementation of Selector for Solaris.
  35  */
  36 
  37 class PollSelectorImpl
  38     extends AbstractPollSelectorImpl
  39 {
  40 
  41     // File descriptors used for interrupt
  42     private int fd0;
  43     private int fd1;
  44 
  45     // Lock for interrupt triggering and clearing
  46     private final Object interruptLock = new Object();
  47     private boolean interruptTriggered = false;
  48 
  49     /**
  50      * Package private constructor called by factory method in
  51      * the abstract superclass Selector.
  52      */
  53     PollSelectorImpl(SelectorProvider sp) {
  54         super(sp, 1, 1);
  55         long pipeFds = IOUtil.makePipe(false);
  56         fd0 = (int) (pipeFds >>> 32);
  57         fd1 = (int) pipeFds;
  58         try {
  59             pollWrapper = new PollArrayWrapper(INIT_CAP);
  60             pollWrapper.initInterrupt(fd0, fd1);
  61             channelArray = new SelectionKeyImpl[INIT_CAP];
  62         } catch (Throwable t) {
  63             try {
  64                 FileDispatcherImpl.closeIntFD(fd0);
  65             } catch (IOException ioe0) {
  66                 t.addSuppressed(ioe0);
  67             }
  68             try {
  69                 FileDispatcherImpl.closeIntFD(fd1);
  70             } catch (IOException ioe1) {
  71                 t.addSuppressed(ioe1);
  72             }
  73             throw t;
  74         }
  75     }
  76 
  77     protected int doSelect(long timeout)
  78         throws IOException
  79     {
  80         if (channelArray == null)
  81             throw new ClosedSelectorException();
  82         processDeregisterQueue();
  83         try {
  84             begin();
  85             pollWrapper.poll(totalChannels, 0, timeout);
  86         } finally {
  87             end();
  88         }
  89         processDeregisterQueue();
  90         int numKeysUpdated = updateSelectedKeys();
  91         if (pollWrapper.getReventOps(0) != 0) {
  92             // Clear the wakeup pipe
  93             pollWrapper.putReventOps(0, 0);
  94             synchronized (interruptLock) {
  95                 IOUtil.drain(fd0);
  96                 interruptTriggered = false;
  97             }
  98         }
  99         return numKeysUpdated;
 100     }
 101 
 102     protected void implCloseInterrupt() throws IOException {
 103         // prevent further wakeup
 104         synchronized (interruptLock) {
 105             interruptTriggered = true;
 106         }
 107         FileDispatcherImpl.closeIntFD(fd0);
 108         FileDispatcherImpl.closeIntFD(fd1);
 109         fd0 = -1;
 110         fd1 = -1;
 111         pollWrapper.release(0);
 112     }
 113 
 114     public Selector wakeup() {
 115         synchronized (interruptLock) {
 116             if (!interruptTriggered) {
 117                 pollWrapper.interrupt();
 118                 interruptTriggered = true;
 119             }
 120         }
 121         return this;
 122     }
 123 
 124 }