History

My first initial commit with all of mine experimental changes.

Dmitry Kabak (userdima committed 13 years ago

Commit d37b88d1 parent 3f5b46618352 files · +2598093 −-8690Browse files

Showing the changes to java/pkg/sun/nio/ch/NativeThreadSet.java (1 of 18352 files). Show all

Unified · Split

java/pkg/sun/nio/ch/NativeThreadSet.java+111−0
@@ -0,0 +1,111 @@
1+/*
2+ * Copyright (c) 2002, 2009, 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+
29+// Special-purpose data structure for sets of native threads
30+
31+
32+class 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+ if (th == -1)
48+ return -1;
49+ synchronized (this) {
50+ int start = 0;
51+ if (used >= elts.length) {
52+ int on = elts.length;
53+ int nn = on * 2;
54+ long[] nelts = new long[nn];
55+ System.arraycopy(elts, 0, nelts, 0, on);
56+ elts = nelts;
57+ start = on;
58+ }
59+ for (int i = start; i < elts.length; i++) {
60+ if (elts[i] == 0) {
61+ elts[i] = th;
62+ used++;
63+ return i;
64+ }
65+ }
66+ assert false;
67+ return -1;
68+ }
69+ }
70+
71+ // Removes the thread at the given index.
72+ //
73+ void remove(int i) {
74+ if (i < 0)
75+ return;
76+ synchronized (this) {
77+ elts[i] = 0;
78+ used--;
79+ if (used == 0 && waitingToEmpty)
80+ notifyAll();
81+ }
82+ }
83+
84+ // Signals all threads in this set.
85+ //
86+ void signalAndWait() {
87+ synchronized (this) {
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+ NativeThread.signal(th);
95+ if (--u == 0)
96+ break;
97+ }
98+ waitingToEmpty = true;
99+ boolean interrupted = false;
100+ while (used > 0) {
101+ try {
102+ wait();
103+ } catch (InterruptedException e) {
104+ interrupted = true;
105+ }
106+ }
107+ if (interrupted)
108+ Thread.currentThread().interrupt();
109+ }
110+ }
111+}