Path: blob/master/src/java.base/windows/classes/sun/nio/ch/WEPollSelectorImpl.java
41139 views
/*1* Copyright (c) 2020, 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.io.IOException;29import java.nio.ByteBuffer;30import java.nio.channels.ClosedSelectorException;31import java.nio.channels.Pipe;32import java.nio.channels.SelectionKey;33import java.nio.channels.Selector;34import java.nio.channels.spi.SelectorProvider;35import java.util.ArrayDeque;36import java.util.Deque;37import java.util.HashMap;38import java.util.Map;39import java.util.function.Consumer;4041import static sun.nio.ch.WEPoll.*;4243/**44* Windows wepoll based Selector implementation45*/46class WEPollSelectorImpl extends SelectorImpl {47// maximum number of events to poll in one call to epoll_wait48private static final int NUM_EPOLLEVENTS = 256;4950// wepoll handle51private final long eph;5253// address of epoll_event array when polling with epoll_wait54private final long pollArrayAddress;5556// maps SOCKET to selection key, synchronize on selector57private final Map<Integer, SelectionKeyImpl> fdToKey = new HashMap<>();5859// pending new registrations/updates, queued by setEventOps60private final Object updateLock = new Object();61private final Deque<SelectionKeyImpl> updateKeys = new ArrayDeque<>();6263// interrupt/wakeup64private final Object interruptLock = new Object();65private boolean interruptTriggered;66private final PipeImpl pipe;67private final int fd0Val, fd1Val;6869WEPollSelectorImpl(SelectorProvider sp) throws IOException {70super(sp);7172this.eph = WEPoll.create();73this.pollArrayAddress = WEPoll.allocatePollArray(NUM_EPOLLEVENTS);7475// wakeup support76try {77this.pipe = new PipeImpl(sp, /*buffering*/ false);78} catch (IOException ioe) {79WEPoll.freePollArray(pollArrayAddress);80WEPoll.close(eph);81throw ioe;82}83this.fd0Val = pipe.source().getFDVal();84this.fd1Val = pipe.sink().getFDVal();8586// register one end of the pipe for wakeups87WEPoll.ctl(eph, EPOLL_CTL_ADD, fd0Val, WEPoll.EPOLLIN);88}8990private void ensureOpen() {91if (!isOpen())92throw new ClosedSelectorException();93}9495@Override96protected int doSelect(Consumer<SelectionKey> action, long timeout)97throws IOException98{99assert Thread.holdsLock(this);100101// epoll_wait timeout is int102int to = (int) Math.min(timeout, Integer.MAX_VALUE);103boolean blocking = (to != 0);104105int numEntries;106processUpdateQueue();107processDeregisterQueue();108try {109begin(blocking);110numEntries = WEPoll.wait(eph, pollArrayAddress, NUM_EPOLLEVENTS, to);111} finally {112end(blocking);113}114processDeregisterQueue();115return processEvents(numEntries, action);116}117118/**119* Process changes to the interest ops.120*/121private void processUpdateQueue() {122assert Thread.holdsLock(this);123124synchronized (updateLock) {125SelectionKeyImpl ski;126while ((ski = updateKeys.pollFirst()) != null) {127if (ski.isValid()) {128int fd = ski.getFDVal();129// add to fdToKey if needed130SelectionKeyImpl previous = fdToKey.putIfAbsent(fd, ski);131assert (previous == null) || (previous == ski);132int newOps = ski.translateInterestOps();133int registeredOps = ski.registeredEvents();134if (newOps != registeredOps) {135if (newOps == 0) {136// remove from epoll137WEPoll.ctl(eph, EPOLL_CTL_DEL, fd, 0);138} else {139int events = toEPollEvents(newOps);140if (registeredOps == 0) {141// add to epoll142WEPoll.ctl(eph, EPOLL_CTL_ADD, fd, events);143} else {144// modify events145WEPoll.ctl(eph, EPOLL_CTL_MOD, fd, events);146}147}148ski.registeredEvents(newOps);149}150}151}152}153}154155/**156* Process the polled events.157* If the interrupt fd has been selected, drain it and clear the interrupt.158*/159private int processEvents(int numEntries, Consumer<SelectionKey> action)160throws IOException161{162assert Thread.holdsLock(this);163164boolean interrupted = false;165int numKeysUpdated = 0;166for (int i = 0; i < numEntries; i++) {167long event = WEPoll.getEvent(pollArrayAddress, i);168int fd = WEPoll.getDescriptor(event);169if (fd == fd0Val) {170interrupted = true;171} else {172SelectionKeyImpl ski = fdToKey.get(fd);173if (ski != null) {174int events = WEPoll.getEvents(event);175if ((events & WEPoll.EPOLLPRI) != 0) {176Net.discardOOB(ski.getFD());177}178int rOps = toReadyOps(events);179numKeysUpdated += processReadyEvents(rOps, ski, action);180}181}182}183184if (interrupted) {185clearInterrupt();186}187188return numKeysUpdated;189}190191@Override192protected void implClose() throws IOException {193assert !isOpen() && Thread.holdsLock(this);194195// prevent further wakeup196synchronized (interruptLock) {197interruptTriggered = true;198}199200// close the epoll port and free resources201WEPoll.close(eph);202WEPoll.freePollArray(pollArrayAddress);203pipe.sink().close();204pipe.source().close();205}206207@Override208protected void implDereg(SelectionKeyImpl ski) throws IOException {209assert !ski.isValid() && Thread.holdsLock(this);210211int fd = ski.getFDVal();212if (fdToKey.remove(fd) != null) {213if (ski.registeredEvents() != 0) {214WEPoll.ctl(eph, EPOLL_CTL_DEL, fd, 0);215ski.registeredEvents(0);216}217} else {218assert ski.registeredEvents() == 0;219}220}221222@Override223public void setEventOps(SelectionKeyImpl ski) {224ensureOpen();225synchronized (updateLock) {226updateKeys.addLast(ski);227}228}229230@Override231public Selector wakeup() {232synchronized (interruptLock) {233if (!interruptTriggered) {234try {235IOUtil.write1(fd1Val, (byte) 0);236} catch (IOException ioe) {237throw new InternalError(ioe);238}239interruptTriggered = true;240}241}242return this;243}244245private void clearInterrupt() throws IOException {246synchronized (interruptLock) {247IOUtil.drain(fd0Val);248interruptTriggered = false;249}250}251252/**253* Maps interest ops to epoll events254*/255private static int toEPollEvents(int ops) {256int events = EPOLLPRI;257if ((ops & Net.POLLIN) != 0)258events |= EPOLLIN;259if ((ops & (Net.POLLOUT | Net.POLLCONN)) != 0)260events |= EPOLLOUT;261return events;262}263264/**265* Map epoll events to ready ops266*/267private static int toReadyOps(int events) {268int ops = 0;269if ((events & WEPoll.EPOLLIN) != 0) ops |= Net.POLLIN;270if ((events & WEPoll.EPOLLOUT) != 0) ops |= (Net.POLLOUT | Net.POLLCONN);271if ((events & WEPoll.EPOLLHUP) != 0) ops |= Net.POLLHUP;272if ((events & WEPoll.EPOLLERR) != 0) ops |= Net.POLLERR;273return ops;274}275}276277278