Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/misc/InnocuousThread.java
38829 views
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
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
import java.security.AccessControlContext;
29
import java.security.AccessController;
30
import java.security.ProtectionDomain;
31
import java.security.PrivilegedAction;
32
import java.util.concurrent.atomic.AtomicInteger;
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
*
38
* @implNote Based on the implementation of InnocuousForkJoinWorkerThread.
39
*/
40
public final class InnocuousThread extends Thread {
41
private static final Unsafe UNSAFE;
42
private static final long THREAD_LOCALS;
43
private static final long INHERITABLE_THREAD_LOCALS;
44
private static final ThreadGroup INNOCUOUSTHREADGROUP;
45
private static final AccessControlContext ACC;
46
private static final long INHERITEDACCESSCONTROLCONTEXT;
47
private static final long CONTEXTCLASSLOADER;
48
49
private static final AtomicInteger threadNumber = new AtomicInteger(1);
50
private static String newName() {
51
return "InnocuousThread-" + threadNumber.getAndIncrement();
52
}
53
54
/**
55
* Returns a new InnocuousThread with an auto-generated thread name.
56
* Its context class loader is set to null.
57
*/
58
public static Thread newSystemThread(Runnable target) {
59
return newSystemThread(newName(), target);
60
}
61
62
/**
63
* Returns a new InnocuousThread with null context class loader.
64
*/
65
public static Thread newSystemThread(String name, Runnable target) {
66
return new InnocuousThread(INNOCUOUSTHREADGROUP,
67
target, name, null);
68
}
69
70
public InnocuousThread(Runnable target) {
71
super(INNOCUOUSTHREADGROUP, target, newName());
72
UNSAFE.putOrderedObject(this, INHERITEDACCESSCONTROLCONTEXT, ACC);
73
eraseThreadLocals();
74
}
75
76
private InnocuousThread(ThreadGroup group, Runnable target, String name, ClassLoader tccl) {
77
super(group, target, name, 0L);
78
UNSAFE.putOrderedObject(this, INHERITEDACCESSCONTROLCONTEXT, ACC);
79
UNSAFE.putOrderedObject(this, CONTEXTCLASSLOADER, tccl);
80
eraseThreadLocals();
81
}
82
83
@Override
84
public ClassLoader getContextClassLoader() {
85
// always report system class loader
86
return ClassLoader.getSystemClassLoader();
87
}
88
89
@Override
90
public void setUncaughtExceptionHandler(UncaughtExceptionHandler x) {
91
// silently fail
92
}
93
94
@Override
95
public void setContextClassLoader(ClassLoader cl) {
96
throw new SecurityException("setContextClassLoader");
97
}
98
99
// ensure run method is run only once
100
private volatile boolean hasRun;
101
102
@Override
103
public void run() {
104
if (Thread.currentThread() == this && !hasRun) {
105
hasRun = true;
106
super.run();
107
}
108
}
109
110
/**
111
* Drops all thread locals (and inherited thread locals).
112
*/
113
public void eraseThreadLocals() {
114
UNSAFE.putObject(this, THREAD_LOCALS, null);
115
UNSAFE.putObject(this, INHERITABLE_THREAD_LOCALS, null);
116
}
117
118
// Use Unsafe to access Thread group and ThreadGroup parent fields
119
static {
120
try {
121
ACC = new AccessControlContext(new ProtectionDomain[] {
122
new ProtectionDomain(null, null)
123
});
124
125
// Find and use topmost ThreadGroup as parent of new group
126
UNSAFE = Unsafe.getUnsafe();
127
Class<?> tk = Thread.class;
128
Class<?> gk = ThreadGroup.class;
129
130
THREAD_LOCALS = UNSAFE.objectFieldOffset
131
(tk.getDeclaredField("threadLocals"));
132
INHERITABLE_THREAD_LOCALS = UNSAFE.objectFieldOffset
133
(tk.getDeclaredField("inheritableThreadLocals"));
134
INHERITEDACCESSCONTROLCONTEXT = UNSAFE.objectFieldOffset
135
(tk.getDeclaredField("inheritedAccessControlContext"));
136
CONTEXTCLASSLOADER = UNSAFE.objectFieldOffset
137
(tk.getDeclaredField("contextClassLoader"));
138
139
long tg = UNSAFE.objectFieldOffset(tk.getDeclaredField("group"));
140
long gp = UNSAFE.objectFieldOffset(gk.getDeclaredField("parent"));
141
ThreadGroup group = (ThreadGroup)
142
UNSAFE.getObject(Thread.currentThread(), tg);
143
144
while (group != null) {
145
ThreadGroup parent = (ThreadGroup)UNSAFE.getObject(group, gp);
146
if (parent == null)
147
break;
148
group = parent;
149
}
150
final ThreadGroup root = group;
151
INNOCUOUSTHREADGROUP = AccessController.doPrivileged(
152
new PrivilegedAction<ThreadGroup>() {
153
@Override
154
public ThreadGroup run() {
155
return new ThreadGroup(root, "InnocuousThreadGroup");
156
}
157
});
158
} catch (Exception e) {
159
throw new Error(e);
160
}
161
}
162
}
163
164