< prev index next >

src/java.base/linux/classes/sun/nio/ch/EPollPoller.java

Print this page
*** 23,10 ***
--- 23,13 ---
   * questions.
   */
  package sun.nio.ch;
  
  import java.io.IOException;
+ import java.io.UncheckedIOException;
+ import java.lang.ref.Cleaner.Cleanable;
+ import jdk.internal.ref.CleanerFactory;
  import static sun.nio.ch.EPoll.*;
  
  /**
   * Poller implementation based on the epoll facility.
   */

*** 36,16 ***
--- 39,43 ---
  
      private final int epfd;
      private final int event;
      private final int maxEvents;
      private final long address;
+     private final Cleanable cleaner;
  
      EPollPoller(boolean subPoller, boolean read) throws IOException {
          this.epfd = EPoll.create();
          this.event = (read) ? EPOLLIN : EPOLLOUT;
          this.maxEvents = (subPoller) ? 64 : 512;
          this.address = EPoll.allocatePollArray(maxEvents);
+         if (subPoller) {
+             this.cleaner = CleanerFactory.cleaner().register(this, release(epfd, address));
+         } else {
+             this.cleaner = null;
+         }
+     }
+ 
+     @Override
+     void close() {
+         cleaner.clean();
+     }
+ 
+     /**
+      * Closes epoll instance and release poll array.
+      */
+     private static Runnable release(int epfd, long address) {
+         return () -> {
+             try {
+                 FileDispatcherImpl.closeIntFD(epfd);
+             } catch (IOException ioe) {
+                 throw new UncheckedIOException(ioe);
+             } finally {
+                 // release memory
+                 EPoll.freePollArray(address);
+             }
+         };
      }
  
      @Override
      int fdVal() {
          return epfd;
< prev index next >