author | alanb |
Sun, 15 Feb 2009 12:25:54 +0000 | |
changeset 2057 | 3acf8e5e2ca0 |
parent 895 | 67f1dc69ad10 |
child 5506 | 202f599c92aa |
permissions | -rw-r--r-- |
2 | 1 |
/* |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
895
diff
changeset
|
2 |
* Copyright 2000-2009 Sun Microsystems, Inc. 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 |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
package sun.nio.ch; |
|
27 |
||
28 |
import java.lang.ref.SoftReference; |
|
29 |
import java.lang.reflect.*; |
|
30 |
import java.io.IOException; |
|
31 |
import java.nio.ByteBuffer; |
|
32 |
import java.nio.MappedByteBuffer; |
|
33 |
import java.nio.channels.*; |
|
34 |
import java.security.AccessController; |
|
35 |
import java.security.PrivilegedAction; |
|
36 |
import java.util.*; |
|
37 |
import sun.misc.Unsafe; |
|
38 |
import sun.misc.Cleaner; |
|
39 |
import sun.security.action.GetPropertyAction; |
|
40 |
||
41 |
||
42 |
class Util { |
|
43 |
||
44 |
||
45 |
// -- Caches -- |
|
46 |
||
47 |
// The number of temp buffers in our pool |
|
48 |
private static final int TEMP_BUF_POOL_SIZE = 3; |
|
49 |
||
50 |
// Per-thread soft cache of the last temporary direct buffer |
|
51 | 51 |
private static ThreadLocal<SoftReference<ByteBuffer>>[] bufferPool; |
2 | 52 |
|
895 | 53 |
@SuppressWarnings("unchecked") |
54 |
static ThreadLocal<SoftReference<ByteBuffer>>[] createThreadLocalBufferPool() { |
|
55 |
return new ThreadLocal[TEMP_BUF_POOL_SIZE]; |
|
56 |
} |
|
57 |
||
2 | 58 |
static { |
895 | 59 |
bufferPool = createThreadLocalBufferPool(); |
2 | 60 |
for (int i=0; i<TEMP_BUF_POOL_SIZE; i++) |
51 | 61 |
bufferPool[i] = new ThreadLocal<SoftReference<ByteBuffer>>(); |
2 | 62 |
} |
63 |
||
64 |
static ByteBuffer getTemporaryDirectBuffer(int size) { |
|
65 |
ByteBuffer buf = null; |
|
66 |
// Grab a buffer if available |
|
67 |
for (int i=0; i<TEMP_BUF_POOL_SIZE; i++) { |
|
51 | 68 |
SoftReference<ByteBuffer> ref = bufferPool[i].get(); |
69 |
if ((ref != null) && ((buf = ref.get()) != null) && |
|
2 | 70 |
(buf.capacity() >= size)) { |
71 |
buf.rewind(); |
|
72 |
buf.limit(size); |
|
73 |
bufferPool[i].set(null); |
|
74 |
return buf; |
|
75 |
} |
|
76 |
} |
|
77 |
||
78 |
// Make a new one |
|
79 |
return ByteBuffer.allocateDirect(size); |
|
80 |
} |
|
81 |
||
82 |
static void releaseTemporaryDirectBuffer(ByteBuffer buf) { |
|
83 |
if (buf == null) |
|
84 |
return; |
|
85 |
// Put it in an empty slot if such exists |
|
86 |
for (int i=0; i<TEMP_BUF_POOL_SIZE; i++) { |
|
51 | 87 |
SoftReference<ByteBuffer> ref = bufferPool[i].get(); |
2 | 88 |
if ((ref == null) || (ref.get() == null)) { |
51 | 89 |
bufferPool[i].set(new SoftReference<ByteBuffer>(buf)); |
2 | 90 |
return; |
91 |
} |
|
92 |
} |
|
93 |
// Otherwise replace a smaller one in the cache if such exists |
|
94 |
for (int i=0; i<TEMP_BUF_POOL_SIZE; i++) { |
|
51 | 95 |
SoftReference<ByteBuffer> ref = bufferPool[i].get(); |
96 |
ByteBuffer inCacheBuf = ref.get(); |
|
2 | 97 |
if ((inCacheBuf == null) || (buf.capacity() > inCacheBuf.capacity())) { |
51 | 98 |
bufferPool[i].set(new SoftReference<ByteBuffer>(buf)); |
2 | 99 |
return; |
100 |
} |
|
101 |
} |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
895
diff
changeset
|
102 |
|
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
895
diff
changeset
|
103 |
// release memory |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
895
diff
changeset
|
104 |
((DirectBuffer)buf).cleaner().clean(); |
2 | 105 |
} |
106 |
||
107 |
private static class SelectorWrapper { |
|
108 |
private Selector sel; |
|
109 |
private SelectorWrapper (Selector sel) { |
|
110 |
this.sel = sel; |
|
111 |
Cleaner.create(this, new Closer(sel)); |
|
112 |
} |
|
113 |
private static class Closer implements Runnable { |
|
114 |
private Selector sel; |
|
115 |
private Closer (Selector sel) { |
|
116 |
this.sel = sel; |
|
117 |
} |
|
118 |
public void run () { |
|
119 |
try { |
|
120 |
sel.close(); |
|
121 |
} catch (Throwable th) { |
|
122 |
throw new Error(th); |
|
123 |
} |
|
124 |
} |
|
125 |
} |
|
126 |
public Selector get() { return sel;} |
|
127 |
} |
|
128 |
||
129 |
// Per-thread cached selector |
|
51 | 130 |
private static ThreadLocal<SoftReference<SelectorWrapper>> localSelector |
131 |
= new ThreadLocal<SoftReference<SelectorWrapper>>(); |
|
2 | 132 |
// Hold a reference to the selWrapper object to prevent it from |
133 |
// being cleaned when the temporary selector wrapped is on lease. |
|
51 | 134 |
private static ThreadLocal<SelectorWrapper> localSelectorWrapper |
135 |
= new ThreadLocal<SelectorWrapper>(); |
|
2 | 136 |
|
137 |
// When finished, invoker must ensure that selector is empty |
|
138 |
// by cancelling any related keys and explicitly releasing |
|
139 |
// the selector by invoking releaseTemporarySelector() |
|
140 |
static Selector getTemporarySelector(SelectableChannel sc) |
|
141 |
throws IOException |
|
142 |
{ |
|
51 | 143 |
SoftReference<SelectorWrapper> ref = localSelector.get(); |
2 | 144 |
SelectorWrapper selWrapper = null; |
145 |
Selector sel = null; |
|
146 |
if (ref == null |
|
51 | 147 |
|| ((selWrapper = ref.get()) == null) |
2 | 148 |
|| ((sel = selWrapper.get()) == null) |
149 |
|| (sel.provider() != sc.provider())) { |
|
150 |
sel = sc.provider().openSelector(); |
|
787
637b139ce684
6645197: (so) Timed read with socket adaptor throws ClosedSelectorException if temporary selector GC'ed
sherman
parents:
51
diff
changeset
|
151 |
selWrapper = new SelectorWrapper(sel); |
637b139ce684
6645197: (so) Timed read with socket adaptor throws ClosedSelectorException if temporary selector GC'ed
sherman
parents:
51
diff
changeset
|
152 |
localSelector.set(new SoftReference<SelectorWrapper>(selWrapper)); |
2 | 153 |
} |
787
637b139ce684
6645197: (so) Timed read with socket adaptor throws ClosedSelectorException if temporary selector GC'ed
sherman
parents:
51
diff
changeset
|
154 |
localSelectorWrapper.set(selWrapper); |
2 | 155 |
return sel; |
156 |
} |
|
157 |
||
158 |
static void releaseTemporarySelector(Selector sel) |
|
159 |
throws IOException |
|
160 |
{ |
|
161 |
// Selector should be empty |
|
162 |
sel.selectNow(); // Flush cancelled keys |
|
163 |
assert sel.keys().isEmpty() : "Temporary selector not empty"; |
|
164 |
localSelectorWrapper.set(null); |
|
165 |
} |
|
166 |
||
167 |
||
168 |
// -- Random stuff -- |
|
169 |
||
170 |
static ByteBuffer[] subsequence(ByteBuffer[] bs, int offset, int length) { |
|
171 |
if ((offset == 0) && (length == bs.length)) |
|
172 |
return bs; |
|
173 |
int n = length; |
|
174 |
ByteBuffer[] bs2 = new ByteBuffer[n]; |
|
175 |
for (int i = 0; i < n; i++) |
|
176 |
bs2[i] = bs[offset + i]; |
|
177 |
return bs2; |
|
178 |
} |
|
179 |
||
180 |
static <E> Set<E> ungrowableSet(final Set<E> s) { |
|
181 |
return new Set<E>() { |
|
182 |
||
183 |
public int size() { return s.size(); } |
|
184 |
public boolean isEmpty() { return s.isEmpty(); } |
|
185 |
public boolean contains(Object o) { return s.contains(o); } |
|
186 |
public Object[] toArray() { return s.toArray(); } |
|
187 |
public <T> T[] toArray(T[] a) { return s.toArray(a); } |
|
188 |
public String toString() { return s.toString(); } |
|
189 |
public Iterator<E> iterator() { return s.iterator(); } |
|
190 |
public boolean equals(Object o) { return s.equals(o); } |
|
191 |
public int hashCode() { return s.hashCode(); } |
|
192 |
public void clear() { s.clear(); } |
|
193 |
public boolean remove(Object o) { return s.remove(o); } |
|
194 |
||
195 |
public boolean containsAll(Collection<?> coll) { |
|
196 |
return s.containsAll(coll); |
|
197 |
} |
|
198 |
public boolean removeAll(Collection<?> coll) { |
|
199 |
return s.removeAll(coll); |
|
200 |
} |
|
201 |
public boolean retainAll(Collection<?> coll) { |
|
202 |
return s.retainAll(coll); |
|
203 |
} |
|
204 |
||
205 |
public boolean add(E o){ |
|
206 |
throw new UnsupportedOperationException(); |
|
207 |
} |
|
208 |
public boolean addAll(Collection<? extends E> coll) { |
|
209 |
throw new UnsupportedOperationException(); |
|
210 |
} |
|
211 |
||
212 |
}; |
|
213 |
} |
|
214 |
||
215 |
||
216 |
// -- Unsafe access -- |
|
217 |
||
218 |
private static Unsafe unsafe = Unsafe.getUnsafe(); |
|
219 |
||
220 |
private static byte _get(long a) { |
|
221 |
return unsafe.getByte(a); |
|
222 |
} |
|
223 |
||
224 |
private static void _put(long a, byte b) { |
|
225 |
unsafe.putByte(a, b); |
|
226 |
} |
|
227 |
||
228 |
static void erase(ByteBuffer bb) { |
|
229 |
unsafe.setMemory(((DirectBuffer)bb).address(), bb.capacity(), (byte)0); |
|
230 |
} |
|
231 |
||
232 |
static Unsafe unsafe() { |
|
233 |
return unsafe; |
|
234 |
} |
|
235 |
||
236 |
private static int pageSize = -1; |
|
237 |
||
238 |
static int pageSize() { |
|
239 |
if (pageSize == -1) |
|
240 |
pageSize = unsafe().pageSize(); |
|
241 |
return pageSize; |
|
242 |
} |
|
243 |
||
244 |
private static volatile Constructor directByteBufferConstructor = null; |
|
245 |
||
246 |
private static void initDBBConstructor() { |
|
51 | 247 |
AccessController.doPrivileged(new PrivilegedAction<Void>() { |
248 |
public Void run() { |
|
2 | 249 |
try { |
51 | 250 |
Class<?> cl = Class.forName("java.nio.DirectByteBuffer"); |
2 | 251 |
Constructor ctor = cl.getDeclaredConstructor( |
252 |
new Class[] { int.class, |
|
253 |
long.class, |
|
254 |
Runnable.class }); |
|
255 |
ctor.setAccessible(true); |
|
256 |
directByteBufferConstructor = ctor; |
|
257 |
} catch (ClassNotFoundException x) { |
|
258 |
throw new InternalError(); |
|
259 |
} catch (NoSuchMethodException x) { |
|
260 |
throw new InternalError(); |
|
261 |
} catch (IllegalArgumentException x) { |
|
262 |
throw new InternalError(); |
|
263 |
} catch (ClassCastException x) { |
|
264 |
throw new InternalError(); |
|
265 |
} |
|
266 |
return null; |
|
267 |
}}); |
|
268 |
} |
|
269 |
||
270 |
static MappedByteBuffer newMappedByteBuffer(int size, long addr, |
|
271 |
Runnable unmapper) |
|
272 |
{ |
|
273 |
MappedByteBuffer dbb; |
|
274 |
if (directByteBufferConstructor == null) |
|
275 |
initDBBConstructor(); |
|
276 |
try { |
|
277 |
dbb = (MappedByteBuffer)directByteBufferConstructor.newInstance( |
|
278 |
new Object[] { new Integer(size), |
|
279 |
new Long(addr), |
|
280 |
unmapper }); |
|
281 |
} catch (InstantiationException e) { |
|
282 |
throw new InternalError(); |
|
283 |
} catch (IllegalAccessException e) { |
|
284 |
throw new InternalError(); |
|
285 |
} catch (InvocationTargetException e) { |
|
286 |
throw new InternalError(); |
|
287 |
} |
|
288 |
return dbb; |
|
289 |
} |
|
290 |
||
291 |
private static volatile Constructor directByteBufferRConstructor = null; |
|
292 |
||
293 |
private static void initDBBRConstructor() { |
|
51 | 294 |
AccessController.doPrivileged(new PrivilegedAction<Void>() { |
295 |
public Void run() { |
|
2 | 296 |
try { |
51 | 297 |
Class<?> cl = Class.forName("java.nio.DirectByteBufferR"); |
2 | 298 |
Constructor ctor = cl.getDeclaredConstructor( |
299 |
new Class[] { int.class, |
|
300 |
long.class, |
|
301 |
Runnable.class }); |
|
302 |
ctor.setAccessible(true); |
|
303 |
directByteBufferRConstructor = ctor; |
|
304 |
} catch (ClassNotFoundException x) { |
|
305 |
throw new InternalError(); |
|
306 |
} catch (NoSuchMethodException x) { |
|
307 |
throw new InternalError(); |
|
308 |
} catch (IllegalArgumentException x) { |
|
309 |
throw new InternalError(); |
|
310 |
} catch (ClassCastException x) { |
|
311 |
throw new InternalError(); |
|
312 |
} |
|
313 |
return null; |
|
314 |
}}); |
|
315 |
} |
|
316 |
||
317 |
static MappedByteBuffer newMappedByteBufferR(int size, long addr, |
|
318 |
Runnable unmapper) |
|
319 |
{ |
|
320 |
MappedByteBuffer dbb; |
|
321 |
if (directByteBufferRConstructor == null) |
|
322 |
initDBBRConstructor(); |
|
323 |
try { |
|
324 |
dbb = (MappedByteBuffer)directByteBufferRConstructor.newInstance( |
|
325 |
new Object[] { new Integer(size), |
|
326 |
new Long(addr), |
|
327 |
unmapper }); |
|
328 |
} catch (InstantiationException e) { |
|
329 |
throw new InternalError(); |
|
330 |
} catch (IllegalAccessException e) { |
|
331 |
throw new InternalError(); |
|
332 |
} catch (InvocationTargetException e) { |
|
333 |
throw new InternalError(); |
|
334 |
} |
|
335 |
return dbb; |
|
336 |
} |
|
337 |
||
338 |
||
339 |
// -- Bug compatibility -- |
|
340 |
||
341 |
private static volatile String bugLevel = null; |
|
342 |
||
343 |
static boolean atBugLevel(String bl) { // package-private |
|
344 |
if (bugLevel == null) { |
|
345 |
if (!sun.misc.VM.isBooted()) |
|
346 |
return false; |
|
347 |
String value = AccessController.doPrivileged( |
|
348 |
new GetPropertyAction("sun.nio.ch.bugLevel")); |
|
349 |
bugLevel = (value != null) ? value : ""; |
|
350 |
} |
|
351 |
return bugLevel.equals(bl); |
|
352 |
} |
|
353 |
||
354 |
||
355 |
||
356 |
// -- Initialization -- |
|
357 |
||
358 |
private static boolean loaded = false; |
|
359 |
||
360 |
static void load() { |
|
361 |
synchronized (Util.class) { |
|
362 |
if (loaded) |
|
363 |
return; |
|
364 |
loaded = true; |
|
365 |
java.security.AccessController |
|
366 |
.doPrivileged(new sun.security.action.LoadLibraryAction("net")); |
|
367 |
java.security.AccessController |
|
368 |
.doPrivileged(new sun.security.action.LoadLibraryAction("nio")); |
|
369 |
// IOUtil must be initialized; Its native methods are called from |
|
370 |
// other places in native nio code so they must be set up. |
|
371 |
IOUtil.initIDs(); |
|
372 |
} |
|
373 |
} |
|
374 |
||
375 |
} |