jdk/src/java.base/share/classes/java/lang/ref/Reference.java
author chegar
Sun, 17 Aug 2014 15:54:13 +0100
changeset 25859 3317bb8137f4
parent 23009 jdk/src/share/classes/java/lang/ref/Reference.java@e2c92ddeb57f
child 29919 be906afc335b
permissions -rw-r--r--
8054834: Modular Source Code Reviewed-by: alanb, chegar, ihse, mduigou Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, magnus.ihse.bursie@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, paul.sandoz@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
17716
f9486b530c80 7038914: VM could throw uncaught OOME in ReferenceHandler thread
plevart
parents: 14342
diff changeset
     2
 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.lang.ref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import sun.misc.Cleaner;
23009
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
    29
import sun.misc.JavaLangRefAccess;
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
    30
import sun.misc.SharedSecrets;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * Abstract base class for reference objects.  This class defines the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * operations common to all reference objects.  Because reference objects are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * implemented in close cooperation with the garbage collector, this class may
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * not be subclassed directly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @author   Mark Reinhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @since    1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
public abstract class Reference<T> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    /* A Reference instance is in one of four possible internal states:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     *     Active: Subject to special treatment by the garbage collector.  Some
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     *     time after the collector detects that the reachability of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     *     referent has changed to the appropriate state, it changes the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     *     instance's state to either Pending or Inactive, depending upon
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     *     whether or not the instance was registered with a queue when it was
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     *     created.  In the former case it also adds the instance to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     *     pending-Reference list.  Newly-created instances are Active.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     *     Pending: An element of the pending-Reference list, waiting to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     *     enqueued by the Reference-handler thread.  Unregistered instances
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     *     are never in this state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     *     Enqueued: An element of the queue with which the instance was
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     *     registered when it was created.  When an instance is removed from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     *     its ReferenceQueue, it is made Inactive.  Unregistered instances are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     *     never in this state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     *     Inactive: Nothing more to do.  Once an instance becomes Inactive its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     *     state will never change again.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * The state is encoded in the queue and next fields as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     *     Active: queue = ReferenceQueue with which instance is registered, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     *     ReferenceQueue.NULL if it was not registered with a queue; next =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     *     null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     *     Pending: queue = ReferenceQueue with which instance is registered;
10895
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents: 5506
diff changeset
    73
     *     next = this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     *     Enqueued: queue = ReferenceQueue.ENQUEUED; next = Following instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     *     in queue, or this if at end of list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     *     Inactive: queue = ReferenceQueue.NULL; next = this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * With this scheme the collector need only examine the next field in order
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * to determine whether a Reference instance requires special treatment: If
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * the next field is null then the instance is active; if it is non-null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * then the collector should treat the instance normally.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *
10895
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents: 5506
diff changeset
    85
     * To ensure that a concurrent collector can discover active Reference
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * objects without interfering with application threads that may apply
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * the enqueue() method to those objects, collectors should link
10895
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents: 5506
diff changeset
    88
     * discovered objects through the discovered field. The discovered
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents: 5506
diff changeset
    89
     * field is also used for linking Reference objects in the pending list.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private T referent;         /* Treated specially by GC */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
18815
5da35ed47cfa 8014890: (ref) Reference queues may return more entries than expected
mchung
parents: 17716
diff changeset
    94
    volatile ReferenceQueue<? super T> queue;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
10895
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents: 5506
diff changeset
    96
    /* When active:   NULL
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents: 5506
diff changeset
    97
     *     pending:   this
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents: 5506
diff changeset
    98
     *    Enqueued:   next reference in queue (or this if last)
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents: 5506
diff changeset
    99
     *    Inactive:   this
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents: 5506
diff changeset
   100
     */
19053
69648476a89e 8021429: Fix lint warnings in java.lang.ref
darcy
parents: 18815
diff changeset
   101
    @SuppressWarnings("rawtypes")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    Reference next;
