Callback Based Selectors
David Lloyd
david.lloyd at redhat.com
Thu Mar 29 21:22:51 UTC 2018
On Wed, Mar 28, 2018 at 3:15 PM, Alan Bateman <Alan.Bateman at oracle.com> wrote:
> I don't think there is an issue there as a sequence of callings to
> interestOpsXXX will just queue the translation of the interest ops.
OK I've given it another go. I haven't even been able to compile-test
it yet though due to [1]. In particular, I'm not certain I can
properly set up the VarHandle the way I have. But I wanted to review
the basic idea.
Using atomics, and assuming I understand how the op queueing is
supposed to work, we can actually optimize a bit because we can avoid
calling nioInterestOps(int) unless a new bit has been added which
wasn't there before.
The patch is attached and also at [2].
[1] http://mail.openjdk.java.net/pipermail/hotspot-dev/2018-March/030736.html
[2] https://github.com/dmlloyd/openjdk/commit/atomic-nio-ops-v2
--
- DML
-------------- next part --------------
commit 63569aeb836c3db97072d32e1c154ed4fa4b7800
Author: David M. Lloyd <david.lloyd at redhat.com>
Date: Wed Mar 28 13:13:44 2018 -0500
[JDK-0000000] Add atomic interest op methods to SelectionKey
diff --git a/src/java.base/share/classes/java/nio/channels/SelectionKey.java b/src/java.base/share/classes/java/nio/channels/SelectionKey.java
index 6af664fa96b..8983093ade2 100644
--- a/src/java.base/share/classes/java/nio/channels/SelectionKey.java
+++ b/src/java.base/share/classes/java/nio/channels/SelectionKey.java
@@ -196,6 +196,82 @@ public abstract class SelectionKey {
*/
public abstract SelectionKey interestOps(int ops);
+ /**
+ * Atomically sets this key's interest set to 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. Whether or not it blocks,
+ * and for how long, is implementation-dependent.
+ *
+ * @param ops The interest set to apply
+ *
+ * @return The previous interest set
+ *
+ * @throws IllegalArgumentException
+ * If a bit in the resultant set does not correspond to an operation that
+ * is supported by this key's channel, that is, if
+ * {@code ((interestOps() | ops) & ~channel().validOps()) != 0}
+ *
+ * @throws CancelledKeyException
+ * If this key has been cancelled
+ *
+ * @implSpec The default implementation synchronizes on this {@code SelectionKey}
+ * instance, calling {@link #interestOps()} and {@link #interestOps(int)}
+ * in turn; subclasses should provide a better implementation if
+ * possible (for example, using a
+ * {@link java.lang.invoke.VarHandle VarHandle} may be appropriate).
+ */
+ public int interestOpsOr(int ops) {
+ synchronized (this) {
+ int oldVal = interestOps();
+ interestOps(oldVal | ops);
+ return oldVal;
+ }
+ }
+
+ /**
+ * Atomically sets this key's interest set to 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. Whether or not it blocks,
+ * and for how long, is implementation-dependent.
+ *
+ * @param ops The interest set to apply
+ *
+ * @return The previous interest set
+ *
+ * @throws IllegalArgumentException
+ * If a bit in the resultant set does not correspond to an operation that
+ * is supported by this key's channel, that is, if
+ * {@code (interestOps() & ops & ~channel().validOps()) != 0}
+ *
+ * @throws CancelledKeyException
+ * If this key has been cancelled
+ *
+ * @apiNote The {@code ops} argument may contain bits which are not normally
+ * allowed by this key's channel, allowing bits to be cleared using
+ * bitwise complement values. For example,
+ * {@code interestOpsAnd(~SelectionKey.OP_READ)} will remove the
+ * {@code OP_READ} bit from the set without affecting other bits.
+ *
+ * @implSpec The default implementation synchronizes on this {@code SelectionKey}
+ * instance, calling {@link #interestOps()} and {@link #interestOps(int)}
+ * in turn; subclasses should provide a better implementation if
+ * possible (for example, using a
+ * {@link java.lang.invoke.VarHandle VarHandle} may be appropriate).
+ */
+ public int interestOpsAnd(int ops) {
+ synchronized (this) {
+ int oldVal = interestOps();
+ interestOps(oldVal & ops);
+ return oldVal;
+ }
+ }
+
/**
* Retrieves this key's ready-operation set.
*
diff --git a/src/java.base/share/classes/sun/nio/ch/SelectionKeyImpl.java b/src/java.base/share/classes/sun/nio/ch/SelectionKeyImpl.java
index d4b4c39297d..0e209991d34 100644
--- a/src/java.base/share/classes/sun/nio/ch/SelectionKeyImpl.java
+++ b/src/java.base/share/classes/sun/nio/ch/SelectionKeyImpl.java
@@ -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;
@@ -40,6 +43,10 @@ public final class SelectionKeyImpl
extends AbstractSelectionKey
{
+ private static final VarHandle interestOpsHandle = ConstantBootstraps.fieldVarHandle(
+ MethodHandles.lookup(), "interestOps", VarHandle.class, SelectionKeyImpl.class, int.class
+ );
+
final SelChImpl channel; // package-private
public final SelectorImpl selector;
@@ -86,7 +93,31 @@ public final class SelectionKeyImpl
@Override
public SelectionKey interestOps(int ops) {
ensureValid();
- return nioInterestOps(ops);
+ // newOps is the set of ops that were not previously set
+ int newOps = ops & ~ (int) interestOpsHandle.getAndSet(this, ops);
+ if (newOps != 0) {
+ nioInterestOps(ops);
+ }
+ return this;
+ }
+
+ @Override
+ public int interestOpsOr(final int ops) {
+ ensureValid();
+ int oldVal = (int) interestOpsHandle.getAndBitwiseOr(this, ops);
+ // newOps is the set of ops that were not previously set
+ int newOps = ops & ~oldVal;
+ if (newOps != 0) {
+ // add the ops to the selector
+ nioInterestOps(ops);
+ }
+ return oldVal;
+ }
+
+ @Override
+ public int interestOpsAnd(final int ops) {
+ ensureValid();
+ return (int) interestOpsHandle.getAndBitwiseAnd(this, ops);
}
@Override
More information about the nio-dev
mailing list