< prev index next >
src/java.base/solaris/classes/sun/nio/ch/DevPollSelectorImpl.java
Print this page
rev 50580 : [mq]: select-with-consumer
@@ -25,17 +25,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.Deque;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
+import java.util.function.Consumer;
import static sun.nio.ch.DevPollArrayWrapper.NUM_POLLFDS;
import static sun.nio.ch.DevPollArrayWrapper.POLLREMOVE;
/**
@@ -83,11 +85,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);
long to = timeout;
boolean blocking = (to != 0);
boolean timedPoll = (to > 0);
@@ -115,11 +119,11 @@
} finally {
end(blocking);
}
processDeregisterQueue();
- return updateSelectedKeys(numEntries);
+ return processEvents(numEntries, action);
}
/**
* Process changes to the interest ops.
*/
@@ -163,17 +167,17 @@
pollWrapper.registerMultiple(index);
}
}
/**
- * 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(int numEntries) throws IOException {
+ private int processEvents(int numEntries, Consumer<SelectionKey> action)
+ throws IOException
+ {
assert Thread.holdsLock(this);
- assert Thread.holdsLock(nioSelectedKeys());
boolean interrupted = false;
int numKeysUpdated = 0;
for (int i=0; i<numEntries; i++) {
int fd = pollWrapper.getDescriptor(i);
@@ -181,21 +185,11 @@
interrupted = true;
} else {
SelectionKeyImpl ski = fdToKey.get(fd);
if (ski != null) {
int rOps = pollWrapper.getReventOps(i);
- 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);
}
}
}
if (interrupted) {
< prev index next >