author | alanb |
Fri, 02 Nov 2012 15:50:11 +0000 | |
changeset 14342 | 8435a30053c1 |
parent 12440 | 2689ca179e22 |
child 16921 | e70261f11307 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
12440
2689ca179e22
7118373: (se) Potential leak file descriptor when deregistrating at around the same time as an async close
robm
parents:
5506
diff
changeset
|
2 |
* Copyright (c) 2000, 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 |
import java.io.*; |
|
29 |
import java.nio.ByteBuffer; |
|
30 |
import java.nio.channels.*; |
|
31 |
import java.nio.channels.spi.*; |
|
32 |
||
33 |
||
34 |
class SourceChannelImpl |
|
35 |
extends Pipe.SourceChannel |
|
36 |
implements SelChImpl |
|
37 |
{ |
|
38 |
||
39 |
// Used to make native read and write calls |
|
40 |
private static NativeDispatcher nd; |
|
41 |
||
42 |
// The file descriptor associated with this channel |
|
43 |
FileDescriptor fd; |
|
44 |
||
45 |
// fd value needed for dev/poll. This value will remain valid |
|
46 |
// even after the value in the file descriptor object has been set to -1 |
|
47 |
int fdVal; |
|
48 |
||
49 |
// ID of native thread doing read, for signalling |
|
50 |
private volatile long thread = 0; |
|
51 |
||
52 |
// Lock held by current reading thread |
|
53 |
private final Object lock = new Object(); |
|
54 |
||
55 |
// Lock held by any thread that modifies the state fields declared below |
|
56 |
// DO NOT invoke a blocking I/O operation while holding this lock! |
|
57 |
private final Object stateLock = new Object(); |
|
58 |
||
59 |
// -- The following fields are protected by stateLock |
|
60 |
||
61 |
// Channel state |
|
62 |
private static final int ST_UNINITIALIZED = -1; |
|
63 |
private static final int ST_INUSE = 0; |
|
64 |
private static final int ST_KILLED = 1; |
|
65 |
private volatile int state = ST_UNINITIALIZED; |
|
66 |
||
67 |
// -- End of fields protected by stateLock |
|
68 |
||
69 |
||
70 |
public FileDescriptor getFD() { |
|
71 |
return fd; |
|
72 |
} |
|
73 |
||
74 |
public int getFDVal() { |
|
75 |
return fdVal; |
|
76 |
} |
|
77 |
||
78 |
SourceChannelImpl(SelectorProvider sp, FileDescriptor fd) { |
|
79 |
super(sp); |
|
80 |
this.fd = fd; |
|
81 |
this.fdVal = IOUtil.fdVal(fd); |
|
82 |
this.state = ST_INUSE; |
|
83 |
} |
|
84 |
||
85 |
protected void implCloseSelectableChannel() throws IOException { |
|
86 |
synchronized (stateLock) { |
|
12440
2689ca179e22
7118373: (se) Potential leak file descriptor when deregistrating at around the same time as an async close
robm
parents:
5506
diff
changeset
|
87 |
if (state != ST_KILLED) |
2689ca179e22
7118373: (se) Potential leak file descriptor when deregistrating at around the same time as an async close
robm
parents:
5506
diff
changeset
|
88 |
nd.preClose(fd); |
2 | 89 |
long th = thread; |
90 |
if (th != 0) |
|
91 |
NativeThread.signal(th); |
|
92 |
if (!isRegistered()) |
|
93 |
kill(); |
|
94 |
} |
|
95 |
} |
|
96 |
||
97 |
public void kill() throws IOException { |
|
98 |
synchronized (stateLock) { |
|
99 |
if (state == ST_KILLED) |
|
100 |
return; |
|
101 |
if (state == ST_UNINITIALIZED) { |
|
102 |
state = ST_KILLED; |
|
103 |
return; |
|
104 |
} |
|
105 |
assert !isOpen() && !isRegistered(); |
|
106 |
nd.close(fd); |
|
107 |
state = ST_KILLED; |
|
108 |
} |
|
109 |
} |
|
110 |
||
111 |
protected void implConfigureBlocking(boolean block) throws IOException { |
|
112 |
IOUtil.configureBlocking(fd, block); |
|
113 |
} |
|
114 |
||
115 |
public boolean translateReadyOps(int ops, int initialOps, |
|
116 |
SelectionKeyImpl sk) { |
|
117 |
int intOps = sk.nioInterestOps(); // Do this just once, it synchronizes |
|
118 |
int oldOps = sk.nioReadyOps(); |
|
119 |
int newOps = initialOps; |
|
120 |
||
121 |
if ((ops & PollArrayWrapper.POLLNVAL) != 0) |
|
122 |
throw new Error("POLLNVAL detected"); |
|
123 |
||
124 |
if ((ops & (PollArrayWrapper.POLLERR |
|
125 |
| PollArrayWrapper.POLLHUP)) != 0) { |
|
126 |
newOps = intOps; |
|
127 |
sk.nioReadyOps(newOps); |
|
128 |
return (newOps & ~oldOps) != 0; |
|
129 |
} |
|
130 |
||
131 |
if (((ops & PollArrayWrapper.POLLIN) != 0) && |
|
132 |
((intOps & SelectionKey.OP_READ) != 0)) |
|
133 |
newOps |= SelectionKey.OP_READ; |
|
134 |
||
135 |
sk.nioReadyOps(newOps); |
|
136 |
return (newOps & ~oldOps) != 0; |
|
137 |
} |
|
138 |
||
139 |
public boolean translateAndUpdateReadyOps(int ops, SelectionKeyImpl sk) { |
|
140 |
return translateReadyOps(ops, sk.nioReadyOps(), sk); |
|
141 |
} |
|
142 |
||
143 |
public boolean translateAndSetReadyOps(int ops, SelectionKeyImpl sk) { |
|
144 |
return translateReadyOps(ops, 0, sk); |
|
145 |
} |
|
146 |
||
147 |
public void translateAndSetInterestOps(int ops, SelectionKeyImpl sk) { |
|
148 |
if (ops == SelectionKey.OP_READ) |
|
149 |
ops = PollArrayWrapper.POLLIN; |
|
150 |
sk.selector.putEventOps(sk, ops); |
|
151 |
} |
|
152 |
||
153 |
private void ensureOpen() throws IOException { |
|
154 |
if (!isOpen()) |
|
155 |
throw new ClosedChannelException(); |
|
156 |
} |
|
157 |
||
158 |
public int read(ByteBuffer dst) throws IOException { |
|
159 |
ensureOpen(); |
|
160 |
synchronized (lock) { |
|
161 |
int n = 0; |
|
162 |
try { |
|
163 |
begin(); |
|
164 |
if (!isOpen()) |
|
165 |
return 0; |
|
166 |
thread = NativeThread.current(); |
|
167 |
do { |
|
168 |
n = IOUtil.read(fd, dst, -1, nd, lock); |
|
169 |
} while ((n == IOStatus.INTERRUPTED) && isOpen()); |
|
170 |
return IOStatus.normalize(n); |
|
171 |
} finally { |
|
172 |
thread = 0; |
|
173 |
end((n > 0) || (n == IOStatus.UNAVAILABLE)); |
|
174 |
assert IOStatus.check(n); |
|
175 |
} |
|
176 |
} |
|
177 |
} |
|
178 |
||
179 |
public long read(ByteBuffer[] dsts, int offset, int length) |
|
180 |
throws IOException |
|
181 |
{ |
|
182 |
if ((offset < 0) || (length < 0) || (offset > dsts.length - length)) |
|
183 |
throw new IndexOutOfBoundsException(); |
|
184 |
return read(Util.subsequence(dsts, offset, length)); |
|
185 |
} |
|
186 |
||
187 |
public long read(ByteBuffer[] dsts) throws IOException { |
|
188 |
if (dsts == null) |
|
189 |
throw new NullPointerException(); |
|
190 |
ensureOpen(); |
|
191 |
synchronized (lock) { |
|
192 |
long n = 0; |
|
193 |
try { |
|
194 |
begin(); |
|
195 |
if (!isOpen()) |
|
196 |
return 0; |
|
197 |
thread = NativeThread.current(); |
|
198 |
do { |
|
199 |
n = IOUtil.read(fd, dsts, nd); |
|
200 |
} while ((n == IOStatus.INTERRUPTED) && isOpen()); |
|
201 |
return IOStatus.normalize(n); |
|
202 |
} finally { |
|
203 |
thread = 0; |
|
204 |
end((n > 0) || (n == IOStatus.UNAVAILABLE)); |
|
205 |
assert IOStatus.check(n); |
|
206 |
} |
|
207 |
} |
|
208 |
} |
|
209 |
||
210 |
static { |
|
211 |
Util.load(); |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
2
diff
changeset
|
212 |
nd = new FileDispatcherImpl(); |
2 | 213 |
} |
214 |
||
215 |
} |