1
16
17 package io.netty.util.internal;
18
19 import io.netty.util.concurrent.FastThreadLocal;
20 import io.netty.util.concurrent.FastThreadLocalThread;
21 import io.netty.util.internal.logging.InternalLogger;
22 import io.netty.util.internal.logging.InternalLoggerFactory;
23
24 import java.nio.charset.Charset;
25 import java.nio.charset.CharsetDecoder;
26 import java.nio.charset.CharsetEncoder;
27 import java.util.ArrayList;
28 import java.util.Arrays;
29 import java.util.BitSet;
30 import java.util.IdentityHashMap;
31 import java.util.Map;
32 import java.util.WeakHashMap;
33
34
39 public final class InternalThreadLocalMap extends UnpaddedInternalThreadLocalMap {
40
41 private static final InternalLogger logger = InternalLoggerFactory.getInstance(InternalThreadLocalMap.class);
42
43 private static final int DEFAULT_ARRAY_LIST_INITIAL_CAPACITY = 8;
44 private static final int STRING_BUILDER_INITIAL_SIZE;
45 private static final int STRING_BUILDER_MAX_SIZE;
46 private static final int HANDLER_SHARABLE_CACHE_INITIAL_CAPACITY = 4;
47 private static final int INDEXED_VARIABLE_TABLE_INITIAL_SIZE = 32;
48
49 public static final Object UNSET = new Object();
50
51 private BitSet cleanerFlags;
52
53 static {
54 STRING_BUILDER_INITIAL_SIZE =
55 SystemPropertyUtil.getInt("io.netty.threadLocalMap.stringBuilder.initialSize", 1024);
56 logger.debug("-Dio.netty.threadLocalMap.stringBuilder.initialSize: {}", STRING_BUILDER_INITIAL_SIZE);
57
58 STRING_BUILDER_MAX_SIZE = SystemPropertyUtil.getInt("io.netty.threadLocalMap.stringBuilder.maxSize", 1024 * 4);
59 logger.debug("-Dio.netty.threadLocalMap.stringBuilder.maxSize: {}", STRING_BUILDER_MAX_SIZE);
60 }
61
62 public static InternalThreadLocalMap getIfSet() {
63 Thread thread = Thread.currentThread();
64 if (thread instanceof FastThreadLocalThread) {
65 return ((FastThreadLocalThread) thread).threadLocalMap();
66 }
67 return slowThreadLocalMap.get();
68 }
69
70 public static InternalThreadLocalMap get() {
71 Thread thread = Thread.currentThread();
72 if (thread instanceof FastThreadLocalThread) {
73 return fastGet((FastThreadLocalThread) thread);
74 } else {
75 return slowGet();
76 }
77 }
78
79 private static InternalThreadLocalMap fastGet(FastThreadLocalThread thread) {
80 InternalThreadLocalMap threadLocalMap = thread.threadLocalMap();
81 if (threadLocalMap == null) {
82 thread.setThreadLocalMap(threadLocalMap = new InternalThreadLocalMap());
83 }
84 return threadLocalMap;
85 }
86
87 private static InternalThreadLocalMap slowGet() {
88 ThreadLocal<InternalThreadLocalMap> slowThreadLocalMap = UnpaddedInternalThreadLocalMap.slowThreadLocalMap;
89 InternalThreadLocalMap ret = slowThreadLocalMap.get();
90 if (ret == null) {
91 ret = new InternalThreadLocalMap();
92 slowThreadLocalMap.set(ret);
93 }
94 return ret;
95 }
96
97 public static void remove() {
98 Thread thread = Thread.currentThread();
99 if (thread instanceof FastThreadLocalThread) {
100 ((FastThreadLocalThread) thread).setThreadLocalMap(null);
101 } else {
102 slowThreadLocalMap.remove();
103 }
104 }
105
106 public static void destroy() {
107 slowThreadLocalMap.remove();
108 }
109
110 public static int nextVariableIndex() {
111 int index = nextIndex.getAndIncrement();
112 if (index < 0) {
113 nextIndex.decrementAndGet();
114 throw new IllegalStateException("too many thread-local indexed variables");
115 }
116 return index;
117 }
118
119 public static int lastVariableIndex() {
120 return nextIndex.get() - 1;
121 }
122
123
124
125 public long rp1, rp2, rp3, rp4, rp5, rp6, rp7, rp8, rp9;
126
127 private InternalThreadLocalMap() {
128 super(newIndexedVariableTable());
129 }
130
131 private static Object[] newIndexedVariableTable() {
132 Object[] array = new Object[INDEXED_VARIABLE_TABLE_INITIAL_SIZE];
133 Arrays.fill(array, UNSET);
134 return array;
135 }
136
137 public int size() {
138 int count = 0;
139
140 if (futureListenerStackDepth != 0) {
141 count ++;
142 }
143 if (localChannelReaderStackDepth != 0) {
144 count ++;
145 }
146 if (handlerSharableCache != null) {
147 count ++;
148 }
149 if (counterHashCode != null) {
150 count ++;
151 }
152 if (random != null) {
153 count ++;
154 }
155 if (typeParameterMatcherGetCache != null) {
156 count ++;
157 }
158 if (typeParameterMatcherFindCache != null) {
159 count ++;
160 }
161 if (stringBuilder != null) {
162 count ++;
163 }
164 if (charsetEncoderCache != null) {
165 count ++;
166 }
167 if (charsetDecoderCache != null) {
168 count ++;
169 }
170 if (arrayList != null) {
171 count ++;
172 }
173
174 for (Object o: indexedVariables) {
175 if (o != UNSET) {
176 count ++;
177 }
178 }
179
180
181
182 return count - 1;
183 }
184
185 public StringBuilder stringBuilder() {
186 StringBuilder sb = stringBuilder;
187 if (sb == null) {
188 return stringBuilder = new StringBuilder(STRING_BUILDER_INITIAL_SIZE);
189 }
190 if (sb.capacity() > STRING_BUILDER_MAX_SIZE) {
191 sb.setLength(STRING_BUILDER_INITIAL_SIZE);
192 sb.trimToSize();
193 }
194 sb.setLength(0);
195 return sb;
196 }
197
198 public Map<Charset, CharsetEncoder> charsetEncoderCache() {
199 Map<Charset, CharsetEncoder> cache = charsetEncoderCache;
200 if (cache == null) {
201 charsetEncoderCache = cache = new IdentityHashMap<Charset, CharsetEncoder>();
202 }
203 return cache;
204 }
205
206 public Map<Charset, CharsetDecoder> charsetDecoderCache() {
207 Map<Charset, CharsetDecoder> cache = charsetDecoderCache;
208 if (cache == null) {
209 charsetDecoderCache = cache = new IdentityHashMap<Charset, CharsetDecoder>();
210 }
211 return cache;
212 }
213
214 public <E> ArrayList<E> arrayList() {
215 return arrayList(DEFAULT_ARRAY_LIST_INITIAL_CAPACITY);
216 }
217
218 @SuppressWarnings("unchecked")
219 public <E> ArrayList<E> arrayList(int minCapacity) {
220 ArrayList<E> list = (ArrayList<E>) arrayList;
221 if (list == null) {
222 arrayList = new ArrayList<Object>(minCapacity);
223 return (ArrayList<E>) arrayList;
224 }
225 list.clear();
226 list.ensureCapacity(minCapacity);
227 return list;
228 }
229
230 public int futureListenerStackDepth() {
231 return futureListenerStackDepth;
232 }
233
234 public void setFutureListenerStackDepth(int futureListenerStackDepth) {
235 this.futureListenerStackDepth = futureListenerStackDepth;
236 }
237
238 public ThreadLocalRandom random() {
239 ThreadLocalRandom r = random;
240 if (r == null) {
241 random = r = new ThreadLocalRandom();
242 }
243 return r;
244 }
245
246 public Map<Class<?>, TypeParameterMatcher> typeParameterMatcherGetCache() {
247 Map<Class<?>, TypeParameterMatcher> cache = typeParameterMatcherGetCache;
248 if (cache == null) {
249 typeParameterMatcherGetCache = cache = new IdentityHashMap<Class<?>, TypeParameterMatcher>();
250 }
251 return cache;
252 }
253
254 public Map<Class<?>, Map<String, TypeParameterMatcher>> typeParameterMatcherFindCache() {
255 Map<Class<?>, Map<String, TypeParameterMatcher>> cache = typeParameterMatcherFindCache;
256 if (cache == null) {
257 typeParameterMatcherFindCache = cache = new IdentityHashMap<Class<?>, Map<String, TypeParameterMatcher>>();
258 }
259 return cache;
260 }
261
262 @Deprecated
263 public IntegerHolder counterHashCode() {
264 return counterHashCode;
265 }
266
267 @Deprecated
268 public void setCounterHashCode(IntegerHolder counterHashCode) {
269 this.counterHashCode = counterHashCode;
270 }
271
272 public Map<Class<?>, Boolean> handlerSharableCache() {
273 Map<Class<?>, Boolean> cache = handlerSharableCache;
274 if (cache == null) {
275
276 handlerSharableCache = cache = new WeakHashMap<Class<?>, Boolean>(HANDLER_SHARABLE_CACHE_INITIAL_CAPACITY);
277 }
278 return cache;
279 }
280
281 public int localChannelReaderStackDepth() {
282 return localChannelReaderStackDepth;
283 }
284
285 public void setLocalChannelReaderStackDepth(int localChannelReaderStackDepth) {
286 this.localChannelReaderStackDepth = localChannelReaderStackDepth;
287 }
288
289 public Object indexedVariable(int index) {
290 Object[] lookup = indexedVariables;
291 return index < lookup.length? lookup[index] : UNSET;
292 }
293
294
297 public boolean setIndexedVariable(int index, Object value) {
298 Object[] lookup = indexedVariables;
299 if (index < lookup.length) {
300 Object oldValue = lookup[index];
301 lookup[index] = value;
302 return oldValue == UNSET;
303 } else {
304 expandIndexedVariableTableAndSet(index, value);
305 return true;
306 }
307 }
308
309 private void expandIndexedVariableTableAndSet(int index, Object value) {
310 Object[] oldArray = indexedVariables;
311 final int oldCapacity = oldArray.length;
312 int newCapacity = index;
313 newCapacity |= newCapacity >>> 1;
314 newCapacity |= newCapacity >>> 2;
315 newCapacity |= newCapacity >>> 4;
316 newCapacity |= newCapacity >>> 8;
317 newCapacity |= newCapacity >>> 16;
318 newCapacity ++;
319
320 Object[] newArray = Arrays.copyOf(oldArray, newCapacity);
321 Arrays.fill(newArray, oldCapacity, newArray.length, UNSET);
322 newArray[index] = value;
323 indexedVariables = newArray;
324 }
325
326 public Object removeIndexedVariable(int index) {
327 Object[] lookup = indexedVariables;
328 if (index < lookup.length) {
329 Object v = lookup[index];
330 lookup[index] = UNSET;
331 return v;
332 } else {
333 return UNSET;
334 }
335 }
336
337 public boolean isIndexedVariableSet(int index) {
338 Object[] lookup = indexedVariables;
339 return index < lookup.length && lookup[index] != UNSET;
340 }
341
342 public boolean isCleanerFlagSet(int index) {
343 return cleanerFlags != null && cleanerFlags.get(index);
344 }
345
346 public void setCleanerFlag(int index) {
347 if (cleanerFlags == null) {
348 cleanerFlags = new BitSet();
349 }
350 cleanerFlags.set(index);
351 }
352 }
353