8199611: (se) Minor selector implementation clean-up

Reviewed-by: clanger, redestad, bpb
This commit is contained in:
Alan Bateman 2018-03-15 10:47:58 +00:00
parent 8994d5ad0e
commit 3a7f72200c
15 changed files with 238 additions and 249 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 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
@ -59,6 +59,10 @@ class EPoll {
static final int EPOLL_CTL_DEL = 2;
static final int EPOLL_CTL_MOD = 3;
// events
static final int EPOLLIN = 0x1;
static final int EPOLLOUT = 0x4;
// flags
static final int EPOLLONESHOT = (1 << 30);

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved.
* 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
@ -93,16 +93,10 @@ class EPollArrayWrapper {
private final long pollArrayAddress;
// The fd of the interrupt line going out
private int outgoingInterruptFD;
// The fd of the interrupt line coming in
private int incomingInterruptFD;
// The index of the interrupt FD
private int interruptedIndex;
private final int outgoingInterruptFD;
// Number of updated pollfd entries
int updated;
private int updated;
// object to synchronize fd registration changes
private final Object updateLock = new Object();
@ -125,7 +119,7 @@ class EPollArrayWrapper {
private final BitSet registered = new BitSet();
EPollArrayWrapper() throws IOException {
EPollArrayWrapper(int fd0, int fd1) throws IOException {
// creates the epoll file descriptor
epfd = epollCreate();
@ -133,11 +127,8 @@ class EPollArrayWrapper {
int allocationSize = NUM_EPOLLEVENTS * SIZE_EPOLLEVENT;
pollArray = new AllocatedNativeObject(allocationSize, true);
pollArrayAddress = pollArray.address();
}
void initInterrupt(int fd0, int fd1) {
outgoingInterruptFD = fd1;
incomingInterruptFD = fd0;
epollCtl(epfd, EPOLL_CTL_ADD, fd0, EPOLLIN);
}
@ -255,22 +246,14 @@ class EPollArrayWrapper {
/**
* Close epoll file descriptor and free poll array
*/
void closeEPollFD() throws IOException {
void close() throws IOException {
FileDispatcherImpl.closeIntFD(epfd);
pollArray.free();
}
int poll(long timeout) throws IOException {
updateRegistrations();
updated = epollWait(pollArrayAddress, NUM_EPOLLEVENTS, timeout, epfd);
for (int i=0; i<updated; i++) {
if (getDescriptor(i) == incomingInterruptFD) {
interruptedIndex = i;
interrupted = true;
break;
}
}
return updated;
return epollWait(pollArrayAddress, NUM_EPOLLEVENTS, timeout, epfd);
}
/**
@ -306,25 +289,10 @@ class EPollArrayWrapper {
}
}
// interrupt support
private boolean interrupted = false;
public void interrupt() {
interrupt(outgoingInterruptFD);
}
public int interruptedIndex() {
return interruptedIndex;
}
boolean interrupted() {
return interrupted;
}
void clearInterrupted() {
interrupted = false;
}
static {
IOUtil.load();
init();

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 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
@ -93,7 +93,7 @@ final class EPollPort
try {
socketpair(sv);
// register one end with epoll
epollCtl(epfd, EPOLL_CTL_ADD, sv[0], Net.POLLIN);
epollCtl(epfd, EPOLL_CTL_ADD, sv[0], EPOLLIN);
} catch (IOException x) {
close0(epfd);
throw x;

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
* 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
@ -37,16 +37,15 @@ import java.util.*;
class EPollSelectorImpl
extends SelectorImpl
{
// File descriptors used for interrupt
protected int fd0;
protected int fd1;
private final int fd0;
private final int fd1;
// The poll object
EPollArrayWrapper pollWrapper;
private final EPollArrayWrapper pollWrapper;
// Maps from file descriptors to keys
private Map<Integer,SelectionKeyImpl> fdToKey;
private final Map<Integer, SelectionKeyImpl> fdToKey;
// True if this Selector has been closed
private volatile boolean closed;
@ -65,8 +64,7 @@ class EPollSelectorImpl
fd0 = (int) (pipeFds >>> 32);
fd1 = (int) pipeFds;
try {
pollWrapper = new EPollArrayWrapper();
pollWrapper.initInterrupt(fd0, fd1);
pollWrapper = new EPollArrayWrapper(fd0, fd1);
fdToKey = new HashMap<>();
} catch (Throwable t) {
try {
@ -83,59 +81,64 @@ class EPollSelectorImpl
}
}
protected int doSelect(long timeout) throws IOException {
private void ensureOpen() {
if (closed)
throw new ClosedSelectorException();
}
@Override
protected int doSelect(long timeout) throws IOException {
ensureOpen();
int numEntries;
processDeregisterQueue();
try {
begin();
pollWrapper.poll(timeout);
numEntries = pollWrapper.poll(timeout);
} finally {
end();
}
processDeregisterQueue();
int numKeysUpdated = updateSelectedKeys();
if (pollWrapper.interrupted()) {
// Clear the wakeup pipe
pollWrapper.putEventOps(pollWrapper.interruptedIndex(), 0);
synchronized (interruptLock) {
pollWrapper.clearInterrupted();
IOUtil.drain(fd0);
interruptTriggered = false;
}
}
return numKeysUpdated;
return updateSelectedKeys(numEntries);
}
/**
* Update the keys whose fd's have been selected by the epoll.
* Add the ready keys to the ready queue.
*/
private int updateSelectedKeys() {
int entries = pollWrapper.updated;
private int updateSelectedKeys(int numEntries) throws IOException {
boolean interrupted = false;
int numKeysUpdated = 0;
for (int i=0; i<entries; i++) {
for (int i=0; i<numEntries; i++) {
int nextFD = pollWrapper.getDescriptor(i);
SelectionKeyImpl ski = fdToKey.get(Integer.valueOf(nextFD));
// ski is null in the case of an interrupt
if (ski != null) {
int rOps = pollWrapper.getEventOps(i);
if (selectedKeys.contains(ski)) {
if (ski.channel.translateAndSetReadyOps(rOps, ski)) {
numKeysUpdated++;
}
} else {
ski.channel.translateAndSetReadyOps(rOps, ski);
if ((ski.nioReadyOps() & ski.nioInterestOps()) != 0) {
selectedKeys.add(ski);
numKeysUpdated++;
if (nextFD == fd0) {
interrupted = true;
} else {
SelectionKeyImpl ski = fdToKey.get(Integer.valueOf(nextFD));
if (ski != null) {
int rOps = pollWrapper.getEventOps(i);
if (selectedKeys.contains(ski)) {
if (ski.channel.translateAndSetReadyOps(rOps, ski)) {
numKeysUpdated++;
}
} else {
ski.channel.translateAndSetReadyOps(rOps, ski);
if ((ski.nioReadyOps() & ski.nioInterestOps()) != 0) {
selectedKeys.add(ski);
numKeysUpdated++;
}
}
}
}
}
if (interrupted) {
clearInterrupt();
}
return numKeysUpdated;
}
@Override
protected void implClose() throws IOException {
if (closed)
return;
@ -146,13 +149,10 @@ class EPollSelectorImpl
interruptTriggered = true;
}
pollWrapper.close();
FileDispatcherImpl.closeIntFD(fd0);
FileDispatcherImpl.closeIntFD(fd1);
pollWrapper.closeEPollFD();
// it is possible
selectedKeys = null;
// Deregister channels
Iterator<SelectionKey> i = keys.iterator();
while (i.hasNext()) {
@ -163,14 +163,11 @@ class EPollSelectorImpl
((SelChImpl)selch).kill();
i.remove();
}
fd0 = -1;
fd1 = -1;
}
@Override
protected void implRegister(SelectionKeyImpl ski) {
if (closed)
throw new ClosedSelectorException();
ensureOpen();
SelChImpl ch = ski.channel;
int fd = Integer.valueOf(ch.getFDVal());
fdToKey.put(fd, ski);
@ -178,6 +175,7 @@ class EPollSelectorImpl
keys.add(ski);
}
@Override
protected void implDereg(SelectionKeyImpl ski) throws IOException {
assert (ski.getIndex() >= 0);
SelChImpl ch = ski.channel;
@ -187,19 +185,20 @@ class EPollSelectorImpl
ski.setIndex(-1);
keys.remove(ski);
selectedKeys.remove(ski);
deregister((AbstractSelectionKey)ski);
deregister(ski);
SelectableChannel selch = ski.channel();
if (!selch.isOpen() && !selch.isRegistered())
((SelChImpl)selch).kill();
}
@Override
public void putEventOps(SelectionKeyImpl ski, int ops) {
if (closed)
throw new ClosedSelectorException();
ensureOpen();
SelChImpl ch = ski.channel;
pollWrapper.setInterest(ch.getFDVal(), ops);
}
@Override
public Selector wakeup() {
synchronized (interruptLock) {
if (!interruptTriggered) {
@ -209,4 +208,11 @@ class EPollSelectorImpl
}
return this;
}
private void clearInterrupt() throws IOException {
synchronized (interruptLock) {
IOUtil.drain(fd0);
interruptTriggered = false;
}
}
}