src/java.base/share/classes/java/lang/ref/Reference.java
author mchung
Tue, 20 Feb 2018 11:28:32 -0800
changeset 48922 906025796009
parent 48360 2731c0ee46a9
child 49210 7c795d301dbf
permissions -rw-r--r--
8198441: Replace native Runtime::runFinalization0 method with shared secrets Reviewed-by: martin
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
48922
906025796009 8198441: Replace native Runtime::runFinalization0 method with shared secrets
mchung
parents: 48360
diff changeset
     2
 * Copyright (c) 1997, 2018, 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
35254
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
    28
import jdk.internal.vm.annotation.DontInline;
32834
e1dca5fe4de3 8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents: 32649
diff changeset
    29
import jdk.internal.HotSpotIntrinsicCandidate;
e1dca5fe4de3 8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents: 32649
diff changeset
    30
import jdk.internal.misc.JavaLangRefAccess;
e1dca5fe4de3 8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents: 32649
diff changeset
    31
import jdk.internal.misc.SharedSecrets;
35641
da165fd9c886 8148117: Move sun.misc.Cleaner to jdk.internal.ref
chegar
parents: 35257
diff changeset
    32
import jdk.internal.ref.Cleaner;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * Abstract base class for reference objects.  This class defines the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * operations common to all reference objects.  Because reference objects are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * implemented in close cooperation with the garbage collector, this class may
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * not be subclassed directly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @author   Mark Reinhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @since    1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
public abstract class Reference<T> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    /* A Reference instance is in one of four possible internal states:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     *     Active: Subject to special treatment by the garbage collector.  Some
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     *     time after the collector detects that the reachability of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     *     referent has changed to the appropriate state, it changes the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     *     instance's state to either Pending or Inactive, depending upon
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     *     whether or not the instance was registered with a queue when it was
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     *     created.  In the former case it also adds the instance to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     *     pending-Reference list.  Newly-created instances are Active.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     *     Pending: An element of the pending-Reference list, waiting to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     *     enqueued by the Reference-handler thread.  Unregistered instances
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     *     are never in this state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     *     Enqueued: An element of the queue with which the instance was
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     *     registered when it was created.  When an instance is removed from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     *     its ReferenceQueue, it is made Inactive.  Unregistered instances are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     *     never in this state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     *     Inactive: Nothing more to do.  Once an instance becomes Inactive its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     *     state will never change again.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * The state is encoded in the queue and next fields as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     *     Active: queue = ReferenceQueue with which instance is registered, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     *     ReferenceQueue.NULL if it was not registered with a queue; next =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     *     null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     *     Pending: queue = ReferenceQueue with which instance is registered;
10895
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents: 5506
diff changeset
    75
     *     next = this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     *     Enqueued: queue = ReferenceQueue.ENQUEUED; next = Following instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     *     in queue, or this if at end of list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *     Inactive: queue = ReferenceQueue.NULL; next = this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * With this scheme the collector need only examine the next field in order
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * to determine whether a Reference instance requires special treatment: If
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * the next field is null then the instance is active; if it is non-null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * then the collector should treat the instance normally.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *
10895
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents: 5506
diff changeset
    87
     * To ensure that a concurrent collector can discover active Reference
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * objects without interfering with application threads that may apply
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * the enqueue() method to those objects, collectors should link
10895
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents: 5506
diff changeset
    90
     * discovered objects through the discovered field. The discovered
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents: 5506
diff changeset
    91
     * field is also used for linking Reference objects in the pending list.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private T referent;         /* Treated specially by GC */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
18815
5da35ed47cfa 8014890: (ref) Reference queues may return more entries than expected
mchung
parents: 17716
diff changeset
    96
    volatile ReferenceQueue<? super T> queue;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
10895
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents: 5506
diff changeset
    98
    /* When active:   NULL
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents: 5506
diff changeset
    99
     *     pending:   this
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents: 5506
diff changeset
   100
     *    Enqueued:   next reference in queue (or this if last)
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents: 5506
diff changeset
   101
     *    Inactive:   this
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents: 5506
diff changeset
   102
     */
19053
69648476a89e 8021429: Fix lint warnings in java.lang.ref
darcy
parents: 18815
diff changeset
   103
    @SuppressWarnings("rawtypes")
32100
8a0db19f4e84 8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents: 31671
diff changeset
   104
    volatile Reference next;
