< prev index next >

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

Print this page

 26 package sun.nio.ch;
 27 
 28 // Special-purpose data structure for sets of native threads
 29 
 30 
 31 class NativeThreadSet {
 32 
 33     private long[] elts;
 34     private int used = 0;
 35     private boolean waitingToEmpty;
 36 
 37     NativeThreadSet(int n) {
 38         elts = new long[n];
 39     }
 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.currentNativeThread();
 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             }
 67             assert false;
 68             return -1;
 69         }
 70 
 71     }
 72 
 73     /**
 74      * Removes the thread at the give index.
 75      */
 76     void remove(int i) {
 77         synchronized (this) {
 78             assert (elts[i] == NativeThread.currentNativeThread()) || (elts[i] == -1);
 79             elts[i] = 0;
 80             used--;
 81             if (used == 0 && waitingToEmpty)
 82                 notifyAll();
 83         }
 84     }
 85 
 86     // Signals all threads in this set.
 87     //
 88     synchronized void signalAndWait() {
 89         boolean interrupted = false;
 90         while (used > 0) {
 91             int u = used;
 92             int n = elts.length;
 93             for (int i = 0; i < n; i++) {
 94                 long th = elts[i];
 95                 if (th == 0)
 96                     continue;
 97                 if (th != -1)
 98                     NativeThread.signal(th);

 26 package sun.nio.ch;
 27 
 28 // Special-purpose data structure for sets of native threads
 29 
 30 
 31 class NativeThreadSet {
 32 
 33     private long[] elts;
 34     private int used = 0;
 35     private boolean waitingToEmpty;
 36 
 37     NativeThreadSet(int n) {
 38         elts = new long[n];
 39     }
 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             }
 67             assert false;
 68             return -1;
 69         }
 70 
 71     }
 72 
 73     /**
 74      * Removes the thread at the give index.
 75      */
 76     void remove(int i) {
 77         synchronized (this) {
 78             assert (elts[i] == NativeThread.current()) || (elts[i] == -1);
 79             elts[i] = 0;
 80             used--;
 81             if (used == 0 && waitingToEmpty)
 82                 notifyAll();
 83         }
 84     }
 85 
 86     // Signals all threads in this set.
 87     //
 88     synchronized void signalAndWait() {
 89         boolean interrupted = false;
 90         while (used > 0) {
 91             int u = used;
 92             int n = elts.length;
 93             for (int i = 0; i < n; i++) {
 94                 long th = elts[i];
 95                 if (th == 0)
 96                     continue;
 97                 if (th != -1)
 98                     NativeThread.signal(th);
< prev index next >