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
26package sun.nio.ch;
27
28
29// Special-purpose data structure for sets of native threads
30
31
32class 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);
96                if (--u == 0)
97                    break;
98            }
99            waitingToEmpty = true;
100            try {
101                wait(50);
102            } catch (InterruptedException e) {
103                interrupted = true;
104            } finally {
105                waitingToEmpty = false;
106            }
107        }
108        if (interrupted)
109            Thread.currentThread().interrupt();
110    }
111}
112