Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/jdk/internal/ref/CleanerImpl.java
41159 views
1
/*
2
* Copyright (c) 2015, 2020, 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 jdk.internal.ref;
27
28
import java.lang.ref.Cleaner;
29
import java.lang.ref.Cleaner.Cleanable;
30
import java.lang.ref.ReferenceQueue;
31
import java.security.AccessController;
32
import java.security.PrivilegedAction;
33
import java.util.concurrent.ThreadFactory;
34
import java.util.concurrent.atomic.AtomicInteger;
35
import java.util.function.Function;
36
37
import jdk.internal.misc.InnocuousThread;
38
39
/**
40
* CleanerImpl manages a set of object references and corresponding cleaning actions.
41
* CleanerImpl provides the functionality of {@link java.lang.ref.Cleaner}.
42
*/
43
public final class CleanerImpl implements Runnable {
44
45
/**
46
* An object to access the CleanerImpl from a Cleaner; set by Cleaner init.
47
*/
48
private static Function<Cleaner, CleanerImpl> cleanerImplAccess = null;
49
50
/**
51
* Heads of a CleanableList for each reference type.
52
*/
53
final PhantomCleanable<?> phantomCleanableList;
54
55
// The ReferenceQueue of pending cleaning actions
56
final ReferenceQueue<Object> queue;
57
58
/**
59
* Called by Cleaner static initialization to provide the function
60
* to map from Cleaner to CleanerImpl.
61
* @param access a function to map from Cleaner to CleanerImpl
62
*/
63
public static void setCleanerImplAccess(Function<Cleaner, CleanerImpl> access) {
64
if (cleanerImplAccess == null) {
65
cleanerImplAccess = access;
66
} else {
67
throw new InternalError("cleanerImplAccess");
68
}
69
}
70
71
/**
72
* Called to get the CleanerImpl for a Cleaner.
73
* @param cleaner the cleaner
74
* @return the corresponding CleanerImpl
75
*/
76
static CleanerImpl getCleanerImpl(Cleaner cleaner) {
77
return cleanerImplAccess.apply(cleaner);
78
}
79
80
/**
81
* Constructor for CleanerImpl.
82
*/
83
public CleanerImpl() {
84
queue = new ReferenceQueue<>();
85
phantomCleanableList = new PhantomCleanableRef();
86
}
87
88
/**
89
* Starts the Cleaner implementation.
90
* Ensure this is the CleanerImpl for the Cleaner.
91
* When started waits for Cleanables to be queued.
92
* @param cleaner the cleaner
93
* @param threadFactory the thread factory
94
*/
95
public void start(Cleaner cleaner, ThreadFactory threadFactory) {
96
if (getCleanerImpl(cleaner) != this) {
97
throw new AssertionError("wrong cleaner");
98
}
99
// schedule a nop cleaning action for the cleaner, so the associated thread
100
// will continue to run at least until the cleaner is reclaimable.
101
new CleanerCleanable(cleaner);
102
103
if (threadFactory == null) {
104
threadFactory = CleanerImpl.InnocuousThreadFactory.factory();
105
}
106
107
// now that there's at least one cleaning action, for the cleaner,
108
// we can start the associated thread, which runs until
109
// all cleaning actions have been run.
110
Thread thread = threadFactory.newThread(this);
111
thread.setDaemon(true);
112
thread.start();
113
}
114
115
/**
116
* Process queued Cleanables as long as the cleanable lists are not empty.
117
* A Cleanable is in one of the lists for each Object and for the Cleaner
118
* itself.
119
* Terminates when the Cleaner is no longer reachable and
120
* has been cleaned and there are no more Cleanable instances
121
* for which the object is reachable.
122
* <p>
123
* If the thread is a ManagedLocalsThread, the threadlocals
124
* are erased before each cleanup
125
*/
126
@Override
127
public void run() {
128
Thread t = Thread.currentThread();
129
InnocuousThread mlThread = (t instanceof InnocuousThread)
130
? (InnocuousThread) t
131
: null;
132
while (!phantomCleanableList.isListEmpty()) {
133
if (mlThread != null) {
134
// Clear the thread locals
135
mlThread.eraseThreadLocals();
136
}
137
try {
138
// Wait for a Ref, with a timeout to avoid getting hung
139
// due to a race with clear/clean
140
Cleanable ref = (Cleanable) queue.remove(60 * 1000L);
141
if (ref != null) {
142
ref.clean();
143
}
144
} catch (Throwable e) {
145
// ignore exceptions from the cleanup action
146
// (including interruption of cleanup thread)
147
}
148
}
149
}
150
151
/**
152
* Perform cleaning on an unreachable PhantomReference.
153
*/
154
public static final class PhantomCleanableRef extends PhantomCleanable<Object> {
155
private final Runnable action;
156
157
/**
158
* Constructor for a phantom cleanable reference.
159
* @param obj the object to monitor
160
* @param cleaner the cleaner
161
* @param action the action Runnable
162
*/
163
public PhantomCleanableRef(Object obj, Cleaner cleaner, Runnable action) {
164
super(obj, cleaner);
165
this.action = action;
166
}
167
168
/**
169
* Constructor used only for root of phantom cleanable list.
170
*/
171
PhantomCleanableRef() {
172
super();
173
this.action = null;
174
}
175
176
@Override
177
protected void performCleanup() {
178
action.run();
179
}
180
181
/**
182
* Prevent access to referent even when it is still alive.
183
*
184
* @throws UnsupportedOperationException always
185
*/
186
@Override
187
public Object get() {
188
throw new UnsupportedOperationException("get");
189
}
190
191
/**
192
* Direct clearing of the referent is not supported.
193
*
194
* @throws UnsupportedOperationException always
195
*/
196
@Override
197
public void clear() {
198
throw new UnsupportedOperationException("clear");
199
}
200
}
201
202
/**
203
* A ThreadFactory for InnocuousThreads.
204
* The factory is a singleton.
205
*/
206
static final class InnocuousThreadFactory implements ThreadFactory {
207
static final ThreadFactory factory = new InnocuousThreadFactory();
208
209
static ThreadFactory factory() {
210
return factory;
211
}
212
213
final AtomicInteger cleanerThreadNumber = new AtomicInteger();
214
215
public Thread newThread(Runnable r) {
216
return InnocuousThread.newThread("Cleaner-" + cleanerThreadNumber.getAndIncrement(),
217
r, Thread.MIN_PRIORITY - 2);
218
}
219
}
220
221
/**
222
* A PhantomCleanable implementation for tracking the Cleaner itself.
223
*/
224
static final class CleanerCleanable extends PhantomCleanable<Cleaner> {
225
CleanerCleanable(Cleaner cleaner) {
226
super(cleaner, cleaner);
227
}
228
229
@Override
230
protected void performCleanup() {
231
// no action
232
}
233
}
234
}
235
236