Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/rmi/transport/ObjectTable.java
38831 views
/*1* Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package sun.rmi.transport;2526import java.lang.ref.ReferenceQueue;27import java.rmi.NoSuchObjectException;28import java.rmi.Remote;29import java.rmi.dgc.VMID;30import java.rmi.server.ExportException;31import java.rmi.server.ObjID;32import java.security.AccessController;33import java.security.PrivilegedAction;34import java.util.HashMap;35import java.util.Map;36import sun.misc.GC;37import sun.rmi.runtime.Log;38import sun.rmi.runtime.NewThreadAction;39import sun.security.action.GetLongAction;4041/**42* Object table shared by all implementors of the Transport interface.43* This table maps object ids to remote object targets in this address44* space.45*46* @author Ann Wollrath47* @author Peter Jones48*/49public final class ObjectTable {5051/** maximum interval between complete garbage collections of local heap */52private final static long gcInterval = // default 1 hour53AccessController.doPrivileged(54new GetLongAction("sun.rmi.dgc.server.gcInterval", 3600000));5556/**57* lock guarding objTable and implTable.58* Holders MAY acquire a Target instance's lock or keepAliveLock.59*/60private static final Object tableLock = new Object();6162/** tables mapping to Target, keyed from ObjectEndpoint and impl object */63private static final Map<ObjectEndpoint,Target> objTable =64new HashMap<>();65private static final Map<WeakRef,Target> implTable =66new HashMap<>();6768/**69* lock guarding keepAliveCount, reaper, and gcLatencyRequest.70* Holders may NOT acquire a Target instance's lock or tableLock.71*/72private static final Object keepAliveLock = new Object();7374/** count of non-permanent objects in table or still processing calls */75private static int keepAliveCount = 0;7677/** thread to collect unreferenced objects from table */78private static Thread reaper = null;7980/** queue notified when weak refs in the table are cleared */81static final ReferenceQueue<Object> reapQueue = new ReferenceQueue<>();8283/** handle for GC latency request (for future cancellation) */84private static GC.LatencyRequest gcLatencyRequest = null;8586/*87* Disallow anyone from creating one of these.88*/89private ObjectTable() {}9091/**92* Returns the target associated with the object id.93*/94static Target getTarget(ObjectEndpoint oe) {95synchronized (tableLock) {96return objTable.get(oe);97}98}99100/**101* Returns the target associated with the remote object102*/103public static Target getTarget(Remote impl) {104synchronized (tableLock) {105return implTable.get(new WeakRef(impl));106}107}108109/**110* Returns the stub for the remote object <b>obj</b> passed111* 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 the116* remote object could not be found.117*/118public static Remote getStub(Remote impl)119throws NoSuchObjectException120{121Target target = getTarget(impl);122if (target == null) {123throw new NoSuchObjectException("object not exported");124} else {125return target.getStub();126}127}128129/**130* Remove the remote object, obj, from the RMI runtime. If131* successful, the object can no longer accept incoming RMI calls.132* If the force parameter is true, the object is forcibly unexported133* even if there are pending calls to the remote object or the134* remote object still has calls in progress. If the force135* parameter is false, the object is only unexported if there are136* no pending or in progress calls to the object.137*138* @param obj the remote object to be unexported139* @param force if true, unexports the object even if there are140* pending or in-progress calls; if false, only unexports the object141* if there are no pending or in-progress calls142* @return true if operation is successful, false otherwise143* @exception NoSuchObjectException if the remote object is not144* currently exported145*/146public static boolean unexportObject(Remote obj, boolean force)147throws java.rmi.NoSuchObjectException148{149synchronized (tableLock) {150Target target = getTarget(obj);151if (target == null) {152throw new NoSuchObjectException("object not exported");153} else {154if (target.unexport(force)) {155removeTarget(target);156return true;157} else {158return false;159}160}161}162}163164/**165* Add target to object table. If it is not a permanent entry, then166* make sure that reaper thread is running to remove collected entries167* and keep VM alive.168*/169static void putTarget(Target target) throws ExportException {170ObjectEndpoint oe = target.getObjectEndpoint();171WeakRef weakImpl = target.getWeakImpl();172173if (DGCImpl.dgcLog.isLoggable(Log.VERBOSE)) {174DGCImpl.dgcLog.log(Log.VERBOSE, "add object " + oe);175}176177synchronized (tableLock) {178/**179* Do nothing if impl has already been collected (see 6597112). Check while180* holding tableLock to ensure that Reaper cannot process weakImpl in between181* null check and put/increment effects.182*/183if (target.getImpl() != null) {184if (objTable.containsKey(oe)) {185throw new ExportException(186"internal error: ObjID already in use");187} else if (implTable.containsKey(weakImpl)) {188throw new ExportException("object already exported");189}190191objTable.put(oe, target);192implTable.put(weakImpl, target);193194if (!target.isPermanent()) {195incrementKeepAliveCount();196}197}198}199}200201/**202* Remove target from object table.203*204* NOTE: This method must only be invoked while synchronized on205* the "tableLock" object, because it does not do so itself.206*/207private static void removeTarget(Target target) {208// assert Thread.holdsLock(tableLock);209210ObjectEndpoint oe = target.getObjectEndpoint();211WeakRef weakImpl = target.getWeakImpl();212213if (DGCImpl.dgcLog.isLoggable(Log.VERBOSE)) {214DGCImpl.dgcLog.log(Log.VERBOSE, "remove object " + oe);215}216217objTable.remove(oe);218implTable.remove(weakImpl);219220target.markRemoved(); // handles decrementing keep-alive count221}222223/**224* Process client VM signalling reference for given ObjID: forward to225* corresponding Target entry. If ObjID is not found in table,226* no action is taken.227*/228static void referenced(ObjID id, long sequenceNum, VMID vmid) {229synchronized (tableLock) {230ObjectEndpoint oe =231new ObjectEndpoint(id, Transport.currentTransport());232Target target = objTable.get(oe);233if (target != null) {234target.referenced(sequenceNum, vmid);235}236}237}238239/**240* Process client VM dropping reference for given ObjID: forward to241* corresponding Target entry. If ObjID is not found in table,242* no action is taken.243*/244static void unreferenced(ObjID id, long sequenceNum, VMID vmid,245boolean strong)246{247synchronized (tableLock) {248ObjectEndpoint oe =249new ObjectEndpoint(id, Transport.currentTransport());250Target target = objTable.get(oe);251if (target != null)252target.unreferenced(sequenceNum, vmid, strong);253}254}255256/**257* Increments the "keep-alive count".258*259* The "keep-alive count" is the number of non-permanent remote objects260* that are either in the object table or still have calls in progress.261* Therefore, this method should be invoked exactly once for every262* non-permanent remote object exported (a remote object must be263* exported before it can have any calls in progress).264*265* The VM is "kept alive" while the keep-alive count is greater than266* zero; this is accomplished by keeping a non-daemon thread running.267*268* Because non-permanent objects are those that can be garbage269* collected while exported, and thus those for which the "reaper"270* thread operates, the reaper thread also serves as the non-daemon271* VM keep-alive thread; a new reaper thread is created if necessary.272*/273static void incrementKeepAliveCount() {274synchronized (keepAliveLock) {275keepAliveCount++;276277if (reaper == null) {278reaper = AccessController.doPrivileged(279new NewThreadAction(new Reaper(), "Reaper", false));280reaper.start();281}282283/*284* While there are non-"permanent" objects in the object table,285* request a maximum latency for inspecting the entire heap286* from the local garbage collector, to place an upper bound287* on the time to discover remote objects that have become288* unreachable (and thus can be removed from the table).289*/290if (gcLatencyRequest == null) {291gcLatencyRequest = GC.requestLatency(gcInterval);292}293}294}295296/**297* Decrements the "keep-alive count".298*299* The "keep-alive count" is the number of non-permanent remote objects300* that are either in the object table or still have calls in progress.301* Therefore, this method should be invoked exactly once for every302* previously-exported non-permanent remote object that both has been303* removed from the object table and has no calls still in progress.304*305* If the keep-alive count is decremented to zero, then the current306* reaper thread is terminated to cease keeping the VM alive (and307* because there are no more non-permanent remote objects to reap).308*/309static void decrementKeepAliveCount() {310synchronized (keepAliveLock) {311keepAliveCount--;312313if (keepAliveCount == 0) {314if (!(reaper != null)) { throw new AssertionError(); }315AccessController.doPrivileged(new PrivilegedAction<Void>() {316public Void run() {317reaper.interrupt();318return null;319}320});321reaper = null;322323/*324* If there are no longer any non-permanent objects in the325* object table, we are no longer concerned with the latency326* of local garbage collection here.327*/328gcLatencyRequest.cancel();329gcLatencyRequest = null;330}331}332}333334/**335* The Reaper thread waits for notifications that weak references in the336* object table have been cleared. When it receives a notification, it337* removes the corresponding entry from the table.338*339* Since the Reaper is created as a non-daemon thread, it also serves340* to keep the VM from exiting while there are objects in the table341* (other than permanent entries that should neither be reaped nor342* keep the VM alive).343*/344private static class Reaper implements Runnable {345346public void run() {347try {348do {349// wait for next cleared weak reference350WeakRef weakImpl = (WeakRef) reapQueue.remove();351352synchronized (tableLock) {353Target target = implTable.get(weakImpl);354if (target != null) {355if (!target.isEmpty()) {356throw new Error(357"object with known references collected");358} else if (target.isPermanent()) {359throw new Error("permanent object collected");360}361removeTarget(target);362}363}364} while (!Thread.interrupted());365} catch (InterruptedException e) {366// pass away if interrupted367}368}369}370}371372373