< prev index next >

src/java.base/share/classes/sun/nio/ch/Poller.java

Print this page
@@ -34,10 +34,11 @@
  import java.util.concurrent.Executors;
  import java.util.concurrent.ThreadFactory;
  import java.util.concurrent.locks.LockSupport;
  import java.util.function.BooleanSupplier;
  import jdk.internal.misc.InnocuousThread;
+ import jdk.internal.vm.annotation.Stable;
  
  /**
   * Polls file descriptors. Virtual threads invoke the poll method to park
   * until a given file descriptor is ready for I/O.
   */

@@ -51,10 +52,13 @@
          } catch (IOException ioe) {
              throw new ExceptionInInitializerError(ioe);
          }
      }
  
+     // the poller or sub-poller thread
+     private @Stable Thread owner;
+ 
      // maps file descriptors to parked Thread
      private final Map<Integer, Thread> map = new ConcurrentHashMap<>();
  
      /**
       * Poller mode.

@@ -236,10 +240,11 @@
      /**
       * Master polling loop. The {@link #polled(int)} method is invoked for each file
       * descriptor that is polled.
       */
      private void pollerLoop() {
+         owner = Thread.currentThread();
          try {
              for (;;) {
                  poll(-1);
              }
          } catch (Exception e) {

@@ -256,10 +261,11 @@
       * again when there are no more events. The sub-poller yields after each poll to help
       * with fairness and to avoid re-registering with the master poller where possible.
       */
      private void subPollerLoop(Poller masterPoller) {
          assert Thread.currentThread().isVirtual();
+         owner = Thread.currentThread();
          try {
              int polled = 0;
              for (;;) {
                  if (polled == 0) {
                      masterPoller.poll(fdVal(), 0, () -> true);  // park

@@ -280,11 +286,12 @@
          return map.size();
      }
  
      @Override
      public String toString() {
-         return Objects.toIdentityString(this) + " [registered = " + registered() + "]";
+         return String.format("%s [registered = %d, owner = %s]",
+                 Objects.toIdentityString(this), registered(), owner);
      }
  
      /**
       * The Pollers used for read and write events.
       */

@@ -440,6 +447,27 @@
              } catch (Exception e) {
                  throw new InternalError(e);
              }
          }
      }
+ 
+     /**
+      * Return the master poller or null if there is no master poller.
+      */
+     public static Poller masterPoller() {
+         return POLLERS.masterPoller();
+     }
+ 
+     /**
+      * Return the list of read pollers.
+      */
+     public static List<Poller> readPollers() {
+         return POLLERS.readPollers();
+     }
+ 
+     /**
+      * Return the list of write pollers.
+      */
+     public static List<Poller> writePollers() {
+         return POLLERS.writePollers();
+     }
  }
< prev index next >