001    /* ThreadLocal -- a variable with a unique value per thread
002       Copyright (C) 2000, 2002, 2003, 2006 Free Software Foundation, Inc.
003    
004    This file is part of GNU Classpath.
005    
006    GNU Classpath is free software; you can redistribute it and/or modify
007    it under the terms of the GNU General Public License as published by
008    the Free Software Foundation; either version 2, or (at your option)
009    any later version.
010    
011    GNU Classpath is distributed in the hope that it will be useful, but
012    WITHOUT ANY WARRANTY; without even the implied warranty of
013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014    General Public License for more details.
015    
016    You should have received a copy of the GNU General Public License
017    along with GNU Classpath; see the file COPYING.  If not, write to the
018    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019    02110-1301 USA.
020    
021    Linking this library statically or dynamically with other modules is
022    making a combined work based on this library.  Thus, the terms and
023    conditions of the GNU General Public License cover the whole
024    combination.
025    
026    As a special exception, the copyright holders of this library give you
027    permission to link this library with independent modules to produce an
028    executable, regardless of the license terms of these independent
029    modules, and to copy and distribute the resulting executable under
030    terms of your choice, provided that you also meet, for each linked
031    independent module, the terms and conditions of the license of that
032    module.  An independent module is a module which is not derived from
033    or based on this library.  If you modify this library, you may extend
034    this exception to your version of the library, but you are not
035    obligated to do so.  If you do not wish to do so, delete this
036    exception statement from your version. */
037    
038    package java.lang;
039    
040    import java.util.Map;
041    
042    
043    /**
044     * ThreadLocal objects have a different state associated with every
045     * Thread that accesses them. Every access to the ThreadLocal object
046     * (through the <code>get()</code> and <code>set()</code> methods)
047     * only affects the state of the object as seen by the currently
048     * executing Thread.
049     *
050     * <p>The first time a ThreadLocal object is accessed on a particular
051     * Thread, the state for that Thread's copy of the local variable is set by
052     * executing the method <code>initialValue()</code>.
053     * </p>
054     *
055     * <p>An example how you can use this:
056     * </p>
057     *
058     * <pre>
059     * class Connection
060     * {
061     *   private static ThreadLocal owner = new ThreadLocal()
062     *     {
063     *       public Object initialValue()
064     *       {
065     *         return("nobody");
066     *       }
067     *     };
068     * ...
069     * }
070     * </pre>
071     *
072     * <p>Now all instances of connection can see who the owner of the currently
073     * executing Thread is by calling <code>owner.get()</code>. By default any
074     * Thread would be associated with 'nobody'. But the Connection object could
075     * offer a method that changes the owner associated with the Thread on
076     * which the method was called by calling <code>owner.put("somebody")</code>.
077     * (Such an owner changing method should then be guarded by security checks.)
078     * </p>
079     *
080     * <p>When a Thread is garbage collected all references to values of
081     * the ThreadLocal objects associated with that Thread are removed.
082     * </p>
083     *
084     * @author Mark Wielaard (mark@klomp.org)
085     * @author Eric Blake (ebb9@email.byu.edu)
086     * @since 1.2
087     * @status updated to 1.5
088     */
089    public class ThreadLocal<T>
090    {
091      /**
092       * Placeholder to distinguish between uninitialized and null set by the
093       * user. Do not expose this to the public. Package visible for use by
094       * InheritableThreadLocal
095       */
096      static final Object sentinel = new Object();
097    
098      /**
099       * The base for the computation of the next hash for a thread local.
100       */
101      private static int nextHashBase = 1;
102    
103      /**
104       * Allocate a new hash.
105       */
106      private synchronized int computeNextHash() 
107      {
108        return nextHashBase++ * 6709;
109      }
110    
111      /**
112       * Hash code computed for ThreadLocalMap
113       */
114      final int fastHash;
115    
116      /**
117       * Creates a ThreadLocal object without associating any value to it yet.
118       */
119      public ThreadLocal()
120      {
121        constructNative();
122        fastHash = computeNextHash();
123      }
124    
125      /**
126       * Called once per thread on the first invocation of get(), if set() was
127       * not already called. The default implementation returns <code>null</code>.
128       * Often, this method is overridden to create the appropriate initial object
129       * for the current thread's view of the ThreadLocal.
130       *
131       * @return the initial value of the variable in this thread
132       */
133      protected T initialValue()
134      {
135        return null;
136      }
137    
138      /**
139       * Gets the value associated with the ThreadLocal object for the currently
140       * executing Thread. If this is the first time the current thread has called
141       * get(), and it has not already called set(), the value is obtained by
142       * <code>initialValue()</code>.
143       *
144       * @return the value of the variable in this thread
145       */
146      public native T get();
147    
148      private final Object internalGet()
149      {
150        ThreadLocalMap map = Thread.getThreadLocals();
151        // Note that we don't have to synchronize, as only this thread will
152        // ever modify the map.
153        T value = (T) map.get(this);
154        if (value == sentinel)
155          {
156            value = initialValue();
157            map.set(this, value);
158          }
159        return value;
160      }
161    
162      /**
163       * Sets the value associated with the ThreadLocal object for the currently
164       * executing Thread. This overrides any existing value associated with the
165       * current Thread and prevents <code>initialValue()</code> from being
166       * called if this is the first access to this ThreadLocal in this Thread.
167       *
168       * @param value the value to set this thread's view of the variable to
169       */
170      public native void set(T value);
171    
172      private final void internalSet(Object value)
173      {
174        ThreadLocalMap map = Thread.getThreadLocals();
175        // Note that we don't have to synchronize, as only this thread will
176        // ever modify the map.
177        map.set(this, value);
178      }
179    
180      /**
181       * Removes the value associated with the ThreadLocal object for the
182       * currently executing Thread.
183       * @since 1.5
184       */
185      public native void remove();
186    
187      private final void internalRemove()
188      {
189        ThreadLocalMap map = Thread.getThreadLocals();
190        map.remove(this);
191      }
192    
193      protected native void finalize () throws Throwable;
194    
195      private native void constructNative();
196    
197      private gnu.gcj.RawData TLSPointer;
198    }