You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by el...@apache.org on 2013/01/04 18:02:00 UTC

svn commit: r1428982 - in /mina/mina/trunk/core/src/main/java/org/apache/mina/transport/nio: NioSelectorLoop.java NioTcpSession.java SelectorLoop.java

Author: elecharny
Date: Fri Jan  4 17:01:59 2013
New Revision: 1428982

URL: http://svn.apache.org/viewvc?rev=1428982&view=rev
Log:
o Encaspulated the call to selector.wakeup() in the wakeup() method
o Call wakeup() in the modifyRegistration() method only when it's necessary (ie, only when the register method is not called in the select loop). This spare a spurious wakeup.
o Called the SelectedKey.readyOps() only once.

Modified:
    mina/mina/trunk/core/src/main/java/org/apache/mina/transport/nio/NioSelectorLoop.java
    mina/mina/trunk/core/src/main/java/org/apache/mina/transport/nio/NioTcpSession.java
    mina/mina/trunk/core/src/main/java/org/apache/mina/transport/nio/SelectorLoop.java

Modified: mina/mina/trunk/core/src/main/java/org/apache/mina/transport/nio/NioSelectorLoop.java
URL: http://svn.apache.org/viewvc/mina/mina/trunk/core/src/main/java/org/apache/mina/transport/nio/NioSelectorLoop.java?rev=1428982&r1=1428981&r2=1428982&view=diff
==============================================================================
--- mina/mina/trunk/core/src/main/java/org/apache/mina/transport/nio/NioSelectorLoop.java (original)
+++ mina/mina/trunk/core/src/main/java/org/apache/mina/transport/nio/NioSelectorLoop.java Fri Jan  4 17:01:59 2013
@@ -126,7 +126,7 @@ public class NioSelectorLoop implements 
         registrationQueue.add(new Registration(ops, channel, listener, callback));
 
         // Now, wakeup the selector in order to let it update the selectionKey status
-        selector.wakeup();
+        wakeup();
     }
 
     /**
@@ -134,7 +134,7 @@ public class NioSelectorLoop implements 
      */
     @Override
     public void modifyRegistration(final boolean accept, final boolean read, final boolean write,
-            final SelectorListener listener, final SelectableChannel channel) {
+            final SelectorListener listener, final SelectableChannel channel, boolean wakeup) {
         logger.debug("modifying registration : {} for accept : {}, read : {}, write : {}, channel : {}", new Object[] {
                 listener, accept, read, write, channel });
 
@@ -161,7 +161,9 @@ public class NioSelectorLoop implements 
         key.interestOps(ops);
 
         // we need to wakeup for the registration to be modified (TODO : not needed if we are in the worker thread)
-        selector.wakeup();
+        if (wakeup) {
+            wakeup();
+        }
     }
 
     /**
@@ -200,22 +202,26 @@ public class NioSelectorLoop implements 
                     logger.debug("selecting...");
                     final int readyCount = selector.select();
                     logger.debug("... done selecting : {} events", readyCount);
-                    final Iterator<SelectionKey> it = selector.selectedKeys().iterator();
 
-                    while (it.hasNext()) {
-                        final SelectionKey key = it.next();
-                        final SelectorListener listener = (SelectorListener) key.attachment();
-                        logger.debug("key : {}", key);
-                        boolean isAcceptable = key.isAcceptable();
-                        boolean isConnectable = key.isConnectable();
-                        boolean isReadable = key.isReadable();
-                        boolean isWritable = key.isWritable();
-                        listener.ready(isAcceptable, isConnectable, isReadable, isReadable ? readBuffer : null,
-                                isWritable);
-                        // if you don't remove the event of the set, the selector will present you this event again and
-                        // again
-                        logger.debug("remove");
-                        it.remove();
+                    if (readyCount > 0) {
+                        final Iterator<SelectionKey> it = selector.selectedKeys().iterator();
+
+                        while (it.hasNext()) {
+                            final SelectionKey key = it.next();
+                            final SelectorListener listener = (SelectorListener) key.attachment();
+                            logger.debug("key : {}", key);
+                            int ops = key.readyOps();
+                            boolean isAcceptable = (ops & SelectionKey.OP_ACCEPT) == SelectionKey.OP_ACCEPT;
+                            boolean isConnectable = (ops & SelectionKey.OP_CONNECT) == SelectionKey.OP_CONNECT;
+                            boolean isReadable = (ops & SelectionKey.OP_READ) == SelectionKey.OP_READ;
+                            boolean isWritable = (ops & SelectionKey.OP_WRITE) == SelectionKey.OP_WRITE;
+                            listener.ready(isAcceptable, isConnectable, isReadable, isReadable ? readBuffer : null,
+                                    isWritable);
+                            // if you don't remove the event of the set, the selector will present you this event again and
+                            // again
+                            logger.debug("remove");
+                            it.remove();
+                        }
                     }
 
                     // new registration

Modified: mina/mina/trunk/core/src/main/java/org/apache/mina/transport/nio/NioTcpSession.java
URL: http://svn.apache.org/viewvc/mina/mina/trunk/core/src/main/java/org/apache/mina/transport/nio/NioTcpSession.java?rev=1428982&r1=1428981&r2=1428982&view=diff
==============================================================================
--- mina/mina/trunk/core/src/main/java/org/apache/mina/transport/nio/NioTcpSession.java (original)
+++ mina/mina/trunk/core/src/main/java/org/apache/mina/transport/nio/NioTcpSession.java Fri Jan  4 17:01:59 2013
@@ -222,7 +222,7 @@ public class NioTcpSession extends Abstr
     @Override
     public void flushWriteQueue() {
         // register for write
-        selectorLoop.modifyRegistration(false, !isReadSuspended(), true, this, channel);
+        selectorLoop.modifyRegistration(false, !isReadSuspended(), true, this, channel, true);
     }
 
     /**
@@ -359,7 +359,7 @@ public class NioTcpSession extends Abstr
                         channelClose();
                     } else {
                         // no more write event needed
-                        selectorLoop.modifyRegistration(false, !isReadSuspended(), false, this, channel);
+                        selectorLoop.modifyRegistration(false, !isReadSuspended(), false, this, channel, false);
 
                         // Reset the flag in IoSession too
                         setNotRegisteredForWrite();

Modified: mina/mina/trunk/core/src/main/java/org/apache/mina/transport/nio/SelectorLoop.java
URL: http://svn.apache.org/viewvc/mina/mina/trunk/core/src/main/java/org/apache/mina/transport/nio/SelectorLoop.java?rev=1428982&r1=1428981&r2=1428982&view=diff
==============================================================================
--- mina/mina/trunk/core/src/main/java/org/apache/mina/transport/nio/SelectorLoop.java (original)
+++ mina/mina/trunk/core/src/main/java/org/apache/mina/transport/nio/SelectorLoop.java Fri Jan  4 17:01:59 2013
@@ -33,12 +33,13 @@ public interface SelectorLoop {
      * @param write Registers for OP_WRITE events
      * @param listener The listener
      * @param channel
+     * @param wakeup Tells if we should do a wakeup() on the selector
      */
     void register(boolean accept, boolean connect, boolean read, boolean write, SelectorListener listener,
             SelectableChannel channel, RegistrationCallback callback);
 
     void modifyRegistration(boolean accept, boolean read, boolean write, SelectorListener listener,
-            SelectableChannel channel);
+            SelectableChannel channel, boolean wakeup);
 
     void unregister(SelectorListener listener, SelectableChannel channel);