< prev index next >

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

Print this page




  26 package sun.nio.ch;
  27 
  28 
  29 // Special-purpose data structure for sets of native threads
  30 
  31 
  32 class NativeThreadSet {
  33 
  34     private long[] elts;
  35     private int used = 0;
  36     private boolean waitingToEmpty;
  37 
  38     NativeThreadSet(int n) {
  39         elts = new long[n];
  40     }
  41 
  42     // Adds the current native thread to this set, returning its index so that
  43     // it can efficiently be removed later.
  44     //
  45     int add() {
  46         long th = NativeThread.current();
  47         // 0 and -1 are treated as placeholders, not real thread handles
  48         if (th == 0)
  49             th = -1;
  50         synchronized (this) {
  51             int start = 0;
  52             if (used >= elts.length) {
  53                 int on = elts.length;
  54                 int nn = on * 2;
  55                 long[] nelts = new long[nn];
  56                 System.arraycopy(elts, 0, nelts, 0, on);
  57                 elts = nelts;
  58                 start = on;
  59             }
  60             for (int i = start; i < elts.length; i++) {
  61                 if (elts[i] == 0) {
  62                     elts[i] = th;
  63                     used++;
  64                     return i;
  65                 }
  66             }




  26 package sun.nio.ch;
  27 
  28 
  29 // Special-purpose data structure for sets of native threads
  30 
  31 
  32 class NativeThreadSet {
  33 
  34     private long[] elts;
  35     private int used = 0;
  36     private boolean waitingToEmpty;
  37 
  38     NativeThreadSet(int n) {
  39         elts = new long[n];
  40     }
  41 
  42     // Adds the current native thread to this set, returning its index so that
  43     // it can efficiently be removed later.
  44     //
  45     int add() {
  46         long th = NativeThread.currentKernelThread();
  47         // 0 and -1 are treated as placeholders, not real thread handles
  48         if (th == 0)
  49             th = -1;
  50         synchronized (this) {
  51             int start = 0;
  52             if (used >= elts.length) {
  53                 int on = elts.length;
  54                 int nn = on * 2;
  55                 long[] nelts = new long[nn];
  56                 System.arraycopy(elts, 0, nelts, 0, on);
  57                 elts = nelts;
  58                 start = on;
  59             }
  60             for (int i = start; i < elts.length; i++) {
  61                 if (elts[i] == 0) {
  62                     elts[i] = th;
  63                     used++;
  64                     return i;
  65                 }
  66             }


< prev index next >