1 /*
  2  * Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.  Oracle designates this
  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);
105                 if (--u == 0)
106                     break;
107             }
108             waitingToEmpty = true;
109             try {
110                 wait(50);
111             } catch (InterruptedException e) {
112                 interrupted = true;
113             } finally {
114                 waitingToEmpty = false;
115             }
116         }
117         if (interrupted)
118             Thread.currentThread().interrupt();
119     }
120 }
--- EOF ---