Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/classes/sun/nio/ch/PollSelectorImpl.java
32288 views
/*1* Copyright (c) 2001, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.nio.ch;2627import java.io.IOException;28import java.nio.channels.*;29import java.nio.channels.spi.*;30import java.util.*;31import sun.misc.*;323334/**35* An implementation of Selector for Solaris.36*/3738class PollSelectorImpl39extends AbstractPollSelectorImpl40{4142// File descriptors used for interrupt43private int fd0;44private int fd1;4546// Lock for interrupt triggering and clearing47private Object interruptLock = new Object();48private boolean interruptTriggered = false;4950/**51* Package private constructor called by factory method in52* the abstract superclass Selector.53*/54PollSelectorImpl(SelectorProvider sp) {55super(sp, 1, 1);56long pipeFds = IOUtil.makePipe(false);57fd0 = (int) (pipeFds >>> 32);58fd1 = (int) pipeFds;59try {60pollWrapper = new PollArrayWrapper(INIT_CAP);61pollWrapper.initInterrupt(fd0, fd1);62channelArray = new SelectionKeyImpl[INIT_CAP];63} catch (Throwable t) {64try {65FileDispatcherImpl.closeIntFD(fd0);66} catch (IOException ioe0) {67t.addSuppressed(ioe0);68}69try {70FileDispatcherImpl.closeIntFD(fd1);71} catch (IOException ioe1) {72t.addSuppressed(ioe1);73}74throw t;75}76}7778protected int doSelect(long timeout)79throws IOException80{81if (channelArray == null)82throw new ClosedSelectorException();83processDeregisterQueue();84try {85begin();86pollWrapper.poll(totalChannels, 0, timeout);87} finally {88end();89}90processDeregisterQueue();91int numKeysUpdated = updateSelectedKeys();92if (pollWrapper.getReventOps(0) != 0) {93// Clear the wakeup pipe94pollWrapper.putReventOps(0, 0);95synchronized (interruptLock) {96IOUtil.drain(fd0);97interruptTriggered = false;98}99}100return numKeysUpdated;101}102103protected void implCloseInterrupt() throws IOException {104// prevent further wakeup105synchronized (interruptLock) {106interruptTriggered = true;107}108FileDispatcherImpl.closeIntFD(fd0);109FileDispatcherImpl.closeIntFD(fd1);110fd0 = -1;111fd1 = -1;112pollWrapper.release(0);113}114115public Selector wakeup() {116synchronized (interruptLock) {117if (!interruptTriggered) {118pollWrapper.interrupt();119interruptTriggered = true;120}121}122return this;123}124125}126127128