1 /*
   2  * Copyright (c) 2001, 2010, 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.function.Consumer;
  32 
  33 
  34 /**
  35  * An implementation of Selector for Solaris.
  36  */
  37 
  38 class PollSelectorImpl
  39     extends AbstractPollSelectorImpl
  40 {
  41 
  42     // File descriptors used for interrupt
  43     private int fd0;
  44     private int fd1;
  45 
  46     // Lock for interrupt triggering and clearing
  47     private Object interruptLock = new Object();
  48     private boolean interruptTriggered = false;
  49 
  50     /**
  51      * Package private constructor called by factory method in
  52      * the abstract superclass Selector.
  53      */
  54     PollSelectorImpl(SelectorProvider sp) {
  55         super(sp, 1, 1);
  56         long pipeFds = IOUtil.makePipe(false);
  57         fd0 = (int) (pipeFds >>> 32);
  58         fd1 = (int) pipeFds;
  59         pollWrapper = new PollArrayWrapper(INIT_CAP);
  60         pollWrapper.initInterrupt(fd0, fd1);
  61         channelArray = new SelectionKeyImpl[INIT_CAP];
  62     }
  63 
  64     @Override
  65     protected int doSelect(long timeout) throws IOException {
  66         pollWrapper(timeout);
  67         int numKeysUpdated = updateSelectedKeys();
  68         clearWakeupPipeIfInterrupted();
  69         return numKeysUpdated;
  70     }
  71 
  72     @Override
  73     protected int doSelect(Consumer<SelectionKey> handler, long timeout) throws IOException {
  74         pollWrapper(timeout);
  75         int numKeysUpdated = 0;
  76         // Skip zeroth entry; it is for interrupts only
  77         for (int i=channelOffset; i<totalChannels; i++) {
  78             int rOps = pollWrapper.getReventOps(i);
  79             if (rOps != 0) {
  80                 SelectionKeyImpl ski = channelArray[i];
  81                 pollWrapper.putReventOps(i, 0);
  82                 ski.channel.translateAndSetReadyOps(rOps, ski);
  83                 if (ski.hasOps()) {
  84                     handler.accept(ski);
  85                     numKeysUpdated++;
  86                 }
  87             }
  88         }
  89         clearWakeupPipeIfInterrupted();
  90         return numKeysUpdated;
  91     }
  92 
  93     private void clearWakeupPipeIfInterrupted() throws IOException {
  94         if (pollWrapper.getReventOps(0) != 0) {
  95             // Clear the wakeup pipe
  96             pollWrapper.putReventOps(0, 0);
  97             synchronized (interruptLock) {
  98                 IOUtil.drain(fd0);
  99                 interruptTriggered = false;
 100             }
 101         }
 102     }
 103 
 104     private void pollWrapper(long timeout) throws ClosedSelectorException, IOException {
 105         if (channelArray == null)
 106             throw new ClosedSelectorException();
 107         processDeregisterQueue();
 108         try {
 109             begin();
 110             pollWrapper.poll(totalChannels, 0, timeout);
 111         } finally {
 112             end();
 113         }
 114         processDeregisterQueue();
 115     }
 116 
 117     @Override
 118     protected void implCloseInterrupt() throws IOException {
 119         // prevent further wakeup
 120         synchronized (interruptLock) {
 121             interruptTriggered = true;
 122         }
 123         FileDispatcherImpl.closeIntFD(fd0);
 124         FileDispatcherImpl.closeIntFD(fd1);
 125         fd0 = -1;
 126         fd1 = -1;
 127         pollWrapper.release(0);
 128     }
 129 
 130     public Selector wakeup() {
 131         synchronized (interruptLock) {
 132             if (!interruptTriggered) {
 133                 pollWrapper.interrupt();
 134                 interruptTriggered = true;
 135             }
 136         }
 137         return this;
 138     }
 139 
 140 }