< prev index next >

src/java.base/unix/classes/sun/nio/ch/PollSelectorImpl.java

Print this page
rev 50580 : [mq]: select-with-consumer

@@ -24,17 +24,19 @@
  */
 package sun.nio.ch;
 
 import java.io.IOException;
 import java.nio.channels.ClosedSelectorException;
+import java.nio.channels.SelectionKey;
 import java.nio.channels.Selector;
 import java.nio.channels.spi.SelectorProvider;
 import java.util.ArrayDeque;
 import java.util.ArrayList;
 import java.util.Deque;
 import java.util.List;
 import java.util.concurrent.TimeUnit;
+import java.util.function.Consumer;
 
 import jdk.internal.misc.Unsafe;
 
 /**
  * Selector implementation based on poll

@@ -90,11 +92,13 @@
         if (!isOpen())
             throw new ClosedSelectorException();
     }
 
     @Override
-    protected int doSelect(long timeout) throws IOException {
+    protected int doSelect(Consumer<SelectionKey> action, long timeout)
+        throws IOException
+    {
         assert Thread.holdsLock(this);
 
         int to = (int) Math.min(timeout, Integer.MAX_VALUE); // max poll timeout
         boolean blocking = (to != 0);
         boolean timedPoll = (to > 0);

@@ -123,11 +127,11 @@
         } finally {
             end(blocking);
         }
 
         processDeregisterQueue();
-        return updateSelectedKeys();
+        return processEvents(action);
     }
 
     /**
      * Process changes to the interest ops.
      */

@@ -155,37 +159,27 @@
             }
         }
     }
 
     /**
-     * Update the keys of file descriptors that were polled and add them to
-     * the selected-key set.
+     * Process the polled events.
      * If the interrupt fd has been selected, drain it and clear the interrupt.
      */
-    private int updateSelectedKeys() throws IOException {
+    private int processEvents(Consumer<SelectionKey> action)
+        throws IOException
+    {
         assert Thread.holdsLock(this);
-        assert Thread.holdsLock(nioSelectedKeys());
         assert pollArraySize > 0 && pollArraySize == pollKeys.size();
 
         int numKeysUpdated = 0;
         for (int i = 1; i < pollArraySize; i++) {
             int rOps = getReventOps(i);
             if (rOps != 0) {
                 SelectionKeyImpl ski = pollKeys.get(i);
                 assert ski.getFDVal() == getDescriptor(i);
                 if (ski.isValid()) {
-                    if (selectedKeys.contains(ski)) {
-                        if (ski.translateAndUpdateReadyOps(rOps)) {
-                            numKeysUpdated++;
-                        }
-                    } else {
-                        ski.translateAndSetReadyOps(rOps);
-                        if ((ski.nioReadyOps() & ski.nioInterestOps()) != 0) {
-                            selectedKeys.add(ski);
-                            numKeysUpdated++;
-                        }
-                    }
+                    numKeysUpdated += processReadyEvents(rOps, ski, action);
                 }
             }
         }
 
         // check for interrupt
< prev index next >