< prev index next > src/java.base/linux/classes/sun/nio/ch/EPollPoller.java
Print this page
* questions.
*/
package sun.nio.ch;
import java.io.IOException;
+ import java.lang.ref.Cleaner.Cleanable;
+ import jdk.internal.ref.CleanerFactory;
import static sun.nio.ch.EPoll.*;
/**
* Poller implementation based on the epoll facility.
*/
private final int epfd;
private final int event;
private final int maxEvents;
private final long address;
+ private final EventFD eventfd; // wakeup event, used for shutdown
+
+ // close action, and cleaner if this is subpoller
+ private final Runnable closer;
+ private final Cleanable cleaner;
EPollPoller(boolean subPoller, boolean read) throws IOException {
- this.epfd = EPoll.create();
+ int maxEvents = (subPoller) ? 16 : 64;
+
+ int epfd = -1;
+ long address = 0L;
+ EventFD eventfd = null;
+ try {
+ epfd = EPoll.create();
+ address = EPoll.allocatePollArray(maxEvents);
+
+ // register event with epoll to allow for wakeup
+ eventfd = new EventFD();
+ IOUtil.configureBlocking(eventfd.efd(), false);
+ EPoll.ctl(epfd, EPOLL_CTL_ADD, eventfd.efd(), EPOLLIN);
+ } catch (Throwable e) {
+ if (epfd >= 0) FileDispatcherImpl.closeIntFD(epfd);
+ if (address != 0L) EPoll.freePollArray(address);
+ if (eventfd != null) eventfd.close();
+ throw e;
+ }
+
+ this.epfd = epfd;
this.event = (read) ? EPOLLIN : EPOLLOUT;
- this.maxEvents = (subPoller) ? 64 : 512;
- this.address = EPoll.allocatePollArray(maxEvents);
+ this.maxEvents = maxEvents;
+ this.address = address;
+ this.eventfd = eventfd;
+
+ // create action to close epoll instance, register cleaner if this is a subpoller
+ this.closer = closer(epfd, address, eventfd);
+ if (subPoller) {
+ this.cleaner = CleanerFactory.cleaner().register(this, closer);
+ } else {
+ this.cleaner = null;
+ }
+ }
+
+ /**
+ * Returns an action to close the epoll instance and release other resources.
+ */
+ private static Runnable closer(int epfd, long address, EventFD eventfd) {
+ return () -> {
+ try {
+ FileDispatcherImpl.closeIntFD(epfd);
+ EPoll.freePollArray(address);
+ eventfd.close();
+ } catch (IOException _) { }
+ };
+ }
+
+ @Override
+ void close() {
+ if (cleaner != null) {
+ cleaner.clean();
+ } else {
+ closer.run();
+ }
}
@Override
int fdVal() {
return epfd;
}
@Override
void implRegister(int fdVal) throws IOException {
- // re-arm
+ // re-enable if already registered but disabled (previously polled)
int err = EPoll.ctl(epfd, EPOLL_CTL_MOD, fdVal, (event | EPOLLONESHOT));
if (err == ENOENT)
err = EPoll.ctl(epfd, EPOLL_CTL_ADD, fdVal, (event | EPOLLONESHOT));
if (err != 0)
throw new IOException("epoll_ctl failed: " + err);
if (!polled) {
EPoll.ctl(epfd, EPOLL_CTL_DEL, fdVal, 0);
}
}
+ @Override
+ void wakeupPoller() throws IOException {
+ eventfd.set();
+ }
+
@Override
int poll(int timeout) throws IOException {
int n = EPoll.wait(epfd, address, maxEvents, timeout);
+ int polled = 0;
int i = 0;
while (i < n) {
long eventAddress = EPoll.getEvent(address, i);
- int fdVal = EPoll.getDescriptor(eventAddress);
- polled(fdVal);
+ int fd = EPoll.getDescriptor(eventAddress);
+ if (fd != eventfd.efd()) {
+ polled(fd);
+ polled++;
+ }
i++;
}
- return n;
+ return polled;
}
}
< prev index next >