2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / java / lang / ref / Reference.java
bloba6385a816174f92e78afeb104a2aa36bf13024d9
1 /* java.lang.ref.Reference
2 Copyright (C) 1999, 2002, 2003 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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.
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.
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.
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.
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. */
39 package java.lang.ref;
41 /**
42 * This is the base class of all references. A reference allows
43 * refering to an object without preventing the garbage collector 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>
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>
52 * There are currently three types of references: soft reference,
53 * weak reference and phantom reference. <br>
55 * Soft references will be cleared if the garbage collector is told
56 * to free some memory and there are no unreferenced or weakly referenced
57 * objects. It is useful for caches. <br>
59 * Weak references will be cleared as soon as the garbage collector
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.
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.
70 * @author Jochen Hoenicke
71 * @see java.util.WeakHashtable
73 public abstract class Reference
75 /**
76 * The underlying object. This field is handled in a special way by
77 * the garbage collector.
78 * GCJ LOCAL:
79 * This is a RawData because it must be disguised from the GC.
80 * END GCJ LOCAL
82 gnu.gcj.RawData referent;
84 /**
85 * This is like REFERENT but is not scanned by the GC. We keep a
86 * copy around so that we can clean up our internal data structure
87 * even after clear() is called.
88 * GCJ LOCAL:
89 * This field doesn't exist in Classpath.
90 * END GCJ LOCAL
92 gnu.gcj.RawData copy;
94 /**
95 * Set to true if {@link #clear()} is called.
96 * GCJ LOCAL:
97 * This field doesn't exist in Classpath. It is used internally in
98 * natReference.cc, which enqueues the reference unless it is true
99 * (has been cleared).
100 * END GCJ LOCAL
102 boolean cleared = false;
105 * The queue this reference is registered on. This is null, if this
106 * wasn't registered to any queue or reference was already enqueued.
108 ReferenceQueue queue;
111 * Link to the next entry on the queue. If this is null, this
112 * reference is not enqueued. Otherwise it points to the next
113 * reference. The last reference on a queue will point to itself
114 * (not to null, that value is used to mark a not enqueued
115 * reference).
117 Reference nextOnQueue;
120 * This lock should be taken by the garbage collector, before
121 * determining reachability. It will prevent the get()-method to
122 * return the reference so that reachability doesn't change.
124 static Object lock = new Object();
127 * Creates a new reference that is not registered to any queue.
128 * Since it is package private, it is not possible to overload this
129 * class in a different package.
130 * @param referent the object we refer to.
132 Reference(Object ref)
134 create (ref);
138 * Creates a reference that is registered to a queue. Since this is
139 * package private, it is not possible to overload this class in a
140 * different package.
141 * @param referent the object we refer to.
142 * @param q the reference queue to register on.
143 * @exception NullPointerException if q is null.
145 Reference(Object ref, ReferenceQueue q)
147 if (q == null)
148 throw new NullPointerException();
149 queue = q;
150 create (ref);
154 * Notifies the VM that a new Reference has been created.
156 private native void create (Object o);
159 * Returns the object, this reference refers to.
160 * @return the object, this reference refers to, or null if the
161 * reference was cleared.
163 public Object get()
165 synchronized (lock)
167 return referent;
172 * Clears the reference, so that it doesn't refer to its object
173 * anymore. For soft and weak references this is called by the
174 * garbage collector. For phantom references you should call
175 * this when enqueuing the reference.
177 public void clear()
179 // Must synchronize so changes are visible in finalizer thread.
180 synchronized (lock)
182 referent = null;
183 cleared = true;
188 * Tells if the object is enqueued on a reference queue.
189 * @return true if it is enqueued, false otherwise.
191 public boolean isEnqueued()
193 return nextOnQueue != null;
197 * Enqueue an object on a reference queue. This is normally executed
198 * by the garbage collector.
200 public boolean enqueue()
202 if (queue != null && nextOnQueue == null)
204 queue.enqueue(this);
205 queue = null;
206 return true;
208 return false;