author | alanb |
Thu, 12 Aug 2010 19:53:25 +0100 | |
changeset 6301 | c90a67d75c9f |
parent 5506 | 202f599c92aa |
child 7025 | 6e002f9a2899 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2000, 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 |
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 |
// -- Caches -- |
|
45 |
||
46 |
// The number of temp buffers in our pool |
|
6301 | 47 |
private static final int TEMP_BUF_POOL_SIZE = 8; |
2 | 48 |
|
6301 | 49 |
// Per-thread cache of temporary direct buffers |
50 |
private static ThreadLocal<BufferCache> bufferCache = |
|
51 |
new ThreadLocal<BufferCache>() |
|
52 |
{ |
|
53 |
@Override |
|
54 |
protected BufferCache initialValue() { |
|
55 |
return new BufferCache(); |
|
56 |
} |
|
57 |
}; |
|
58 |
||
59 |
/** |
|
60 |
* A simple cache of direct buffers. |
|
61 |
*/ |
|
62 |
private static class BufferCache { |
|
63 |
// the array of buffers |
|
64 |
private ByteBuffer[] buffers; |
|
2 | 65 |
|
6301 | 66 |
// the number of buffers in the cache |
67 |
private int count; |
|
68 |
||
69 |
// the index of the first valid buffer (undefined if count == 0) |
|
70 |
private int start; |
|
71 |
||
72 |
private int next(int i) { |
|
73 |
return (i + 1) % TEMP_BUF_POOL_SIZE; |
|
74 |
} |
|
75 |
||
76 |
BufferCache() { |
|
77 |
buffers = new ByteBuffer[TEMP_BUF_POOL_SIZE]; |
|
78 |
} |
|
79 |
||
80 |
/** |
|
81 |
* Removes and returns a buffer from the cache of at least the given |
|
82 |
* size (or null if no suitable buffer is found). |
|
83 |
*/ |
|
84 |
ByteBuffer get(int size) { |
|
85 |
if (count == 0) |
|
86 |
return null; // cache is empty |
|
895 | 87 |
|
6301 | 88 |
ByteBuffer[] buffers = this.buffers; |
2 | 89 |
|
6301 | 90 |
// search for suitable buffer (often the first buffer will do) |
91 |
ByteBuffer buf = buffers[start]; |
|
92 |
if (buf.capacity() < size) { |
|
93 |
buf = null; |
|
94 |
int i = start; |
|
95 |
while ((i = next(i)) != start) { |
|
96 |
ByteBuffer bb = buffers[i]; |
|
97 |
if (bb == null) |
|
98 |
break; |
|
99 |
if (bb.capacity() >= size) { |
|
100 |
buf = bb; |
|
101 |
break; |
|
102 |
} |
|
103 |
} |
|
104 |
if (buf == null) |
|
105 |
return null; |
|
106 |
// move first element to here to avoid re-packing |
|
107 |
buffers[i] = buffers[start]; |
|
108 |
} |
|
109 |
||
110 |
// remove first element |
|
111 |
buffers[start] = null; |
|
112 |
start = next(start); |
|
113 |
count--; |
|
114 |
||
115 |
// prepare the buffer and return it |
|
116 |
buf.rewind(); |
|
117 |
buf.limit(size); |
|
118 |
return buf; |
|
119 |
} |
|
120 |
||
121 |
boolean offerFirst(ByteBuffer buf) { |
|
122 |
if (count >= TEMP_BUF_POOL_SIZE) { |
|
123 |
return false; |
|
124 |
} else { |
|
125 |
start = (start + TEMP_BUF_POOL_SIZE - 1) % TEMP_BUF_POOL_SIZE; |
|
126 |
buffers[start] = buf; |
|
127 |
count++; |
|
128 |
return true; |
|
2 | 129 |
} |
130 |
} |
|
131 |
||
6301 | 132 |
boolean offerLast(ByteBuffer buf) { |
133 |
if (count >= TEMP_BUF_POOL_SIZE) { |
|
134 |
return false; |
|
135 |
} else { |
|
136 |
int next = (start + count) % TEMP_BUF_POOL_SIZE; |
|
137 |
buffers[next] = buf; |
|
138 |
count++; |
|
139 |
return true; |
|
2 | 140 |
} |
141 |
} |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
895
diff
changeset
|
142 |
|
6301 | 143 |
boolean isEmpty() { |
144 |
return count == 0; |
|
145 |
} |
|
146 |
||
147 |
ByteBuffer removeFirst() { |
|
148 |
assert count > 0; |
|
149 |
ByteBuffer buf = buffers[start]; |
|
150 |
buffers[start] = null; |
|
151 |
start = next(start); |
|
152 |
count--; |
|
153 |
return buf; |
|
154 |
} |
|
155 |
} |
|
156 |
||
157 |
/** |
|
158 |
* Returns a temporary buffer of at least the given size |
|
159 |
*/ |
|
160 |
static ByteBuffer getTemporaryDirectBuffer(int size) { |
|
161 |
BufferCache cache = bufferCache.get(); |
|
162 |
ByteBuffer buf = cache.get(size); |
|
163 |
if (buf != null) { |
|
164 |
return buf; |
|
165 |
} else { |
|
166 |
// No suitable buffer in the cache so we need to allocate a new |
|
167 |
// one. To avoid the cache growing then we remove the first |
|
168 |
// buffer from the cache and free it. |
|
169 |
if (!cache.isEmpty()) { |
|
170 |
buf = cache.removeFirst(); |
|
171 |
free(buf); |
|
172 |
} |
|
173 |
return ByteBuffer.allocateDirect(size); |
|
174 |
} |
|
175 |
} |
|
176 |
||
177 |
/** |
|
178 |
* Releases a temporary buffer by returning to the cache or freeing it. |
|
179 |
*/ |
|
180 |
static void releaseTemporaryDirectBuffer(ByteBuffer buf) { |
|
181 |
offerFirstTemporaryDirectBuffer(buf); |
|
182 |
} |
|
183 |
||
184 |
/** |
|
185 |
* Releases a temporary buffer by returning to the cache or freeing it. If |
|
186 |
* returning to the cache then insert it at the start so that it is |
|
187 |
* likely to be returned by a subsequent call to getTemporaryDirectBuffer. |
|
188 |
*/ |
|
189 |
static void offerFirstTemporaryDirectBuffer(ByteBuffer buf) { |
|
190 |
assert buf != null; |
|
191 |
BufferCache cache = bufferCache.get(); |
|
192 |
if (!cache.offerFirst(buf)) { |
|
193 |
// cache is full |
|
194 |
free(buf); |
|
195 |
} |
|
196 |
} |
|
197 |
||
198 |
/** |
|
199 |
* Releases a temporary buffer by returning to the cache or freeing it. If |
|
200 |
* returning to the cache then insert it at the end. This makes it |
|
201 |
* suitable for scatter/gather operations where the buffers are returned to |
|
202 |
* cache in same order that they were obtained. |
|
203 |
*/ |
|
204 |
static void offerLastTemporaryDirectBuffer(ByteBuffer buf) { |
|
205 |
assert buf != null; |
|
206 |
BufferCache cache = bufferCache.get(); |
|
207 |
if (!cache.offerLast(buf)) { |
|
208 |
// cache is full |
|
209 |
free(buf); |
|
210 |
} |
|
211 |
} |
|
212 |
||
213 |
/** |
|
214 |
* Frees the memory for the given direct buffer |
|
215 |
*/ |
|
216 |
private static void free(ByteBuffer buf) { |
|
217 |
((DirectBuffer)buf).cleaner().clean(); |
|
2 | 218 |
} |
219 |
||
220 |
private static class SelectorWrapper { |
|
221 |
private Selector sel; |
|
222 |
private SelectorWrapper (Selector sel) { |
|
223 |
this.sel = sel; |
|
224 |
Cleaner.create(this, new Closer(sel)); |
|
225 |
} |
|
226 |
private static class Closer implements Runnable { |
|
227 |
private Selector sel; |
|
228 |
private Closer (Selector sel) { |
|
229 |
this.sel = sel; |
|
230 |
} |
|
231 |
public void run () { |
|
232 |
try { |
|
233 |
sel.close(); |
|
234 |
} catch (Throwable th) { |
|
235 |
throw new Error(th); |
|
236 |
} |
|
237 |
} |
|
238 |
} |
|
239 |
public Selector get() { return sel;} |
|
240 |
} |
|
241 |
||
242 |
// Per-thread cached selector |
|
51 | 243 |
private static ThreadLocal<SoftReference<SelectorWrapper>> localSelector |
244 |
= new ThreadLocal<SoftReference<SelectorWrapper>>(); |
|
2 | 245 |
// Hold a reference to the selWrapper object to prevent it from |
246 |
// being cleaned when the temporary selector wrapped is on lease. |
|
51 | 247 |
private static ThreadLocal<SelectorWrapper> localSelectorWrapper |
248 |
= new ThreadLocal<SelectorWrapper>(); |
|
2 | 249 |
|
250 |
// When finished, invoker must ensure that selector is empty |
|
251 |
// by cancelling any related keys and explicitly releasing |
|
252 |
// the selector by invoking releaseTemporarySelector() |
|
253 |
static Selector getTemporarySelector(SelectableChannel sc) |
|
254 |
throws IOException |
|
255 |
{ |
|
51 | 256 |
SoftReference<SelectorWrapper> ref = localSelector.get(); |
2 | 257 |
SelectorWrapper selWrapper = null; |
258 |
Selector sel = null; |
|
259 |
if (ref == null |
|
51 | 260 |
|| ((selWrapper = ref.get()) == null) |
2 | 261 |
|| ((sel = selWrapper.get()) == null) |
262 |
|| (sel.provider() != sc.provider())) { |
|
263 |
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
|
264 |
selWrapper = new SelectorWrapper(sel); |
637b139ce684
6645197: (so) Timed read with socket adaptor throws ClosedSelectorException if temporary selector GC'ed
sherman
parents:
51
diff
changeset
|
265 |
localSelector.set(new SoftReference<SelectorWrapper>(selWrapper)); |
2 | 266 |
} |
787
637b139ce684
6645197: (so) Timed read with socket adaptor throws ClosedSelectorException if temporary selector GC'ed
sherman
parents:
51
diff
changeset
|
267 |
localSelectorWrapper.set(selWrapper); |
2 | 268 |
return sel; |
269 |
} |
|
270 |
||
271 |
static void releaseTemporarySelector(Selector sel) |
|
272 |
throws IOException |
|
273 |
{ |
|
274 |
// Selector should be empty |
|
275 |
sel.selectNow(); // Flush cancelled keys |
|
276 |
assert sel.keys().isEmpty() : "Temporary selector not empty"; |
|
277 |
localSelectorWrapper.set(null); |
|
278 |
} |
|
279 |
||
280 |
||
281 |
// -- Random stuff -- |
|
282 |
||
283 |
static ByteBuffer[] subsequence(ByteBuffer[] bs, int offset, int length) { |
|
284 |
if ((offset == 0) && (length == bs.length)) |
|
285 |
return bs; |
|
286 |
int n = length; |
|
287 |
ByteBuffer[] bs2 = new ByteBuffer[n]; |
|
288 |
for (int i = 0; i < n; i++) |
|
289 |
bs2[i] = bs[offset + i]; |
|
290 |
return bs2; |
|
291 |
} |
|
292 |
||
293 |
static <E> Set<E> ungrowableSet(final Set<E> s) { |
|
294 |
return new Set<E>() { |
|
295 |
||
296 |
public int size() { return s.size(); } |
|
297 |
public boolean isEmpty() { return s.isEmpty(); } |
|
298 |
public boolean contains(Object o) { return s.contains(o); } |
|
299 |
public Object[] toArray() { return s.toArray(); } |
|
300 |
public <T> T[] toArray(T[] a) { return s.toArray(a); } |
|
301 |
public String toString() { return s.toString(); } |
|
302 |
public Iterator<E> iterator() { return s.iterator(); } |
|
303 |
public boolean equals(Object o) { return s.equals(o); } |
|
304 |
public int hashCode() { return s.hashCode(); } |
|
305 |
public void clear() { s.clear(); } |
|
306 |
public boolean remove(Object o) { return s.remove(o); } |
|
307 |
||
308 |
public boolean containsAll(Collection<?> coll) { |
|
309 |
return s.containsAll(coll); |
|
310 |
} |
|
311 |
public boolean removeAll(Collection<?> coll) { |
|
312 |
return s.removeAll(coll); |
|
313 |
} |
|
314 |
public boolean retainAll(Collection<?> coll) { |
|
315 |
return s.retainAll(coll); |
|
316 |
} |
|
317 |
||
318 |
public boolean add(E o){ |
|
319 |
throw new UnsupportedOperationException(); |
|
320 |
} |
|
321 |
public boolean addAll(Collection<? extends E> coll) { |
|
322 |
throw new UnsupportedOperationException(); |
|
323 |
} |
|
324 |
||
325 |
}; |
|
326 |
} |
|
327 |
||
328 |
||
329 |
// -- Unsafe access -- |
|
330 |
||
331 |
private static Unsafe unsafe = Unsafe.getUnsafe(); |
|
332 |
||
333 |
private static byte _get(long a) { |
|
334 |
return unsafe.getByte(a); |
|
335 |
} |
|
336 |
||
337 |
private static void _put(long a, byte b) { |
|
338 |
unsafe.putByte(a, b); |
|
339 |
} |
|
340 |
||
341 |
static void erase(ByteBuffer bb) { |
|
342 |
unsafe.setMemory(((DirectBuffer)bb).address(), bb.capacity(), (byte)0); |
|
343 |
} |
|
344 |
||
345 |
static Unsafe unsafe() { |
|
346 |
return unsafe; |
|
347 |
} |
|
348 |
||
349 |
private static int pageSize = -1; |
|
350 |
||
351 |
static int pageSize() { |
|
352 |
if (pageSize == -1) |
|
353 |
pageSize = unsafe().pageSize(); |
|
354 |
return pageSize; |
|
355 |
} |
|
356 |
||
357 |
private static volatile Constructor directByteBufferConstructor = null; |
|
358 |
||
359 |
private static void initDBBConstructor() { |
|
51 | 360 |
AccessController.doPrivileged(new PrivilegedAction<Void>() { |
361 |
public Void run() { |
|
2 | 362 |
try { |
51 | 363 |
Class<?> cl = Class.forName("java.nio.DirectByteBuffer"); |
2 | 364 |
Constructor ctor = cl.getDeclaredConstructor( |
365 |
new Class[] { int.class, |
|
366 |
long.class, |
|
367 |
Runnable.class }); |
|
368 |
ctor.setAccessible(true); |
|
369 |
directByteBufferConstructor = ctor; |
|
370 |
} catch (ClassNotFoundException x) { |
|
371 |
throw new InternalError(); |
|
372 |
} catch (NoSuchMethodException x) { |
|
373 |
throw new InternalError(); |
|
374 |
} catch (IllegalArgumentException x) { |
|
375 |
throw new InternalError(); |
|
376 |
} catch (ClassCastException x) { |
|
377 |
throw new InternalError(); |
|
378 |
} |
|
379 |
return null; |
|
380 |
}}); |
|
381 |
} |
|
382 |
||
383 |
static MappedByteBuffer newMappedByteBuffer(int size, long addr, |
|
384 |
Runnable unmapper) |
|
385 |
{ |
|
386 |
MappedByteBuffer dbb; |
|
387 |
if (directByteBufferConstructor == null) |
|
388 |
initDBBConstructor(); |
|
389 |
try { |
|
390 |
dbb = (MappedByteBuffer)directByteBufferConstructor.newInstance( |
|
391 |
new Object[] { new Integer(size), |
|
392 |
new Long(addr), |
|
393 |
unmapper }); |
|
394 |
} catch (InstantiationException e) { |
|
395 |
throw new InternalError(); |
|
396 |
} catch (IllegalAccessException e) { |
|
397 |
throw new InternalError(); |
|
398 |
} catch (InvocationTargetException e) { |
|
399 |
throw new InternalError(); |
|
400 |
} |
|
401 |
return dbb; |
|
402 |
} |
|
403 |
||
404 |
private static volatile Constructor directByteBufferRConstructor = null; |
|
405 |
||
406 |
private static void initDBBRConstructor() { |
|
51 | 407 |
AccessController.doPrivileged(new PrivilegedAction<Void>() { |
408 |
public Void run() { |
|
2 | 409 |
try { |
51 | 410 |
Class<?> cl = Class.forName("java.nio.DirectByteBufferR"); |
2 | 411 |
Constructor ctor = cl.getDeclaredConstructor( |
412 |
new Class[] { int.class, |
|
413 |
long.class, |
|
414 |
Runnable.class }); |
|
415 |
ctor.setAccessible(true); |
|
416 |
directByteBufferRConstructor = ctor; |
|
417 |
} catch (ClassNotFoundException x) { |
|
418 |
throw new InternalError(); |
|
419 |
} catch (NoSuchMethodException x) { |
|
420 |
throw new InternalError(); |
|
421 |
} catch (IllegalArgumentException x) { |
|
422 |
throw new InternalError(); |
|
423 |
} catch (ClassCastException x) { |
|
424 |
throw new InternalError(); |
|
425 |
} |
|
426 |
return null; |
|
427 |
}}); |
|
428 |
} |
|
429 |
||
430 |
static MappedByteBuffer newMappedByteBufferR(int size, long addr, |
|
431 |
Runnable unmapper) |
|
432 |
{ |
|
433 |
MappedByteBuffer dbb; |
|
434 |
if (directByteBufferRConstructor == null) |
|
435 |
initDBBRConstructor(); |
|
436 |
try { |
|
437 |
dbb = (MappedByteBuffer)directByteBufferRConstructor.newInstance( |
|
438 |
new Object[] { new Integer(size), |
|
439 |
new Long(addr), |
|
440 |
unmapper }); |
|
441 |
} catch (InstantiationException e) { |
|
442 |
throw new InternalError(); |
|
443 |
} catch (IllegalAccessException e) { |
|
444 |
throw new InternalError(); |
|
445 |
} catch (InvocationTargetException e) { |
|
446 |
throw new InternalError(); |
|
447 |
} |
|
448 |
return dbb; |
|
449 |
} |
|
450 |
||
451 |
||
452 |
// -- Bug compatibility -- |
|
453 |
||
454 |
private static volatile String bugLevel = null; |
|
455 |
||
456 |
static boolean atBugLevel(String bl) { // package-private |
|
457 |
if (bugLevel == null) { |
|
458 |
if (!sun.misc.VM.isBooted()) |
|
459 |
return false; |
|
460 |
String value = AccessController.doPrivileged( |
|
461 |
new GetPropertyAction("sun.nio.ch.bugLevel")); |
|
462 |
bugLevel = (value != null) ? value : ""; |
|
463 |
} |
|
464 |
return bugLevel.equals(bl); |
|
465 |
} |
|
466 |
||
467 |
||
468 |
||
469 |
// -- Initialization -- |
|
470 |
||
471 |
private static boolean loaded = false; |
|
472 |
||
473 |
static void load() { |
|
474 |
synchronized (Util.class) { |
|
475 |
if (loaded) |
|
476 |
return; |
|
477 |
loaded = true; |
|
478 |
java.security.AccessController |
|
479 |
.doPrivileged(new sun.security.action.LoadLibraryAction("net")); |
|
480 |
java.security.AccessController |
|
481 |
.doPrivileged(new sun.security.action.LoadLibraryAction("nio")); |
|
482 |
// IOUtil must be initialized; Its native methods are called from |
|
483 |
// other places in native nio code so they must be set up. |
|
484 |
IOUtil.initIDs(); |
|
485 |
} |
|
486 |
} |
|
487 |
||
488 |
} |