jdk/src/share/classes/java/lang/ThreadLocal.java
author martin
Mon, 10 Mar 2008 14:32:51 -0700
changeset 48 dc5744ca15ea
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
4960438: (process) Need IO redirection API for subprocesses Reviewed-by: alanb, iris
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2007 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.lang;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.lang.ref.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.concurrent.atomic.AtomicInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * This class provides thread-local variables.  These variables differ from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * their normal counterparts in that each thread that accesses one (via its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * <tt>get</tt> or <tt>set</tt> method) has its own, independently initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * copy of the variable.  <tt>ThreadLocal</tt> instances are typically private
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * static fields in classes that wish to associate state with a thread (e.g.,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * a user ID or Transaction ID).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <p>For example, the class below generates unique identifiers local to each
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * A thread's id is assigned the first time it invokes <tt>ThreadId.get()</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * and remains unchanged on subsequent calls.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * import java.util.concurrent.atomic.AtomicInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * public class ThreadId {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *     // Atomic integer containing the next thread ID to be assigned
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *     private static final AtomicInteger nextId = new AtomicInteger(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *     // Thread local variable containing each thread's ID
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *     private static final ThreadLocal&lt;Integer> threadId =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *         new ThreadLocal&lt;Integer>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *             &#64;Override protected Integer initialValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *                 return nextId.getAndIncrement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *     };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *     // Returns the current thread's unique ID, assigning it if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *     public static int get() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *         return threadId.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * <p>Each thread holds an implicit reference to its copy of a thread-local
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * variable as long as the thread is alive and the <tt>ThreadLocal</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * instance is accessible; after a thread goes away, all of its copies of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * thread-local instances are subject to garbage collection (unless other
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * references to these copies exist).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * @author  Josh Bloch and Doug Lea
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * @since   1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
public class ThreadLocal<T> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * ThreadLocals rely on per-thread linear-probe hash maps attached
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * to each thread (Thread.threadLocals and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * inheritableThreadLocals).  The ThreadLocal objects act as keys,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * searched via threadLocalHashCode.  This is a custom hash code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * (useful only within ThreadLocalMaps) that eliminates collisions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * in the common case where consecutively constructed ThreadLocals
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * are used by the same threads, while remaining well-behaved in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * less common cases.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    private final int threadLocalHashCode = nextHashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * The next hash code to be given out. Updated atomically. Starts at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    private static AtomicInteger nextHashCode =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        new AtomicInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * The difference between successively generated hash codes - turns
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * implicit sequential thread-local IDs into near-optimally spread
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * multiplicative hash values for power-of-two-sized tables.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    private static final int HASH_INCREMENT = 0x61c88647;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * Returns the next hash code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    private static int nextHashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        return nextHashCode.getAndAdd(HASH_INCREMENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * Returns the current thread's "initial value" for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * thread-local variable.  This method will be invoked the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * time a thread accesses the variable with the {@link #get}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * method, unless the thread previously invoked the {@link #set}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * method, in which case the <tt>initialValue</tt> method will not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * be invoked for the thread.  Normally, this method is invoked at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * most once per thread, but it may be invoked again in case of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * subsequent invocations of {@link #remove} followed by {@link #get}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * <p>This implementation simply returns <tt>null</tt>; if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * programmer desires thread-local variables to have an initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * value other than <tt>null</tt>, <tt>ThreadLocal</tt> must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * subclassed, and this method overridden.  Typically, an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * anonymous inner class will be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @return the initial value for this thread-local
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    protected T initialValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * Creates a thread local variable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    public ThreadLocal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * Returns the value in the current thread's copy of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * thread-local variable.  If the variable has no value for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * current thread, it is first initialized to the value returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * by an invocation of the {@link #initialValue} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @return the current thread's value of this thread-local
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    public T get() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        Thread t = Thread.currentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        ThreadLocalMap map = getMap(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        if (map != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            ThreadLocalMap.Entry e = map.getEntry(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            if (e != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                return (T)e.value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        return setInitialValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Variant of set() to establish initialValue. Used instead
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * of set() in case user has overridden the set() method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @return the initial value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    private T setInitialValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        T value = initialValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        Thread t = Thread.currentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        ThreadLocalMap map = getMap(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        if (map != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            map.set(this, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            createMap(t, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * Sets the current thread's copy of this thread-local variable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * to the specified value.  Most subclasses will have no need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * override this method, relying solely on the {@link #initialValue}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * method to set the values of thread-locals.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @param value the value to be stored in the current thread's copy of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     *        this thread-local.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    public void set(T value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        Thread t = Thread.currentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        ThreadLocalMap map = getMap(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        if (map != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            map.set(this, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            createMap(t, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * Removes the current thread's value for this thread-local
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * variable.  If this thread-local variable is subsequently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * {@linkplain #get read} by the current thread, its value will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * reinitialized by invoking its {@link #initialValue} method,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * unless its value is {@linkplain #set set} by the current thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * in the interim.  This may result in multiple invocations of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * <tt>initialValue</tt> method in the current thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
         ThreadLocalMap m = getMap(Thread.currentThread());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
         if (m != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
             m.remove(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * Get the map associated with a ThreadLocal. Overridden in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * InheritableThreadLocal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * @param  t the current thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @return the map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    ThreadLocalMap getMap(Thread t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        return t.threadLocals;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * Create the map associated with a ThreadLocal. Overridden in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * InheritableThreadLocal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @param t the current thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * @param firstValue value for the initial entry of the map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * @param map the map to store.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    void createMap(Thread t, T firstValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        t.threadLocals = new ThreadLocalMap(this, firstValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * Factory method to create map of inherited thread locals.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * Designed to be called only from Thread constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * @param  parentMap the map associated with parent thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * @return a map containing the parent's inheritable bindings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    static ThreadLocalMap createInheritedMap(ThreadLocalMap parentMap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        return new ThreadLocalMap(parentMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * Method childValue is visibly defined in subclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * InheritableThreadLocal, but is internally defined here for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * sake of providing createInheritedMap factory method without
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * needing to subclass the map class in InheritableThreadLocal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * This technique is preferable to the alternative of embedding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * instanceof tests in methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    T childValue(T parentValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * ThreadLocalMap is a customized hash map suitable only for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * maintaining thread local values. No operations are exported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * outside of the ThreadLocal class. The class is package private to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * allow declaration of fields in class Thread.  To help deal with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * very large and long-lived usages, the hash table entries use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * WeakReferences for keys. However, since reference queues are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * used, stale entries are guaranteed to be removed only when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * the table starts running out of space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    static class ThreadLocalMap {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
         * The entries in this hash map extend WeakReference, using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
         * its main ref field as the key (which is always a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
         * ThreadLocal object).  Note that null keys (i.e. entry.get()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
         * == null) mean that the key is no longer referenced, so the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
         * entry can be expunged from table.  Such entries are referred to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
         * as "stale entries" in the code that follows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        static class Entry extends WeakReference<ThreadLocal> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            /** The value associated with this ThreadLocal. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            Object value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            Entry(ThreadLocal k, Object v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                super(k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                value = v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
         * The initial capacity -- MUST be a power of two.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        private static final int INITIAL_CAPACITY = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
         * The table, resized as necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
         * table.length MUST always be a power of two.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        private Entry[] table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
         * The number of entries in the table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        private int size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
         * The next size value at which to resize.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        private int threshold; // Default to 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
         * Set the resize threshold to maintain at worst a 2/3 load factor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        private void setThreshold(int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            threshold = len * 2 / 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
         * Increment i modulo len.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        private static int nextIndex(int i, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            return ((i + 1 < len) ? i + 1 : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
         * Decrement i modulo len.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        private static int prevIndex(int i, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            return ((i - 1 >= 0) ? i - 1 : len - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
         * Construct a new map initially containing (firstKey, firstValue).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
         * ThreadLocalMaps are constructed lazily, so we only create
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
         * one when we have at least one entry to put in it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        ThreadLocalMap(ThreadLocal firstKey, Object firstValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            table = new Entry[INITIAL_CAPACITY];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            int i = firstKey.threadLocalHashCode & (INITIAL_CAPACITY - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            table[i] = new Entry(firstKey, firstValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            size = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            setThreshold(INITIAL_CAPACITY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
         * Construct a new map including all Inheritable ThreadLocals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
         * from given parent map. Called only by createInheritedMap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
         * @param parentMap the map associated with parent thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        private ThreadLocalMap(ThreadLocalMap parentMap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            Entry[] parentTable = parentMap.table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            int len = parentTable.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            setThreshold(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            table = new Entry[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            for (int j = 0; j < len; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                Entry e = parentTable[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                if (e != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                    ThreadLocal key = e.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                    if (key != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                        Object value = key.childValue(e.value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                        Entry c = new Entry(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                        int h = key.threadLocalHashCode & (len - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                        while (table[h] != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                            h = nextIndex(h, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                        table[h] = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                        size++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
         * Get the entry associated with key.  This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
         * itself handles only the fast path: a direct hit of existing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
         * key. It otherwise relays to getEntryAfterMiss.  This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
         * designed to maximize performance for direct hits, in part
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
         * by making this method readily inlinable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
         * @param  key the thread local object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
         * @return the entry associated with key, or null if no such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        private Entry getEntry(ThreadLocal key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            int i = key.threadLocalHashCode & (table.length - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            Entry e = table[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            if (e != null && e.get() == key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                return getEntryAfterMiss(key, i, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
         * Version of getEntry method for use when key is not found in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
         * its direct hash slot.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
         * @param  key the thread local object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
         * @param  i the table index for key's hash code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
         * @param  e the entry at table[i]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
         * @return the entry associated with key, or null if no such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        private Entry getEntryAfterMiss(ThreadLocal key, int i, Entry e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            Entry[] tab = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            int len = tab.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            while (e != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                ThreadLocal k = e.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                if (k == key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                    return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                if (k == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                    expungeStaleEntry(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                    i = nextIndex(i, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                e = tab[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
         * Set the value associated with key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
         * @param key the thread local object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
         * @param value the value to be set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        private void set(ThreadLocal key, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            // We don't use a fast path as with get() because it is at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            // least as common to use set() to create new entries as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            // it is to replace existing ones, in which case, a fast
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            // path would fail more often than not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            Entry[] tab = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            int len = tab.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            int i = key.threadLocalHashCode & (len-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            for (Entry e = tab[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                 e != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                 e = tab[i = nextIndex(i, len)]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                ThreadLocal k = e.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                if (k == key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                    e.value = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                if (k == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                    replaceStaleEntry(key, value, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            tab[i] = new Entry(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            int sz = ++size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            if (!cleanSomeSlots(i, sz) && sz >= threshold)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                rehash();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
         * Remove the entry for key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        private void remove(ThreadLocal key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            Entry[] tab = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            int len = tab.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            int i = key.threadLocalHashCode & (len-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            for (Entry e = tab[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                 e != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                 e = tab[i = nextIndex(i, len)]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                if (e.get() == key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                    e.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                    expungeStaleEntry(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
         * Replace a stale entry encountered during a set operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
         * with an entry for the specified key.  The value passed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
         * the value parameter is stored in the entry, whether or not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
         * an entry already exists for the specified key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
         * As a side effect, this method expunges all stale entries in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
         * "run" containing the stale entry.  (A run is a sequence of entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
         * between two null slots.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
         * @param  key the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
         * @param  value the value to be associated with key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
         * @param  staleSlot index of the first stale entry encountered while
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
         *         searching for key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        private void replaceStaleEntry(ThreadLocal key, Object value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                                       int staleSlot) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            Entry[] tab = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            int len = tab.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            Entry e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            // Back up to check for prior stale entry in current run.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            // We clean out whole runs at a time to avoid continual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            // incremental rehashing due to garbage collector freeing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            // up refs in bunches (i.e., whenever the collector runs).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            int slotToExpunge = staleSlot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            for (int i = prevIndex(staleSlot, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                 (e = tab[i]) != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                 i = prevIndex(i, len))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                if (e.get() == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                    slotToExpunge = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            // Find either the key or trailing null slot of run, whichever
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            // occurs first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            for (int i = nextIndex(staleSlot, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                 (e = tab[i]) != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                 i = nextIndex(i, len)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                ThreadLocal k = e.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                // If we find key, then we need to swap it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                // with the stale entry to maintain hash table order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                // The newly stale slot, or any other stale slot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                // encountered above it, can then be sent to expungeStaleEntry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                // to remove or rehash all of the other entries in run.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                if (k == key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                    e.value = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                    tab[i] = tab[staleSlot];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                    tab[staleSlot] = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                    // Start expunge at preceding stale entry if it exists
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                    if (slotToExpunge == staleSlot)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                        slotToExpunge = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                    cleanSomeSlots(expungeStaleEntry(slotToExpunge), len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                // If we didn't find stale entry on backward scan, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                // first stale entry seen while scanning for key is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                // first still present in the run.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                if (k == null && slotToExpunge == staleSlot)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                    slotToExpunge = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            // If key not found, put new entry in stale slot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            tab[staleSlot].value = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            tab[staleSlot] = new Entry(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            // If there are any other stale entries in run, expunge them
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            if (slotToExpunge != staleSlot)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                cleanSomeSlots(expungeStaleEntry(slotToExpunge), len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
         * Expunge a stale entry by rehashing any possibly colliding entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
         * lying between staleSlot and the next null slot.  This also expunges
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
         * any other stale entries encountered before the trailing null.  See
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
         * Knuth, Section 6.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
         * @param staleSlot index of slot known to have null key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
         * @return the index of the next null slot after staleSlot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
         * (all between staleSlot and this slot will have been checked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
         * for expunging).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        private int expungeStaleEntry(int staleSlot) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            Entry[] tab = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            int len = tab.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            // expunge entry at staleSlot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            tab[staleSlot].value = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            tab[staleSlot] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            size--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            // Rehash until we encounter null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            Entry e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            for (i = nextIndex(staleSlot, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                 (e = tab[i]) != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                 i = nextIndex(i, len)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                ThreadLocal k = e.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                if (k == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                    e.value = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                    tab[i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                    size--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                    int h = k.threadLocalHashCode & (len - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                    if (h != i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                        tab[i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                        // Unlike Knuth 6.4 Algorithm R, we must scan until
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                        // null because multiple entries could have been stale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                        while (tab[h] != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                            h = nextIndex(h, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                        tab[h] = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
         * Heuristically scan some cells looking for stale entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
         * This is invoked when either a new element is added, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
         * another stale one has been expunged. It performs a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
         * logarithmic number of scans, as a balance between no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
         * scanning (fast but retains garbage) and a number of scans
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
         * proportional to number of elements, that would find all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
         * garbage but would cause some insertions to take O(n) time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
         * @param i a position known NOT to hold a stale entry. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
         * scan starts at the element after i.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
         * @param n scan control: <tt>log2(n)</tt> cells are scanned,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
         * unless a stale entry is found, in which case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
         * <tt>log2(table.length)-1</tt> additional cells are scanned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
         * When called from insertions, this parameter is the number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
         * of elements, but when from replaceStaleEntry, it is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
         * table length. (Note: all this could be changed to be either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
         * more or less aggressive by weighting n instead of just
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
         * using straight log n. But this version is simple, fast, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
         * seems to work well.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
         * @return true if any stale entries have been removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        private boolean cleanSomeSlots(int i, int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            boolean removed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            Entry[] tab = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            int len = tab.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                i = nextIndex(i, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                Entry e = tab[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                if (e != null && e.get() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                    n = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                    removed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                    i = expungeStaleEntry(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            } while ( (n >>>= 1) != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            return removed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
         * Re-pack and/or re-size the table. First scan the entire
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
         * table removing stale entries. If this doesn't sufficiently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
         * shrink the size of the table, double the table size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        private void rehash() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            expungeStaleEntries();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            // Use lower threshold for doubling to avoid hysteresis
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            if (size >= threshold - threshold / 4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                resize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
         * Double the capacity of the table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        private void resize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            Entry[] oldTab = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            int oldLen = oldTab.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            int newLen = oldLen * 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            Entry[] newTab = new Entry[newLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            for (int j = 0; j < oldLen; ++j) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                Entry e = oldTab[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                if (e != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                    ThreadLocal k = e.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                    if (k == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                        e.value = null; // Help the GC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                        int h = k.threadLocalHashCode & (newLen - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                        while (newTab[h] != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                            h = nextIndex(h, newLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                        newTab[h] = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                        count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            setThreshold(newLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            size = count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            table = newTab;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
         * Expunge all stale entries in the table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        private void expungeStaleEntries() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            Entry[] tab = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            int len = tab.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            for (int j = 0; j < len; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                Entry e = tab[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                if (e != null && e.get() == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                    expungeStaleEntry(j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
}