10895
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents: 5506
diff changeset
   105
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents: 5506
diff changeset
   106
    /* 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
   107
     *     pending:   next element in the pending list (or null if last)
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents: 5506
diff changeset
   108
     *   otherwise:   NULL
7434064aba55 4243978: (ref) Race condition in Reference.enqueue()
ysr
parents: 5506
diff changeset
   109
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32100
diff changeset
   110
    private transient Reference<T> discovered;  /* used by VM */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /* High-priority thread to enqueue pending References
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
34716
7477a052aecc 8056152: API to create Threads that do not inherit inheritable thread-local initial values
chegar
parents: 32834
diff changeset
   115
    private static class ReferenceHandler extends Thread {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
22615
e929c13f8d11 8022321: java/lang/ref/OOMEInReferenceHandler.java fails intermittently
plevart
parents: 19053
diff changeset
   117
        private static void ensureClassInitialized(Class<?> clazz) {
e929c13f8d11 8022321: java/lang/ref/OOMEInReferenceHandler.java fails intermittently
plevart
parents: 19053
diff changeset
   118
            try {
e929c13f8d11 8022321: java/lang/ref/OOMEInReferenceHandler.java fails intermittently
plevart
parents: 19053
diff changeset
   119
                Class.forName(clazz.getName(), true, clazz.getClassLoader());
e929c13f8d11 8022321: java/lang/ref/OOMEInReferenceHandler.java fails intermittently
plevart
parents: 19053
diff changeset
   120
            } catch (ClassNotFoundException e) {
e929c13f8d11 8022321: java/lang/ref/OOMEInReferenceHandler.java fails intermittently
plevart
parents: 19053
diff changeset
   121
                throw (Error) new NoClassDefFoundError(e.getMessage()).initCause(e);
e929c13f8d11 8022321: java/lang/ref/OOMEInReferenceHandler.java fails intermittently
plevart
parents: 19053
diff changeset
   122
            }
e929c13f8d11 8022321: java/lang/ref/OOMEInReferenceHandler.java fails intermittently
plevart
parents: 19053
diff changeset
   123
        }
e929c13f8d11 8022321: java/lang/ref/OOMEInReferenceHandler.java fails intermittently
plevart
parents: 19053
diff changeset
   124
e929c13f8d11 8022321: java/lang/ref/OOMEInReferenceHandler.java fails intermittently
plevart
parents: 19053
diff changeset
   125
        static {
40954
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   126
            // pre-load and initialize Cleaner class so that we don't
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   127
            // get into trouble later in the run loop if there's
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   128
            // memory shortage while loading/initializing it lazily.
22615
e929c13f8d11 8022321: java/lang/ref/OOMEInReferenceHandler.java fails intermittently
plevart
parents: 19053
diff changeset
   129
            ensureClassInitialized(Cleaner.class);
e929c13f8d11 8022321: java/lang/ref/OOMEInReferenceHandler.java fails intermittently
plevart
parents: 19053
diff changeset
   130
        }
e929c13f8d11 8022321: java/lang/ref/OOMEInReferenceHandler.java fails intermittently
plevart
parents: 19053
diff changeset
   131
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        ReferenceHandler(ThreadGroup g, String name) {
34716
7477a052aecc 8056152: API to create Threads that do not inherit inheritable thread-local initial values
chegar
parents: 32834
diff changeset
   133
            super(g, null, name, 0, false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        public void run() {
23009
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   137
            while (true) {
40954
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   138
                processPendingReferences();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
44119
e813e246ef17 8175797: (ref) Reference::enqueue method should clear referent before enqueuing
mchung
parents: 40954
diff changeset
   143
    /*
e813e246ef17 8175797: (ref) Reference::enqueue method should clear referent before enqueuing
mchung
parents: 40954
diff changeset
   144
     * Atomically get and clear (set to null) the VM's pending list.
40954
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   145
     */
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   146
    private static native Reference<Object> getAndClearReferencePendingList();
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   147
44119
e813e246ef17 8175797: (ref) Reference::enqueue method should clear referent before enqueuing
mchung
parents: 40954
diff changeset
   148
    /*
e813e246ef17 8175797: (ref) Reference::enqueue method should clear referent before enqueuing
mchung
parents: 40954
diff changeset
   149
     * Test whether the VM's pending list contains any entries.
40954
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   150
     */
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   151
    private static native boolean hasReferencePendingList();
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   152
44119
e813e246ef17 8175797: (ref) Reference::enqueue method should clear referent before enqueuing
mchung
parents: 40954
diff changeset
   153
    /*
e813e246ef17 8175797: (ref) Reference::enqueue method should clear referent before enqueuing
mchung
parents: 40954
diff changeset
   154
     * Wait until the VM's pending list may be non-null.
23009
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   155
     */
