mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-28 15:24:43 +02:00
8199791: (se) More Selector cleanup
Reviewed-by: redestad, bpb
This commit is contained in:
parent
de23920e05
commit
3bb85f5fc5
33 changed files with 754 additions and 1516 deletions
|
@ -109,11 +109,11 @@ class EPoll {
|
|||
|
||||
private static native int dataOffset();
|
||||
|
||||
static native int epollCreate() throws IOException;
|
||||
static native int create() throws IOException;
|
||||
|
||||
static native int epollCtl(int epfd, int opcode, int fd, int events);
|
||||
static native int ctl(int epfd, int opcode, int fd, int events);
|
||||
|
||||
static native int epollWait(int epfd, long pollAddress, int numfds)
|
||||
static native int wait(int epfd, long pollAddress, int numfds, int timeout)
|
||||
throws IOException;
|
||||
|
||||
static {
|
||||
|
|
|
@ -1,309 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package sun.nio.ch;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.security.AccessController;
|
||||
import java.util.BitSet;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import sun.security.action.GetIntegerAction;
|
||||
|
||||
/**
|
||||
* Manipulates a native array of epoll_event structs on Linux:
|
||||
*
|
||||
* typedef union epoll_data {
|
||||
* void *ptr;
|
||||
* int fd;
|
||||
* __uint32_t u32;
|
||||
* __uint64_t u64;
|
||||
* } epoll_data_t;
|
||||
*
|
||||
* struct epoll_event {
|
||||
* __uint32_t events;
|
||||
* epoll_data_t data;
|
||||
* };
|
||||
*
|
||||
* The system call to wait for I/O events is epoll_wait(2). It populates an
|
||||
* array of epoll_event structures that are passed to the call. The data
|
||||
* member of the epoll_event structure contains the same data as was set
|
||||
* when the file descriptor was registered to epoll via epoll_ctl(2). In
|
||||
* this implementation we set data.fd to be the file descriptor that we
|
||||
* register. That way, we have the file descriptor available when we
|
||||
* process the events.
|
||||
*/
|
||||
|
||||
class EPollArrayWrapper {
|
||||
// EPOLL_EVENTS
|
||||
private static final int EPOLLIN = 0x001;
|
||||
|
||||
// opcodes
|
||||
private static final int EPOLL_CTL_ADD = 1;
|
||||
private static final int EPOLL_CTL_DEL = 2;
|
||||
private static final int EPOLL_CTL_MOD = 3;
|
||||
|
||||
// Miscellaneous constants
|
||||
private static final int SIZE_EPOLLEVENT = sizeofEPollEvent();
|
||||
private static final int EVENT_OFFSET = 0;
|
||||
private static final int DATA_OFFSET = offsetofData();
|
||||
private static final int FD_OFFSET = DATA_OFFSET;
|
||||
private static final int OPEN_MAX = IOUtil.fdLimit();
|
||||
private static final int NUM_EPOLLEVENTS = Math.min(OPEN_MAX, 8192);
|
||||
|
||||
// Special value to indicate that an update should be ignored
|
||||
private static final byte KILLED = (byte)-1;
|
||||
|
||||
// Initial size of arrays for fd registration changes
|
||||
private static final int INITIAL_PENDING_UPDATE_SIZE = 64;
|
||||
|
||||
// maximum size of updatesLow
|
||||
private static final int MAX_UPDATE_ARRAY_SIZE = AccessController.doPrivileged(
|
||||
new GetIntegerAction("sun.nio.ch.maxUpdateArraySize", Math.min(OPEN_MAX, 64*1024)));
|
||||
|
||||
// The fd of the epoll driver
|
||||
private final int epfd;
|
||||
|
||||
// The epoll_event array for results from epoll_wait
|
||||
private final AllocatedNativeObject pollArray;
|
||||
|
||||
// Base address of the epoll_event array
|
||||
private final long pollArrayAddress;
|
||||
|
||||
// The fd of the interrupt line going out
|
||||
private final int outgoingInterruptFD;
|
||||
|
||||
// Number of updated pollfd entries
|
||||
private int updated;
|
||||
|
||||
// object to synchronize fd registration changes
|
||||
private final Object updateLock = new Object();
|
||||
|
||||
// number of file descriptors with registration changes pending
|
||||
private int updateCount;
|
||||
|
||||
// file descriptors with registration changes pending
|
||||
private int[] updateDescriptors = new int[INITIAL_PENDING_UPDATE_SIZE];
|
||||
|
||||
// events for file descriptors with registration changes pending, indexed
|
||||
// by file descriptor and stored as bytes for efficiency reasons. For
|
||||
// file descriptors higher than MAX_UPDATE_ARRAY_SIZE (unlimited case at
|
||||
// least) then the update is stored in a map.
|
||||
private final byte[] eventsLow = new byte[MAX_UPDATE_ARRAY_SIZE];
|
||||
private final Map<Integer,Byte> eventsHigh = new HashMap<>();
|
||||
|
||||
// Used by release and updateRegistrations to track whether a file
|
||||
// descriptor is registered with epoll.
|
||||
private final BitSet registered = new BitSet();
|
||||
|
||||
|
||||
EPollArrayWrapper(int fd0, int fd1) throws IOException {
|
||||
// creates the epoll file descriptor
|
||||
epfd = epollCreate();
|
||||
|
||||
// the epoll_event array passed to epoll_wait
|
||||
int allocationSize = NUM_EPOLLEVENTS * SIZE_EPOLLEVENT;
|
||||
pollArray = new AllocatedNativeObject(allocationSize, true);
|
||||
pollArrayAddress = pollArray.address();
|
||||
|
||||
outgoingInterruptFD = fd1;
|
||||
epollCtl(epfd, EPOLL_CTL_ADD, fd0, EPOLLIN);
|
||||
}
|
||||
|
||||
void putEventOps(int i, int event) {
|
||||
int offset = SIZE_EPOLLEVENT * i + EVENT_OFFSET;
|
||||
pollArray.putInt(offset, event);
|
||||
}
|
||||
|
||||
void putDescriptor(int i, int fd) {
|
||||
int offset = SIZE_EPOLLEVENT * i + FD_OFFSET;
|
||||
pollArray.putInt(offset, fd);
|
||||
}
|
||||
|
||||
int getEventOps(int i) {
|
||||
int offset = SIZE_EPOLLEVENT * i + EVENT_OFFSET;
|
||||
return pollArray.getInt(offset);
|
||||
}
|
||||
|
||||
int getDescriptor(int i) {
|
||||
int offset = SIZE_EPOLLEVENT * i + FD_OFFSET;
|
||||
return pollArray.getInt(offset);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns {@code true} if updates for the given key (file
|
||||
* descriptor) are killed.
|
||||
*/
|
||||
private boolean isEventsHighKilled(Integer key) {
|
||||
assert key >= MAX_UPDATE_ARRAY_SIZE;
|
||||
Byte value = eventsHigh.get(key);
|
||||
return (value != null && value == KILLED);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the pending update events for the given file descriptor. This
|
||||
* method has no effect if the update events is already set to KILLED,
|
||||
* unless {@code force} is {@code true}.
|
||||
*/
|
||||
private void setUpdateEvents(int fd, byte events, boolean force) {
|
||||
if (fd < MAX_UPDATE_ARRAY_SIZE) {
|
||||
if ((eventsLow[fd] != KILLED) || force) {
|
||||
eventsLow[fd] = events;
|
||||
}
|
||||
} else {
|
||||
Integer key = Integer.valueOf(fd);
|
||||
if (!isEventsHighKilled(key) || force) {
|
||||
eventsHigh.put(key, Byte.valueOf(events));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the pending update events for the given file descriptor.
|
||||
*/
|
||||
private byte getUpdateEvents(int fd) {
|
||||
if (fd < MAX_UPDATE_ARRAY_SIZE) {
|
||||
return eventsLow[fd];
|
||||
} else {
|
||||
Byte result = eventsHigh.get(Integer.valueOf(fd));
|
||||
// result should never be null
|
||||
return result.byteValue();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the events for a given file descriptor
|
||||
*/
|
||||
void setInterest(int fd, int mask) {
|
||||
synchronized (updateLock) {
|
||||
// record the file descriptor and events
|
||||
int oldCapacity = updateDescriptors.length;
|
||||
if (updateCount == oldCapacity) {
|
||||
int newCapacity = oldCapacity + INITIAL_PENDING_UPDATE_SIZE;
|
||||
int[] newDescriptors = new int[newCapacity];
|
||||
System.arraycopy(updateDescriptors, 0, newDescriptors, 0, oldCapacity);
|
||||
updateDescriptors = newDescriptors;
|
||||
}
|
||||
updateDescriptors[updateCount++] = fd;
|
||||
|
||||
// events are stored as bytes for efficiency reasons
|
||||
byte b = (byte)mask;
|
||||
assert (b == mask) && (b != KILLED);
|
||||
setUpdateEvents(fd, b, false);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a file descriptor
|
||||
*/
|
||||
void add(int fd) {
|
||||
// force the initial update events to 0 as it may be KILLED by a
|
||||
// previous registration.
|
||||
synchronized (updateLock) {
|
||||
assert !registered.get(fd);
|
||||
setUpdateEvents(fd, (byte)0, true);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a file descriptor
|
||||
*/
|
||||
void remove(int fd) {
|
||||
synchronized (updateLock) {
|
||||
// kill pending and future update for this file descriptor
|
||||
setUpdateEvents(fd, KILLED, false);
|
||||
|
||||
// remove from epoll
|
||||
if (registered.get(fd)) {
|
||||
epollCtl(epfd, EPOLL_CTL_DEL, fd, 0);
|
||||
registered.clear(fd);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Close epoll file descriptor and free poll array
|
||||
*/
|
||||
void close() throws IOException {
|
||||
FileDispatcherImpl.closeIntFD(epfd);
|
||||
pollArray.free();
|
||||
}
|
||||
|
||||
int poll(long timeout) throws IOException {
|
||||
updateRegistrations();
|
||||
return epollWait(pollArrayAddress, NUM_EPOLLEVENTS, timeout, epfd);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the pending registrations.
|
||||
*/
|
||||
private void updateRegistrations() {
|
||||
synchronized (updateLock) {
|
||||
int j = 0;
|
||||
while (j < updateCount) {
|
||||
int fd = updateDescriptors[j];
|
||||
short events = getUpdateEvents(fd);
|
||||
boolean isRegistered = registered.get(fd);
|
||||
int opcode = 0;
|
||||
|
||||
if (events != KILLED) {
|
||||
if (isRegistered) {
|
||||
opcode = (events != 0) ? EPOLL_CTL_MOD : EPOLL_CTL_DEL;
|
||||
} else {
|
||||
opcode = (events != 0) ? EPOLL_CTL_ADD : 0;
|
||||
}
|
||||
if (opcode != 0) {
|
||||
epollCtl(epfd, opcode, fd, events);
|
||||
if (opcode == EPOLL_CTL_ADD) {
|
||||
registered.set(fd);
|
||||
} else if (opcode == EPOLL_CTL_DEL) {
|
||||
registered.clear(fd);
|
||||
}
|
||||
}
|
||||
}
|
||||
j++;
|
||||
}
|
||||
updateCount = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public void interrupt() {
|
||||
interrupt(outgoingInterruptFD);
|
||||
}
|
||||
|
||||
static {
|
||||
IOUtil.load();
|
||||
init();
|
||||
}
|
||||
|
||||
private native int epollCreate();
|
||||
private native void epollCtl(int epfd, int opcode, int fd, int events);
|
||||
private native int epollWait(long pollAddress, int numfds, long timeout,
|
||||
int epfd) throws IOException;
|
||||
private static native int sizeofEPollEvent();
|
||||
private static native int offsetofData();
|
||||
private static native void interrupt(int fd);
|
||||
private static native void init();
|
||||
}
|
|
@ -30,7 +30,13 @@ import java.io.IOException;
|
|||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.concurrent.RejectedExecutionException;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import static sun.nio.ch.EPoll.*;
|
||||
|
||||
import static sun.nio.ch.EPoll.EPOLLIN;
|
||||
import static sun.nio.ch.EPoll.EPOLLONESHOT;
|
||||
import static sun.nio.ch.EPoll.EPOLL_CTL_ADD;
|
||||
import static sun.nio.ch.EPoll.EPOLL_CTL_DEL;
|
||||
import static sun.nio.ch.EPoll.EPOLL_CTL_MOD;
|
||||
|
||||
|
||||
/**
|
||||
* AsynchronousChannelGroup implementation based on the Linux epoll facility.
|
||||
|
@ -48,6 +54,9 @@ final class EPollPort
|
|||
// epoll file descriptor
|
||||
private final int epfd;
|
||||
|
||||
// address of the poll array passed to epoll_wait
|
||||
private final long address;
|
||||
|
||||
// true if epoll closed
|
||||
private boolean closed;
|
||||
|
||||
|
@ -57,9 +66,6 @@ final class EPollPort
|
|||
// number of wakeups pending
|
||||
private final AtomicInteger wakeupCount = new AtomicInteger();
|
||||
|
||||
// address of the poll array passed to epoll_wait
|
||||
private final long address;
|
||||
|
||||
// encapsulates an event for a channel
|
||||
static class Event {
|
||||
final PollableChannel channel;
|
||||
|
@ -85,23 +91,21 @@ final class EPollPort
|
|||
{
|
||||
super(provider, pool);
|
||||
|
||||
// open epoll
|
||||
this.epfd = epollCreate();
|
||||
this.epfd = EPoll.create();
|
||||
this.address = EPoll.allocatePollArray(MAX_EPOLL_EVENTS);
|
||||
|
||||
// create socket pair for wakeup mechanism
|
||||
int[] sv = new int[2];
|
||||
try {
|
||||
socketpair(sv);
|
||||
// register one end with epoll
|
||||
epollCtl(epfd, EPOLL_CTL_ADD, sv[0], EPOLLIN);
|
||||
} catch (IOException x) {
|
||||
close0(epfd);
|
||||
throw x;
|
||||
long fds = IOUtil.makePipe(true);
|
||||
this.sp = new int[]{(int) (fds >>> 32), (int) fds};
|
||||
} catch (IOException ioe) {
|
||||
EPoll.freePollArray(address);
|
||||
FileDispatcherImpl.closeIntFD(epfd);
|
||||
throw ioe;
|
||||
}
|
||||
this.sp = sv;
|
||||
|
||||
// allocate the poll array
|
||||
this.address = allocatePollArray(MAX_EPOLL_EVENTS);
|
||||
// register one end with epoll
|
||||
EPoll.ctl(epfd, EPOLL_CTL_ADD, sp[0], EPOLLIN);
|
||||
|
||||
// create the queue and offer the special event to ensure that the first
|
||||
// threads polls
|
||||
|
@ -123,17 +127,17 @@ final class EPollPort
|
|||
return;
|
||||
closed = true;
|
||||
}
|
||||
freePollArray(address);
|
||||
close0(sp[0]);
|
||||
close0(sp[1]);
|
||||
close0(epfd);
|
||||
try { FileDispatcherImpl.closeIntFD(epfd); } catch (IOException ioe) { }
|
||||
try { FileDispatcherImpl.closeIntFD(sp[0]); } catch (IOException ioe) { }
|
||||
try { FileDispatcherImpl.closeIntFD(sp[1]); } catch (IOException ioe) { }
|
||||
EPoll.freePollArray(address);
|
||||
}
|
||||
|
||||
private void wakeup() {
|
||||
if (wakeupCount.incrementAndGet() == 1) {
|
||||
// write byte to socketpair to force wakeup
|
||||
try {
|
||||
interrupt(sp[1]);
|
||||
IOUtil.write1(sp[1], (byte)0);
|
||||
} catch (IOException x) {
|
||||
throw new AssertionError(x);
|
||||
}
|
||||
|
@ -171,9 +175,9 @@ final class EPollPort
|
|||
@Override
|
||||
void startPoll(int fd, int events) {
|
||||
// update events (or add to epoll on first usage)
|
||||
int err = epollCtl(epfd, EPOLL_CTL_MOD, fd, (events | EPOLLONESHOT));
|
||||
int err = EPoll.ctl(epfd, EPOLL_CTL_MOD, fd, (events | EPOLLONESHOT));
|
||||
if (err == ENOENT)
|
||||
err = epollCtl(epfd, EPOLL_CTL_ADD, fd, (events | EPOLLONESHOT));
|
||||
err = EPoll.ctl(epfd, EPOLL_CTL_ADD, fd, (events | EPOLLONESHOT));
|
||||
if (err != 0)
|
||||
throw new AssertionError(); // should not happen
|
||||
}
|
||||
|
@ -191,7 +195,11 @@ final class EPollPort
|
|||
private Event poll() throws IOException {
|
||||
try {
|
||||
for (;;) {
|
||||
int n = epollWait(epfd, address, MAX_EPOLL_EVENTS);
|
||||
int n;
|
||||
do {
|
||||
n = EPoll.wait(epfd, address, MAX_EPOLL_EVENTS, -1);
|
||||
} while (n == IOStatus.INTERRUPTED);
|
||||
|
||||
/*
|
||||
* 'n' events have been read. Here we map them to their
|
||||
* corresponding channel in batch and queue n-1 so that
|
||||
|
@ -201,14 +209,14 @@ final class EPollPort
|
|||
fdToChannelLock.readLock().lock();
|
||||
try {
|
||||
while (n-- > 0) {
|
||||
long eventAddress = getEvent(address, n);
|
||||
int fd = getDescriptor(eventAddress);
|
||||
long eventAddress = EPoll.getEvent(address, n);
|
||||
int fd = EPoll.getDescriptor(eventAddress);
|
||||
|
||||
// wakeup
|
||||
if (fd == sp[0]) {
|
||||
if (wakeupCount.decrementAndGet() == 0) {
|
||||
// no more wakeups so drain pipe
|
||||
drain1(sp[0]);
|
||||
IOUtil.drain(sp[0]);
|
||||
}
|
||||
|
||||
// queue special event if there are more events
|
||||
|
@ -222,7 +230,7 @@ final class EPollPort
|
|||
|
||||
PollableChannel channel = fdToChannel.get(fd);
|
||||
if (channel != null) {
|
||||
int events = getEvents(eventAddress);
|
||||
int events = EPoll.getEvents(eventAddress);
|
||||
Event ev = new Event(channel, events);
|
||||
|
||||
// n-1 events are queued; This thread handles
|
||||
|
@ -306,18 +314,4 @@ final class EPollPort
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
// -- Native methods --
|
||||
|
||||
private static native void socketpair(int[] sv) throws IOException;
|
||||
|
||||
private static native void interrupt(int fd) throws IOException;
|
||||
|
||||
private static native void drain1(int fd) throws IOException;
|
||||
|
||||
private static native void close0(int fd);
|
||||
|
||||
static {
|
||||
IOUtil.load();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,33 +26,59 @@
|
|||
package sun.nio.ch;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.channels.*;
|
||||
import java.nio.channels.spi.*;
|
||||
import java.util.*;
|
||||
import java.nio.channels.ClosedSelectorException;
|
||||
import java.nio.channels.SelectableChannel;
|
||||
import java.nio.channels.SelectionKey;
|
||||
import java.nio.channels.Selector;
|
||||
import java.nio.channels.spi.SelectorProvider;
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.BitSet;
|
||||
import java.util.Deque;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static sun.nio.ch.EPoll.EPOLLIN;
|
||||
import static sun.nio.ch.EPoll.EPOLL_CTL_ADD;
|
||||
import static sun.nio.ch.EPoll.EPOLL_CTL_DEL;
|
||||
import static sun.nio.ch.EPoll.EPOLL_CTL_MOD;
|
||||
|
||||
|
||||
/**
|
||||
* An implementation of Selector for Linux 2.6+ kernels that uses
|
||||
* the epoll event notification facility.
|
||||
* Linux epoll based Selector implementation
|
||||
*/
|
||||
class EPollSelectorImpl
|
||||
extends SelectorImpl
|
||||
{
|
||||
// File descriptors used for interrupt
|
||||
|
||||
class EPollSelectorImpl extends SelectorImpl {
|
||||
|
||||
// maximum number of events to poll in one call to epoll_wait
|
||||
private static final int NUM_EPOLLEVENTS = Math.min(IOUtil.fdLimit(), 1024);
|
||||
|
||||
// epoll file descriptor
|
||||
private final int epfd;
|
||||
|
||||
// address of poll array when polling with epoll_wait
|
||||
private final long pollArrayAddress;
|
||||
|
||||
// file descriptors used for interrupt
|
||||
private final int fd0;
|
||||
private final int fd1;
|
||||
|
||||
// The poll object
|
||||
private final EPollArrayWrapper pollWrapper;
|
||||
// maps file descriptor to selection key, synchronize on selector
|
||||
private final Map<Integer, SelectionKeyImpl> fdToKey = new HashMap<>();
|
||||
|
||||
// Maps from file descriptors to keys
|
||||
private final Map<Integer, SelectionKeyImpl> fdToKey;
|
||||
// file descriptors registered with epoll, synchronize on selector
|
||||
private final BitSet registered = new BitSet();
|
||||
|
||||
// True if this Selector has been closed
|
||||
private volatile boolean closed;
|
||||
// pending new registrations/updates, queued by implRegister and putEventOps
|
||||
private final Object updateLock = new Object();
|
||||
private final Deque<SelectionKeyImpl> newKeys = new ArrayDeque<>();
|
||||
private final Deque<SelectionKeyImpl> updateKeys = new ArrayDeque<>();
|
||||
private final Deque<Integer> updateOps = new ArrayDeque<>();
|
||||
|
||||
// Lock for interrupt triggering and clearing
|
||||
// interrupt triggering and clearing
|
||||
private final Object interruptLock = new Object();
|
||||
private boolean interruptTriggered = false;
|
||||
private boolean interruptTriggered;
|
||||
|
||||
/**
|
||||
* Package private constructor called by factory method in
|
||||
|
@ -60,40 +86,57 @@ class EPollSelectorImpl
|
|||
*/
|
||||
EPollSelectorImpl(SelectorProvider sp) throws IOException {
|
||||
super(sp);
|
||||
long pipeFds = IOUtil.makePipe(false);
|
||||
fd0 = (int) (pipeFds >>> 32);
|
||||
fd1 = (int) pipeFds;
|
||||
|
||||
this.epfd = EPoll.create();
|
||||
this.pollArrayAddress = EPoll.allocatePollArray(NUM_EPOLLEVENTS);
|
||||
|
||||
try {
|
||||
pollWrapper = new EPollArrayWrapper(fd0, fd1);
|
||||
fdToKey = new HashMap<>();
|
||||
} catch (Throwable t) {
|
||||
try {
|
||||
FileDispatcherImpl.closeIntFD(fd0);
|
||||
} catch (IOException ioe0) {
|
||||
t.addSuppressed(ioe0);
|
||||
}
|
||||
try {
|
||||
FileDispatcherImpl.closeIntFD(fd1);
|
||||
} catch (IOException ioe1) {
|
||||
t.addSuppressed(ioe1);
|
||||
}
|
||||
throw t;
|
||||
long fds = IOUtil.makePipe(false);
|
||||
this.fd0 = (int) (fds >>> 32);
|
||||
this.fd1 = (int) fds;
|
||||
} catch (IOException ioe) {
|
||||
EPoll.freePollArray(pollArrayAddress);
|
||||
FileDispatcherImpl.closeIntFD(epfd);
|
||||
throw ioe;
|
||||
}
|
||||
|
||||
// register one end of the socket pair for wakeups
|
||||
EPoll.ctl(epfd, EPOLL_CTL_ADD, fd0, EPOLLIN);
|
||||
}
|
||||
|
||||
private void ensureOpen() {
|
||||
if (closed)
|
||||
if (!isOpen())
|
||||
throw new ClosedSelectorException();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int doSelect(long timeout) throws IOException {
|
||||
ensureOpen();
|
||||
assert Thread.holdsLock(this);
|
||||
|
||||
int numEntries;
|
||||
processUpdateQueue();
|
||||
processDeregisterQueue();
|
||||
try {
|
||||
begin();
|
||||
numEntries = pollWrapper.poll(timeout);
|
||||
|
||||
// epoll_wait timeout is int
|
||||
int to = (int) Math.min(timeout, Integer.MAX_VALUE);
|
||||
boolean timedPoll = (to > 0);
|
||||
do {
|
||||
long startTime = timedPoll ? System.nanoTime() : 0;
|
||||
numEntries = EPoll.wait(epfd, pollArrayAddress, NUM_EPOLLEVENTS, to);
|
||||
if (numEntries == IOStatus.INTERRUPTED && timedPoll) {
|
||||
// timed poll interrupted so need to adjust timeout
|
||||
long adjust = System.nanoTime() - startTime;
|
||||
to -= TimeUnit.MILLISECONDS.convert(adjust, TimeUnit.NANOSECONDS);
|
||||
if (to <= 0) {
|
||||
// timeout expired so no retry
|
||||
numEntries = 0;
|
||||
}
|
||||
}
|
||||
} while (numEntries == IOStatus.INTERRUPTED);
|
||||
assert IOStatus.check(numEntries);
|
||||
|
||||
} finally {
|
||||
end();
|
||||
}
|
||||
|
@ -101,21 +144,70 @@ class EPollSelectorImpl
|
|||
return updateSelectedKeys(numEntries);
|
||||
}
|
||||
|
||||
/**
|
||||
* Process new registrations and changes to the interest ops.
|
||||
*/
|
||||
private void processUpdateQueue() {
|
||||
assert Thread.holdsLock(this);
|
||||
|
||||
synchronized (updateLock) {
|
||||
SelectionKeyImpl ski;
|
||||
|
||||
// new registrations
|
||||
while ((ski = newKeys.pollFirst()) != null) {
|
||||
if (ski.isValid()) {
|
||||
SelChImpl ch = ski.channel;
|
||||
int fd = ch.getFDVal();
|
||||
SelectionKeyImpl previous = fdToKey.put(fd, ski);
|
||||
assert previous == null;
|
||||
assert registered.get(fd) == false;
|
||||
}
|
||||
}
|
||||
|
||||
// changes to interest ops
|
||||
assert updateKeys.size() == updateOps.size();
|
||||
while ((ski = updateKeys.pollFirst()) != null) {
|
||||
int ops = updateOps.pollFirst();
|
||||
int fd = ski.channel.getFDVal();
|
||||
if (ski.isValid() && fdToKey.containsKey(fd)) {
|
||||
if (registered.get(fd)) {
|
||||
if (ops == 0) {
|
||||
// remove from epoll
|
||||
EPoll.ctl(epfd, EPOLL_CTL_DEL, fd, 0);
|
||||
registered.clear(fd);
|
||||
} else {
|
||||
// modify events
|
||||
EPoll.ctl(epfd, EPOLL_CTL_MOD, fd, ops);
|
||||
}
|
||||
} else if (ops != 0) {
|
||||
// add to epoll
|
||||
EPoll.ctl(epfd, EPOLL_CTL_ADD, fd, ops);
|
||||
registered.set(fd);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the keys whose fd's have been selected by the epoll.
|
||||
* Add the ready keys to the ready queue.
|
||||
*/
|
||||
private int updateSelectedKeys(int numEntries) throws IOException {
|
||||
assert Thread.holdsLock(this);
|
||||
assert Thread.holdsLock(nioSelectedKeys());
|
||||
|
||||
boolean interrupted = false;
|
||||
int numKeysUpdated = 0;
|
||||
for (int i=0; i<numEntries; i++) {
|
||||
int nextFD = pollWrapper.getDescriptor(i);
|
||||
if (nextFD == fd0) {
|
||||
long event = EPoll.getEvent(pollArrayAddress, i);
|
||||
int fd = EPoll.getDescriptor(event);
|
||||
if (fd == fd0) {
|
||||
interrupted = true;
|
||||
} else {
|
||||
SelectionKeyImpl ski = fdToKey.get(Integer.valueOf(nextFD));
|
||||
SelectionKeyImpl ski = fdToKey.get(fd);
|
||||
if (ski != null) {
|
||||
int rOps = pollWrapper.getEventOps(i);
|
||||
int rOps = EPoll.getEvents(event);
|
||||
if (selectedKeys.contains(ski)) {
|
||||
if (ski.channel.translateAndSetReadyOps(rOps, ski)) {
|
||||
numKeysUpdated++;
|
||||
|
@ -140,16 +232,17 @@ class EPollSelectorImpl
|
|||
|
||||
@Override
|
||||
protected void implClose() throws IOException {
|
||||
if (closed)
|
||||
return;
|
||||
closed = true;
|
||||
assert Thread.holdsLock(this);
|
||||
assert Thread.holdsLock(nioKeys());
|
||||
|
||||
// prevent further wakeup
|
||||
synchronized (interruptLock) {
|
||||
interruptTriggered = true;
|
||||
}
|
||||
|
||||
pollWrapper.close();
|
||||
FileDispatcherImpl.closeIntFD(epfd);
|
||||
EPoll.freePollArray(pollArrayAddress);
|
||||
|
||||
FileDispatcherImpl.closeIntFD(fd0);
|
||||
FileDispatcherImpl.closeIntFD(fd1);
|
||||
|
||||
|
@ -167,42 +260,57 @@ class EPollSelectorImpl
|
|||
|
||||
@Override
|
||||
protected void implRegister(SelectionKeyImpl ski) {
|
||||
assert Thread.holdsLock(nioKeys());
|
||||
ensureOpen();
|
||||
SelChImpl ch = ski.channel;
|
||||
int fd = Integer.valueOf(ch.getFDVal());
|
||||
fdToKey.put(fd, ski);
|
||||
pollWrapper.add(fd);
|
||||
synchronized (updateLock) {
|
||||
newKeys.addLast(ski);
|
||||
}
|
||||
keys.add(ski);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void implDereg(SelectionKeyImpl ski) throws IOException {
|
||||
assert (ski.getIndex() >= 0);
|
||||
SelChImpl ch = ski.channel;
|
||||
int fd = ch.getFDVal();
|
||||
fdToKey.remove(Integer.valueOf(fd));
|
||||
pollWrapper.remove(fd);
|
||||
ski.setIndex(-1);
|
||||
keys.remove(ski);
|
||||
assert !ski.isValid();
|
||||
assert Thread.holdsLock(this);
|
||||
assert Thread.holdsLock(nioKeys());
|
||||
assert Thread.holdsLock(nioSelectedKeys());
|
||||
|
||||
int fd = ski.channel.getFDVal();
|
||||
fdToKey.remove(fd);
|
||||
if (registered.get(fd)) {
|
||||
EPoll.ctl(epfd, EPOLL_CTL_DEL, fd, 0);
|
||||
registered.clear(fd);
|
||||
}
|
||||
|
||||
selectedKeys.remove(ski);
|
||||
keys.remove(ski);
|
||||
|
||||
// remove from channel's key set
|
||||
deregister(ski);
|
||||
|
||||
SelectableChannel selch = ski.channel();
|
||||
if (!selch.isOpen() && !selch.isRegistered())
|
||||
((SelChImpl)selch).kill();
|
||||
((SelChImpl) selch).kill();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putEventOps(SelectionKeyImpl ski, int ops) {
|
||||
ensureOpen();
|
||||
SelChImpl ch = ski.channel;
|
||||
pollWrapper.setInterest(ch.getFDVal(), ops);
|
||||
synchronized (updateLock) {
|
||||
updateOps.addLast(ops); // ops first in case adding the key fails
|
||||
updateKeys.addLast(ski);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Selector wakeup() {
|
||||
synchronized (interruptLock) {
|
||||
if (!interruptTriggered) {
|
||||
pollWrapper.interrupt();
|
||||
try {
|
||||
IOUtil.write1(fd1, (byte)0);
|
||||
} catch (IOException ioe) {
|
||||
throw new InternalError(ioe);
|
||||
}
|
||||
interruptTriggered = true;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue