author | amurillo |
Thu, 31 Mar 2016 14:25:38 -0700 | |
changeset 36939 | c27a3b0dcc88 |
parent 36680 | c9fe8f8e7e6f |
parent 36936 | bfcdf736a998 |
child 46873 | 7ac2f551b0d6 |
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 |
||
35786
9d26db88aa14
8149653: Move sun.misc.InnocuousThread to jdk.internal.misc
chegar
parents:
34716
diff
changeset
|
26 |
package jdk.internal.misc; |
23894 | 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); |
36680
c9fe8f8e7e6f
8151571: InnocuousThread cannot be created during early startup
mchung
parents:
36435
diff
changeset
|
48 |
private static String newName() { |
c9fe8f8e7e6f
8151571: InnocuousThread cannot be created during early startup
mchung
parents:
36435
diff
changeset
|
49 |
return "InnocuousThread-" + threadNumber.getAndIncrement(); |
c9fe8f8e7e6f
8151571: InnocuousThread cannot be created during early startup
mchung
parents:
36435
diff
changeset
|
50 |
} |
29903 | 51 |
|
36680
c9fe8f8e7e6f
8151571: InnocuousThread cannot be created during early startup
mchung
parents:
36435
diff
changeset
|
52 |
/** |
c9fe8f8e7e6f
8151571: InnocuousThread cannot be created during early startup
mchung
parents:
36435
diff
changeset
|
53 |
* Returns a new InnocuousThread with an auto-generated thread name |
c9fe8f8e7e6f
8151571: InnocuousThread cannot be created during early startup
mchung
parents:
36435
diff
changeset
|
54 |
* and its context class loader is set to the system class loader. |
c9fe8f8e7e6f
8151571: InnocuousThread cannot be created during early startup
mchung
parents:
36435
diff
changeset
|
55 |
*/ |
c9fe8f8e7e6f
8151571: InnocuousThread cannot be created during early startup
mchung
parents:
36435
diff
changeset
|
56 |
public static Thread newThread(Runnable target) { |
c9fe8f8e7e6f
8151571: InnocuousThread cannot be created during early startup
mchung
parents:
36435
diff
changeset
|
57 |
return newThread(newName(), target); |
29903 | 58 |
} |
59 |
||
36680
c9fe8f8e7e6f
8151571: InnocuousThread cannot be created during early startup
mchung
parents:
36435
diff
changeset
|
60 |
/** |
c9fe8f8e7e6f
8151571: InnocuousThread cannot be created during early startup
mchung
parents:
36435
diff
changeset
|
61 |
* Returns a new InnocuousThread with its context class loader |
c9fe8f8e7e6f
8151571: InnocuousThread cannot be created during early startup
mchung
parents:
36435
diff
changeset
|
62 |
* set to the system class loader. |
c9fe8f8e7e6f
8151571: InnocuousThread cannot be created during early startup
mchung
parents:
36435
diff
changeset
|
63 |
*/ |
c9fe8f8e7e6f
8151571: InnocuousThread cannot be created during early startup
mchung
parents:
36435
diff
changeset
|
64 |
public static Thread newThread(String name, Runnable target) { |
c9fe8f8e7e6f
8151571: InnocuousThread cannot be created during early startup
mchung
parents:
36435
diff
changeset
|
65 |
return new InnocuousThread(INNOCUOUSTHREADGROUP, |
c9fe8f8e7e6f
8151571: InnocuousThread cannot be created during early startup
mchung
parents:
36435
diff
changeset
|
66 |
target, |
c9fe8f8e7e6f
8151571: InnocuousThread cannot be created during early startup
mchung
parents:
36435
diff
changeset
|
67 |
name, |
c9fe8f8e7e6f
8151571: InnocuousThread cannot be created during early startup
mchung
parents:
36435
diff
changeset
|
68 |
ClassLoader.getSystemClassLoader()); |
29903 | 69 |
} |
70 |
||
36680
c9fe8f8e7e6f
8151571: InnocuousThread cannot be created during early startup
mchung
parents:
36435
diff
changeset
|
71 |
/** |
c9fe8f8e7e6f
8151571: InnocuousThread cannot be created during early startup
mchung
parents:
36435
diff
changeset
|
72 |
* Returns a new InnocuousThread with an auto-generated thread name. |
c9fe8f8e7e6f
8151571: InnocuousThread cannot be created during early startup
mchung
parents:
36435
diff
changeset
|
73 |
* Its context class loader is set to null. |
c9fe8f8e7e6f
8151571: InnocuousThread cannot be created during early startup
mchung
parents:
36435
diff
changeset
|
74 |
*/ |
c9fe8f8e7e6f
8151571: InnocuousThread cannot be created during early startup
mchung
parents:
36435
diff
changeset
|
75 |
public static Thread newSystemThread(Runnable target) { |
c9fe8f8e7e6f
8151571: InnocuousThread cannot be created during early startup
mchung
parents:
36435
diff
changeset
|
76 |
return newSystemThread(newName(), target); |
c9fe8f8e7e6f
8151571: InnocuousThread cannot be created during early startup
mchung
parents:
36435
diff
changeset
|
77 |
} |
c9fe8f8e7e6f
8151571: InnocuousThread cannot be created during early startup
mchung
parents:
36435
diff
changeset
|
78 |
|
c9fe8f8e7e6f
8151571: InnocuousThread cannot be created during early startup
mchung
parents:
36435
diff
changeset
|
79 |
/** |
c9fe8f8e7e6f
8151571: InnocuousThread cannot be created during early startup
mchung
parents:
36435
diff
changeset
|
80 |
* Returns a new InnocuousThread with null context class loader. |
c9fe8f8e7e6f
8151571: InnocuousThread cannot be created during early startup
mchung
parents:
36435
diff
changeset
|
81 |
*/ |
c9fe8f8e7e6f
8151571: InnocuousThread cannot be created during early startup
mchung
parents:
36435
diff
changeset
|
82 |
public static Thread newSystemThread(String name, Runnable target) { |
c9fe8f8e7e6f
8151571: InnocuousThread cannot be created during early startup
mchung
parents:
36435
diff
changeset
|
83 |
return new InnocuousThread(INNOCUOUSTHREADGROUP, |
c9fe8f8e7e6f
8151571: InnocuousThread cannot be created during early startup
mchung
parents:
36435
diff
changeset
|
84 |
target, name, null); |
c9fe8f8e7e6f
8151571: InnocuousThread cannot be created during early startup
mchung
parents:
36435
diff
changeset
|
85 |
} |
c9fe8f8e7e6f
8151571: InnocuousThread cannot be created during early startup
mchung
parents:
36435
diff
changeset
|
86 |
|
c9fe8f8e7e6f
8151571: InnocuousThread cannot be created during early startup
mchung
parents:
36435
diff
changeset
|
87 |
private InnocuousThread(ThreadGroup group, Runnable target, String name, ClassLoader tccl) { |
34716
7477a052aecc
8056152: API to create Threads that do not inherit inheritable thread-local initial values
chegar
parents:
33674
diff
changeset
|
88 |
super(group, target, name, 0L, false); |
36936
bfcdf736a998
8152698: Remove obsolete Unsafe.putOrdered{X} methods, usages, runtime and compiler support
shade
parents:
36435
diff
changeset
|
89 |
UNSAFE.putObjectRelease(this, INHERITEDACCESSCONTROLCONTEXT, ACC); |
36939 | 90 |
UNSAFE.putObjectRelease(this, CONTEXTCLASSLOADER, tccl); |
23894 | 91 |
} |
92 |
||
93 |
@Override |
|
94 |
public void setUncaughtExceptionHandler(UncaughtExceptionHandler x) { |
|
95 |
// silently fail |
|
96 |
} |
|
97 |
||
98 |
@Override |
|
99 |
public void setContextClassLoader(ClassLoader cl) { |
|
29904
d5f909aa26bc
8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
29903
diff
changeset
|
100 |
// Allow clearing of the TCCL to remove the reference to the system classloader. |
29903 | 101 |
if (cl == null) |
29904
d5f909aa26bc
8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
29903
diff
changeset
|
102 |
super.setContextClassLoader(null); |
29903 | 103 |
else |
104 |
throw new SecurityException("setContextClassLoader"); |
|
23894 | 105 |
} |
106 |
||
34716
7477a052aecc
8056152: API to create Threads that do not inherit inheritable thread-local initial values
chegar
parents:
33674
diff
changeset
|
107 |
/** |
7477a052aecc
8056152: API to create Threads that do not inherit inheritable thread-local initial values
chegar
parents:
33674
diff
changeset
|
108 |
* 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
|
109 |
*/ |
7477a052aecc
8056152: API to create Threads that do not inherit inheritable thread-local initial values
chegar
parents:
33674
diff
changeset
|
110 |
public final void eraseThreadLocals() { |
7477a052aecc
8056152: API to create Threads that do not inherit inheritable thread-local initial values
chegar
parents:
33674
diff
changeset
|
111 |
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
|
112 |
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
|
113 |
} |
7477a052aecc
8056152: API to create Threads that do not inherit inheritable thread-local initial values
chegar
parents:
33674
diff
changeset
|
114 |
|
23894 | 115 |
// ensure run method is run only once |
116 |
private volatile boolean hasRun; |
|
117 |
||
118 |
@Override |
|
119 |
public void run() { |
|
120 |
if (Thread.currentThread() == this && !hasRun) { |
|
121 |
hasRun = true; |
|
122 |
super.run(); |
|
123 |
} |
|
124 |
} |
|
125 |
||
126 |
// Use Unsafe to access Thread group and ThreadGroup parent fields |
|
127 |
static { |
|
128 |
try { |
|
129 |
ACC = new AccessControlContext(new ProtectionDomain[] { |
|
130 |
new ProtectionDomain(null, null) |
|
131 |
}); |
|
132 |
||
133 |
// 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
|
134 |
UNSAFE = jdk.internal.misc.Unsafe.getUnsafe(); |
23894 | 135 |
Class<?> tk = Thread.class; |
136 |
Class<?> gk = ThreadGroup.class; |
|
137 |
||
34716
7477a052aecc
8056152: API to create Threads that do not inherit inheritable thread-local initial values
chegar
parents:
33674
diff
changeset
|
138 |
THREAD_LOCALS = UNSAFE.objectFieldOffset |
7477a052aecc
8056152: API to create Threads that do not inherit inheritable thread-local initial values
chegar
parents:
33674
diff
changeset
|
139 |
(tk.getDeclaredField("threadLocals")); |
7477a052aecc
8056152: API to create Threads that do not inherit inheritable thread-local initial values
chegar
parents:
33674
diff
changeset
|
140 |
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
|
141 |
(tk.getDeclaredField("inheritableThreadLocals")); |
23894 | 142 |
INHERITEDACCESSCONTROLCONTEXT = UNSAFE.objectFieldOffset |
143 |
(tk.getDeclaredField("inheritedAccessControlContext")); |
|
29904
d5f909aa26bc
8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
29903
diff
changeset
|
144 |
CONTEXTCLASSLOADER = UNSAFE.objectFieldOffset |
d5f909aa26bc
8048210: More Enhancements to InnocuousThread and friends
chegar
parents:
29903
diff
changeset
|
145 |
(tk.getDeclaredField("contextClassLoader")); |
23894 | 146 |
|
147 |
long tg = UNSAFE.objectFieldOffset(tk.getDeclaredField("group")); |
|
148 |
long gp = UNSAFE.objectFieldOffset(gk.getDeclaredField("parent")); |
|
149 |
ThreadGroup group = (ThreadGroup) |
|
150 |
UNSAFE.getObject(Thread.currentThread(), tg); |
|
151 |
||
152 |
while (group != null) { |
|
153 |
ThreadGroup parent = (ThreadGroup)UNSAFE.getObject(group, gp); |
|
154 |
if (parent == null) |
|
155 |
break; |
|
156 |
group = parent; |
|
157 |
} |
|
29903 | 158 |
final ThreadGroup root = group; |
159 |
INNOCUOUSTHREADGROUP = AccessController.doPrivileged( |
|
36435
0408881ad616
8149925: We don't need jdk.internal.ref.Cleaner any more - part1
plevart
parents:
35786
diff
changeset
|
160 |
new PrivilegedAction<ThreadGroup>() { |
0408881ad616
8149925: We don't need jdk.internal.ref.Cleaner any more - part1
plevart
parents:
35786
diff
changeset
|
161 |
@Override |
0408881ad616
8149925: We don't need jdk.internal.ref.Cleaner any more - part1
plevart
parents:
35786
diff
changeset
|
162 |
public ThreadGroup run() { |
0408881ad616
8149925: We don't need jdk.internal.ref.Cleaner any more - part1
plevart
parents:
35786
diff
changeset
|
163 |
return new ThreadGroup(root, "InnocuousThreadGroup"); |
0408881ad616
8149925: We don't need jdk.internal.ref.Cleaner any more - part1
plevart
parents:
35786
diff
changeset
|
164 |
} |
0408881ad616
8149925: We don't need jdk.internal.ref.Cleaner any more - part1
plevart
parents:
35786
diff
changeset
|
165 |
}); |
23894 | 166 |
} catch (Exception e) { |
167 |
throw new Error(e); |
|
168 |
} |
|
169 |
} |
|
170 |
} |