| 1 | /* java.lang.ref.Reference
|
|---|
| 2 | Copyright (C) 1999, 2002 Free Software Foundation, Inc.
|
|---|
| 3 |
|
|---|
| 4 | This file is part of GNU Classpath.
|
|---|
| 5 |
|
|---|
| 6 | GNU Classpath is free software; you can redistribute it and/or modify
|
|---|
| 7 | it under the terms of the GNU General Public License as published by
|
|---|
| 8 | the Free Software Foundation; either version 2, or (at your option)
|
|---|
| 9 | any later version.
|
|---|
| 10 |
|
|---|
| 11 | GNU Classpath is distributed in the hope that it will be useful, but
|
|---|
| 12 | WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 14 | General Public License for more details.
|
|---|
| 15 |
|
|---|
| 16 | You should have received a copy of the GNU General Public License
|
|---|
| 17 | along with GNU Classpath; see the file COPYING. If not, write to the
|
|---|
| 18 | Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|---|
| 19 | 02111-1307 USA.
|
|---|
| 20 |
|
|---|
| 21 | Linking this library statically or dynamically with other modules is
|
|---|
| 22 | making a combined work based on this library. Thus, the terms and
|
|---|
| 23 | conditions of the GNU General Public License cover the whole
|
|---|
| 24 | combination.
|
|---|
| 25 |
|
|---|
| 26 | As a special exception, the copyright holders of this library give you
|
|---|
| 27 | permission to link this library with independent modules to produce an
|
|---|
| 28 | executable, regardless of the license terms of these independent
|
|---|
| 29 | modules, and to copy and distribute the resulting executable under
|
|---|
| 30 | terms of your choice, provided that you also meet, for each linked
|
|---|
| 31 | independent module, the terms and conditions of the license of that
|
|---|
| 32 | module. An independent module is a module which is not derived from
|
|---|
| 33 | or based on this library. If you modify this library, you may extend
|
|---|
| 34 | this exception to your version of the library, but you are not
|
|---|
| 35 | obligated to do so. If you do not wish to do so, delete this
|
|---|
| 36 | exception statement from your version. */
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 | package java.lang.ref;
|
|---|
| 40 |
|
|---|
| 41 | /**
|
|---|
| 42 | * This is the base class of all references. A reference allows
|
|---|
| 43 | * refering to an object without preventing the garbage collection to
|
|---|
| 44 | * collect it. The only way to get the referred object is via the
|
|---|
| 45 | * <code>get()</code>-method. This method will return
|
|---|
| 46 | * <code>null</code> if the object was collected. <br>
|
|---|
| 47 | *
|
|---|
| 48 | * A reference may be registered with a queue. When a referred
|
|---|
| 49 | * element gets collected the reference will be put on the queue, so
|
|---|
| 50 | * that you will be notified. <br>
|
|---|
| 51 | *
|
|---|
| 52 | * There are currently three types of references: soft reference,
|
|---|
| 53 | * weak reference and phantom reference. <br>
|
|---|
| 54 | *
|
|---|
| 55 | * Soft references will be cleared if the garbage collection is told
|
|---|
| 56 | * to free some memory and there are no unreferenced or weakly referenced
|
|---|
| 57 | * objects. It is useful for caches. <br>
|
|---|
| 58 | *
|
|---|
| 59 | * Weak references will be cleared as soon as the garbage collection
|
|---|
| 60 | * determines that the refered object is only weakly reachable. They
|
|---|
| 61 | * are useful as keys in hashtables (see <code>WeakHashtable</code>) as
|
|---|
| 62 | * you get notified when nobody has the key anymore.
|
|---|
| 63 | *
|
|---|
| 64 | * Phantom references don't prevent finalization. If an object is only
|
|---|
| 65 | * phantom reachable, it will be finalized, and the reference will be
|
|---|
| 66 | * enqueued, but not cleared. Since you mustn't access an finalized
|
|---|
| 67 | * object, the <code>get</code> method of a phantom reference will never
|
|---|
| 68 | * work. It is useful to keep track, when an object is finalized.
|
|---|
| 69 | *
|
|---|
| 70 | * @author Jochen Hoenicke
|
|---|
| 71 | * @see java.util.WeakHashtable
|
|---|
| 72 | */
|
|---|
| 73 | public abstract class Reference
|
|---|
| 74 | {
|
|---|
| 75 | /**
|
|---|
| 76 | * The underlying object. This field is handled in a special way by
|
|---|
| 77 | * the garbage collection.
|
|---|
| 78 | * GCJ LOCAL:
|
|---|
| 79 | * This is a RawData because it must be disguised from the GC.
|
|---|
| 80 | * END GCJ LOCAL
|
|---|
| 81 | */
|
|---|
| 82 | gnu.gcj.RawData referent;
|
|---|
| 83 |
|
|---|
| 84 | /**
|
|---|
| 85 | * This is like REFERENT but is not scanned by the GC. We keep a
|
|---|
| 86 | * copy around so that we can see when clear() has been called.
|
|---|
| 87 | * GCJ LOCAL:
|
|---|
| 88 | * This field doesn't exist in Classpath; we use it to detect
|
|---|
| 89 | * clearing.
|
|---|
| 90 | * END GCJ LOCAL
|
|---|
| 91 | */
|
|---|
| 92 | gnu.gcj.RawData copy;
|
|---|
| 93 |
|
|---|
| 94 | /**
|
|---|
| 95 | * The queue this reference is registered on. This is null, if this
|
|---|
| 96 | * wasn't registered to any queue or reference was already enqueued.
|
|---|
| 97 | */
|
|---|
| 98 | ReferenceQueue queue;
|
|---|
| 99 |
|
|---|
| 100 | /**
|
|---|
| 101 | * Link to the next entry on the queue. If this is null, this
|
|---|
| 102 | * reference is not enqueued. Otherwise it points to the next
|
|---|
| 103 | * reference. The last reference on a queue will point to itself
|
|---|
| 104 | * (not to null, that value is used to mark a not enqueued
|
|---|
| 105 | * reference).
|
|---|
| 106 | */
|
|---|
| 107 | Reference nextOnQueue;
|
|---|
| 108 |
|
|---|
| 109 | /**
|
|---|
| 110 | * This lock should be taken by the garbage collection, before
|
|---|
| 111 | * determining reachability. It will prevent the get()-method to
|
|---|
| 112 | * return the reference so that reachability doesn't change.
|
|---|
| 113 | */
|
|---|
| 114 | static Object lock = new Object();
|
|---|
| 115 |
|
|---|
| 116 | /**
|
|---|
| 117 | * Creates a new reference that is not registered to any queue.
|
|---|
| 118 | * Since it is package private, it is not possible to overload this
|
|---|
| 119 | * class in a different package.
|
|---|
| 120 | * @param referent the object we refer to.
|
|---|
| 121 | */
|
|---|
| 122 | Reference(Object ref)
|
|---|
| 123 | {
|
|---|
| 124 | create (ref);
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | /**
|
|---|
| 128 | * Creates a reference that is registered to a queue. Since this is
|
|---|
| 129 | * package private, it is not possible to overload this class in a
|
|---|
| 130 | * different package.
|
|---|
| 131 | * @param referent the object we refer to.
|
|---|
| 132 | * @param q the reference queue to register on.
|
|---|
| 133 | * @exception NullPointerException if q is null.
|
|---|
| 134 | */
|
|---|
| 135 | Reference(Object ref, ReferenceQueue q)
|
|---|
| 136 | {
|
|---|
| 137 | if (q == null)
|
|---|
| 138 | throw new NullPointerException();
|
|---|
| 139 | queue = q;
|
|---|
| 140 | create (ref);
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | /**
|
|---|
| 144 | * Notifies the VM that a new Reference has been created.
|
|---|
| 145 | */
|
|---|
| 146 | private native void create (Object o);
|
|---|
| 147 |
|
|---|
| 148 | /**
|
|---|
| 149 | * Returns the object, this reference refers to.
|
|---|
| 150 | * @return the object, this reference refers to, or null if the
|
|---|
| 151 | * reference was cleared.
|
|---|
| 152 | */
|
|---|
| 153 | public Object get()
|
|---|
| 154 | {
|
|---|
| 155 | synchronized(lock)
|
|---|
| 156 | {
|
|---|
| 157 | return referent;
|
|---|
| 158 | }
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | /**
|
|---|
| 162 | * Clears the reference, so that it doesn't refer to its object
|
|---|
| 163 | * anymore. For soft and weak references this is called by the
|
|---|
| 164 | * garbage collection. For phantom references you should call
|
|---|
| 165 | * this when enqueuing the reference.
|
|---|
| 166 | */
|
|---|
| 167 | public void clear()
|
|---|
| 168 | {
|
|---|
| 169 | referent = null;
|
|---|
| 170 | copy = null;
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | /**
|
|---|
| 174 | * Tells if the object is enqueued on a reference queue.
|
|---|
| 175 | * @return true if it is enqueued, false otherwise.
|
|---|
| 176 | */
|
|---|
| 177 | public boolean isEnqueued()
|
|---|
| 178 | {
|
|---|
| 179 | return nextOnQueue != null;
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | /**
|
|---|
| 183 | * Enqueue an object on a reference queue. This is normally executed
|
|---|
| 184 | * by the garbage collection.
|
|---|
| 185 | */
|
|---|
| 186 | public boolean enqueue()
|
|---|
| 187 | {
|
|---|
| 188 | if (queue != null && nextOnQueue == null)
|
|---|
| 189 | {
|
|---|
| 190 | queue.enqueue(this);
|
|---|
| 191 | queue = null;
|
|---|
| 192 | return true;
|
|---|
| 193 | }
|
|---|
| 194 | return false;
|
|---|
| 195 | }
|
|---|
| 196 | }
|
|---|