1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/emul/compact/src/main/java/java/lang/ref/Reference.java Mon Jan 28 18:12:47 2013 +0100
1.3 @@ -0,0 +1,238 @@
1.4 +/*
1.5 + * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1.7 + *
1.8 + * This code is free software; you can redistribute it and/or modify it
1.9 + * under the terms of the GNU General Public License version 2 only, as
1.10 + * published by the Free Software Foundation. Oracle designates this
1.11 + * particular file as subject to the "Classpath" exception as provided
1.12 + * by Oracle in the LICENSE file that accompanied this code.
1.13 + *
1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1.17 + * version 2 for more details (a copy is included in the LICENSE file that
1.18 + * accompanied this code).
1.19 + *
1.20 + * You should have received a copy of the GNU General Public License version
1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1.23 + *
1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1.25 + * or visit www.oracle.com if you need additional information or have any
1.26 + * questions.
1.27 + */
1.28 +
1.29 +package java.lang.ref;
1.30 +
1.31 +import sun.misc.Cleaner;
1.32 +
1.33 +
1.34 +/**
1.35 + * Abstract base class for reference objects. This class defines the
1.36 + * operations common to all reference objects. Because reference objects are
1.37 + * implemented in close cooperation with the garbage collector, this class may
1.38 + * not be subclassed directly.
1.39 + *
1.40 + * @author Mark Reinhold
1.41 + * @since 1.2
1.42 + */
1.43 +
1.44 +public abstract class Reference<T> {
1.45 +
1.46 + /* A Reference instance is in one of four possible internal states:
1.47 + *
1.48 + * Active: Subject to special treatment by the garbage collector. Some
1.49 + * time after the collector detects that the reachability of the
1.50 + * referent has changed to the appropriate state, it changes the
1.51 + * instance's state to either Pending or Inactive, depending upon
1.52 + * whether or not the instance was registered with a queue when it was
1.53 + * created. In the former case it also adds the instance to the
1.54 + * pending-Reference list. Newly-created instances are Active.
1.55 + *
1.56 + * Pending: An element of the pending-Reference list, waiting to be
1.57 + * enqueued by the Reference-handler thread. Unregistered instances
1.58 + * are never in this state.
1.59 + *
1.60 + * Enqueued: An element of the queue with which the instance was
1.61 + * registered when it was created. When an instance is removed from
1.62 + * its ReferenceQueue, it is made Inactive. Unregistered instances are
1.63 + * never in this state.
1.64 + *
1.65 + * Inactive: Nothing more to do. Once an instance becomes Inactive its
1.66 + * state will never change again.
1.67 + *
1.68 + * The state is encoded in the queue and next fields as follows:
1.69 + *
1.70 + * Active: queue = ReferenceQueue with which instance is registered, or
1.71 + * ReferenceQueue.NULL if it was not registered with a queue; next =
1.72 + * null.
1.73 + *
1.74 + * Pending: queue = ReferenceQueue with which instance is registered;
1.75 + * next = Following instance in queue, or this if at end of list.
1.76 + *
1.77 + * Enqueued: queue = ReferenceQueue.ENQUEUED; next = Following instance
1.78 + * in queue, or this if at end of list.
1.79 + *
1.80 + * Inactive: queue = ReferenceQueue.NULL; next = this.
1.81 + *
1.82 + * With this scheme the collector need only examine the next field in order
1.83 + * to determine whether a Reference instance requires special treatment: If
1.84 + * the next field is null then the instance is active; if it is non-null,
1.85 + * then the collector should treat the instance normally.
1.86 + *
1.87 + * To ensure that concurrent collector can discover active Reference
1.88 + * objects without interfering with application threads that may apply
1.89 + * the enqueue() method to those objects, collectors should link
1.90 + * discovered objects through the discovered field.
1.91 + */
1.92 +
1.93 + private T referent; /* Treated specially by GC */
1.94 +
1.95 + ReferenceQueue<? super T> queue;
1.96 +
1.97 + Reference next;
1.98 + transient private Reference<T> discovered; /* used by VM */
1.99 +
1.100 +
1.101 + /* Object used to synchronize with the garbage collector. The collector
1.102 + * must acquire this lock at the beginning of each collection cycle. It is
1.103 + * therefore critical that any code holding this lock complete as quickly
1.104 + * as possible, allocate no new objects, and avoid calling user code.
1.105 + */
1.106 + static private class Lock { };
1.107 + private static Lock lock = new Lock();
1.108 +
1.109 +
1.110 + /* List of References waiting to be enqueued. The collector adds
1.111 + * References to this list, while the Reference-handler thread removes
1.112 + * them. This list is protected by the above lock object.
1.113 + */
1.114 + private static Reference pending = null;
1.115 +
1.116 + /* High-priority thread to enqueue pending References
1.117 + */
1.118 + private static class ReferenceHandler extends Thread {
1.119 +
1.120 + ReferenceHandler(ThreadGroup g, String name) {
1.121 + super(g, name);
1.122 + }
1.123 +
1.124 + public void run() {
1.125 + for (;;) {
1.126 +
1.127 + Reference r;
1.128 + synchronized (lock) {
1.129 + if (pending != null) {
1.130 + r = pending;
1.131 + Reference rn = r.next;
1.132 + pending = (rn == r) ? null : rn;
1.133 + r.next = r;
1.134 + } else {
1.135 + try {
1.136 + lock.wait();
1.137 + } catch (InterruptedException x) { }
1.138 + continue;
1.139 + }
1.140 + }
1.141 +
1.142 + // Fast path for cleaners
1.143 + if (r instanceof Cleaner) {
1.144 + ((Cleaner)r).clean();
1.145 + continue;
1.146 + }
1.147 +
1.148 + ReferenceQueue q = r.queue;
1.149 + if (q != ReferenceQueue.NULL) q.enqueue(r);
1.150 + }
1.151 + }
1.152 + }
1.153 +
1.154 + static {
1.155 + ThreadGroup tg = Thread.currentThread().getThreadGroup();
1.156 + for (ThreadGroup tgn = tg;
1.157 + tgn != null;
1.158 + tg = tgn, tgn = tg.getParent());
1.159 + Thread handler = new ReferenceHandler(tg, "Reference Handler");
1.160 + /* If there were a special system-only priority greater than
1.161 + * MAX_PRIORITY, it would be used here
1.162 + */
1.163 + handler.setPriority(Thread.MAX_PRIORITY);
1.164 + handler.setDaemon(true);
1.165 + handler.start();
1.166 + }
1.167 +
1.168 +
1.169 + /* -- Referent accessor and setters -- */
1.170 +
1.171 + /**
1.172 + * Returns this reference object's referent. If this reference object has
1.173 + * been cleared, either by the program or by the garbage collector, then
1.174 + * this method returns <code>null</code>.
1.175 + *
1.176 + * @return The object to which this reference refers, or
1.177 + * <code>null</code> if this reference object has been cleared
1.178 + */
1.179 + public T get() {
1.180 + return this.referent;
1.181 + }
1.182 +
1.183 + /**
1.184 + * Clears this reference object. Invoking this method will not cause this
1.185 + * object to be enqueued.
1.186 + *
1.187 + * <p> This method is invoked only by Java code; when the garbage collector
1.188 + * clears references it does so directly, without invoking this method.
1.189 + */
1.190 + public void clear() {
1.191 + this.referent = null;
1.192 + }
1.193 +
1.194 +
1.195 + /* -- Queue operations -- */
1.196 +
1.197 + /**
1.198 + * Tells whether or not this reference object has been enqueued, either by
1.199 + * the program or by the garbage collector. If this reference object was
1.200 + * not registered with a queue when it was created, then this method will
1.201 + * always return <code>false</code>.
1.202 + *
1.203 + * @return <code>true</code> if and only if this reference object has
1.204 + * been enqueued
1.205 + */
1.206 + public boolean isEnqueued() {
1.207 + /* In terms of the internal states, this predicate actually tests
1.208 + whether the instance is either Pending or Enqueued */
1.209 + synchronized (this) {
1.210 + return (this.queue != ReferenceQueue.NULL) && (this.next != null);
1.211 + }
1.212 + }
1.213 +
1.214 + /**
1.215 + * Adds this reference object to the queue with which it is registered,
1.216 + * if any.
1.217 + *
1.218 + * <p> This method is invoked only by Java code; when the garbage collector
1.219 + * enqueues references it does so directly, without invoking this method.
1.220 + *
1.221 + * @return <code>true</code> if this reference object was successfully
1.222 + * enqueued; <code>false</code> if it was already enqueued or if
1.223 + * it was not registered with a queue when it was created
1.224 + */
1.225 + public boolean enqueue() {
1.226 + return this.queue.enqueue(this);
1.227 + }
1.228 +
1.229 +
1.230 + /* -- Constructors -- */
1.231 +
1.232 + Reference(T referent) {
1.233 + this(referent, null);
1.234 + }
1.235 +
1.236 + Reference(T referent, ReferenceQueue<? super T> queue) {
1.237 + this.referent = referent;
1.238 + this.queue = (queue == null) ? ReferenceQueue.NULL : queue;
1.239 + }
1.240 +
1.241 +}