< prev index next >

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

Print this page

  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 
 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             }
 67             assert false;
 68             return -1;
 69         }

 70     }
 71 
 72     // Removes the thread at the given index.
 73     //

 74     void remove(int i) {
 75         synchronized (this) {


 76             elts[i] = 0;
 77             used--;
 78             if (used == 0 && waitingToEmpty)
 79                 notifyAll();
 80         }
 81     }
 82 
 83     // Signals all threads in this set.
 84     //
 85     synchronized void signalAndWait() {
 86         boolean interrupted = false;
 87         while (used > 0) {
 88             int u = used;
 89             int n = elts.length;
 90             for (int i = 0; i < n; i++) {
 91                 long th = elts[i];
 92                 if (th == 0)
 93                     continue;
 94                 if (th != -1)
 95                     NativeThread.signal(th);

  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 
 26 package sun.nio.ch;
 27 
 28 import jdk.internal.vm.Continuation;
 29 
 30 // Special-purpose data structure for sets of native threads
 31 
 32 
 33 class NativeThreadSet {
 34 
 35     private long[] elts;
 36     private int used = 0;
 37     private boolean waitingToEmpty;
 38 
 39     NativeThreadSet(int n) {
 40         elts = new long[n];
 41     }
 42 
 43     /**
 44      * Adds the current native thread to this set, returning its index so that
 45      * it can efficiently be removed later.
 46      *
 47      * The current continuation will be pinned until the thread is removed.
 48      */
 49     int add() {
 50         long th = NativeThread.currentKernelThread();
 51         // 0 and -1 are treated as placeholders, not real thread handles
 52         if (th == 0)
 53             th = -1;
 54         synchronized (this) {
 55             int start = 0;
 56             if (used >= elts.length) {
 57                 int on = elts.length;
 58                 int nn = on * 2;
 59                 long[] nelts = new long[nn];
 60                 System.arraycopy(elts, 0, nelts, 0, on);
 61                 elts = nelts;
 62                 start = on;
 63             }
 64             for (int i = start; i < elts.length; i++) {
 65                 if (elts[i] == 0) {
 66                     elts[i] = th;
 67                     used++;
 68                     Continuation.pin();
 69                     return i;
 70                 }
 71             }
 72             assert false;
 73             return -1;
 74         }
 75 
 76     }
 77 
 78     /**
 79      * Removes the thread at the give index.
 80      */
 81     void remove(int i) {
 82         synchronized (this) {
 83             assert (elts[i] == NativeThread.currentKernelThread()) || (elts[i] == -1);
 84             Continuation.unpin();
 85             elts[i] = 0;
 86             used--;
 87             if (used == 0 && waitingToEmpty)
 88                 notifyAll();
 89         }
 90     }
 91 
 92     // Signals all threads in this set.
 93     //
 94     synchronized void signalAndWait() {
 95         boolean interrupted = false;
 96         while (used > 0) {
 97             int u = used;
 98             int n = elts.length;
 99             for (int i = 0; i < n; i++) {
100                 long th = elts[i];
101                 if (th == 0)
102                     continue;
103                 if (th != -1)
104                     NativeThread.signal(th);
< prev index next >