mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-27 14:54:52 +02:00
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:
parent
dce30cad15
commit
2466623e45
3 changed files with 302 additions and 1 deletions
|
@ -189,6 +189,83 @@ public abstract class SelectionKey {
|
|||
*/
|
||||
public abstract SelectionKey interestOps(int ops);
|
||||
|
||||
/**
|
||||
* Atomically sets this key's interest set to the bitwise union ("or") of
|
||||
* the existing interest set and the given value. This method is guaranteed
|
||||
* to be atomic with respect to other concurrent calls to this method or to
|
||||
* {@link #interestOpsAnd(int)}.
|
||||
*
|
||||
* <p> This method may be invoked at any time. If this method is invoked
|
||||
* while a selection operation is in progress then it has no effect upon
|
||||
* that operation; the change to the key's interest set will be seen by the
|
||||
* next selection operation.
|
||||
*
|
||||
* @implSpec The default implementation synchronizes on this key and invokes
|
||||
* {@code interestOps()} and {@code interestOps(int)} to retrieve and set
|
||||
* this key's interest set.
|
||||
*
|
||||
* @param ops The interest set to apply
|
||||
*
|
||||
* @return The previous interest set
|
||||
*
|
||||
* @throws IllegalArgumentException
|
||||
* If a bit in the set does not correspond to an operation that
|
||||
* is supported by this key's channel, that is, if
|
||||
* {@code (ops & ~channel().validOps()) != 0}
|
||||
*
|
||||
* @throws CancelledKeyException
|
||||
* If this key has been cancelled
|
||||
*
|
||||
* @since 11
|
||||
*/
|
||||
public int interestOpsOr(int ops) {
|
||||
synchronized (this) {
|
||||
int oldVal = interestOps();
|
||||
interestOps(oldVal | ops);
|
||||
return oldVal;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Atomically sets this key's interest set to the bitwise intersection ("and")
|
||||
* of the existing interest set and the given value. This method is guaranteed
|
||||
* to be atomic with respect to other concurrent calls to this method or to
|
||||
* {@link #interestOpsOr(int)}.
|
||||
*
|
||||
* <p> This method may be invoked at any time. If this method is invoked
|
||||
* while a selection operation is in progress then it has no effect upon
|
||||
* that operation; the change to the key's interest set will be seen by the
|
||||
* next selection operation.
|
||||
*
|
||||
* @apiNote Unlike the {@code interestOps(int)} and {@code interestOpsOr(int)}
|
||||
* methods, this method does not throw {@code IllegalArgumentException} when
|
||||
* invoked with bits in the interest set that do not correspond to an
|
||||
* operation that is supported by this key's channel. This is to allow
|
||||
* operation bits in the interest set to be cleared using bitwise complement
|
||||
* values, e.g., {@code interestOpsAnd(~SelectionKey.OP_READ)} will remove
|
||||
* the {@code OP_READ} from the interest set without affecting other bits.
|
||||
*
|
||||
* @implSpec The default implementation synchronizes on this key and invokes
|
||||
* {@code interestOps()} and {@code interestOps(int)} to retrieve and set
|
||||
* this key's interest set.
|
||||
*
|
||||
* @param ops The interest set to apply
|
||||
*
|
||||
* @return The previous interest set
|
||||
*
|
||||
* @throws CancelledKeyException
|
||||
* If this key has been cancelled
|
||||
*
|
||||
* @since 11
|
||||
*/
|
||||
public int interestOpsAnd(int ops) {
|
||||
synchronized (this) {
|
||||
int oldVal = interestOps();
|
||||
interestOps(oldVal & ops);
|
||||
return oldVal;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves this key's ready-operation set.
|
||||
*
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue