< prev index next >
src/java.base/macosx/classes/sun/nio/ch/KQueueSelectorImpl.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.KQueue.EVFILT_READ;
import static sun.nio.ch.KQueue.EVFILT_WRITE;
import static sun.nio.ch.KQueue.EV_ADD;
import static sun.nio.ch.KQueue.EV_DELETE;
@@ -98,11 +100,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 = Math.min(timeout, Integer.MAX_VALUE); // max kqueue timeout
boolean blocking = (to != 0);
boolean timedPoll = (to > 0);
@@ -130,11 +134,11 @@
} finally {
end(blocking);
}
processDeregisterQueue();
- return updateSelectedKeys(numEntries);
+ return processEvents(numEntries, action);
}
/**
* Process changes to the interest ops.
*/
@@ -178,17 +182,17 @@
}
}
}
/**
- * 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());
int numKeysUpdated = 0;
boolean interrupted = false;
// A file descriptor may be registered with kqueue with more than one
@@ -212,28 +216,16 @@
if (filter == EVFILT_READ) {
rOps |= Net.POLLIN;
} else if (filter == EVFILT_WRITE) {
rOps |= Net.POLLOUT;
}
-
- if (selectedKeys.contains(ski)) {
- if (ski.translateAndUpdateReadyOps(rOps)) {
- // file descriptor may be polled more than once per poll
- if (ski.lastPolled != pollCount) {
+ int updated = processReadyEvents(rOps, ski, action);
+ if (updated > 0 && ski.lastPolled != pollCount) {
numKeysUpdated++;
ski.lastPolled = pollCount;
}
}
- } else {
- ski.translateAndSetReadyOps(rOps);
- if ((ski.nioReadyOps() & ski.nioInterestOps()) != 0) {
- selectedKeys.add(ski);
- numKeysUpdated++;
- ski.lastPolled = pollCount;
- }
- }
- }
}
}
if (interrupted) {
clearInterrupt();
< prev index next >