1 /*
2  * Copyright 2014 The Netty Project
3  *
4  * The Netty Project licenses this file to you under the Apache License,
5  * version 2.0 (the "License"); you may not use this file except in compliance
6  * with the License. You may obtain a copy of the License at:
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations
14  * under the License.
15  */

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 /**
35  * The internal data structure that stores the thread-local variables for Netty and all {@link FastThreadLocal}s.
36  * Note that this class is for internal use only and is subject to change at any time.  Use {@link FastThreadLocal}
37  * unless you know what you are doing.
38  */

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     // Cache line padding (must be public)
124     // With CompressedOops enabled, an instance of this class should occupy at least 128 bytes.
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         // We should subtract 1 from the count because the first element in 'indexedVariables' is reserved
181         // by 'FastThreadLocal' to keep the list of 'FastThreadLocal's to remove on 'FastThreadLocal.removeAll()'.
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             // Start with small capacity to keep memory overhead as low as possible.
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     /**
295      * @return {@code trueif and only if a new thread-local variable has been created
296      */

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