40954
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   156
    private static native void waitForReferencePendingList();
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   157
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   158
    private static final Object processPendingLock = new Object();
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   159
    private static boolean processPendingActive = false;
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   160
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   161
    private static void processPendingReferences() {
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   162
        // Only the singleton reference processing thread calls
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   163
        // waitForReferencePendingList() and getAndClearReferencePendingList().
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   164
        // These are separate operations to avoid a race with other threads
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   165
        // that are calling waitForReferenceProcessing().
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   166
        waitForReferencePendingList();
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   167
        Reference<Object> pendingList;
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   168
        synchronized (processPendingLock) {
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   169
            pendingList = getAndClearReferencePendingList();
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   170
            processPendingActive = true;
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   171
        }
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   172
        while (pendingList != null) {
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   173
            Reference<Object> ref = pendingList;
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   174
            pendingList = ref.discovered;
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   175
            ref.discovered = null;
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   176
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   177
            if (ref instanceof Cleaner) {
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   178
                ((Cleaner)ref).clean();
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   179
                // Notify any waiters that progress has been made.
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   180
                // This improves latency for nio.Bits waiters, which
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   181
                // are the only important ones.
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   182
                synchronized (processPendingLock) {
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   183
                    processPendingLock.notifyAll();
23009
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   184
                }
40954
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   185
            } else {
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   186
                ReferenceQueue<? super Object> q = ref.queue;
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   187
                if (q != ReferenceQueue.NULL) q.enqueue(ref);
23009
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   188
            }
40954
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   189
        }
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   190
        // Notify any waiters of completion of current round.
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   191
        synchronized (processPendingLock) {
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   192
            processPendingActive = false;
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   193
            processPendingLock.notifyAll();
23009
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   194
        }
40954
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   195
    }
