Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/nio/ch/SelectorImpl.java
38918 views
/*1* Copyright (c) 2000, 2012, 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.net.SocketException;31import java.util.*;323334/**35* Base Selector implementation class.36*/3738public abstract class SelectorImpl39extends AbstractSelector40{4142// The set of keys with data ready for an operation43protected Set<SelectionKey> selectedKeys;4445// The set of keys registered with this Selector46protected HashSet<SelectionKey> keys;4748// Public views of the key sets49private Set<SelectionKey> publicKeys; // Immutable50private Set<SelectionKey> publicSelectedKeys; // Removal allowed, but not addition5152protected SelectorImpl(SelectorProvider sp) {53super(sp);54keys = new HashSet<SelectionKey>();55selectedKeys = new HashSet<SelectionKey>();56if (Util.atBugLevel("1.4")) {57publicKeys = keys;58publicSelectedKeys = selectedKeys;59} else {60publicKeys = Collections.unmodifiableSet(keys);61publicSelectedKeys = Util.ungrowableSet(selectedKeys);62}63}6465public Set<SelectionKey> keys() {66if (!isOpen() && !Util.atBugLevel("1.4"))67throw new ClosedSelectorException();68return publicKeys;69}7071public Set<SelectionKey> selectedKeys() {72if (!isOpen() && !Util.atBugLevel("1.4"))73throw new ClosedSelectorException();74return publicSelectedKeys;75}7677protected abstract int doSelect(long timeout) throws IOException;7879private int lockAndDoSelect(long timeout) throws IOException {80synchronized (this) {81if (!isOpen())82throw new ClosedSelectorException();83synchronized (publicKeys) {84synchronized (publicSelectedKeys) {85return doSelect(timeout);86}87}88}89}9091public int select(long timeout)92throws IOException93{94if (timeout < 0)95throw new IllegalArgumentException("Negative timeout");96return lockAndDoSelect((timeout == 0) ? -1 : timeout);97}9899public int select() throws IOException {100return select(0);101}102103public int selectNow() throws IOException {104return lockAndDoSelect(0);105}106107public void implCloseSelector() throws IOException {108wakeup();109synchronized (this) {110synchronized (publicKeys) {111synchronized (publicSelectedKeys) {112implClose();113}114}115}116}117118protected abstract void implClose() throws IOException;119120public void putEventOps(SelectionKeyImpl sk, int ops) { }121122protected final SelectionKey register(AbstractSelectableChannel ch,123int ops,124Object attachment)125{126if (!(ch instanceof SelChImpl))127throw new IllegalSelectorException();128SelectionKeyImpl k = new SelectionKeyImpl((SelChImpl)ch, this);129k.attach(attachment);130synchronized (publicKeys) {131implRegister(k);132}133k.interestOps(ops);134return k;135}136137protected abstract void implRegister(SelectionKeyImpl ski);138139void processDeregisterQueue() throws IOException {140// Precondition: Synchronized on this, keys, and selectedKeys141Set<SelectionKey> cks = cancelledKeys();142synchronized (cks) {143if (!cks.isEmpty()) {144Iterator<SelectionKey> i = cks.iterator();145while (i.hasNext()) {146SelectionKeyImpl ski = (SelectionKeyImpl)i.next();147try {148implDereg(ski);149} catch (SocketException se) {150throw new IOException("Error deregistering key", se);151} finally {152i.remove();153}154}155}156}157}158159protected abstract void implDereg(SelectionKeyImpl ski) throws IOException;160161abstract public Selector wakeup();162163}164165166