Callback Based Selectors

Alan Bateman Alan.Bateman at oracle.com
Wed Mar 28 17:09:31 UTC 2018


On 28/03/2018 15:45, David Lloyd wrote:
> :
> I wonder, since we're moving these APIs to be more
> concurrency-friendly, if you could consider an additional small
> enhancement to add a couple atomic operations to SelectionKey for
> interest ops like getAndSetBits/getAndClearBits?  They would only have
> to be atomic with respect to each other, so they could be emulated
> with locks for compatibility.  At present, coordinating adding
> interest ops across threads means adding a lock, which is super
> heavyweight considering that interest ops are backed a volatile int
> field in most if not all cases.
>
> I could work up a patch if you want; since you've been so busy in the
> NIO area lately I've been hesitant to try anything in that area, at
> least until things settle down a little.
interestOpsOr(int ops) and interestOpsAnd(int ops) wouldn't look out of 
place. It would be a good use of var handles as you could implement it 
with something like:

static final VarHandle INTERESTOPS;
static {
     try {
         MethodHandles.Lookup l = MethodHandles.lookup();
         INTERESTOPS = l.findVarHandle(SelectionKey.class, 
"interestOps", int.class);
     } catch (Exception e) {
         throw new InternalError(e);
     }
}

public SelectionKey interestOpsOr(int ops) {
     :
     INTERESTOPS.getAndBitwiseOr(this, ops);
     :
}

There's one patch pending that changes the the code in nioInterestOps in 
how queue changes but I don't have other changes in my patch queue that  
SelectionKeyImpl. So go ahead!

-Alan




More information about the nio-dev mailing list