Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.rmi/share/classes/sun/rmi/transport/ObjectTable.java
41155 views
1
/*
2
* Copyright (c) 1996, 2021, 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
package sun.rmi.transport;
26
27
import java.lang.ref.ReferenceQueue;
28
import java.rmi.NoSuchObjectException;
29
import java.rmi.Remote;
30
import java.rmi.dgc.VMID;
31
import java.rmi.server.ExportException;
32
import java.rmi.server.ObjID;
33
import java.security.AccessController;
34
import java.security.PrivilegedAction;
35
import java.util.HashMap;
36
import java.util.Map;
37
import sun.rmi.runtime.Log;
38
import sun.rmi.runtime.NewThreadAction;
39
40
/**
41
* Object table shared by all implementors of the Transport interface.
42
* This table maps object ids to remote object targets in this address
43
* space.
44
*
45
* @author Ann Wollrath
46
* @author Peter Jones
47
*/
48
public final class ObjectTable {
49
50
/** maximum interval between complete garbage collections of local heap */
51
@SuppressWarnings("removal")
52
private final static long gcInterval = // default 1 hour
53
AccessController.doPrivileged((PrivilegedAction<Long>) () ->
54
Long.getLong("sun.rmi.dgc.server.gcInterval", 3600000));
55
56
/**
57
* lock guarding objTable and implTable.
58
* Holders MAY acquire a Target instance's lock or keepAliveLock.
59
*/
60
private static final Object tableLock = new Object();
61
62
/** tables mapping to Target, keyed from ObjectEndpoint and impl object */
63
private static final Map<ObjectEndpoint,Target> objTable =
64
new HashMap<>();
65
private static final Map<WeakRef,Target> implTable =
66
new HashMap<>();
67
68
/**
69
* lock guarding keepAliveCount, reaper, and gcLatencyRequest.
70
* Holders may NOT acquire a Target instance's lock or tableLock.
71
*/
72
private static final Object keepAliveLock = new Object();
73
74
/** count of non-permanent objects in table or still processing calls */
75
private static int keepAliveCount = 0;
76
77
/** thread to collect unreferenced objects from table */
78
private static Thread reaper = null;
79
80
/** queue notified when weak refs in the table are cleared */
81
static final ReferenceQueue<Object> reapQueue = new ReferenceQueue<>();
82
83
/** handle for GC latency request (for future cancellation) */
84
private static GC.LatencyRequest gcLatencyRequest = null;
85
86
/*
87
* Disallow anyone from creating one of these.
88
*/
89
private ObjectTable() {}
90
91
/**
92
* Returns the target associated with the object id.
93
*/
94
static Target getTarget(ObjectEndpoint oe) {
95
synchronized (tableLock) {
96
return objTable.get(oe);
97
}
98
}
99
100
/**
101
* Returns the target associated with the remote object
102
*/
103
public static Target getTarget(Remote impl) {
104
synchronized (tableLock) {
105
return implTable.get(new WeakRef(impl));
106
}
107
}
108
109
/**
110
* Returns the stub for the remote object <b>obj</b> passed
111
* as a parameter. This operation is only valid <i>after</i>
112
* the object has been exported.
113
*
114
* @return the stub for the remote object, <b>obj</b>.
115
* @exception NoSuchObjectException if the stub for the
116
* remote object could not be found.
117
*/
118
public static Remote getStub(Remote impl)
119
throws NoSuchObjectException
120
{
121
Target target = getTarget(impl);
122
if (target == null) {
123
throw new NoSuchObjectException("object not exported");
124
} else {
125
return target.getStub();
126
}
127
}
128
129
/**
130
* Remove the remote object, obj, from the RMI runtime. If
131
* successful, the object can no longer accept incoming RMI calls.
132
* If the force parameter is true, the object is forcibly unexported
133
* even if there are pending calls to the remote object or the
134
* remote object still has calls in progress. If the force
135
* parameter is false, the object is only unexported if there are
136
* no pending or in progress calls to the object.
137
*
138
* @param obj the remote object to be unexported
139
* @param force if true, unexports the object even if there are
140
* pending or in-progress calls; if false, only unexports the object
141
* if there are no pending or in-progress calls
142
* @return true if operation is successful, false otherwise
143
* @exception NoSuchObjectException if the remote object is not
144
* currently exported
145
*/
146
public static boolean unexportObject(Remote obj, boolean force)
147
throws java.rmi.NoSuchObjectException
148
{
149
synchronized (tableLock) {
150
Target target = getTarget(obj);
151
if (target == null) {
152
throw new NoSuchObjectException("object not exported");
153
} else {
154
if (target.unexport(force)) {
155
removeTarget(target);
156
return true;
157
} else {
158
return false;
159
}
160
}
161
}
162
}
163
164
/**
165
* Add target to object table. If it is not a permanent entry, then
166
* make sure that reaper thread is running to remove collected entries
167
* and keep VM alive.
168
*/
169
static void putTarget(Target target) throws ExportException {
170
ObjectEndpoint oe = target.getObjectEndpoint();
171
WeakRef weakImpl = target.getWeakImpl();
172
173
if (DGCImpl.dgcLog.isLoggable(Log.VERBOSE)) {
174
DGCImpl.dgcLog.log(Log.VERBOSE, "add object " + oe);
175
}
176
177
synchronized (tableLock) {
178
/**
179
* Do nothing if impl has already been collected (see 6597112). Check while
180
* holding tableLock to ensure that Reaper cannot process weakImpl in between
181
* null check and put/increment effects.
182
*/
183
if (target.getImpl() != null) {
184
if (objTable.containsKey(oe)) {
185
throw new ExportException(
186
"internal error: ObjID already in use");
187
} else if (implTable.containsKey(weakImpl)) {
188
throw new ExportException("object already exported");
189
}
190
191
objTable.put(oe, target);
192
implTable.put(weakImpl, target);
193
194
if (!target.isPermanent()) {
195
incrementKeepAliveCount();
196
}
197
}
198
}
199
}
200
201
/**
202
* Remove target from object table.
203
*
204
* NOTE: This method must only be invoked while synchronized on
205
* the "tableLock" object, because it does not do so itself.
206
*/
207
private static void removeTarget(Target target) {
208
// assert Thread.holdsLock(tableLock);
209
210
ObjectEndpoint oe = target.getObjectEndpoint();
211
WeakRef weakImpl = target.getWeakImpl();
212
213
if (DGCImpl.dgcLog.isLoggable(Log.VERBOSE)) {
214
DGCImpl.dgcLog.log(Log.VERBOSE, "remove object " + oe);
215
}
216
217
objTable.remove(oe);
218
implTable.remove(weakImpl);
219
220
target.markRemoved(); // handles decrementing keep-alive count
221
}
222
223
/**
224
* Process client VM signalling reference for given ObjID: forward to
225
* corresponding Target entry. If ObjID is not found in table,
226
* no action is taken.
227
*/
228
static void referenced(ObjID id, long sequenceNum, VMID vmid) {
229
synchronized (tableLock) {
230
ObjectEndpoint oe =
231
new ObjectEndpoint(id, Transport.currentTransport());
232
Target target = objTable.get(oe);
233
if (target != null) {
234
target.referenced(sequenceNum, vmid);
235
}
236
}
237
}
238
239
/**
240
* Process client VM dropping reference for given ObjID: forward to
241
* corresponding Target entry. If ObjID is not found in table,
242
* no action is taken.
243
*/
244
static void unreferenced(ObjID id, long sequenceNum, VMID vmid,
245
boolean strong)
246
{
247
synchronized (tableLock) {
248
ObjectEndpoint oe =
249
new ObjectEndpoint(id, Transport.currentTransport());
250
Target target = objTable.get(oe);
251
if (target != null)
252
target.unreferenced(sequenceNum, vmid, strong);
253
}
254
}
255
256
/**
257
* Increments the "keep-alive count".
258
*
259
* The "keep-alive count" is the number of non-permanent remote objects
260
* that are either in the object table or still have calls in progress.
261
* Therefore, this method should be invoked exactly once for every
262
* non-permanent remote object exported (a remote object must be
263
* exported before it can have any calls in progress).
264
*
265
* The VM is "kept alive" while the keep-alive count is greater than
266
* zero; this is accomplished by keeping a non-daemon thread running.
267
*
268
* Because non-permanent objects are those that can be garbage
269
* collected while exported, and thus those for which the "reaper"
270
* thread operates, the reaper thread also serves as the non-daemon
271
* VM keep-alive thread; a new reaper thread is created if necessary.
272
*/
273
@SuppressWarnings("removal")
274
static void incrementKeepAliveCount() {
275
synchronized (keepAliveLock) {
276
keepAliveCount++;
277
278
if (reaper == null) {
279
reaper = AccessController.doPrivileged(
280
new NewThreadAction(new Reaper(), "Reaper", false));
281
reaper.start();
282
}
283
284
/*
285
* While there are non-"permanent" objects in the object table,
286
* request a maximum latency for inspecting the entire heap
287
* from the local garbage collector, to place an upper bound
288
* on the time to discover remote objects that have become
289
* unreachable (and thus can be removed from the table).
290
*/
291
if (gcLatencyRequest == null) {
292
gcLatencyRequest = GC.requestLatency(gcInterval);
293
}
294
}
295
}
296
297
/**
298
* Decrements the "keep-alive count".
299
*
300
* The "keep-alive count" is the number of non-permanent remote objects
301
* that are either in the object table or still have calls in progress.
302
* Therefore, this method should be invoked exactly once for every
303
* previously-exported non-permanent remote object that both has been
304
* removed from the object table and has no calls still in progress.
305
*
306
* If the keep-alive count is decremented to zero, then the current
307
* reaper thread is terminated to cease keeping the VM alive (and
308
* because there are no more non-permanent remote objects to reap).
309
*/
310
@SuppressWarnings("removal")
311
static void decrementKeepAliveCount() {
312
synchronized (keepAliveLock) {
313
keepAliveCount--;
314
315
if (keepAliveCount == 0) {
316
if (!(reaper != null)) { throw new AssertionError(); }
317
AccessController.doPrivileged(new PrivilegedAction<Void>() {
318
public Void run() {
319
reaper.interrupt();
320
return null;
321
}
322
});
323
reaper = null;
324
325
/*
326
* If there are no longer any non-permanent objects in the
327
* object table, we are no longer concerned with the latency
328
* of local garbage collection here.
329
*/
330
gcLatencyRequest.cancel();
331
gcLatencyRequest = null;
332
}
333
}
334
}
335
336
/**
337
* The Reaper thread waits for notifications that weak references in the
338
* object table have been cleared. When it receives a notification, it
339
* removes the corresponding entry from the table.
340
*
341
* Since the Reaper is created as a non-daemon thread, it also serves
342
* to keep the VM from exiting while there are objects in the table
343
* (other than permanent entries that should neither be reaped nor
344
* keep the VM alive).
345
*/
346
private static class Reaper implements Runnable {
347
348
public void run() {
349
try {
350
do {
351
// wait for next cleared weak reference
352
WeakRef weakImpl = (WeakRef) reapQueue.remove();
353
354
synchronized (tableLock) {
355
Target target = implTable.get(weakImpl);
356
if (target != null) {
357
if (!target.isEmpty()) {
358
throw new Error(
359
"object with known references collected");
360
} else if (target.isPermanent()) {
361
throw new Error("permanent object collected");
362
}
363
removeTarget(target);
364
}
365
}
366
} while (!Thread.interrupted());
367
} catch (InterruptedException e) {
368
// pass away if interrupted
369
}
370
}
371
}
372
}
373
374