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