From: <ls...@us...> - 2009-03-01 08:55:35
|
Revision: 5077 http://jnode.svn.sourceforge.net/jnode/?rev=5077&view=rev Author: lsantha Date: 2009-03-01 08:55:23 +0000 (Sun, 01 Mar 2009) Log Message: ----------- OpenJDK integration. Added Paths: ----------- trunk/core/src/openjdk/java/java/lang/ref/ trunk/core/src/openjdk/java/java/lang/ref/FinalReference.java trunk/core/src/openjdk/java/java/lang/ref/Finalizer.java trunk/core/src/openjdk/java/java/lang/ref/PhantomReference.java trunk/core/src/openjdk/java/java/lang/ref/Reference.java trunk/core/src/openjdk/java/java/lang/ref/ReferenceQueue.java trunk/core/src/openjdk/java/java/lang/ref/SoftReference.java trunk/core/src/openjdk/java/java/lang/ref/WeakReference.java trunk/core/src/openjdk/java/java/lang/ref/package.html trunk/core/src/openjdk/vm/java/lang/ref/ trunk/core/src/openjdk/vm/java/lang/ref/NativeFinalizer.java Removed Paths: ------------- trunk/core/src/classpath/java/java/lang/ref/ Added: trunk/core/src/openjdk/java/java/lang/ref/FinalReference.java =================================================================== --- trunk/core/src/openjdk/java/java/lang/ref/FinalReference.java (rev 0) +++ trunk/core/src/openjdk/java/java/lang/ref/FinalReference.java 2009-03-01 08:55:23 UTC (rev 5077) @@ -0,0 +1,37 @@ +/* + * Copyright 1997-2003 Sun Microsystems, Inc. All Rights Reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, + * CA 95054 USA or visit www.sun.com if you need additional information or + * have any questions. + */ + +package java.lang.ref; + + +/* Final references, used to implement finalization */ + +class FinalReference<T> extends Reference<T> { + + public FinalReference(T referent, ReferenceQueue<? super T> q) { + super(referent, q); + } + +} Added: trunk/core/src/openjdk/java/java/lang/ref/Finalizer.java =================================================================== --- trunk/core/src/openjdk/java/java/lang/ref/Finalizer.java (rev 0) +++ trunk/core/src/openjdk/java/java/lang/ref/Finalizer.java 2009-03-01 08:55:23 UTC (rev 5077) @@ -0,0 +1,197 @@ +/* + * Copyright 1997-2004 Sun Microsystems, Inc. All Rights Reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, + * CA 95054 USA or visit www.sun.com if you need additional information or + * have any questions. + */ + +package java.lang.ref; + +import java.security.PrivilegedAction; +import java.security.AccessController; + + +final class Finalizer extends FinalReference { /* Package-private; must be in + same package as the Reference + class */ + + /* A native method that invokes an arbitrary object's finalize method is + required since the finalize method is protected + */ + static native void invokeFinalizeMethod(Object o) throws Throwable; + + static private ReferenceQueue queue = new ReferenceQueue(); + static private Finalizer unfinalized = null; + static private Object lock = new Object(); + + private Finalizer + next = null, + prev = null; + + private boolean hasBeenFinalized() { + return (next == this); + } + + private void add() { + synchronized (lock) { + if (unfinalized != null) { + this.next = unfinalized; + unfinalized.prev = this; + } + unfinalized = this; + } + } + + private void remove() { + synchronized (lock) { + if (unfinalized == this) { + if (this.next != null) { + unfinalized = this.next; + } else { + unfinalized = this.prev; + } + } + if (this.next != null) { + this.next.prev = this.prev; + } + if (this.prev != null) { + this.prev.next = this.next; + } + this.next = this; /* Indicates that this has been finalized */ + this.prev = this; + } + } + + private Finalizer(Object finalizee) { + super(finalizee, queue); + add(); + } + + /* Invoked by VM */ + static void register(Object finalizee) { + new Finalizer(finalizee); + } + + private void runFinalizer() { + synchronized (this) { + if (hasBeenFinalized()) return; + remove(); + } + try { + Object finalizee = this.get(); + if (finalizee != null && !(finalizee instanceof java.lang.Enum)) { + invokeFinalizeMethod(finalizee); + /* Clear stack slot containing this variable, to decrease + the chances of false retention with a conservative GC */ + finalizee = null; + } + } catch (Throwable x) { } + super.clear(); + } + + /* Create a privileged secondary finalizer thread in the system thread + group for the given Runnable, and wait for it to complete. + + This method is used by both runFinalization and runFinalizersOnExit. + The former method invokes all pending finalizers, while the latter + invokes all uninvoked finalizers if on-exit finalization has been + enabled. + + These two methods could have been implemented by offloading their work + to the regular finalizer thread and waiting for that thread to finish. + The advantage of creating a fresh thread, however, is that it insulates + invokers of these methods from a stalled or deadlocked finalizer thread. + */ + private static void forkSecondaryFinalizer(final Runnable proc) { + PrivilegedAction pa = new PrivilegedAction() { + public Object run() { + ThreadGroup tg = Thread.currentThread().getThreadGroup(); + for (ThreadGroup tgn = tg; + tgn != null; + tg = tgn, tgn = tg.getParent()); + Thread sft = new Thread(tg, proc, "Secondary finalizer"); + sft.start(); + try { + sft.join(); + } catch (InterruptedException x) { + /* Ignore */ + } + return null; + }}; + AccessController.doPrivileged(pa); + } + + /* Called by Runtime.runFinalization() */ + static void runFinalization() { + forkSecondaryFinalizer(new Runnable() { + public void run() { + for (;;) { + Finalizer f = (Finalizer)queue.poll(); + if (f == null) break; + f.runFinalizer(); + } + } + }); + } + + /* Invoked by java.lang.Shutdown */ + static void runAllFinalizers() { + forkSecondaryFinalizer(new Runnable() { + public void run() { + for (;;) { + Finalizer f; + synchronized (lock) { + f = unfinalized; + if (f == null) break; + unfinalized = f.next; + } + f.runFinalizer(); + }}}); + } + + private static class FinalizerThread extends Thread { + FinalizerThread(ThreadGroup g) { + super(g, "Finalizer"); + } + public void run() { + for (;;) { + try { + Finalizer f = (Finalizer)queue.remove(); + f.runFinalizer(); + } catch (InterruptedException x) { + continue; + } + } + } + } + + static { + ThreadGroup tg = Thread.currentThread().getThreadGroup(); + for (ThreadGroup tgn = tg; + tgn != null; + tg = tgn, tgn = tg.getParent()); + Thread finalizer = new FinalizerThread(tg); + finalizer.setPriority(Thread.MAX_PRIORITY - 2); + finalizer.setDaemon(true); + finalizer.start(); + } + +} Added: trunk/core/src/openjdk/java/java/lang/ref/PhantomReference.java =================================================================== --- trunk/core/src/openjdk/java/java/lang/ref/PhantomReference.java (rev 0) +++ trunk/core/src/openjdk/java/java/lang/ref/PhantomReference.java 2009-03-01 08:55:23 UTC (rev 5077) @@ -0,0 +1,83 @@ +/* + * Copyright 1997-2003 Sun Microsystems, Inc. All Rights Reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, + * CA 95054 USA or visit www.sun.com if you need additional information or + * have any questions. + */ + +package java.lang.ref; + + +/** + * Phantom reference objects, which are enqueued after the collector + * determines that their referents may otherwise be reclaimed. Phantom + * references are most often used for scheduling pre-mortem cleanup actions in + * a more flexible way than is possible with the Java finalization mechanism. + * + * <p> If the garbage collector determines at a certain point in time that the + * referent of a phantom reference is <a + * href="package-summary.html#reachability">phantom reachable</a>, then at that + * time or at some later time it will enqueue the reference. + * + * <p> In order to ensure that a reclaimable object remains so, the referent of + * a phantom reference may not be retrieved: The <code>get</code> method of a + * phantom reference always returns <code>null</code>. + * + * <p> Unlike soft and weak references, phantom references are not + * automatically cleared by the garbage collector as they are enqueued. An + * object that is reachable via phantom references will remain so until all + * such references are cleared or themselves become unreachable. + * + * @author Mark Reinhold + * @since 1.2 + */ + +public class PhantomReference<T> extends Reference<T> { + + /** + * Returns this reference object's referent. Because the referent of a + * phantom reference is always inaccessible, this method always returns + * <code>null</code>. + * + * @return <code>null</code> + */ + public T get() { + return null; + } + + /** + * Creates a new phantom reference that refers to the given object and + * is registered with the given queue. + * + * <p> It is possible to create a phantom reference with a <tt>null</tt> + * queue, but such a reference is completely useless: Its <tt>get</tt> + * method will always return null and, since it does not have a queue, it + * will never be enqueued. + * + * @param referent the object the new phantom reference will refer to + * @param q the queue with which the reference is to be registered, + * or <tt>null</tt> if registration is not required + */ + public PhantomReference(T referent, ReferenceQueue<? super T> q) { + super(referent, q); + } + +} Added: trunk/core/src/openjdk/java/java/lang/ref/Reference.java =================================================================== --- trunk/core/src/openjdk/java/java/lang/ref/Reference.java (rev 0) +++ trunk/core/src/openjdk/java/java/lang/ref/Reference.java 2009-03-01 08:55:23 UTC (rev 5077) @@ -0,0 +1,238 @@ +/* + * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, + * CA 95054 USA or visit www.sun.com if you need additional information or + * have any questions. + */ + +package java.lang.ref; + +import sun.misc.Cleaner; + + +/** + * Abstract base class for reference objects. This class defines the + * operations common to all reference objects. Because reference objects are + * implemented in close cooperation with the garbage collector, this class may + * not be subclassed directly. + * + * @author Mark Reinhold + * @since 1.2 + */ + +public abstract class Reference<T> { + + /* A Reference instance is in one of four possible internal states: + * + * Active: Subject to special treatment by the garbage collector. Some + * time after the collector detects that the reachability of the + * referent has changed to the appropriate state, it changes the + * instance's state to either Pending or Inactive, depending upon + * whether or not the instance was registered with a queue when it was + * created. In the former case it also adds the instance to the + * pending-Reference list. Newly-created instances are Active. + * + * Pending: An element of the pending-Reference list, waiting to be + * enqueued by the Reference-handler thread. Unregistered instances + * are never in this state. + * + * Enqueued: An element of the queue with which the instance was + * registered when it was created. When an instance is removed from + * its ReferenceQueue, it is made Inactive. Unregistered instances are + * never in this state. + * + * Inactive: Nothing more to do. Once an instance becomes Inactive its + * state will never change again. + * + * The state is encoded in the queue and next fields as follows: + * + * Active: queue = ReferenceQueue with which instance is registered, or + * ReferenceQueue.NULL if it was not registered with a queue; next = + * null. + * + * Pending: queue = ReferenceQueue with which instance is registered; + * next = Following instance in queue, or this if at end of list. + * + * Enqueued: queue = ReferenceQueue.ENQUEUED; next = Following instance + * in queue, or this if at end of list. + * + * Inactive: queue = ReferenceQueue.NULL; next = this. + * + * With this scheme the collector need only examine the next field in order + * to determine whether a Reference instance requires special treatment: If + * the next field is null then the instance is active; if it is non-null, + * then the collector should treat the instance normally. + * + * To ensure that concurrent collector can discover active Reference + * objects without interfering with application threads that may apply + * the enqueue() method to those objects, collectors should link + * discovered objects through the discovered field. + */ + + private T referent; /* Treated specially by GC */ + + ReferenceQueue<? super T> queue; + + Reference next; + transient private Reference<T> discovered; /* used by VM */ + + + /* Object used to synchronize with the garbage collector. The collector + * must acquire this lock at the beginning of each collection cycle. It is + * therefore critical that any code holding this lock complete as quickly + * as possible, allocate no new objects, and avoid calling user code. + */ + static private class Lock { }; + private static Lock lock = new Lock(); + + + /* List of References waiting to be enqueued. The collector adds + * References to this list, while the Reference-handler thread removes + * them. This list is protected by the above lock object. + */ + private static Reference pending = null; + + /* High-priority thread to enqueue pending References + */ + private static class ReferenceHandler extends Thread { + + ReferenceHandler(ThreadGroup g, String name) { + super(g, name); + } + + public void run() { + for (;;) { + + Reference r; + synchronized (lock) { + if (pending != null) { + r = pending; + Reference rn = r.next; + pending = (rn == r) ? null : rn; + r.next = r; + } else { + try { + lock.wait(); + } catch (InterruptedException x) { } + continue; + } + } + + // Fast path for cleaners + if (r instanceof Cleaner) { + ((Cleaner)r).clean(); + continue; + } + + ReferenceQueue q = r.queue; + if (q != ReferenceQueue.NULL) q.enqueue(r); + } + } + } + + static { + ThreadGroup tg = Thread.currentThread().getThreadGroup(); + for (ThreadGroup tgn = tg; + tgn != null; + tg = tgn, tgn = tg.getParent()); + Thread handler = new ReferenceHandler(tg, "Reference Handler"); + /* If there were a special system-only priority greater than + * MAX_PRIORITY, it would be used here + */ + handler.setPriority(Thread.MAX_PRIORITY); + handler.setDaemon(true); + handler.start(); + } + + + /* -- Referent accessor and setters -- */ + + /** + * Returns this reference object's referent. If this reference object has + * been cleared, either by the program or by the garbage collector, then + * this method returns <code>null</code>. + * + * @return The object to which this reference refers, or + * <code>null</code> if this reference object has been cleared + */ + public T get() { + return this.referent; + } + + /** + * Clears this reference object. Invoking this method will not cause this + * object to be enqueued. + * + * <p> This method is invoked only by Java code; when the garbage collector + * clears references it does so directly, without invoking this method. + */ + public void clear() { + this.referent = null; + } + + + /* -- Queue operations -- */ + + /** + * Tells whether or not this reference object has been enqueued, either by + * the program or by the garbage collector. If this reference object was + * not registered with a queue when it was created, then this method will + * always return <code>false</code>. + * + * @return <code>true</code> if and only if this reference object has + * been enqueued + */ + public boolean isEnqueued() { + /* In terms of the internal states, this predicate actually tests + whether the instance is either Pending or Enqueued */ + synchronized (this) { + return (this.queue != ReferenceQueue.NULL) && (this.next != null); + } + } + + /** + * Adds this reference object to the queue with which it is registered, + * if any. + * + * <p> This method is invoked only by Java code; when the garbage collector + * enqueues references it does so directly, without invoking this method. + * + * @return <code>true</code> if this reference object was successfully + * enqueued; <code>false</code> if it was already enqueued or if + * it was not registered with a queue when it was created + */ + public boolean enqueue() { + return this.queue.enqueue(this); + } + + + /* -- Constructors -- */ + + Reference(T referent) { + this(referent, null); + } + + Reference(T referent, ReferenceQueue<? super T> queue) { + this.referent = referent; + this.queue = (queue == null) ? ReferenceQueue.NULL : queue; + } + +} Added: trunk/core/src/openjdk/java/java/lang/ref/ReferenceQueue.java =================================================================== --- trunk/core/src/openjdk/java/java/lang/ref/ReferenceQueue.java (rev 0) +++ trunk/core/src/openjdk/java/java/lang/ref/ReferenceQueue.java 2009-03-01 08:55:23 UTC (rev 5077) @@ -0,0 +1,152 @@ +/* + * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, + * CA 95054 USA or visit www.sun.com if you need additional information or + * have any questions. + */ + +package java.lang.ref; + +/** + * Reference queues, to which registered reference objects are appended by the + * garbage collector after the appropriate reachability changes are detected. + * + * @author Mark Reinhold + * @since 1.2 + */ + +public class ReferenceQueue<T> { + + /** + * Constructs a new reference-object queue. + */ + public ReferenceQueue() { } + + private static class Null extends ReferenceQueue { + boolean enqueue(Reference r) { + return false; + } + } + + static ReferenceQueue NULL = new Null(); + static ReferenceQueue ENQUEUED = new Null(); + + static private class Lock { }; + private Lock lock = new Lock(); + private Reference<? extends T> head = null; + private long queueLength = 0; + + boolean enqueue(Reference<? extends T> r) { /* Called only by Reference class */ + synchronized (r) { + if (r.queue == ENQUEUED) return false; + synchronized (lock) { + r.queue = ENQUEUED; + r.next = (head == null) ? r : head; + head = r; + queueLength++; + if (r instanceof FinalReference) { + sun.misc.VM.addFinalRefCount(1); + } + lock.notifyAll(); + return true; + } + } + } + + private Reference<? extends T> reallyPoll() { /* Must hold lock */ + if (head != null) { + Reference<? extends T> r = head; + head = (r.next == r) ? null : r.next; + r.queue = NULL; + r.next = r; + queueLength--; + if (r instanceof FinalReference) { + sun.misc.VM.addFinalRefCount(-1); + } + return r; + } + return null; + } + + /** + * Polls this queue to see if a reference object is available. If one is + * available without further delay then it is removed from the queue and + * returned. Otherwise this method immediately returns <tt>null</tt>. + * + * @return A reference object, if one was immediately available, + * otherwise <code>null</code> + */ + public Reference<? extends T> poll() { + synchronized (lock) { + return reallyPoll(); + } + } + + /** + * Removes the next reference object in this queue, blocking until either + * one becomes available or the given timeout period expires. + * + * <p> This method does not offer real-time guarantees: It schedules the + * timeout as if by invoking the {@link Object#wait(long)} method. + * + * @param timeout If positive, block for up to <code>timeout</code> + * milliseconds while waiting for a reference to be + * added to this queue. If zero, block indefinitely. + * + * @return A reference object, if one was available within the specified + * timeout period, otherwise <code>null</code> + * + * @throws IllegalArgumentException + * If the value of the timeout argument is negative + * + * @throws InterruptedException + * If the timeout wait is interrupted + */ + public Reference<? extends T> remove(long timeout) + throws IllegalArgumentException, InterruptedException + { + if (timeout < 0) { + throw new IllegalArgumentException("Negative timeout value"); + } + synchronized (lock) { + Reference<? extends T> r = reallyPoll(); + if (r != null) return r; + for (;;) { + lock.wait(timeout); + r = reallyPoll(); + if (r != null) return r; + if (timeout != 0) return null; + } + } + } + + /** + * Removes the next reference object in this queue, blocking until one + * becomes available. + * + * @return A reference object, blocking until one becomes available + * @throws InterruptedException If the wait is interrupted + */ + public Reference<? extends T> remove() throws InterruptedException { + return remove(0); + } + +} Added: trunk/core/src/openjdk/java/java/lang/ref/SoftReference.java =================================================================== --- trunk/core/src/openjdk/java/java/lang/ref/SoftReference.java (rev 0) +++ trunk/core/src/openjdk/java/java/lang/ref/SoftReference.java 2009-03-01 08:55:23 UTC (rev 5077) @@ -0,0 +1,115 @@ +/* + * Copyright 1997-2003 Sun Microsystems, Inc. All Rights Reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, + * CA 95054 USA or visit www.sun.com if you need additional information or + * have any questions. + */ + +package java.lang.ref; + + +/** + * Soft reference objects, which are cleared at the discretion of the garbage + * collector in response to memory demand. Soft references are most often used + * to implement memory-sensitive caches. + * + * <p> Suppose that the garbage collector determines at a certain point in time + * that an object is <a href="package-summary.html#reachability">softly + * reachable</a>. At that time it may choose to clear atomically all soft + * references to that object and all soft references to any other + * softly-reachable objects from which that object is reachable through a chain + * of strong references. At the same time or at some later time it will + * enqueue those newly-cleared soft references that are registered with + * reference queues. + * + * <p> All soft references to softly-reachable objects are guaranteed to have + * been cleared before the virtual machine throws an + * <code>OutOfMemoryError</code>. Otherwise no constraints are placed upon the + * time at which a soft reference will be cleared or the order in which a set + * of such references to different objects will be cleared. Virtual machine + * implementations are, however, encouraged to bias against clearing + * recently-created or recently-used soft references. + * + * <p> Direct instances of this class may be used to implement simple caches; + * this class or derived subclasses may also be used in larger data structures + * to implement more sophisticated caches. As long as the referent of a soft + * reference is strongly reachable, that is, is actually in use, the soft + * reference will not be cleared. Thus a sophisticated cache can, for example, + * prevent its most recently used entries from being discarded by keeping + * strong referents to those entries, leaving the remaining entries to be + * discarded at the discretion of the garbage collector. + * + * @author Mark Reinhold + * @since 1.2 + */ + +public class SoftReference<T> extends Reference<T> { + + /* Timestamp clock, updated by the garbage collector + */ + static private long clock; + + /* Timestamp updated by each invocation of the get method. The VM may use + * this field when selecting soft references to be cleared, but it is not + * required to do so. + */ + private long timestamp; + + /** + * Creates a new soft reference that refers to the given object. The new + * reference is not registered with any queue. + * + * @param referent object the new soft reference will refer to + */ + public SoftReference(T referent) { + super(referent); + this.timestamp = clock; + } + + /** + * Creates a new soft reference that refers to the given object and is + * registered with the given queue. + * + * @param referent object the new soft reference will refer to + * @param q the queue with which the reference is to be registered, + * or <tt>null</tt> if registration is not required + * + */ + public SoftReference(T referent, ReferenceQueue<? super T> q) { + super(referent, q); + this.timestamp = clock; + } + + /** + * Returns this reference object's referent. If this reference object has + * been cleared, either by the program or by the garbage collector, then + * this method returns <code>null</code>. + * + * @return The object to which this reference refers, or + * <code>null</code> if this reference object has been cleared + */ + public T get() { + T o = super.get(); + if (o != null) this.timestamp = clock; + return o; + } + +} Added: trunk/core/src/openjdk/java/java/lang/ref/WeakReference.java =================================================================== --- trunk/core/src/openjdk/java/java/lang/ref/WeakReference.java (rev 0) +++ trunk/core/src/openjdk/java/java/lang/ref/WeakReference.java 2009-03-01 08:55:23 UTC (rev 5077) @@ -0,0 +1,72 @@ +/* + * Copyright 1997-2003 Sun Microsystems, Inc. All Rights Reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, + * CA 95054 USA or visit www.sun.com if you need additional information or + * have any questions. + */ + +package java.lang.ref; + + +/** + * Weak reference objects, which do not prevent their referents from being + * made finalizable, finalized, and then reclaimed. Weak references are most + * often used to implement canonicalizing mappings. + * + * <p> Suppose that the garbage collector determines at a certain point in time + * that an object is <a href="package-summary.html#reachability">weakly + * reachable</a>. At that time it will atomically clear all weak references to + * that object and all weak references to any other weakly-reachable objects + * from which that object is reachable through a chain of strong and soft + * references. At the same time it will declare all of the formerly + * weakly-reachable objects to be finalizable. At the same time or at some + * later time it will enqueue those newly-cleared weak references that are + * registered with reference queues. + * + * @author Mark Reinhold + * @since 1.2 + */ + +public class WeakReference<T> extends Reference<T> { + + /** + * Creates a new weak reference that refers to the given object. The new + * reference is not registered with any queue. + * + * @param referent object the new weak reference will refer to + */ + public WeakReference(T referent) { + super(referent); + } + + /** + * Creates a new weak reference that refers to the given object and is + * registered with the given queue. + * + * @param referent object the new weak reference will refer to + * @param q the queue with which the reference is to be registered, + * or <tt>null</tt> if registration is not required + */ + public WeakReference(T referent, ReferenceQueue<? super T> q) { + super(referent, q); + } + +} Added: trunk/core/src/openjdk/java/java/lang/ref/package.html =================================================================== --- trunk/core/src/openjdk/java/java/lang/ref/package.html (rev 0) +++ trunk/core/src/openjdk/java/java/lang/ref/package.html 2009-03-01 08:55:23 UTC (rev 5077) @@ -0,0 +1,147 @@ +<!-- + Copyright 1998-2003 Sun Microsystems, Inc. All Rights Reserved. + DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + + This code is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License version 2 only, as + published by the Free Software Foundation. Sun designates this + particular file as subject to the "Classpath" exception as provided + by Sun in the LICENSE file that accompanied this code. + + This code is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + version 2 for more details (a copy is included in the LICENSE file that + accompanied this code). + + You should have received a copy of the GNU General Public License version + 2 along with this work; if not, write to the Free Software Foundation, + Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + + Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, + CA 95054 USA or visit www.sun.com if you need additional information or + have any questions. +--> + +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> +<html> +<body bgcolor="white"> + + +Provides reference-object classes, which support a limited degree of +interaction with the garbage collector. A program may use a reference object +to maintain a reference to some other object in such a way that the latter +object may still be reclaimed by the collector. A program may also arrange to +be notified some time after the collector has determined that the reachability +of a given object has changed. + + +<h2>Package Specification</h2> + +A <em>reference object</em> encapsulates a reference to some other object so +that the reference itself may be examined and manipulated like any other +object. Three types of reference objects are provided, each weaker than the +last: <em>soft</em>, <em>weak</em>, and <em>phantom</em>. Each type +corresponds to a different level of reachability, as defined below. Soft +references are for implementing memory-sensitive caches, weak references are +for implementing canonicalizing mappings that do not prevent their keys (or +values) from being reclaimed, and phantom references are for scheduling +pre-mortem cleanup actions in a more flexible way than is possible with the +Java finalization mechanism. + +<p> Each reference-object type is implemented by a subclass of the abstract +base <code>{@link java.lang.ref.Reference}</code> class. An instance of one of +these subclasses encapsulates a single reference to a particular object, called +the <em>referent</em>. Every reference object provides methods for getting and +clearing the reference. Aside from the clearing operation reference objects +are otherwise immutable, so no <code>set</code> operation is provided. A +program may further subclass these subclasses, adding whatever fields and +methods are required for its purposes, or it may use these subclasses without +change. + + +<h3>Notification</h3> + +A program may request to be notified of changes in an object's reachability by +<em>registering</em> an appropriate reference object with a <em>reference +queue</em> at the time the reference object is created. Some time after the +garbage collector determines that the reachability of the referent has changed +to the value corresponding to the type of the reference, it will add the +reference to the associated queue. At this point, the reference is considered +to be <em>enqueued</em>. The program may remove references from a queue either +by polling or by blocking until a reference becomes available. Reference +queues are implemented by the <code>{@link java.lang.ref.ReferenceQueue}</code> +class. + +<p> The relationship between a registered reference object and its queue is +one-sided. That is, a queue does not keep track of the references that are +registered with it. If a registered reference becomes unreachable itself, then +it will never be enqueued. It is the responsibility of the program using +reference objects to ensure that the objects remain reachable for as long as +the program is interested in their referents. + +<p> While some programs will choose to dedicate a thread to removing reference +objects from one or more queues and processing them, this is by no means +necessary. A tactic that often works well is to examine a reference queue in +the course of performing some other fairly-frequent action. For example, a +hashtable that uses weak references to implement weak keys could poll its +reference queue each time the table is accessed. This is how the <code>{@link +java.util.WeakHashMap}</code> class works. Because the <code>{@link +java.lang.ref.ReferenceQueue#poll ReferenceQueue.poll}</code> method simply +checks an internal data structure, this check will add little overhead to the +hashtable access methods. + + +<h3>Automatically-cleared references</h3> + +Soft and weak references are automatically cleared by the collector before +being added to the queues with which they are registered, if any. Therefore +soft and weak references need not be registered with a queue in order to be +useful, while phantom references do. An object that is reachable via phantom +references will remain so until all such references are cleared or themselves +become unreachable. + + +<a name="reachability"></a> +<h3>Reachability</h3> + +Going from strongest to weakest, the different levels of reachability reflect +the life cycle of an object. They are operationally defined as follows: + +<ul> + +<li> An object is <em>strongly reachable</em> if it can be reached by some +thread without traversing any reference objects. A newly-created object is +strongly reachable by the thread that created it. + +<li> An object is <em>softly reachable</em> if it is not strongly reachable but +can be reached by traversing a soft reference. + +<li> An object is <em>weakly reachable</em> if it is neither strongly nor +softly reachable but can be reached by traversing a weak reference. When the +weak references to a weakly-reachable object are cleared, the object becomes +eligible for finalization. + +<li> An object is <em>phantom reachable</em> if it is neither strongly, softly, +nor weakly reachable, it has been finalized, and some phantom reference refers +to it. + +<li> Finally, an object is <em>unreachable</em>, and therefore eligible for +reclamation, when it is not reachable in any of the above ways. + +</ul> + + +@author Mark Reinhold +@since 1.2 + +<!-- +<h2>Related Documentation</h2> + +For overviews, tutorials, examples, guides, and tool documentation, please see: +<ul> + <li><a href="">##### REFER TO NON-SPEC DOCUMENTATION HERE #####</a> +</ul> +--> +</body> +</html> Added: trunk/core/src/openjdk/vm/java/lang/ref/NativeFinalizer.java =================================================================== --- trunk/core/src/openjdk/vm/java/lang/ref/NativeFinalizer.java (rev 0) +++ trunk/core/src/openjdk/vm/java/lang/ref/NativeFinalizer.java 2009-03-01 08:55:23 UTC (rev 5077) @@ -0,0 +1,13 @@ +package java.lang.ref; + +/** + * @see java.lang.ref.Finalizer + */ +class NativeFinalizer { + /** + * @see java.lang.ref.Finalizer#invokeFinalizeMethod(java.lang.Object) + */ + private static void invokeFinalizeMethod(Object arg1) { + //todo implement it + } +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |