< prev index next >

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

Print this page
*** 23,10 ***
--- 23,11 ---
   * questions.
   */
  
  package sun.nio.ch;
  
+ import jdk.internal.vm.Continuation;
  
  // Special-purpose data structure for sets of native threads
  
  
  class NativeThreadSet {

*** 37,15 ***
  
      NativeThreadSet(int n) {
          elts = new long[n];
      }
  
!     // Adds the current native thread to this set, returning its index so that
!     // it can efficiently be removed later.
!     //
      int add() {
!         long th = NativeThread.current();
          // 0 and -1 are treated as placeholders, not real thread handles
          if (th == 0)
              th = -1;
          synchronized (this) {
              int start = 0;
--- 38,18 ---
  
      NativeThreadSet(int n) {
          elts = new long[n];
      }
  
!     /**
!      * Adds the current native thread to this set, returning its index so that
!      * it can efficiently be removed later.
+      *
+      * The current continuation will be pinned until the thread is removed.
+      */
      int add() {
!         long th = NativeThread.currentKernelThread();
          // 0 and -1 are treated as placeholders, not real thread handles
          if (th == 0)
              th = -1;
          synchronized (this) {
              int start = 0;

*** 59,22 ***
              }
              for (int i = start; i < elts.length; i++) {
                  if (elts[i] == 0) {
                      elts[i] = th;
                      used++;
                      return i;
                  }
              }
              assert false;
              return -1;
          }
      }
  
!     // Removes the thread at the given index.
!     //
      void remove(int i) {
          synchronized (this) {
              elts[i] = 0;
              used--;
              if (used == 0 && waitingToEmpty)
                  notifyAll();
          }
--- 63,27 ---
              }
              for (int i = start; i < elts.length; i++) {
                  if (elts[i] == 0) {
                      elts[i] = th;
                      used++;
+                     Continuation.pin();
                      return i;
                  }
              }
              assert false;
              return -1;
          }
+ 
      }
  
!     /**
!      * Removes the thread at the give index.
+      */
      void remove(int i) {
          synchronized (this) {
+             assert (elts[i] == NativeThread.currentKernelThread()) || (elts[i] == -1);
+             Continuation.unpin();
              elts[i] = 0;
              used--;
              if (used == 0 && waitingToEmpty)
                  notifyAll();
          }
< prev index next >