author | chegar |
Fri, 18 Dec 2015 16:06:24 +0000 | |
changeset 34716 | 7477a052aecc |
parent 33674 | 566777f73c32 |
permissions | -rw-r--r-- |
23894 | 1 |
/* |
2 |
* Copyright (c) 2013, 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 |
|
23932 | 6 |
* under the terms of the GNU General Public License version 2 only, as |
23894 | 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 |
* |
|
23932 | 11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
23894 | 12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
23932 | 13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
23894 | 14 |
* version 2 for more details (a copy is included in the LICENSE file that |
15 |
* accompanied this code). |
|
16 |
* |
|
23932 | 17 |
* You should have received a copy of the GNU General Public License version |
23894 | 18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
23932 | 19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
23894 | 20 |
* |
23932 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
23894 | 22 |
* or visit www.oracle.com if you need additional information or have any |
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package sun.misc; |
|
27 |
||
28 |
import java.security.AccessControlContext; |
|
29903 | 29 |
import java.security.AccessController; |
23894 | 30 |
import java.security.ProtectionDomain; |
29903 | 31 |
import java.security.PrivilegedAction; |
32 |
import java.util.concurrent.atomic.AtomicInteger; |
|
23894 | 33 |
|
34 |
/** |
|
35 |
* A thread that has no permissions, is not a member of any user-defined |
|
36 |
* ThreadGroup and supports the ability to erase ThreadLocals. |
|
37 |
*/ |
|
34716
7477a052aecc
8056152: API to create Threads that do not inherit inheritable thread-local initial values
chegar
parents:
33674
diff
changeset
|
38 |
public final class InnocuousThread extends Thread { |
33674
566777f73c32
8140606: Update library code to use internal Unsafe
chegar
parents:
29904
diff
changeset
|
39 |
private static final jdk.internal.misc.Unsafe UNSAFE; |
34716
7477a052aecc
8056152: API to create Threads that do not inherit inheritable thread-local initial values
chegar
parents:
33674
diff
changeset
|
40 |
private static final long THREAD_LOCALS; |
7477a052aecc
8056152: API to create Threads that do not inherit inheritable thread-local initial values
chegar
parents:
33674
diff
changeset
|
41 |
private static final long INHERITABLE_THREAD_LOCALS; |
29903 | 42 |
private static final ThreadGroup INNOCUOUSTHREADGROUP; |
23894 | 43 |
private static final AccessControlContext ACC; |
44 |
private static final long INHERITEDACCESSCONTROLCONTEXT; |
|
29904
d5f909aa26bc
8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
29903
diff
changeset
|
45 |
private static final long CONTEXTCLASSLOADER; |
23894 | 46 |
|
29903 | 47 |
private static final AtomicInteger threadNumber = new AtomicInteger(1); |
48 |
||
23894 | 49 |
public InnocuousThread(Runnable target) { |
29903 | 50 |
this(INNOCUOUSTHREADGROUP, target, |
51 |
"InnocuousThread-" + threadNumber.getAndIncrement()); |
|
52 |
} |
|
53 |
||
54 |
public InnocuousThread(Runnable target, String name) { |
|
55 |
this(INNOCUOUSTHREADGROUP, target, name); |
|
56 |
} |
|
57 |
||
29904
d5f909aa26bc
8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
29903
diff
changeset
|
58 |
public InnocuousThread(ThreadGroup group, Runnable target, String name) { |
34716
7477a052aecc
8056152: API to create Threads that do not inherit inheritable thread-local initial values
chegar
parents:
33674
diff
changeset
|
59 |
super(group, target, name, 0L, false); |
23894 | 60 |
UNSAFE.putOrderedObject(this, INHERITEDACCESSCONTROLCONTEXT, ACC); |
29904
d5f909aa26bc
8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
29903
diff
changeset
|
61 |
UNSAFE.putOrderedObject(this, CONTEXTCLASSLOADER, ClassLoader.getSystemClassLoader()); |
23894 | 62 |
} |
63 |
||
64 |
@Override |
|
65 |
public void setUncaughtExceptionHandler(UncaughtExceptionHandler x) { |
|
66 |
// silently fail |
|
67 |
} |
|
68 |
||
69 |
@Override |
|
70 |
public void setContextClassLoader(ClassLoader cl) { |
|
29904
d5f909aa26bc
8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
29903
diff
changeset
|
71 |
// Allow clearing of the TCCL to remove the reference to the system classloader. |
29903 | 72 |
if (cl == null) |
29904
d5f909aa26bc
8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
29903
diff
changeset
|
73 |
super.setContextClassLoader(null); |
29903 | 74 |
else |
75 |
throw new SecurityException("setContextClassLoader"); |
|
23894 | 76 |
} |
77 |
||
34716
7477a052aecc
8056152: API to create Threads that do not inherit inheritable thread-local initial values
chegar
parents:
33674
diff
changeset
|
78 |
/** |
7477a052aecc
8056152: API to create Threads that do not inherit inheritable thread-local initial values
chegar
parents:
33674
diff
changeset
|
79 |
* Drops all thread locals (and inherited thread locals). |
7477a052aecc
8056152: API to create Threads that do not inherit inheritable thread-local initial values
chegar
parents:
33674
diff
changeset
|
80 |
*/ |
7477a052aecc
8056152: API to create Threads that do not inherit inheritable thread-local initial values
chegar
parents:
33674
diff
changeset
|
81 |
public final void eraseThreadLocals() { |
7477a052aecc
8056152: API to create Threads that do not inherit inheritable thread-local initial values
chegar
parents:
33674
diff
changeset
|
82 |
UNSAFE.putObject(this, THREAD_LOCALS, null); |
7477a052aecc
8056152: API to create Threads that do not inherit inheritable thread-local initial values
chegar
parents:
33674
diff
changeset
|
83 |
UNSAFE.putObject(this, INHERITABLE_THREAD_LOCALS, null); |
7477a052aecc
8056152: API to create Threads that do not inherit inheritable thread-local initial values
chegar
parents:
33674
diff
changeset
|
84 |
} |
7477a052aecc
8056152: API to create Threads that do not inherit inheritable thread-local initial values
chegar
parents:
33674
diff
changeset
|
85 |
|
23894 | 86 |
// ensure run method is run only once |
87 |
private volatile boolean hasRun; |
|
88 |
||
89 |
@Override |
|
90 |
public void run() { |
|
91 |
if (Thread.currentThread() == this && !hasRun) { |
|
92 |
hasRun = true; |
|
93 |
super.run(); |
|
94 |
} |
|
95 |
} |
|
96 |
||
97 |
// Use Unsafe to access Thread group and ThreadGroup parent fields |
|
98 |
static { |
|
99 |
try { |
|
100 |
ACC = new AccessControlContext(new ProtectionDomain[] { |
|
101 |
new ProtectionDomain(null, null) |
|
102 |
}); |
|
103 |
||
104 |
// Find and use topmost ThreadGroup as parent of new group |
|
33674
566777f73c32
8140606: Update library code to use internal Unsafe
chegar
parents:
29904
diff
changeset
|
105 |
UNSAFE = jdk.internal.misc.Unsafe.getUnsafe(); |
23894 | 106 |
Class<?> tk = Thread.class; |
107 |
Class<?> gk = ThreadGroup.class; |
|
108 |
||
34716
7477a052aecc
8056152: API to create Threads that do not inherit inheritable thread-local initial values
chegar
parents:
33674
diff
changeset
|
109 |
THREAD_LOCALS = UNSAFE.objectFieldOffset |
7477a052aecc
8056152: API to create Threads that do not inherit inheritable thread-local initial values
chegar
parents:
33674
diff
changeset
|
110 |
(tk.getDeclaredField("threadLocals")); |
7477a052aecc
8056152: API to create Threads that do not inherit inheritable thread-local initial values
chegar
parents:
33674
diff
changeset
|
111 |
INHERITABLE_THREAD_LOCALS = UNSAFE.objectFieldOffset |
7477a052aecc
8056152: API to create Threads that do not inherit inheritable thread-local initial values
chegar
parents:
33674
diff
changeset
|
112 |
(tk.getDeclaredField("inheritableThreadLocals")); |
23894 | 113 |
INHERITEDACCESSCONTROLCONTEXT = UNSAFE.objectFieldOffset |
114 |
(tk.getDeclaredField("inheritedAccessControlContext")); |
|
29904
d5f909aa26bc
8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
29903
diff
changeset
|
115 |
CONTEXTCLASSLOADER = UNSAFE.objectFieldOffset |
d5f909aa26bc
8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
29903
diff
changeset
|
116 |
(tk.getDeclaredField("contextClassLoader")); |
23894 | 117 |
|
118 |
long tg = UNSAFE.objectFieldOffset(tk.getDeclaredField("group")); |
|
119 |
long gp = UNSAFE.objectFieldOffset(gk.getDeclaredField("parent")); |
|
120 |
ThreadGroup group = (ThreadGroup) |
|
121 |
UNSAFE.getObject(Thread.currentThread(), tg); |
|
122 |
||
123 |
while (group != null) { |
|
124 |
ThreadGroup parent = (ThreadGroup)UNSAFE.getObject(group, gp); |
|
125 |
if (parent == null) |
|
126 |
break; |
|
127 |
group = parent; |
|
128 |
} |
|
29903 | 129 |
final ThreadGroup root = group; |
130 |
INNOCUOUSTHREADGROUP = AccessController.doPrivileged( |
|
131 |
(PrivilegedAction<ThreadGroup>) () -> |
|
132 |
{ return new ThreadGroup(root, "InnocuousThreadGroup"); }); |
|
23894 | 133 |
} catch (Exception e) { |
134 |
throw new Error(e); |
|
135 |
} |
|
136 |
} |
|
137 |
} |