1 /* |
|
2 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
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(); |
|
54 } |
|
55 |
|
56 public ManagedLocalsThread(Runnable target, String name) { |
|
57 super(target, name); |
|
58 eraseThreadLocals(); |
|
59 } |
|
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 |
|