1 /* 2 * Copyright (c) 2002, 2022, 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 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); 99 if (--u == 0) 100 break; 101 } 102 waitingToEmpty = true; 103 try { 104 wait(50); 105 } catch (InterruptedException e) { 106 interrupted = true; 107 } finally { 108 waitingToEmpty = false; 109 } 110 } 111 if (interrupted) 112 Thread.currentThread().interrupt(); 113 } 114 }