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/java2d/Disposer.java
38829 views
1
/*
2
* Copyright (c) 2002, 2014, 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.java2d;
27
28
import sun.misc.ThreadGroupUtils;
29
30
import java.lang.ref.Reference;
31
import java.lang.ref.ReferenceQueue;
32
import java.lang.ref.PhantomReference;
33
import java.lang.ref.WeakReference;
34
import java.security.AccessController;
35
import java.security.PrivilegedAction;
36
import java.util.ArrayList;
37
import java.util.Hashtable;
38
39
/**
40
* This class is used for registering and disposing the native
41
* data associated with java objects.
42
*
43
* The object can register itself by calling one of the addRecord
44
* methods and providing either the pointer to the native disposal
45
* method or a descendant of the DisposerRecord class with overridden
46
* dispose() method.
47
*
48
* When the object becomes unreachable, the dispose() method
49
* of the associated DisposerRecord object will be called.
50
*
51
* @see DisposerRecord
52
*/
53
public class Disposer implements Runnable {
54
private static final ReferenceQueue queue = new ReferenceQueue();
55
private static final Hashtable records = new Hashtable();
56
57
private static Disposer disposerInstance;
58
public static final int WEAK = 0;
59
public static final int PHANTOM = 1;
60
public static int refType = PHANTOM;
61
62
static {
63
java.security.AccessController.doPrivileged(
64
new java.security.PrivilegedAction<Void>() {
65
public Void run() {
66
System.loadLibrary("awt");
67
return null;
68
}
69
});
70
initIDs();
71
String type = (String) java.security.AccessController.doPrivileged(
72
new sun.security.action.GetPropertyAction("sun.java2d.reftype"));
73
if (type != null) {
74
if (type.equals("weak")) {
75
refType = WEAK;
76
System.err.println("Using WEAK refs");
77
} else {
78
refType = PHANTOM;
79
System.err.println("Using PHANTOM refs");
80
}
81
}
82
disposerInstance = new Disposer();
83
AccessController.doPrivileged(
84
(PrivilegedAction<Void>) () -> {
85
/* The thread must be a member of a thread group
86
* which will not get GCed before VM exit.
87
* Make its parent the top-level thread group.
88
*/
89
ThreadGroup rootTG = ThreadGroupUtils.getRootThreadGroup();
90
Thread t = new Thread(rootTG, disposerInstance, "Java2D Disposer");
91
t.setContextClassLoader(null);
92
t.setDaemon(true);
93
t.setPriority(Thread.MAX_PRIORITY);
94
t.start();
95
return null;
96
}
97
);
98
}
99
100
/**
101
* Registers the object and the native data for later disposal.
102
* @param target Object to be registered
103
* @param disposeMethod pointer to the native disposal method
104
* @param pData pointer to the data to be passed to the
105
* native disposal method
106
*/
107
public static void addRecord(Object target,
108
long disposeMethod, long pData)
109
{
110
disposerInstance.add(target,
111
new DefaultDisposerRecord(disposeMethod, pData));
112
}
113
114
/**
115
* Registers the object and the native data for later disposal.
116
* @param target Object to be registered
117
* @param rec the associated DisposerRecord object
118
* @see DisposerRecord
119
*/
120
public static void addRecord(Object target, DisposerRecord rec) {
121
disposerInstance.add(target, rec);
122
}
123
124
/**
125
* Performs the actual registration of the target object to be disposed.
126
* @param target Object to be registered, or if target is an instance
127
* of DisposerTarget, its associated disposer referent
128
* will be the Object that is registered
129
* @param rec the associated DisposerRecord object
130
* @see DisposerRecord
131
*/
132
synchronized void add(Object target, DisposerRecord rec) {
133
if (target instanceof DisposerTarget) {
134
target = ((DisposerTarget)target).getDisposerReferent();
135
}
136
java.lang.ref.Reference ref;
137
if (refType == PHANTOM) {
138
ref = new PhantomReference(target, queue);
139
} else {
140
ref = new WeakReference(target, queue);
141
}
142
records.put(ref, rec);
143
}
144
145
public void run() {
146
while (true) {
147
try {
148
Object obj = queue.remove();
149
((Reference)obj).clear();
150
DisposerRecord rec = (DisposerRecord)records.remove(obj);
151
rec.dispose();
152
obj = null;
153
rec = null;
154
clearDeferredRecords();
155
} catch (Exception e) {
156
System.out.println("Exception while removing reference.");
157
}
158
}
159
}
160
161
/*
162
* This is a marker interface that, if implemented, means it
163
* doesn't acquire any special locks, and is safe to
164
* be disposed in the poll loop on whatever thread
165
* which happens to be the Toolkit thread, is in use.
166
*/
167
public static interface PollDisposable {
168
};
169
170
private static ArrayList<DisposerRecord> deferredRecords = null;
171
172
private static void clearDeferredRecords() {
173
if (deferredRecords == null || deferredRecords.isEmpty()) {
174
return;
175
}
176
for (int i=0;i<deferredRecords.size(); i++) {
177
try {
178
DisposerRecord rec = deferredRecords.get(i);
179
rec.dispose();
180
} catch (Exception e) {
181
System.out.println("Exception while disposing deferred rec.");
182
}
183
}
184
deferredRecords.clear();
185
}
186
187
/*
188
* Set to indicate the queue is presently being polled.
189
*/
190
public static volatile boolean pollingQueue = false;
191
192
/*
193
* The pollRemove() method is called back from a dispose method
194
* that is running on the toolkit thread and wants to
195
* dispose any pending refs that are safe to be disposed
196
* on that thread.
197
*/
198
public static void pollRemove() {
199
200
/* This should never be called recursively, so this check
201
* is just a safeguard against the unexpected.
202
*/
203
if (pollingQueue) {
204
return;
205
}
206
Object obj;
207
pollingQueue = true;
208
int freed = 0;
209
int deferred = 0;
210
try {
211
while ((obj = queue.poll()) != null
212
&& freed < 10000 && deferred < 100) {
213
freed++;
214
((Reference)obj).clear();
215
DisposerRecord rec = (DisposerRecord)records.remove(obj);
216
if (rec instanceof PollDisposable) {
217
rec.dispose();
218
obj = null;
219
rec = null;
220
} else {
221
if (rec == null) { // shouldn't happen, but just in case.
222
continue;
223
}
224
deferred++;
225
if (deferredRecords == null) {
226
deferredRecords = new ArrayList<DisposerRecord>(5);
227
}
228
deferredRecords.add(rec);
229
}
230
}
231
} catch (Exception e) {
232
System.out.println("Exception while removing reference.");
233
} finally {
234
pollingQueue = false;
235
}
236
}
237
238
private static native void initIDs();
239
240
/*
241
* This was added for use by the 2D font implementation to avoid creation
242
* of an additional disposer thread.
243
* WARNING: this thread class monitors a specific queue, so a reference
244
* added here must have been created with this queue. Failure to do
245
* so will clutter the records hashmap and no one will be cleaning up
246
* the reference queue.
247
*/
248
public static void addReference(Reference ref, DisposerRecord rec) {
249
records.put(ref, rec);
250
}
251
252
public static void addObjectRecord(Object obj, DisposerRecord rec) {
253
records.put(new WeakReference(obj, queue) , rec);
254
}
255
256
/* This is intended for use in conjunction with addReference(..)
257
*/
258
public static ReferenceQueue getQueue() {
259
return queue;
260
}
261
262
}
263
264