10895
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents: 5506
diff changeset
   103
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents: 5506
diff changeset
   104
    /* When active:   next element in a discovered reference list maintained by GC (or this if last)
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents: 5506
diff changeset
   105
     *     pending:   next element in the pending list (or null if last)
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents: 5506
diff changeset
   106
     *   otherwise:   NULL
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents: 5506
diff changeset
   107
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    transient private Reference<T> discovered;  /* used by VM */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /* Object used to synchronize with the garbage collector.  The collector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * must acquire this lock at the beginning of each collection cycle.  It is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * therefore critical that any code holding this lock complete as quickly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * as possible, allocate no new objects, and avoid calling user code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
22615
e929c13f8d11 8022321: java/lang/ref/OOMEInReferenceHandler.java fails intermittently
plevart
parents: 19053
diff changeset
   116
    static private class Lock { }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    private static Lock lock = new Lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /* List of References waiting to be enqueued.  The collector adds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * References to this list, while the Reference-handler thread removes
10895
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents: 5506
diff changeset
   122
     * them.  This list is protected by the above lock object. The
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents: 5506
diff changeset
   123
     * list uses the discovered field to link its elements.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
19053
69648476a89e 8021429: Fix lint warnings in java.lang.ref
darcy
parents: 18815
diff changeset
   125
    private static Reference<Object> pending = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /* High-priority thread to enqueue pending References
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    private static class ReferenceHandler extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
22615
e929c13f8d11 8022321: java/lang/ref/OOMEInReferenceHandler.java fails intermittently
plevart
parents: 19053
diff changeset
   131
        private static void ensureClassInitialized(Class<?> clazz) {
e929c13f8d11 8022321: java/lang/ref/OOMEInReferenceHandler.java fails intermittently
plevart
parents: 19053
diff changeset
   132
            try {
e929c13f8d11 8022321: java/lang/ref/OOMEInReferenceHandler.java fails intermittently
plevart
parents: 19053
diff changeset
   133
                Class.forName(clazz.getName(), true, clazz.getClassLoader());
e929c13f8d11 8022321: java/lang/ref/OOMEInReferenceHandler.java fails intermittently
plevart
parents: 19053
diff changeset
   134
            } catch (ClassNotFoundException e) {
e929c13f8d11 8022321: java/lang/ref/OOMEInReferenceHandler.java fails intermittently
plevart
parents: 19053
diff changeset
   135
                throw (Error) new NoClassDefFoundError(e.getMessage()).initCause(e);
e929c13f8d11 8022321: java/lang/ref/OOMEInReferenceHandler.java fails intermittently
plevart
parents: 19053
diff changeset
   136
            }
e929c13f8d11 8022321: java/lang/ref/OOMEInReferenceHandler.java fails intermittently
plevart
parents: 19053
diff changeset
   137
        }
e929c13f8d11 8022321: java/lang/ref/OOMEInReferenceHandler.java fails intermittently
plevart
parents: 19053
diff changeset
   138
e929c13f8d11 8022321: java/lang/ref/OOMEInReferenceHandler.java fails intermittently
plevart
parents: 19053
diff changeset
   139
        static {
e929c13f8d11 8022321: java/lang/ref/OOMEInReferenceHandler.java fails intermittently
plevart
parents: 19053
diff changeset
   140
            // pre-load and initialize InterruptedException and Cleaner classes
e929c13f8d11 8022321: java/lang/ref/OOMEInReferenceHandler.java fails intermittently
plevart
parents: 19053
diff changeset
   141
            // so that we don't get into trouble later in the run loop if there's
e929c13f8d11 8022321: java/lang/ref/OOMEInReferenceHandler.java fails intermittently
plevart
parents: 19053
diff changeset
   142
            // memory shortage while loading/initializing them lazily.
e929c13f8d11 8022321: java/lang/ref/OOMEInReferenceHandler.java fails intermittently
plevart
parents: 19053
diff changeset
   143
            ensureClassInitialized(InterruptedException.class);
e929c13f8d11 8022321: java/lang/ref/OOMEInReferenceHandler.java fails intermittently
plevart
parents: 19053
diff changeset
   144
            ensureClassInitialized(Cleaner.class);
e929c13f8d11 8022321: java/lang/ref/OOMEInReferenceHandler.java fails intermittently
plevart
parents: 19053
diff changeset
   145
        }
e929c13f8d11 8022321: java/lang/ref/OOMEInReferenceHandler.java fails intermittently
plevart
parents: 19053
diff changeset
   146
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        ReferenceHandler(ThreadGroup g, String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            super(g, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        public void run() {
23009
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   152
            while (true) {
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   153
                tryHandlePending(true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
23009
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   158
    /**
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   159
     * Try handle pending {@link Reference} if there is one.<p>
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   160
     * Return {@code true} as a hint that there might be another
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   161
     * {@link Reference} pending or {@code false} when there are no more pending
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   162
     * {@link Reference}s at the moment and the program can do some other
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   163
     * useful work instead of looping.
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   164
     *
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   165
     * @param waitForNotify if {@code true} and there was no pending
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   166
     *                      {@link Reference}, wait until notified from VM
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   167
     *                      or interrupted; if {@code false}, return immediately
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   168
     *                      when there is no pending {@link Reference}.
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   169
     * @return {@code true} if there was a {@link Reference} pending and it
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   170
     *         was processed, or we waited for notification and either got it
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   171
     *         or thread was interrupted before being notified;
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   172
     *         {@code false} otherwise.
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   173
     */
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   174
    static boolean tryHandlePending(boolean waitForNotify) {
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   175
        Reference<Object> r;
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   176
        Cleaner c;
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   177
        try {
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   178
            synchronized (lock) {
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   179
                if (pending != null) {
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   180
                    r = pending;
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   181
                    // 'instanceof' might throw OutOfMemoryError sometimes
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   182
                    // so do this before un-linking 'r' from the 'pending' chain...
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   183
                    c = r instanceof Cleaner ? (Cleaner) r : null;
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   184
                    // unlink 'r' from 'pending' chain
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   185
                    pending = r.discovered;
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   186
                    r.discovered = null;
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   187
                } else {
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   188
                    // The waiting on the lock may cause an OutOfMemoryError
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   189
                    // because it may try to allocate exception objects.
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   190
                    if (waitForNotify) {
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   191
                        lock.wait();
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   192
                    }
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   193
                    // retry if waited
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   194
                    return waitForNotify;
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   195
                }
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   196
            }
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   197
        } catch (OutOfMemoryError x) {
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   198
            // Give other threads CPU time so they hopefully drop some live references
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   199
            // and GC reclaims some space.
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   200
            // Also prevent CPU intensive spinning in case 'r instanceof Cleaner' above
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   201
            // persistently throws OOME for some time...
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   202
            Thread.yield();
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   203
            // retry
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   204
            return true;
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   205
        } catch (InterruptedException x) {
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   206
            // retry
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   207
            return true;
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   208
        }
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   209
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   210
        // Fast path for cleaners
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   211
        if (c != null) {
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   212
            c.clean();
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   213
            return true;
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   214
        }
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   215
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   216
        ReferenceQueue<? super Object> q = r.queue;
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   217
        if (q != ReferenceQueue.NULL) q.enqueue(r);
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   218
        return true;
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   219
    }
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   220
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        ThreadGroup tg = Thread.currentThread().getThreadGroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        for (ThreadGroup tgn = tg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
             tgn != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
             tg = tgn, tgn = tg.getParent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        Thread handler = new ReferenceHandler(tg, "Reference Handler");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        /* If there were a special system-only priority greater than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
         * MAX_PRIORITY, it would be used here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        handler.setPriority(Thread.MAX_PRIORITY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        handler.setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        handler.start();
23009
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   233
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   234
        // provide access in SharedSecrets
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   235
        SharedSecrets.setJavaLangRefAccess(new JavaLangRefAccess() {
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   236
            @Override
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   237
            public boolean tryHandlePendingReference() {
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   238
                return tryHandlePending(false);
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   239
            }
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   240
        });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    /* -- Referent accessor and setters -- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * Returns this reference object's referent.  If this reference object has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * been cleared, either by the program or by the garbage collector, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * this method returns <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @return   The object to which this reference refers, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     *           <code>null</code> if this reference object has been cleared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    public T get() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        return this.referent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * Clears this reference object.  Invoking this method will not cause this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * object to be enqueued.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * <p> This method is invoked only by Java code; when the garbage collector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * clears references it does so directly, without invoking this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        this.referent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    /* -- Queue operations -- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * Tells whether or not this reference object has been enqueued, either by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * the program or by the garbage collector.  If this reference object was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * not registered with a queue when it was created, then this method will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * always return <code>false</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * @return   <code>true</code> if and only if this reference object has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     *           been enqueued
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    public boolean isEnqueued() {
18815
5da35ed47cfa 8014890: (ref) Reference queues may return more entries than expected
mchung
parents: 17716
diff changeset
   281
        return (this.queue == ReferenceQueue.ENQUEUED);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * Adds this reference object to the queue with which it is registered,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * if any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * <p> This method is invoked only by Java code; when the garbage collector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * enqueues references it does so directly, without invoking this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * @return   <code>true</code> if this reference object was successfully
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     *           enqueued; <code>false</code> if it was already enqueued or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *           it was not registered with a queue when it was created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    public boolean enqueue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        return this.queue.enqueue(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    /* -- Constructors -- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    Reference(T referent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        this(referent, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    Reference(T referent, ReferenceQueue<? super T> queue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        this.referent = referent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        this.queue = (queue == null) ? ReferenceQueue.NULL : queue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
}