// When finished, invoker must ensure that selector is empty // by cancelling any related keys and explicitly releasing // the selector by invoking releaseTemporarySelector() static Selector getTemporarySelector(SelectableChannel sc) throws IOException { SoftReference ref = (SoftReference) localSelector.get(); SelectorWrapper selWrapper = null; Selector sel = null; if (ref == null || ((selWrapper = (SelectorWrapper) ref.get()) == null) || ((sel = selWrapper.get()) == null) || (sel.provider() != sc.provider())) { sel = sc.provider().openSelector(); localSelector.set(new SoftReference(new SelectorWrapper(sel))); } else { localSelectorWrapper.set(selWrapper); } return sel; }
/** Returns a temporary buffer of at least the given size */ public static ByteBuffer getTemporaryDirectBuffer(int size) { // If a buffer of this size is too large for the cache, there // should not be a buffer in the cache that is at least as // large. So we'll just create a new one. Also, we don't have // to remove the buffer from the cache (as this method does // below) given that we won't put the new buffer in the cache. if (isBufferTooLarge(size)) { return ByteBuffer.allocateDirect(size); } BufferCache cache = bufferCache.get(); ByteBuffer buf = cache.get(size); if (buf != null) { return buf; } else { // No suitable buffer in the cache so we need to allocate a new // one. To avoid the cache growing then we remove the first // buffer from the cache and free it. if (!cache.isEmpty()) { buf = cache.removeFirst(); free(buf); } return ByteBuffer.allocateDirect(size); } }
/** * @param log Logger. * @param time Time. * @param msg Message. */ private static void log0(@Nullable IgniteLogger log, long time, String msg) { if (log != null) { if (log.isDebugEnabled()) log.debug(msg); else log.warning(msg); } else X.println( String.format( "[%s][%s]%s", DEBUG_DATE_FMT.get().format(time), Thread.currentThread().getName(), msg)); }
/** * Releases a temporary buffer by returning to the cache or freeing it. If returning to the cache * then insert it at the end. This makes it suitable for scatter/gather operations where the * buffers are returned to cache in same order that they were obtained. */ static void offerLastTemporaryDirectBuffer(ByteBuffer buf) { // If the buffer is too large for the cache we don't have to // check the cache. We'll just free it. if (isBufferTooLarge(buf)) { free(buf); return; } assert buf != null; BufferCache cache = bufferCache.get(); if (!cache.offerLast(buf)) { // cache is full free(buf); } }
static void releaseTemporarySelector(Selector sel) throws IOException { // Selector should be empty sel.selectNow(); // Flush cancelled keys assert sel.keys().isEmpty() : "Temporary selector not empty"; localSelectorWrapper.set(null); }