0N/A/*
2362N/A * Copyright (c) 2002, 2009, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.nio.ch;
0N/A
0N/A
0N/A// Special-purpose data structure for sets of native threads
0N/A
0N/A
0N/Aclass NativeThreadSet {
0N/A
0N/A private long[] elts;
0N/A private int used = 0;
893N/A private boolean waitingToEmpty;
0N/A
0N/A NativeThreadSet(int n) {
0N/A elts = new long[n];
0N/A }
0N/A
0N/A // Adds the current native thread to this set, returning its index so that
0N/A // it can efficiently be removed later.
0N/A //
0N/A int add() {
0N/A long th = NativeThread.current();
5028N/A // 0 and -1 are treated as placeholders, not real thread handles
5028N/A if (th == 0)
5028N/A th = -1;
0N/A synchronized (this) {
0N/A int start = 0;
0N/A if (used >= elts.length) {
0N/A int on = elts.length;
0N/A int nn = on * 2;
0N/A long[] nelts = new long[nn];
0N/A System.arraycopy(elts, 0, nelts, 0, on);
0N/A elts = nelts;
0N/A start = on;
0N/A }
0N/A for (int i = start; i < elts.length; i++) {
0N/A if (elts[i] == 0) {
0N/A elts[i] = th;
0N/A used++;
0N/A return i;
0N/A }
0N/A }
0N/A assert false;
0N/A return -1;
0N/A }
0N/A }
0N/A
0N/A // Removes the thread at the given index.
0N/A //
0N/A void remove(int i) {
0N/A synchronized (this) {
0N/A elts[i] = 0;
0N/A used--;
893N/A if (used == 0 && waitingToEmpty)
893N/A notifyAll();
0N/A }
0N/A }
0N/A
0N/A // Signals all threads in this set.
0N/A //
893N/A void signalAndWait() {
0N/A synchronized (this) {
0N/A int u = used;
0N/A int n = elts.length;
0N/A for (int i = 0; i < n; i++) {
0N/A long th = elts[i];
0N/A if (th == 0)
0N/A continue;
5028N/A if (th != -1)
5028N/A NativeThread.signal(th);
0N/A if (--u == 0)
0N/A break;
0N/A }
893N/A waitingToEmpty = true;
4224N/A boolean interrupted = false;
893N/A while (used > 0) {
893N/A try {
893N/A wait();
4224N/A } catch (InterruptedException e) {
4224N/A interrupted = true;
4224N/A }
893N/A }
4224N/A if (interrupted)
4224N/A Thread.currentThread().interrupt();
0N/A }
0N/A }
0N/A}