src/java.base/share/classes/sun/misc/ManagedLocalsThread.java

Print this page
rev 12972 : 8140606: Update library code to use internal Unsafe
Reviewed-by: duke


  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.misc;
  27 
  28 /**
  29  * A thread that has it's thread locals, and inheritable thread
  30  * locals erased on construction.
  31  */
  32 public class ManagedLocalsThread extends Thread {
  33     private static final Unsafe UNSAFE;
  34     private static final long THREAD_LOCALS;
  35     private static final long INHERITABLE_THREAD_LOCALS;
  36 
  37     public ManagedLocalsThread() {
  38         eraseThreadLocals();
  39     }
  40 
  41     public ManagedLocalsThread(Runnable target) {
  42         super(target);
  43         eraseThreadLocals();
  44     }
  45 
  46     public ManagedLocalsThread(String name) {
  47         super(name);
  48         eraseThreadLocals();
  49     }
  50 
  51     public ManagedLocalsThread(ThreadGroup group, Runnable target) {
  52         super(group, target);
  53         eraseThreadLocals();


  60 
  61     public ManagedLocalsThread(ThreadGroup group, String name) {
  62         super(group, name);
  63         eraseThreadLocals();
  64     }
  65 
  66     public ManagedLocalsThread(ThreadGroup group, Runnable target, String name) {
  67         super(group, target, name);
  68         eraseThreadLocals();
  69     }
  70 
  71     /**
  72      * Drops all thread locals (and inherited thread locals).
  73      */
  74     public final void eraseThreadLocals() {
  75         UNSAFE.putObject(this, THREAD_LOCALS, null);
  76         UNSAFE.putObject(this, INHERITABLE_THREAD_LOCALS, null);
  77     }
  78 
  79     static {
  80         UNSAFE = Unsafe.getUnsafe();
  81         Class<?> t = Thread.class;
  82         try {
  83             THREAD_LOCALS = UNSAFE.objectFieldOffset
  84                 (t.getDeclaredField("threadLocals"));
  85             INHERITABLE_THREAD_LOCALS = UNSAFE.objectFieldOffset
  86                 (t.getDeclaredField("inheritableThreadLocals"));
  87         } catch (Exception e) {
  88             throw new Error(e);
  89         }
  90     }
  91 }
  92 


  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.misc;
  27 
  28 /**
  29  * A thread that has it's thread locals, and inheritable thread
  30  * locals erased on construction.
  31  */
  32 public class ManagedLocalsThread extends Thread {
  33     private static final jdk.internal.misc.Unsafe UNSAFE;
  34     private static final long THREAD_LOCALS;
  35     private static final long INHERITABLE_THREAD_LOCALS;
  36 
  37     public ManagedLocalsThread() {
  38         eraseThreadLocals();
  39     }
  40 
  41     public ManagedLocalsThread(Runnable target) {
  42         super(target);
  43         eraseThreadLocals();
  44     }
  45 
  46     public ManagedLocalsThread(String name) {
  47         super(name);
  48         eraseThreadLocals();
  49     }
  50 
  51     public ManagedLocalsThread(ThreadGroup group, Runnable target) {
  52         super(group, target);
  53         eraseThreadLocals();


  60 
  61     public ManagedLocalsThread(ThreadGroup group, String name) {
  62         super(group, name);
  63         eraseThreadLocals();
  64     }
  65 
  66     public ManagedLocalsThread(ThreadGroup group, Runnable target, String name) {
  67         super(group, target, name);
  68         eraseThreadLocals();
  69     }
  70 
  71     /**
  72      * Drops all thread locals (and inherited thread locals).
  73      */
  74     public final void eraseThreadLocals() {
  75         UNSAFE.putObject(this, THREAD_LOCALS, null);
  76         UNSAFE.putObject(this, INHERITABLE_THREAD_LOCALS, null);
  77     }
  78 
  79     static {
  80         UNSAFE = jdk.internal.misc.Unsafe.getUnsafe();
  81         Class<?> t = Thread.class;
  82         try {
  83             THREAD_LOCALS = UNSAFE.objectFieldOffset
  84                 (t.getDeclaredField("threadLocals"));
  85             INHERITABLE_THREAD_LOCALS = UNSAFE.objectFieldOffset
  86                 (t.getDeclaredField("inheritableThreadLocals"));
  87         } catch (Exception e) {
  88             throw new Error(e);
  89         }
  90     }
  91 }
  92