Imported Upstream version 3.6.0

Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
This commit is contained in:
Jo Shields
2014-08-13 10:39:27 +01:00
commit a575963da9
50588 changed files with 8155799 additions and 0 deletions

View File

@ -0,0 +1,178 @@
/*
Copyright (C) 2003-2012 Jeroen Frijters
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
Jeroen Frijters
jeroen@frijters.net
*/
package java.lang.ref;
import cli.System.Collections.Hashtable;
import sun.misc.Cleaner;
public abstract class Reference<T>
{
// accessed by inner class
volatile cli.System.WeakReference weakRef;
volatile T strongRef;
volatile ReferenceQueue<? super T> queue;
volatile Reference next;
private static native boolean noclassgc();
Reference(T referent)
{
this(referent, null);
}
Reference(T referent, ReferenceQueue<? super T> queue)
{
this.queue = queue == null ? ReferenceQueue.NULL : queue;
if (referent != null)
{
if (referent instanceof Class && noclassgc())
{
// We don't do Class gc, so no point in using a weak reference for classes.
strongRef = referent;
}
else
{
weakRef = new cli.System.WeakReference(referent, this instanceof PhantomReference);
if (queue != null || referent instanceof Cleaner || this instanceof SoftReference)
{
new QueueWatcher(this);
}
}
}
}
private static final boolean debug = false;
private static final class QueueWatcher
{
private static final Hashtable keepAlive = Hashtable.Synchronized(new Hashtable());
private cli.System.WeakReference handle;
QueueWatcher(Reference r)
{
handle = new cli.System.WeakReference(r, true);
// FXBUG when a WeakReference is finalizer reachable, it gets cleared by the GC (even if we call GC.SuppressFinalize),
// so we have to maintain a strong reference to it to prevent it from being cleared.
keepAlive.Add(handle, null);
}
boolean check(Reference r)
{
r.strongRef = null;
boolean alive = false;
try
{
if (false) throw new cli.System.InvalidOperationException();
cli.System.WeakReference referent = r.weakRef;
if (referent == null)
{
// ref was explicitly cleared, so we don't enqueue
return false;
}
alive = referent.get_IsAlive();
}
catch (cli.System.InvalidOperationException x)
{
// this happens if the reference is already finalized (if we were
// the only one still hanging on to it)
}
if (alive)
{
// we don't want to keep creating finalizable objects during shutdown
if (!cli.System.Environment.get_HasShutdownStarted())
{
return true;
}
}
else
{
if (r instanceof Cleaner)
{
((Cleaner)r).clean();
}
else if (r.queue != null)
{
r.queue.enqueue(r);
}
}
return false;
}
protected void finalize()
{
Reference r = (Reference)handle.get_Target();
if (debug)
cli.System.Console.WriteLine("~QueueWatcher: " + hashCode() + " on " + r);
if (r != null && r.next == null && check(r))
{
cli.System.GC.ReRegisterForFinalize(QueueWatcher.this);
}
else
{
handle.set_Target(null);
keepAlive.Remove(handle);
}
}
}
public T get()
{
try
{
if (false) throw new cli.System.InvalidOperationException();
cli.System.WeakReference referent = this.weakRef;
if (referent == null)
{
return strongRef;
}
T value = (T)referent.get_Target();
if (value == null)
{
queue.enqueue(this);
}
return value;
}
catch (cli.System.InvalidOperationException x)
{
// we were already finalized, so we just return null.
return null;
}
}
public void clear()
{
weakRef = null;
strongRef = null;
}
public synchronized boolean isEnqueued()
{
return queue != ReferenceQueue.NULL && next != null;
}
public boolean enqueue()
{
return queue.enqueue(this);
}
}

View File

@ -0,0 +1,117 @@
/*
* Copyright (c) 1997, 2003, Oracle and/or its affiliates. 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. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.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);
strongRef = referent;
}
/**
* 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);
strongRef = referent;
}
/**
* 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();
strongRef = o;
return o;
}
}