< prev index next >

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

Print this page
*** 29,13 ***
  
  class NativeThreadSet {
      private static final int OTHER_THREAD_INDEX = -99;
  
      private final int initialCapacity;
!     private long[] threads;             // array of thread handles, created lazily
!     private int used;                   // number of thread handles in threads array
!     private int otherThreads;           // count of threads without a native thread handle
      private boolean waitingToEmpty;
  
      NativeThreadSet(int n) {
          initialCapacity = n;
      }
--- 29,13 ---
  
  class NativeThreadSet {
      private static final int OTHER_THREAD_INDEX = -99;
  
      private final int initialCapacity;
!     private NativeThread[] threads;     // array of native threads, created lazily
!     private int used;                   // number of elements in threads array
!     private int otherThreads;           // count of threads without a native thread object
      private boolean waitingToEmpty;
  
      NativeThreadSet(int n) {
          initialCapacity = n;
      }

*** 43,32 ***
      /**
       * Adds the current thread handle to this set, returning an index so that
       * it can efficiently be removed later.
       */
      int add() {
!         long th = NativeThread.current();
          synchronized (this) {
!             if (!NativeThread.isNativeThread(th)) {
                  otherThreads++;
                  return OTHER_THREAD_INDEX;
              }
  
              // add native thread handle to array, creating or growing array if needed
              int start = 0;
              if (threads == null) {
!                 threads = new long[initialCapacity];
              } else if (used >= threads.length) {
                  int on = threads.length;
                  int nn = on * 2;
!                 long[] nthreads = new long[nn];
                  System.arraycopy(threads, 0, nthreads, 0, on);
                  threads = nthreads;
                  start = on;
              }
              for (int i = start; i < threads.length; i++) {
!                 if (threads[i] == 0) {
!                     threads[i] = th;
                      used++;
                      return i;
                  }
              }
              throw new InternalError();
--- 43,32 ---
      /**
       * Adds the current thread handle to this set, returning an index so that
       * it can efficiently be removed later.
       */
      int add() {
!         NativeThread nt = NativeThread.currentNativeThread();
          synchronized (this) {
!             if (nt == null) {
                  otherThreads++;
                  return OTHER_THREAD_INDEX;
              }
  
              // add native thread handle to array, creating or growing array if needed
              int start = 0;
              if (threads == null) {
!                 threads = new NativeThread[initialCapacity];
              } else if (used >= threads.length) {
                  int on = threads.length;
                  int nn = on * 2;
!                 NativeThread[] nthreads = new NativeThread[nn];
                  System.arraycopy(threads, 0, nthreads, 0, on);
                  threads = nthreads;
                  start = on;
              }
              for (int i = start; i < threads.length; i++) {
!                 if (threads[i] == null) {
!                     threads[i] = nt;
                      used++;
                      return i;
                  }
              }
              throw new InternalError();

*** 79,12 ***
       * Removes the thread at the given index. A no-op if index is -1.
       */
      void remove(int i) {
          synchronized (this) {
              if (i >= 0) {
!                 assert threads[i] == NativeThread.current();
!                 threads[i] = 0;
                  used--;
              } else if (i == OTHER_THREAD_INDEX) {
                  otherThreads--;
              } else {
                  assert i == -1;
--- 79,12 ---
       * Removes the thread at the given index. A no-op if index is -1.
       */
      void remove(int i) {
          synchronized (this) {
              if (i >= 0) {
!                 assert threads[i] == NativeThread.currentNativeThread();
!                 threads[i] = null;
                  used--;
              } else if (i == OTHER_THREAD_INDEX) {
                  otherThreads--;
              } else {
                  assert i == -1;

*** 102,13 ***
      synchronized void signalAndWait() {
          boolean interrupted = false;
          while (used > 0 || otherThreads > 0) {
              int u = used, i = 0;
              while (u > 0 && i < threads.length) {
!                 long th = threads[i];
!                 if (th != 0) {
!                     NativeThread.signal(th);
                      u--;
                  }
                  i++;
              }
              waitingToEmpty = true;
--- 102,13 ---
      synchronized void signalAndWait() {
          boolean interrupted = false;
          while (used > 0 || otherThreads > 0) {
              int u = used, i = 0;
              while (u > 0 && i < threads.length) {
!                 NativeThread nt = threads[i];
!                 if (nt != null) {
!                     nt.signal();
                      u--;
                  }
                  i++;
              }
              waitingToEmpty = true;
< prev index next >