1 /* ThreadLocalMap -- a weak hash map optimised for ThreadLocal storage
2 Copyright (C) 2000, 2002, 2003, 2004, 2005, 2006, 2007
3 Free Software Foundation, Inc.
5 This file is part of GNU Classpath.
7 GNU Classpath is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU Classpath is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Classpath; see the file COPYING. If not, write to the
19 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 Linking this library statically or dynamically with other modules is
23 making a combined work based on this library. Thus, the terms and
24 conditions of the GNU General Public License cover the whole
27 As a special exception, the copyright holders of this library give you
28 permission to link this library with independent modules to produce an
29 executable, regardless of the license terms of these independent
30 modules, and to copy and distribute the resulting executable under
31 terms of your choice, provided that you also meet, for each linked
32 independent module, the terms and conditions of the license of that
33 module. An independent module is a module which is not derived from
34 or based on this library. If you modify this library, you may extend
35 this exception to your version of the library, but you are not
36 obligated to do so. If you do not wish to do so, delete this
37 exception statement from your version. */
41 //import java.lang.ref.WeakReference;
44 * ThreadLocalMap is the basic storage for the map of ThreadLocal instance
45 * to a thread's current value.
47 * Some applications really work out ThreadLocals, leading to this
48 * optimized implementation.
50 final class ThreadLocalMap
53 * The log (base 2) of the initial size of the map
55 private static final int LOG_INITIAL_SIZE = 3;
58 * The maximum occupancy rate (after which we grow)
60 private static final float MAX_OCCUPANCY = 0.7f;
63 * The target occupancy rate.
65 private static final float TARGET_OCCUPANCY = 0.5f;
68 * The deleted entry sentinel value.
70 private static final Entry deletedEntry = new Entry(null);
77 /* Dummy value to ensure fast path can be optimized */
78 entries = new Entry[1];
86 private Entry[] entries;
89 * Used for start index computation
94 * The number of entries currently in the map
99 * Create or grow the table to the specified size. The size must be a
100 * power of two for the efficient mask/hash computation.
102 * @param newSize The new table size.
104 private void newEntryArray(int newSize)
106 int mask = newSize - 1;
107 Entry[] oldEntries = this.entries;
108 this.entries = new Entry[newSize];
109 this.hashMask = mask;
111 /* Copy old entries to new table */
113 if (oldEntries != null)
115 //for(Entry e: oldEntries)
116 for(int j = 0; j < oldEntries.length; j++)
121 ThreadLocal/*<?>*/ key = e.get();
122 if (e != deletedEntry && key != null)
124 for(int i = key.fastHash & mask;; i = (i + 1) & mask)
126 if (entries[i] == null)
140 * We have run out of space in our locals. We use this as the
141 * trigger to attempt to find unused slots as ThreadLocals have
142 * died. If we recover any slots this way then we do not grow.
144 private void overflow()
146 /* First 'actual' use */
147 if (entries.length == 1)
149 newEntryArray(1 << LOG_INITIAL_SIZE);
153 /* Attempt to recover unused slots */
155 for(int i=0; i < entries.length; i++)
157 Entry e = entries[i];
160 if (e == deletedEntry)
164 else if (e.get() == null)
166 entries[i] = deletedEntry;
172 if ((count-deleted) <= (TARGET_OCCUPANCY * entries.length))
174 /* We currently rehash by simple reallocating into a same-sized table.
175 * An alternative would be to implement a clever hashing algorithm but
176 * as this happens infrequently this seems preferred */
177 newEntryArray(entries.length);
181 /* Double the size */
182 newEntryArray(entries.length << 1);
186 * This is the class that is used to refer to a thread local weakly.
188 * As we want to minimize indirections we extend WeakReference.
190 static final class Entry /*extends WeakReference<ThreadLocal<?>>*/ {
192 * The value stored in this slot
200 Entry(ThreadLocal/*<?>*/ threadLocal) {
201 //super(threadLocal);
211 * Gets the value associated with the ThreadLocal object for the currently
212 * executing Thread. If this is the first time the current thread has called
213 * get(), and it has not already called set(), the sentinel value is returned.
215 * @return the value of the variable in this thread, or sentinel if not present.
217 public Object get(ThreadLocal/*<?>*/ key)
219 int mask = this.hashMask;
220 for(int i = key.fastHash & mask;; i = (i + 1) & mask) {
221 Entry e = entries[i];
223 if (e.get() == key) {
227 return ThreadLocal.sentinel;
233 * Sets the value associated with the ThreadLocal object for the currently
234 * executing Thread. This overrides any existing value associated with the
235 * current Thread and prevents <code>initialValue()</code> from being
236 * called if this is the first access to this ThreadLocal in this Thread.
238 * @param value the value to set this thread's view of the variable to
240 public void set(ThreadLocal/*<?>*/ key, Object value)
243 if ((count+1) >= (MAX_OCCUPANCY * entries.length))
249 int mask = this.hashMask;
250 for(int i = key.fastHash & mask;; i = (i + 1) & mask)
252 Entry e = entries[i];
253 if (e == null || e == deletedEntry)
256 if (e == null) count++;
257 entries[i] = e = new Entry(key);
263 ThreadLocal/*<?>*/ entryKey = e.get();
264 if (entryKey == null)
266 entries[i] = deletedEntry;
268 else if (entryKey == key)
279 * Removes the value associated with the ThreadLocal object for the
280 * currently executing Thread.
283 public void remove(ThreadLocal/*<?>*/ key)
285 int mask = this.hashMask;
286 for(int i = key.fastHash & mask;; i = (i + 1) & mask)
288 Entry e = entries[i];
291 ThreadLocal/*<?>*/ entryKey = e.get();
294 if (entryKey == null) {
295 entries[i] = deletedEntry;
301 /* Remove from the table */
302 entries[i] = deletedEntry;
310 * Clear out the map. Done once during thread death.
317 * Inherit all the InheritableThreadLocal instances from the given parent.
319 * @param parentMap The map to inherit from.
321 /*public void inherit(ThreadLocalMap parentMap) {
322 for(Entry e: parentMap.entries)
324 if (e != null && e != deletedEntry)
326 ThreadLocal<?> key = e.get();
327 if (key instanceof InheritableThreadLocal)
329 set(key, ((InheritableThreadLocal)key).childValue(e.value));