23009
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   196
40954
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   197
    // Wait for progress in reference processing.
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   198
    //
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   199
    // Returns true after waiting (for notification from the reference
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   200
    // processing thread) if either (1) the VM has any pending
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   201
    // references, or (2) the reference processing thread is
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   202
    // processing references. Otherwise, returns false immediately.
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   203
    private static boolean waitForReferenceProcessing()
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   204
        throws InterruptedException
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   205
    {
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   206
        synchronized (processPendingLock) {
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   207
            if (processPendingActive || hasReferencePendingList()) {
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   208
                // Wait for progress, not necessarily completion.
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   209
                processPendingLock.wait();
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   210
                return true;
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   211
            } else {
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   212
                return false;
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   213
            }
23009
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
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        ThreadGroup tg = Thread.currentThread().getThreadGroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        for (ThreadGroup tgn = tg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
             tgn != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
             tg = tgn, tgn = tg.getParent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        Thread handler = new ReferenceHandler(tg, "Reference Handler");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        /* If there were a special system-only priority greater than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
         * MAX_PRIORITY, it would be used here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        handler.setPriority(Thread.MAX_PRIORITY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        handler.setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        handler.start();
23009
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   229
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   230
        // provide access in SharedSecrets
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   231
        SharedSecrets.setJavaLangRefAccess(new JavaLangRefAccess() {
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   232
            @Override
40954
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   233
            public boolean waitForReferenceProcessing()
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   234
                throws InterruptedException
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   235
            {
72ca7f63532a 8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents: 35641
diff changeset
   236
                return Reference.waitForReferenceProcessing();
23009
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   237
            }
48922
906025796009 8198441: Replace native Runtime::runFinalization0 method with shared secrets
mchung
parents: 48360
diff changeset
   238
906025796009 8198441: Replace native Runtime::runFinalization0 method with shared secrets
mchung
parents: 48360
diff changeset
   239
            @Override
906025796009 8198441: Replace native Runtime::runFinalization0 method with shared secrets
mchung
parents: 48360
diff changeset
   240
            public void runFinalization() {
906025796009 8198441: Replace native Runtime::runFinalization0 method with shared secrets
mchung
parents: 48360
diff changeset
   241
                Finalizer.runFinalization();
906025796009 8198441: Replace native Runtime::runFinalization0 method with shared secrets
mchung
parents: 48360
diff changeset
   242
            }
23009
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 22615
diff changeset
   243
        });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    /* -- Referent accessor and setters -- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * Returns this reference object's referent.  If this reference object has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * been cleared, either by the program or by the garbage collector, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * this method returns <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * @return   The object to which this reference refers, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     *           <code>null</code> if this reference object has been cleared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     */
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 29919
diff changeset
   256
    @HotSpotIntrinsicCandidate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    public T get() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        return this.referent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * Clears this reference object.  Invoking this method will not cause this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * object to be enqueued.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * <p> This method is invoked only by Java code; when the garbage collector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * clears references it does so directly, without invoking this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        this.referent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    /* -- Queue operations -- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * Tells whether or not this reference object has been enqueued, either by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * the program or by the garbage collector.  If this reference object was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * not registered with a queue when it was created, then this method will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * always return <code>false</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @return   <code>true</code> if and only if this reference object has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     *           been enqueued
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    public boolean isEnqueued() {
18815
5da35ed47cfa 8014890: (ref) Reference queues may return more entries than expected
mchung
parents: 17716
diff changeset
   284
        return (this.queue == ReferenceQueue.ENQUEUED);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    /**
44119
e813e246ef17 8175797: (ref) Reference::enqueue method should clear referent before enqueuing
mchung
parents: 40954
diff changeset
   288
     * Clears this reference object and adds it to the queue with which
e813e246ef17 8175797: (ref) Reference::enqueue method should clear referent before enqueuing
mchung
parents: 40954
diff changeset
   289
     * it is registered, if any.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * <p> This method is invoked only by Java code; when the garbage collector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * enqueues references it does so directly, without invoking this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @return   <code>true</code> if this reference object was successfully
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *           enqueued; <code>false</code> if it was already enqueued or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *           it was not registered with a queue when it was created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    public boolean enqueue() {
48360
2731c0ee46a9 8193780: (ref) Remove the undocumented "jdk.lang.ref.disableClearBeforeEnqueue" system property
mchung
parents: 47216
diff changeset
   299
        this.referent = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        return this.queue.enqueue(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    /* -- Constructors -- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    Reference(T referent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        this(referent, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    Reference(T referent, ReferenceQueue<? super T> queue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        this.referent = referent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        this.queue = (queue == null) ? ReferenceQueue.NULL : queue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
35254
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   314
    /**
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   315
     * Ensures that the object referenced by the given reference remains
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   316
     * <a href="package-summary.html#reachability"><em>strongly reachable</em></a>,
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   317
     * regardless of any prior actions of the program that might otherwise cause
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   318
     * the object to become unreachable; thus, the referenced object is not
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   319
     * reclaimable by garbage collection at least until after the invocation of
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   320
     * this method.  Invocation of this method does not itself initiate garbage
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   321
     * collection or finalization.
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   322
     *
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   323
     * <p> This method establishes an ordering for
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   324
     * <a href="package-summary.html#reachability"><em>strong reachability</em></a>
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   325
     * with respect to garbage collection.  It controls relations that are
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   326
     * otherwise only implicit in a program -- the reachability conditions
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   327
     * triggering garbage collection.  This method is designed for use in
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   328
     * uncommon situations of premature finalization where using
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   329
     * {@code synchronized} blocks or methods, or using other synchronization
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   330
     * facilities are not possible or do not provide the desired control.  This
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   331
     * method is applicable only when reclamation may have visible effects,
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   332
     * which is possible for objects with finalizers (See
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   333
     * <a href="https://docs.oracle.com/javase/specs/jls/se8/html/jls-12.html#jls-12.6">
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   334
     * Section 12.6 17 of <cite>The Java&trade; Language Specification</cite></a>)
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   335
     * that are implemented in ways that rely on ordering control for correctness.
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   336
     *
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   337
     * @apiNote
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   338
     * Finalization may occur whenever the virtual machine detects that no
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   339
     * reference to an object will ever be stored in the heap: The garbage
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   340
     * collector may reclaim an object even if the fields of that object are
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   341
     * still in use, so long as the object has otherwise become unreachable.
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   342
     * This may have surprising and undesirable effects in cases such as the
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   343
     * following example in which the bookkeeping associated with a class is
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   344
     * managed through array indices.  Here, method {@code action} uses a
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   345
     * {@code reachabilityFence} to ensure that the {@code Resource} object is
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   346
     * not reclaimed before bookkeeping on an associated
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   347
     * {@code ExternalResource} has been performed; in particular here, to
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   348
     * ensure that the array slot holding the {@code ExternalResource} is not
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   349
     * nulled out in method {@link Object#finalize}, which may otherwise run
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   350
     * concurrently.
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   351
     *
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   352
     * <pre> {@code
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   353
     * class Resource {
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   354
     *   private static ExternalResource[] externalResourceArray = ...
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   355
     *
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   356
     *   int myIndex;
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   357
     *   Resource(...) {
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   358
     *     myIndex = ...
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   359
     *     externalResourceArray[myIndex] = ...;
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   360
     *     ...
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   361
     *   }
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   362
     *   protected void finalize() {
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   363
     *     externalResourceArray[myIndex] = null;
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   364
     *     ...
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   365
     *   }
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   366
     *   public void action() {
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   367
     *     try {
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   368
     *       // ...
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   369
     *       int i = myIndex;
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   370
     *       Resource.update(externalResourceArray[i]);
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   371
     *     } finally {
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   372
     *       Reference.reachabilityFence(this);
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   373
     *     }
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   374
     *   }
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   375
     *   private static void update(ExternalResource ext) {
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   376
     *     ext.status = ...;
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   377
     *   }
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   378
     * }}</pre>
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   379
     *
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   380
     * Here, the invocation of {@code reachabilityFence} is nonintuitively
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   381
     * placed <em>after</em> the call to {@code update}, to ensure that the
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   382
     * array slot is not nulled out by {@link Object#finalize} before the
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   383
     * update, even if the call to {@code action} was the last use of this
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   384
     * object.  This might be the case if, for example a usage in a user program
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   385
     * had the form {@code new Resource().action();} which retains no other
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   386
     * reference to this {@code Resource}.  While probably overkill here,
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   387
     * {@code reachabilityFence} is placed in a {@code finally} block to ensure
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   388
     * that it is invoked across all paths in the method.  In a method with more
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   389
     * complex control paths, you might need further precautions to ensure that
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   390
     * {@code reachabilityFence} is encountered along all of them.
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   391
     *
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   392
     * <p> It is sometimes possible to better encapsulate use of
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   393
     * {@code reachabilityFence}.  Continuing the above example, if it were
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   394
     * acceptable for the call to method {@code update} to proceed even if the
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   395
     * finalizer had already executed (nulling out slot), then you could
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   396
     * localize use of {@code reachabilityFence}:
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   397
     *
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   398
     * <pre> {@code
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   399
     * public void action2() {
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   400
     *   // ...
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   401
     *   Resource.update(getExternalResource());
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   402
     * }
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   403
     * private ExternalResource getExternalResource() {
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   404
     *   ExternalResource ext = externalResourceArray[myIndex];
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   405
     *   Reference.reachabilityFence(this);
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   406
     *   return ext;
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   407
     * }}</pre>
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   408
     *
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   409
     * <p> Method {@code reachabilityFence} is not required in constructions
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   410
     * that themselves ensure reachability.  For example, because objects that
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   411
     * are locked cannot, in general, be reclaimed, it would suffice if all
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   412
     * accesses of the object, in all methods of class {@code Resource}
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   413
     * (including {@code finalize}) were enclosed in {@code synchronized (this)}
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   414
     * blocks.  (Further, such blocks must not include infinite loops, or
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   415
     * themselves be unreachable, which fall into the corner case exceptions to
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   416
     * the "in general" disclaimer.)  However, method {@code reachabilityFence}
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   417
     * remains a better option in cases where this approach is not as efficient,
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   418
     * desirable, or possible; for example because it would encounter deadlock.
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   419
     *
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   420
     * @param ref the reference. If {@code null}, this method has no effect.
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   421
     * @since 9
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   422
     */
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   423
    @DontInline
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   424
    public static void reachabilityFence(Object ref) {
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   425
        // Does nothing, because this method is annotated with @DontInline
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   426
        // HotSpot needs to retain the ref and not GC it before a call to this
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   427
        // method
ba6a26f39b86 8133348: Reference.reachabilityFence
psandoz
parents: 32834
diff changeset
   428
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
}