/** * Returns the current thread's {@code ThreadLocalRandom}. * * @return the current thread's {@code ThreadLocalRandom} */ public static ThreadLocalRandom current() { return InternalThreadLocalMap.get().random(); }
/** * Returns the current thread's {@code ThreadLocalRandom}. * * @return the current thread's {@code ThreadLocalRandom} */ public static ThreadLocalRandom current() { return InternalThreadLocalMap.get().random(); }
/** * Return {@code true} if the implementation is {@link Sharable} and so can be added * to different {@link ChannelPipeline}s. */ public boolean isSharable() { /** * Cache the result of {@link Sharable} annotation detection to workaround a condition. We use a * {@link ThreadLocal} and {@link WeakHashMap} to eliminate the volatile write/reads. Using different * {@link WeakHashMap} instances per {@link Thread} is good enough for us and the number of * {@link Thread}s are quite limited anyway. * * See <a href="https://github.com/netty/netty/issues/2289">#2289</a>. */ Class<?> clazz = getClass(); Map<Class<?>, Boolean> cache = InternalThreadLocalMap.get().handlerSharableCache(); Boolean sharable = cache.get(clazz); if (sharable == null) { sharable = clazz.isAnnotationPresent(Sharable.class); cache.put(clazz, sharable); } return sharable; }
public static TypeParameterMatcher get(final Class<?> parameterType) { final Map<Class<?>, TypeParameterMatcher> getCache = InternalThreadLocalMap.get().typeParameterMatcherGetCache(); TypeParameterMatcher matcher = getCache.get(parameterType); if (matcher == null) { if (parameterType == Object.class) { matcher = NOOP; } else { matcher = new ReflectiveMatcher(parameterType); } getCache.put(parameterType, matcher); } return matcher; }
public static TypeParameterMatcher find( final Object object, final Class<?> parametrizedSuperclass, final String typeParamName) { final Map<Class<?>, Map<String, TypeParameterMatcher>> findCache = InternalThreadLocalMap.get().typeParameterMatcherFindCache(); final Class<?> thisClass = object.getClass(); Map<String, TypeParameterMatcher> map = findCache.get(thisClass); if (map == null) { map = new HashMap<String, TypeParameterMatcher>(); findCache.put(thisClass, map); } TypeParameterMatcher matcher = map.get(typeParamName); if (matcher == null) { matcher = get(find0(object, parametrizedSuperclass, typeParamName)); map.put(typeParamName, matcher); } return matcher; }
/** * Returns a cached thread-local {@link CharsetDecoder} for the specified {@link Charset}. * * @param charset The specified charset * @return The decoder for the specified {@code charset} */ public static CharsetDecoder decoder(Charset charset) { checkNotNull(charset, "charset"); Map<Charset, CharsetDecoder> map = InternalThreadLocalMap.get().charsetDecoderCache(); CharsetDecoder d = map.get(charset); if (d != null) { d.reset().onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE); return d; } d = decoder(charset, CodingErrorAction.REPLACE, CodingErrorAction.REPLACE); map.put(charset, d); return d; }
/** * Returns the current thread's {@code ThreadLocalRandom}. * * @return the current thread's {@code ThreadLocalRandom} */ public static ThreadLocalRandom current() { return InternalThreadLocalMap.get().random(); }
public static TypeParameterMatcher get(final Class<?> parameterType) { final Map<Class<?>, TypeParameterMatcher> getCache = InternalThreadLocalMap.get().typeParameterMatcherGetCache(); TypeParameterMatcher matcher = getCache.get(parameterType); if (matcher == null) { if (parameterType == Object.class) { matcher = NOOP; } else { matcher = new ReflectiveMatcher(parameterType); } getCache.put(parameterType, matcher); } return matcher; }
/** * Returns a cached thread-local {@link CharsetEncoder} for the specified {@link Charset}. * * @param charset The specified charset * @return The encoder for the specified {@code charset} */ public static CharsetEncoder encoder(Charset charset) { checkNotNull(charset, "charset"); Map<Charset, CharsetEncoder> map = InternalThreadLocalMap.get().charsetEncoderCache(); CharsetEncoder e = map.get(charset); if (e != null) { e.reset().onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE); return e; } e = encoder(charset, CodingErrorAction.REPLACE, CodingErrorAction.REPLACE); map.put(charset, e); return e; }
public static TypeParameterMatcher find( final Object object, final Class<?> parametrizedSuperclass, final String typeParamName) { final Map<Class<?>, Map<String, TypeParameterMatcher>> findCache = InternalThreadLocalMap.get().typeParameterMatcherFindCache(); final Class<?> thisClass = object.getClass(); Map<String, TypeParameterMatcher> map = findCache.get(thisClass); if (map == null) { map = new HashMap<String, TypeParameterMatcher>(); findCache.put(thisClass, map); } TypeParameterMatcher matcher = map.get(typeParamName); if (matcher == null) { matcher = get(find0(object, parametrizedSuperclass, typeParamName)); map.put(typeParamName, matcher); } return matcher; }
/** * Returns a cached thread-local {@link CharsetEncoder} for the specified {@link Charset}. * * @param charset The specified charset * @return The encoder for the specified {@code charset} */ public static CharsetEncoder encoder(Charset charset) { checkNotNull(charset, "charset"); Map<Charset, CharsetEncoder> map = InternalThreadLocalMap.get().charsetEncoderCache(); CharsetEncoder e = map.get(charset); if (e != null) { e.reset().onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE); return e; } e = encoder(charset, CodingErrorAction.REPLACE, CodingErrorAction.REPLACE); map.put(charset, e); return e; }
/** * Returns a cached thread-local {@link CharsetDecoder} for the specified {@link Charset}. * * @param charset The specified charset * @return The decoder for the specified {@code charset} */ public static CharsetDecoder decoder(Charset charset) { checkNotNull(charset, "charset"); Map<Charset, CharsetDecoder> map = InternalThreadLocalMap.get().charsetDecoderCache(); CharsetDecoder d = map.get(charset); if (d != null) { d.reset().onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE); return d; } d = decoder(charset, CodingErrorAction.REPLACE, CodingErrorAction.REPLACE); map.put(charset, d); return d; }
/** * Set the value for the current thread. */ public final void set(V value) { if (value != InternalThreadLocalMap.UNSET) { InternalThreadLocalMap threadLocalMap = InternalThreadLocalMap.get(); if (setKnownNotUnset(threadLocalMap, value)) { registerCleaner(threadLocalMap); } } else { remove(); } }
/** * Returns the current value for the current thread */ @SuppressWarnings("unchecked") public final V get() { InternalThreadLocalMap threadLocalMap = InternalThreadLocalMap.get(); Object v = threadLocalMap.indexedVariable(index); if (v != InternalThreadLocalMap.UNSET) { return (V) v; } V value = initialize(threadLocalMap); registerCleaner(threadLocalMap); return value; }
/** * Set the value for the current thread. */ public final void set(V value) { if (value != InternalThreadLocalMap.UNSET) { InternalThreadLocalMap threadLocalMap = InternalThreadLocalMap.get(); if (setKnownNotUnset(threadLocalMap, value)) { registerCleaner(threadLocalMap); } } else { remove(); } }
/** * Returns the current value for the current thread */ @SuppressWarnings("unchecked") public final V get() { InternalThreadLocalMap threadLocalMap = InternalThreadLocalMap.get(); Object v = threadLocalMap.indexedVariable(index); if (v != InternalThreadLocalMap.UNSET) { return (V) v; } V value = initialize(threadLocalMap); registerCleaner(threadLocalMap); return value; }
/** * The logic in this method should be identical to {@link #notifyListeners()} but * cannot share code because the listener(s) cannot be cached for an instance of {@link DefaultPromise} since the * listener(s) may be changed and is protected by a synchronized operation. */ private static void notifyListenerWithStackOverFlowProtection(final EventExecutor executor, final Future<?> future, final GenericFutureListener<?> listener) { if (executor.inEventLoop()) { final InternalThreadLocalMap threadLocals = InternalThreadLocalMap.get(); final int stackDepth = threadLocals.futureListenerStackDepth(); if (stackDepth < MAX_LISTENER_STACK_DEPTH) { threadLocals.setFutureListenerStackDepth(stackDepth + 1); try { notifyListener0(future, listener); } finally { threadLocals.setFutureListenerStackDepth(stackDepth); } return; } } safeExecute(executor, new Runnable() { @Override public void run() { notifyListener0(future, listener); } }); }
private void notifyListeners() { EventExecutor executor = executor(); if (executor.inEventLoop()) { final InternalThreadLocalMap threadLocals = InternalThreadLocalMap.get(); final int stackDepth = threadLocals.futureListenerStackDepth(); if (stackDepth < MAX_LISTENER_STACK_DEPTH) { threadLocals.setFutureListenerStackDepth(stackDepth + 1); try { notifyListenersNow(); } finally { threadLocals.setFutureListenerStackDepth(stackDepth); } return; } } safeExecute(executor, new Runnable() { @Override public void run() { notifyListenersNow(); } }); }
@Override protected void doBeginRead() throws Exception { if (readInProgress) { return; } Queue<Object> inboundBuffer = this.inboundBuffer; if (inboundBuffer.isEmpty()) { readInProgress = true; return; } final InternalThreadLocalMap threadLocals = InternalThreadLocalMap.get(); final Integer stackDepth = threadLocals.localChannelReaderStackDepth(); if (stackDepth < MAX_READER_STACK_DEPTH) { threadLocals.setLocalChannelReaderStackDepth(stackDepth + 1); try { readInbound(); } finally { threadLocals.setLocalChannelReaderStackDepth(stackDepth); } } else { try { eventLoop().execute(readTask); } catch (Throwable cause) { logger.warn("Closing Local channels {}-{} because exception occurred!", this, peer, cause); close(); peer.close(); PlatformDependent.throwException(cause); } } }
private void notifyListeners() { EventExecutor executor = executor(); if (executor.inEventLoop()) { final InternalThreadLocalMap threadLocals = InternalThreadLocalMap.get(); final int stackDepth = threadLocals.futureListenerStackDepth(); if (stackDepth < MAX_LISTENER_STACK_DEPTH) { threadLocals.setFutureListenerStackDepth(stackDepth + 1); try { notifyListenersNow(); } finally { threadLocals.setFutureListenerStackDepth(stackDepth); } return; } } safeExecute(executor, new Runnable() { @Override public void run() { notifyListenersNow(); } }); }