From 6acc1d27aca01b217616c781a5f6bb80e68c8316 Mon Sep 17 00:00:00 2001 From: fhanik Date: Wed, 14 May 2008 01:58:27 +0000 Subject: [PATCH] save a few cpu cycles to avoid exceptions being thrown when a key is invalid git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@656084 13f79535-47bb-0310-9956-ffa450edef68 --- .../tomcat/util/net/NioBlockingSelector.java | 25 +++++++++++++--------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/java/org/apache/tomcat/util/net/NioBlockingSelector.java b/java/org/apache/tomcat/util/net/NioBlockingSelector.java index 27117e3f4..3043cdd4c 100644 --- a/java/org/apache/tomcat/util/net/NioBlockingSelector.java +++ b/java/org/apache/tomcat/util/net/NioBlockingSelector.java @@ -193,8 +193,7 @@ public class NioBlockingSelector { protected class BlockPoller extends Thread { protected boolean run = true; protected Selector selector = null; - protected ConcurrentLinkedQueue events = - new ConcurrentLinkedQueue(); + protected ConcurrentLinkedQueue events = new ConcurrentLinkedQueue(); public void disable() { run = false; selector.wakeup();} protected AtomicInteger wakeupCounter = new AtomicInteger(0); public void cancelKey(final NioChannel socket, final SelectionKey key) { @@ -232,6 +231,8 @@ public class NioBlockingSelector { try { if (sk == null) { sk = ch.register(selector, ops, key); + } else if (!sk.isValid()) { + cancel(sk,key,ops); } else { sk.interestOps(sk.interestOps() | ops); } @@ -260,12 +261,17 @@ public class NioBlockingSelector { if (SelectionKey.OP_WRITE==(ops&SelectionKey.OP_WRITE)) countDown(key.getWriteLatch()); if (SelectionKey.OP_READ==(ops&SelectionKey.OP_READ))countDown(key.getReadLatch()); } else { - sk.interestOps(sk.interestOps() & (~ops)); - if (SelectionKey.OP_WRITE==(ops&SelectionKey.OP_WRITE)) countDown(key.getWriteLatch()); - if (SelectionKey.OP_READ==(ops&SelectionKey.OP_READ))countDown(key.getReadLatch()); - if (sk.interestOps()==0) { - sk.cancel(); - sk.attach(null); + if (sk.isValid()) { + sk.interestOps(sk.interestOps() & (~ops)); + if (SelectionKey.OP_WRITE==(ops&SelectionKey.OP_WRITE)) countDown(key.getWriteLatch()); + if (SelectionKey.OP_READ==(ops&SelectionKey.OP_READ))countDown(key.getReadLatch()); + if (sk.interestOps()==0) { + sk.cancel(); + sk.attach(null); + } + }else { + sk.cancel(); + sk.attach(null); } } }catch (CancelledKeyException cx) { @@ -321,8 +327,7 @@ public class NioBlockingSelector { continue; } - Iterator iterator = - keyCount > 0 ? selector.selectedKeys().iterator() : null; + Iterator iterator = keyCount > 0 ? selector.selectedKeys().iterator() : null; // Walk through the collection of ready keys and dispatch // any active event. -- 2.11.0