author | alanb |
Mon, 09 May 2011 18:45:33 +0100 | |
changeset 9679 | d98ae8bc45fc |
parent 5506 | 202f599c92aa |
child 9687 | e41f9c6d9c0d |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2002, 2009, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 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; |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
715
diff
changeset
|
36 |
private boolean waitingToEmpty; |
2 | 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(); |
|
693
6b497b9cb292
6596323: (fc) ClosedByInterruptException not thrown by the interrupt method (lnx)
alanb
parents:
2
diff
changeset
|
47 |
if (th == -1) |
2 | 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--; |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
715
diff
changeset
|
79 |
if (used == 0 && waitingToEmpty) |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
715
diff
changeset
|
80 |
notifyAll(); |
2 | 81 |
} |
82 |
} |
|
83 |
||
84 |
// Signals all threads in this set. |
|
85 |
// |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
715
diff
changeset
|
86 |
void signalAndWait() { |
2 | 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 |
} |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
715
diff
changeset
|
98 |
waitingToEmpty = true; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
715
diff
changeset
|
99 |
while (used > 0) { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
715
diff
changeset
|
100 |
try { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
715
diff
changeset
|
101 |
wait(); |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
715
diff
changeset
|
102 |
} catch (InterruptedException ignore) { } |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
715
diff
changeset
|
103 |
} |
2 | 104 |
} |
105 |
} |
|
106 |
} |