Path: blob/master/src/java.base/share/classes/sun/nio/ch/SelectionKeyImpl.java
41159 views
/*1* Copyright (c) 2000, 2021, 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.FileDescriptor;28import java.lang.invoke.ConstantBootstraps;29import java.lang.invoke.MethodHandles;30import java.lang.invoke.VarHandle;31import java.nio.channels.CancelledKeyException;32import java.nio.channels.SelectableChannel;33import java.nio.channels.SelectionKey;34import java.nio.channels.Selector;35import java.nio.channels.spi.AbstractSelectionKey;363738/**39* An implementation of SelectionKey.40*/4142public final class SelectionKeyImpl43extends AbstractSelectionKey44{45private static final VarHandle INTERESTOPS =46ConstantBootstraps.fieldVarHandle(47MethodHandles.lookup(),48"interestOps",49VarHandle.class,50SelectionKeyImpl.class, int.class);5152private final SelChImpl channel;53private final SelectorImpl selector;5455private volatile int interestOps;56private volatile int readyOps;5758// registered events in kernel, used by some Selector implementations59private int registeredEvents;6061// registered events need to be reset, used by some Selector implementations62private volatile boolean reset;6364// index of key in pollfd array, used by some Selector implementations65private int index;6667SelectionKeyImpl(SelChImpl ch, SelectorImpl sel) {68channel = ch;69selector = sel;70}7172private void ensureValid() {73if (!isValid())74throw new CancelledKeyException();75}7677FileDescriptor getFD() {78return channel.getFD();79}8081int getFDVal() {82return channel.getFDVal();83}8485@Override86public SelectableChannel channel() {87return (SelectableChannel)channel;88}8990@Override91public Selector selector() {92return selector;93}9495@Override96public int interestOps() {97ensureValid();98return interestOps;99}100101@Override102public SelectionKey interestOps(int ops) {103ensureValid();104if ((ops & ~channel().validOps()) != 0)105throw new IllegalArgumentException();106int oldOps = (int) INTERESTOPS.getAndSet(this, ops);107if (ops != oldOps) {108selector.setEventOps(this);109}110return this;111}112113@Override114public int interestOpsOr(int ops) {115ensureValid();116if ((ops & ~channel().validOps()) != 0)117throw new IllegalArgumentException();118int oldVal = (int) INTERESTOPS.getAndBitwiseOr(this, ops);119if (oldVal != (oldVal | ops)) {120selector.setEventOps(this);121}122return oldVal;123}124125@Override126public int interestOpsAnd(int ops) {127ensureValid();128int oldVal = (int) INTERESTOPS.getAndBitwiseAnd(this, ops);129if (oldVal != (oldVal & ops)) {130selector.setEventOps(this);131}132return oldVal;133}134135@Override136public int readyOps() {137ensureValid();138return readyOps;139}140141// The nio versions of these operations do not care if a key142// has been invalidated. They are for internal use by nio code.143144public void nioReadyOps(int ops) {145readyOps = ops;146}147148public int nioReadyOps() {149return readyOps;150}151152public SelectionKey nioInterestOps(int ops) {153if ((ops & ~channel().validOps()) != 0)154throw new IllegalArgumentException();155interestOps = ops;156selector.setEventOps(this);157return this;158}159160public int nioInterestOps() {161return interestOps;162}163164int translateInterestOps() {165return channel.translateInterestOps(interestOps);166}167168boolean translateAndSetReadyOps(int ops) {169return channel.translateAndSetReadyOps(ops, this);170}171172boolean translateAndUpdateReadyOps(int ops) {173return channel.translateAndUpdateReadyOps(ops, this);174}175176void registeredEvents(int events) {177// assert Thread.holdsLock(selector);178this.registeredEvents = events;179}180181int registeredEvents() {182// assert Thread.holdsLock(selector);183return registeredEvents;184}185186int getIndex() {187return index;188}189190void setIndex(int i) {191index = i;192}193194/**195* Sets the reset flag, re-queues the key, and wakeups up the Selector196*/197void reset() {198reset = true;199selector.setEventOps(this);200selector.wakeup();201}202203/**204* Clears the reset flag, returning the previous value of the flag205*/206boolean getAndClearReset() {207assert Thread.holdsLock(selector);208boolean r = reset;209if (r)210reset = false;211return r;212}213214@Override215public String toString() {216StringBuilder sb = new StringBuilder();217sb.append("channel=")218.append(channel)219.append(", selector=")220.append(selector);221if (isValid()) {222sb.append(", interestOps=")223.append(interestOps)224.append(", readyOps=")225.append(readyOps);226} else {227sb.append(", invalid");228}229return sb.toString();230}231232// used by Selector implementations to record when the key was selected233int lastPolled;234}235236237