2
Copyright (C) 2003-2012 Jeroen Frijters
4
This software is provided 'as-is', without any express or implied
5
warranty. In no event will the authors be held liable for any damages
6
arising from the use of this software.
8
Permission is granted to anyone to use this software for any purpose,
9
including commercial applications, and to alter it and redistribute it
10
freely, subject to the following restrictions:
12
1. The origin of this software must not be misrepresented; you must not
13
claim that you wrote the original software. If you use this software
14
in a product, an acknowledgment in the product documentation would be
15
appreciated but is not required.
16
2. Altered source versions must be plainly marked as such, and must not be
17
misrepresented as being the original software.
18
3. This notice may not be removed or altered from any source distribution.
24
package java.lang.ref;
26
import cli.System.Collections.Hashtable;
27
import sun.misc.Cleaner;
29
public abstract class Reference<T>
31
// accessed by inner class
32
volatile cli.System.WeakReference weakRef;
34
volatile ReferenceQueue<? super T> queue;
35
volatile Reference next;
37
private static native boolean noclassgc();
44
Reference(T referent, ReferenceQueue<? super T> queue)
46
this.queue = queue == null ? ReferenceQueue.NULL : queue;
49
if (referent instanceof Class && noclassgc())
51
// We don't do Class gc, so no point in using a weak reference for classes.
56
weakRef = new cli.System.WeakReference(referent, this instanceof PhantomReference);
57
if (queue != null || referent instanceof Cleaner || this instanceof SoftReference)
59
new QueueWatcher(this);
65
private static final boolean debug = false;
67
private static final class QueueWatcher
69
private static final Hashtable keepAlive = Hashtable.Synchronized(new Hashtable());
70
private cli.System.WeakReference handle;
72
QueueWatcher(Reference r)
74
handle = new cli.System.WeakReference(r, true);
75
// FXBUG when a WeakReference is finalizer reachable, it gets cleared by the GC (even if we call GC.SuppressFinalize),
76
// so we have to maintain a strong reference to it to prevent it from being cleared.
77
keepAlive.Add(handle, null);
80
boolean check(Reference r)
83
boolean alive = false;
86
if (false) throw new cli.System.InvalidOperationException();
87
cli.System.WeakReference referent = r.weakRef;
90
// ref was explicitly cleared, so we don't enqueue
93
alive = referent.get_IsAlive();
95
catch (cli.System.InvalidOperationException x)
97
// this happens if the reference is already finalized (if we were
98
// the only one still hanging on to it)
102
// we don't want to keep creating finalizable objects during shutdown
103
if (!cli.System.Environment.get_HasShutdownStarted())
110
if (r instanceof Cleaner)
112
((Cleaner)r).clean();
114
else if (r.queue != null)
122
protected void finalize()
124
Reference r = (Reference)handle.get_Target();
126
cli.System.Console.WriteLine("~QueueWatcher: " + hashCode() + " on " + r);
127
if (r != null && r.next == null && check(r))
129
cli.System.GC.ReRegisterForFinalize(QueueWatcher.this);
133
handle.set_Target(null);
134
keepAlive.Remove(handle);
143
if (false) throw new cli.System.InvalidOperationException();
144
cli.System.WeakReference referent = this.weakRef;
145
if (referent == null)
149
T value = (T)referent.get_Target();
156
catch (cli.System.InvalidOperationException x)
158
// we were already finalized, so we just return null.
169
public synchronized boolean isEnqueued()
171
return queue != ReferenceQueue.NULL && next != null;
174
public boolean enqueue()
176
return queue.enqueue(this);