author | rriggs |
Fri, 01 Dec 2017 16:40:08 -0500 | |
changeset 48224 | be0df5ab3093 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
14342
8435a30053c1
7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents:
11909
diff
changeset
|
2 |
* Copyright (c) 2002, 2012, 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(); |
|
11909
a1a7165ac1fa
6346658: (se) Selector briefly spins when asynchronously closing a registered channel [win]
alanb
parents:
9687
diff
changeset
|
47 |
// 0 and -1 are treated as placeholders, not real thread handles |
a1a7165ac1fa
6346658: (se) Selector briefly spins when asynchronously closing a registered channel [win]
alanb
parents:
9687
diff
changeset
|
48 |
if (th == 0) |
a1a7165ac1fa
6346658: (se) Selector briefly spins when asynchronously closing a registered channel [win]
alanb
parents:
9687
diff
changeset
|
49 |
th = -1; |
2 | 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--; |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
715
diff
changeset
|
78 |
if (used == 0 && waitingToEmpty) |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
715
diff
changeset
|
79 |
notifyAll(); |
2 | 80 |
} |
81 |
} |
|
82 |
||
83 |
// Signals all threads in this set. |
|
84 |
// |
|
22561
5ee70f9edbb7
7133499: (fc) FileChannel.read not preempted by asynchronous close on OS X
alanb
parents:
14342
diff
changeset
|
85 |
synchronized void signalAndWait() { |
5ee70f9edbb7
7133499: (fc) FileChannel.read not preempted by asynchronous close on OS X
alanb
parents:
14342
diff
changeset
|
86 |
boolean interrupted = false; |
5ee70f9edbb7
7133499: (fc) FileChannel.read not preempted by asynchronous close on OS X
alanb
parents:
14342
diff
changeset
|
87 |
while (used > 0) { |
2 | 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; |
|
11909
a1a7165ac1fa
6346658: (se) Selector briefly spins when asynchronously closing a registered channel [win]
alanb
parents:
9687
diff
changeset
|
94 |
if (th != -1) |
a1a7165ac1fa
6346658: (se) Selector briefly spins when asynchronously closing a registered channel [win]
alanb
parents:
9687
diff
changeset
|
95 |
NativeThread.signal(th); |
2 | 96 |
if (--u == 0) |
97 |
break; |
|
98 |
} |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
715
diff
changeset
|
99 |
waitingToEmpty = true; |
22561
5ee70f9edbb7
7133499: (fc) FileChannel.read not preempted by asynchronous close on OS X
alanb
parents:
14342
diff
changeset
|
100 |
try { |
5ee70f9edbb7
7133499: (fc) FileChannel.read not preempted by asynchronous close on OS X
alanb
parents:
14342
diff
changeset
|
101 |
wait(50); |
5ee70f9edbb7
7133499: (fc) FileChannel.read not preempted by asynchronous close on OS X
alanb
parents:
14342
diff
changeset
|
102 |
} catch (InterruptedException e) { |
5ee70f9edbb7
7133499: (fc) FileChannel.read not preempted by asynchronous close on OS X
alanb
parents:
14342
diff
changeset
|
103 |
interrupted = true; |
5ee70f9edbb7
7133499: (fc) FileChannel.read not preempted by asynchronous close on OS X
alanb
parents:
14342
diff
changeset
|
104 |
} finally { |
5ee70f9edbb7
7133499: (fc) FileChannel.read not preempted by asynchronous close on OS X
alanb
parents:
14342
diff
changeset
|
105 |
waitingToEmpty = false; |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
715
diff
changeset
|
106 |
} |
2 | 107 |
} |
22561
5ee70f9edbb7
7133499: (fc) FileChannel.read not preempted by asynchronous close on OS X
alanb
parents:
14342
diff
changeset
|
108 |
if (interrupted) |
5ee70f9edbb7
7133499: (fc) FileChannel.read not preempted by asynchronous close on OS X
alanb
parents:
14342
diff
changeset
|
109 |
Thread.currentThread().interrupt(); |
2 | 110 |
} |
111 |
} |