8200583: (se) Selector clean-up, part 4

Reviewed-by: bpb, chegar
This commit is contained in:
Alan Bateman 2018-04-05 15:01:57 +01:00
parent 9968548d57
commit d185d65b69
11 changed files with 156 additions and 269 deletions

View file

@ -49,7 +49,7 @@ public interface SelChImpl extends Channel {
* contains at least one bit that the previous value did not
* contain
*/
boolean translateAndUpdateReadyOps(int ops, SelectionKeyImpl sk);
boolean translateAndUpdateReadyOps(int ops, SelectionKeyImpl ski);
/**
* Sets the specified ops if present in interestOps. The specified
@ -59,7 +59,7 @@ public interface SelChImpl extends Channel {
* contains at least one bit that the previous value did not
* contain
*/
boolean translateAndSetReadyOps(int ops, SelectionKeyImpl sk);
boolean translateAndSetReadyOps(int ops, SelectionKeyImpl ski);
/**
* Translates an interest operation set into a native event set

View file

@ -39,7 +39,7 @@ import java.nio.channels.spi.AbstractSelectionKey;
public final class SelectionKeyImpl
extends AbstractSelectionKey
{
final SelChImpl channel; // package-private
private final SelChImpl channel;
private final SelectorImpl selector;
private volatile int interestOps;
@ -61,6 +61,10 @@ public final class SelectionKeyImpl
throw new CancelledKeyException();
}
int getFDVal() {
return channel.getFDVal();
}
@Override
public SelectableChannel channel() {
return (SelectableChannel)channel;
@ -103,8 +107,8 @@ public final class SelectionKeyImpl
public SelectionKey nioInterestOps(int ops) {
if ((ops & ~channel().validOps()) != 0)
throw new IllegalArgumentException();
selector.putEventOps(this, channel.translateInterestOps(ops));
interestOps = ops;
selector.setEventOps(this);
return this;
}
@ -112,6 +116,18 @@ public final class SelectionKeyImpl
return interestOps;
}
int translateInterestOps() {
return channel.translateInterestOps(interestOps);
}
boolean translateAndSetReadyOps(int ops) {
return channel.translateAndSetReadyOps(ops, this);
}
boolean translateAndUpdateReadyOps(int ops) {
return channel.translateAndUpdateReadyOps(ops, this);
}
void registeredEvents(int events) {
// assert Thread.holdsLock(selector);
this.registeredEvents = events;

View file

@ -64,17 +64,20 @@ public abstract class SelectorImpl
publicSelectedKeys = Util.ungrowableSet(selectedKeys);
}
@Override
public final Set<SelectionKey> keys() {
private void ensureOpen() {
if (!isOpen())
throw new ClosedSelectorException();
}
@Override
public final Set<SelectionKey> keys() {
ensureOpen();
return publicKeys;
}
@Override
public final Set<SelectionKey> selectedKeys() {
if (!isOpen())
throw new ClosedSelectorException();
ensureOpen();
return publicSelectedKeys;
}
@ -112,8 +115,7 @@ public abstract class SelectorImpl
private int lockAndDoSelect(long timeout) throws IOException {
synchronized (this) {
if (!isOpen())
throw new ClosedSelectorException();
ensureOpen();
synchronized (publicKeys) {
synchronized (publicSelectedKeys) {
return doSelect(timeout);
@ -176,7 +178,8 @@ public abstract class SelectorImpl
throw new IllegalSelectorException();
SelectionKeyImpl k = new SelectionKeyImpl((SelChImpl)ch, this);
k.attach(attachment);
// register before adding to key set
// register with selector (if needed) before adding to key set
implRegister(k);
synchronized (publicKeys) {
keys.add(k);
@ -185,7 +188,15 @@ public abstract class SelectorImpl
return k;
}
protected abstract void implRegister(SelectionKeyImpl ski);
/**
* Register the key in the selector.
*
* The default implementation checks if the selector is open. It should
* be overridden by selector implementations as needed.
*/
protected void implRegister(SelectionKeyImpl ski) {
ensureOpen();
}
protected abstract void implDereg(SelectionKeyImpl ski) throws IOException;
@ -222,5 +233,5 @@ public abstract class SelectorImpl
/**
* Change the event set in the selector
*/
protected abstract void putEventOps(SelectionKeyImpl ski, int events);
protected abstract void setEventOps(SelectionKeyImpl ski);
}