6350055: (se) SelectionKey.interestOps variants to atomically update interest ops

Co-authored-by: David M Lloyd <david.lloyd@redhat.com>
Reviewed-by: bpb
This commit is contained in:
Alan Bateman 2018-06-07 09:18:39 +01:00
parent dce30cad15
commit 2466623e45
3 changed files with 302 additions and 1 deletions

View file

@ -25,6 +25,9 @@
package sun.nio.ch;
import java.lang.invoke.ConstantBootstraps;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;
import java.nio.channels.CancelledKeyException;
import java.nio.channels.SelectableChannel;
import java.nio.channels.SelectionKey;
@ -39,6 +42,13 @@ import java.nio.channels.spi.AbstractSelectionKey;
public final class SelectionKeyImpl
extends AbstractSelectionKey
{
private static final VarHandle INTERESTOPS =
ConstantBootstraps.fieldVarHandle(
MethodHandles.lookup(),
"interestOps",
VarHandle.class,
SelectionKeyImpl.class, int.class);
private final SelChImpl channel;
private final SelectorImpl selector;
@ -84,7 +94,35 @@ public final class SelectionKeyImpl
@Override
public SelectionKey interestOps(int ops) {
ensureValid();
return nioInterestOps(ops);
if ((ops & ~channel().validOps()) != 0)
throw new IllegalArgumentException();
int oldOps = (int) INTERESTOPS.getAndSet(this, ops);
if (ops != oldOps) {
selector.setEventOps(this);
}
return this;
}
@Override
public int interestOpsOr(int ops) {
ensureValid();
if ((ops & ~channel().validOps()) != 0)
throw new IllegalArgumentException();
int oldVal = (int) INTERESTOPS.getAndBitwiseOr(this, ops);
if (oldVal != (oldVal | ops)) {
selector.setEventOps(this);
}
return oldVal;
}
@Override
public int interestOpsAnd(int ops) {
ensureValid();
int oldVal = (int) INTERESTOPS.getAndBitwiseAnd(this, ops);
if (oldVal != (oldVal & ops)) {
selector.setEventOps(this);
}
return oldVal;
}
@Override