nl.topicus.jdbc.shaded.io.netty.util.internal.InternalThreadLocalMap Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spanner-jdbc Show documentation
Show all versions of spanner-jdbc Show documentation
JDBC Driver for Google Cloud Spanner
/*
* Copyright 2014 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in nl.topicus.jdbc.shaded.com.liance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.nl.topicus.jdbc.shaded.org.licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package nl.topicus.jdbc.shaded.io.nl.topicus.jdbc.shaded.net.y.util.internal;
import nl.topicus.jdbc.shaded.io.nl.topicus.jdbc.shaded.net.y.util.concurrent.FastThreadLocal;
import nl.topicus.jdbc.shaded.io.nl.topicus.jdbc.shaded.net.y.util.concurrent.FastThreadLocalThread;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.IdentityHashMap;
import java.util.Map;
import java.util.WeakHashMap;
/**
* The internal data structure that stores the thread-local variables for Netty and all {@link FastThreadLocal}s.
* Note that this class is for internal use only and is subject to change at any time. Use {@link FastThreadLocal}
* unless you know what you are doing.
*/
public final class InternalThreadLocalMap extends UnpaddedInternalThreadLocalMap {
private static final int STRING_BUILDER_MAX_CAPACITY = 1024 << 6;
private static final int DEFAULT_ARRAY_LIST_INITIAL_CAPACITY = 8;
public static final Object UNSET = new Object();
public static InternalThreadLocalMap getIfSet() {
Thread thread = Thread.currentThread();
if (thread instanceof FastThreadLocalThread) {
return ((FastThreadLocalThread) thread).threadLocalMap();
}
return slowThreadLocalMap.get();
}
public static InternalThreadLocalMap get() {
Thread thread = Thread.currentThread();
if (thread instanceof FastThreadLocalThread) {
return fastGet((FastThreadLocalThread) thread);
} else {
return slowGet();
}
}
private static InternalThreadLocalMap fastGet(FastThreadLocalThread thread) {
InternalThreadLocalMap threadLocalMap = thread.threadLocalMap();
if (threadLocalMap == null) {
thread.setThreadLocalMap(threadLocalMap = new InternalThreadLocalMap());
}
return threadLocalMap;
}
private static InternalThreadLocalMap slowGet() {
ThreadLocal slowThreadLocalMap = UnpaddedInternalThreadLocalMap.slowThreadLocalMap;
InternalThreadLocalMap ret = slowThreadLocalMap.get();
if (ret == null) {
ret = new InternalThreadLocalMap();
slowThreadLocalMap.set(ret);
}
return ret;
}
public static void remove() {
Thread thread = Thread.currentThread();
if (thread instanceof FastThreadLocalThread) {
((FastThreadLocalThread) thread).setThreadLocalMap(null);
} else {
slowThreadLocalMap.remove();
}
}
public static void destroy() {
slowThreadLocalMap.remove();
}
public static int nextVariableIndex() {
int index = nextIndex.getAndIncrement();
if (index < 0) {
nextIndex.decrementAndGet();
throw new IllegalStateException("too many thread-local indexed variables");
}
return index;
}
public static int lastVariableIndex() {
return nextIndex.get() - 1;
}
// Cache line padding (must be public)
// With CompressedOops enabled, an instance of this class should occupy at least 128 bytes.
public long rp1, rp2, rp3, rp4, rp5, rp6, rp7, rp8, rp9;
private InternalThreadLocalMap() {
super(newIndexedVariableTable());
}
private static Object[] newIndexedVariableTable() {
Object[] array = new Object[32];
Arrays.fill(array, UNSET);
return array;
}
public int size() {
int count = 0;
if (futureListenerStackDepth != 0) {
count ++;
}
if (localChannelReaderStackDepth != 0) {
count ++;
}
if (handlerSharableCache != null) {
count ++;
}
if (counterHashCode != null) {
count ++;
}
if (random != null) {
count ++;
}
if (typeParameterMatcherGetCache != null) {
count ++;
}
if (typeParameterMatcherFindCache != null) {
count ++;
}
if (stringBuilder != null) {
count ++;
}
if (charsetEncoderCache != null) {
count ++;
}
if (charsetDecoderCache != null) {
count ++;
}
if (arrayList != null) {
count ++;
}
for (Object o: indexedVariables) {
if (o != UNSET) {
count ++;
}
}
// We should subtract 1 from the count because the first element in 'indexedVariables' is reserved
// by 'FastThreadLocal' to keep the list of 'FastThreadLocal's to remove on 'FastThreadLocal.removeAll()'.
return count - 1;
}
public StringBuilder stringBuilder() {
StringBuilder builder = stringBuilder;
if (builder == null || builder.capacity() > STRING_BUILDER_MAX_CAPACITY) {
stringBuilder = builder = new StringBuilder(512);
} else {
builder.setLength(0);
}
return builder;
}
public Map charsetEncoderCache() {
Map cache = charsetEncoderCache;
if (cache == null) {
charsetEncoderCache = cache = new IdentityHashMap();
}
return cache;
}
public Map charsetDecoderCache() {
Map cache = charsetDecoderCache;
if (cache == null) {
charsetDecoderCache = cache = new IdentityHashMap();
}
return cache;
}
public ArrayList arrayList() {
return arrayList(DEFAULT_ARRAY_LIST_INITIAL_CAPACITY);
}
@SuppressWarnings("unchecked")
public ArrayList arrayList(int minCapacity) {
ArrayList list = (ArrayList) arrayList;
if (list == null) {
arrayList = new ArrayList