1 /*
  2  * Copyright (c) 2002, 2023, 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 // Special-purpose data structure for sets of native threads
 29 
 30 class NativeThreadSet {
 31     private static final int OTHER_THREAD_INDEX = -99;
 32 
 33     private final int initialCapacity;
 34     private Thread[] threads;     // array of platform threads, created lazily
 35     private int used;             // number of elements in threads array
 36     private int otherThreads;     // additional threads that can't be signalled
 37     private boolean waitingToEmpty;
 38 
 39     NativeThreadSet(int n) {
 40         initialCapacity = n;
 41     }
 42 
 43     /**
 44      * Adds the current thread handle to this set, returning an index so that
 45      * it can efficiently be removed later.
 46      */
 47     int add() {
 48         synchronized (this) {
 49             final Thread t = NativeThread.threadToSignal();
 50             if (t == null || t.isVirtual()) {
 51                 otherThreads++;
 52                 return OTHER_THREAD_INDEX;
 53             }
 54 
 55             // add platform threads to array, creating or growing array if needed
 56             int start = 0;
 57             if (threads == null) {
 58                 threads = new Thread[initialCapacity];
 59             } else if (used >= threads.length) {
 60                 int on = threads.length;
 61                 int nn = on * 2;
 62                 Thread[] nthreads = new Thread[nn];
 63                 System.arraycopy(threads, 0, nthreads, 0, on);
 64                 threads = nthreads;
 65                 start = on;
 66             }
 67             for (int i = start; i < threads.length; i++) {
 68                 if (threads[i] == null) {
 69                     threads[i] = t;
 70                     used++;
 71                     return i;
 72                 }
 73             }
 74             throw new InternalError();
 75         }
 76     }
 77 
 78     /**
 79      * Removes the thread at the given index. A no-op if index is -1.
 80      */
 81     void remove(int i) {
 82         synchronized (this) {
 83             if (i >= 0) {
 84                 threads[i] = null;
 85                 used--;
 86             } else if (i == OTHER_THREAD_INDEX) {
 87                 otherThreads--;
 88             } else {
 89                 assert i == -1;
 90                 return;
 91             }
 92             if (used == 0 && otherThreads == 0 && waitingToEmpty) {
 93                 notifyAll();
 94             }
 95         }
 96     }
 97 
 98     /**
 99      * Signals all native threads in the thread set and wait for the thread set to empty.
100      */
101     synchronized void signalAndWait() {
102         boolean interrupted = false;
103         while (used > 0 || otherThreads > 0) {
104             int u = used, i = 0;
105             while (u > 0 && i < threads.length) {
106                 Thread t = threads[i];
107                 if (t != null) {
108                     NativeThread.signal(t);
109                     u--;
110                 }
111                 i++;
112             }
113             waitingToEmpty = true;
114             try {
115                 wait(50);
116             } catch (InterruptedException e) {
117                 interrupted = true;
118             } finally {
119                 waitingToEmpty = false;
120             }
121         }
122         if (interrupted)
123             Thread.currentThread().interrupt();
124     }
125 }