author | chegar |
Thu, 17 Oct 2019 20:54:25 +0100 | |
branch | datagramsocketimpl-branch |
changeset 58679 | 9c3209ff7550 |
parent 58678 | 9cf78a70fa4f |
parent 58288 | 48e480e56aad |
permissions | -rw-r--r-- |
2 | 1 |
/* |
55351
8038250f9939
8225675: Outdated citation of JLS in java.lang.ref.Reference
darcy
parents:
52510
diff
changeset
|
2 |
* Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.lang.ref; |
|
27 |
||
49210
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48922
diff
changeset
|
28 |
import jdk.internal.vm.annotation.ForceInline; |
32834
e1dca5fe4de3
8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents:
32649
diff
changeset
|
29 |
import jdk.internal.HotSpotIntrinsicCandidate; |
52427
3c6aa484536c
8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents:
50277
diff
changeset
|
30 |
import jdk.internal.access.JavaLangRefAccess; |
3c6aa484536c
8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents:
50277
diff
changeset
|
31 |
import jdk.internal.access.SharedSecrets; |
35641
da165fd9c886
8148117: Move sun.misc.Cleaner to jdk.internal.ref
chegar
parents:
35257
diff
changeset
|
32 |
import jdk.internal.ref.Cleaner; |
2 | 33 |
|
34 |
/** |
|
35 |
* Abstract base class for reference objects. This class defines the |
|
36 |
* operations common to all reference objects. Because reference objects are |
|
37 |
* implemented in close cooperation with the garbage collector, this class may |
|
38 |
* not be subclassed directly. |
|
39 |
* |
|
40 |
* @author Mark Reinhold |
|
41 |
* @since 1.2 |
|
42 |
*/ |
|
43 |
||
44 |
public abstract class Reference<T> { |
|
45 |
||
50277
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
46 |
/* The state of a Reference object is characterized by two attributes. It |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
47 |
* may be either "active", "pending", or "inactive". It may also be |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
48 |
* either "registered", "enqueued", "dequeued", or "unregistered". |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
49 |
* |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
50 |
* Active: Subject to special treatment by the garbage collector. Some |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
51 |
* time after the collector detects that the reachability of the |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
52 |
* referent has changed to the appropriate state, the collector |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
53 |
* "notifies" the reference, changing the state to either "pending" or |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
54 |
* "inactive". |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
55 |
* referent != null; discovered = null, or in GC discovered list. |
2 | 56 |
* |
50277
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
57 |
* Pending: An element of the pending-Reference list, waiting to be |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
58 |
* processed by the ReferenceHandler thread. The pending-Reference |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
59 |
* list is linked through the discovered fields of references in the |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
60 |
* list. |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
61 |
* referent = null; discovered = next element in pending-Reference list. |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
62 |
* |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
63 |
* Inactive: Neither Active nor Pending. |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
64 |
* referent = null. |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
65 |
* |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
66 |
* Registered: Associated with a queue when created, and not yet added |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
67 |
* to the queue. |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
68 |
* queue = the associated queue. |
2 | 69 |
* |
50277
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
70 |
* Enqueued: Added to the associated queue, and not yet removed. |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
71 |
* queue = ReferenceQueue.ENQUEUE; next = next entry in list, or this to |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
72 |
* indicate end of list. |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
73 |
* |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
74 |
* Dequeued: Added to the associated queue and then removed. |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
75 |
* queue = ReferenceQueue.NULL; next = this. |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
76 |
* |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
77 |
* Unregistered: Not associated with a queue when created. |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
78 |
* queue = ReferenceQueue.NULL. |
2 | 79 |
* |
50277
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
80 |
* The collector only needs to examine the referent field and the |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
81 |
* discovered field to determine whether a (non-FinalReference) Reference |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
82 |
* object needs special treatment. If the referent is non-null and not |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
83 |
* known to be live, then it may need to be discovered for possible later |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
84 |
* notification. But if the discovered field is non-null, then it has |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
85 |
* already been discovered. |
2 | 86 |
* |
50277
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
87 |
* FinalReference (which exists to support finalization) differs from |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
88 |
* other references, because a FinalReference is not cleared when |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
89 |
* notified. The referent being null or not cannot be used to distinguish |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
90 |
* between the active state and pending or inactive states. However, |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
91 |
* FinalReferences do not support enqueue(). Instead, the next field of a |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
92 |
* FinalReference object is set to "this" when it is added to the |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
93 |
* pending-Reference list. The use of "this" as the value of next in the |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
94 |
* enqueued and dequeued states maintains the non-active state. An |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
95 |
* additional check that the next field is null is required to determine |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
96 |
* that a FinalReference object is active. |
2 | 97 |
* |
50277
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
98 |
* Initial states: |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
99 |
* [active/registered] |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
100 |
* [active/unregistered] [1] |
2 | 101 |
* |
50277
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
102 |
* Transitions: |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
103 |
* clear |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
104 |
* [active/registered] -------> [inactive/registered] |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
105 |
* | | |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
106 |
* | | enqueue [2] |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
107 |
* | GC enqueue [2] | |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
108 |
* | -----------------| |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
109 |
* | | |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
110 |
* v | |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
111 |
* [pending/registered] --- v |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
112 |
* | | ReferenceHandler |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
113 |
* | enqueue [2] |---> [inactive/enqueued] |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
114 |
* v | | |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
115 |
* [pending/enqueued] --- | |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
116 |
* | | poll/remove |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
117 |
* | poll/remove | |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
118 |
* | | |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
119 |
* v ReferenceHandler v |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
120 |
* [pending/dequeued] ------> [inactive/dequeued] |
2 | 121 |
* |
122 |
* |
|
50277
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
123 |
* clear/enqueue/GC [3] |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
124 |
* [active/unregistered] ------ |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
125 |
* | | |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
126 |
* | GC | |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
127 |
* | |--> [inactive/unregistered] |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
128 |
* v | |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
129 |
* [pending/unregistered] ------ |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
130 |
* ReferenceHandler |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
131 |
* |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
132 |
* Terminal states: |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
133 |
* [inactive/dequeued] |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
134 |
* [inactive/unregistered] |
2 | 135 |
* |
50277
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
136 |
* Unreachable states (because enqueue also clears): |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
137 |
* [active/enqeued] |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
138 |
* [active/dequeued] |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
139 |
* |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
140 |
* [1] Unregistered is not permitted for FinalReferences. |
2 | 141 |
* |
50277
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
142 |
* [2] These transitions are not possible for FinalReferences, making |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
143 |
* [pending/enqueued] and [pending/dequeued] unreachable, and |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
144 |
* [inactive/registered] terminal. |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
145 |
* |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
146 |
* [3] The garbage collector may directly transition a Reference |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
147 |
* from [active/unregistered] to [inactive/unregistered], |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
148 |
* bypassing the pending-Reference list. |
2 | 149 |
*/ |
150 |
||
151 |
private T referent; /* Treated specially by GC */ |
|
152 |
||
50277
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
153 |
/* The queue this reference gets enqueued to by GC notification or by |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
154 |
* calling enqueue(). |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
155 |
* |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
156 |
* When registered: the queue with which this reference is registered. |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
157 |
* enqueued: ReferenceQueue.ENQUEUE |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
158 |
* dequeued: ReferenceQueue.NULL |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
159 |
* unregistered: ReferenceQueue.NULL |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
160 |
*/ |
18815
5da35ed47cfa
8014890: (ref) Reference queues may return more entries than expected
mchung
parents:
17716
diff
changeset
|
161 |
volatile ReferenceQueue<? super T> queue; |
2 | 162 |
|
50277
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
163 |
/* The link in a ReferenceQueue's list of Reference objects. |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
164 |
* |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
165 |
* When registered: null |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
166 |
* enqueued: next element in queue (or this if last) |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
167 |
* dequeued: this (marking FinalReferences as inactive) |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
168 |
* unregistered: null |
10895 | 169 |
*/ |
19053 | 170 |
@SuppressWarnings("rawtypes") |
32100
8a0db19f4e84
8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents:
31671
diff
changeset
|
171 |
volatile Reference next; |
10895 | 172 |
|
50277
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
173 |
/* Used by the garbage collector to accumulate Reference objects that need |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
174 |
* to be revisited in order to decide whether they should be notified. |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
175 |
* Also used as the link in the pending-Reference list. The discovered |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
176 |
* field and the next field are distinct to allow the enqueue() method to |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
177 |
* be applied to a Reference object while it is either in the |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
178 |
* pending-Reference list or in the garbage collector's discovered set. |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
179 |
* |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
180 |
* When active: null or next element in a discovered reference list |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
181 |
* maintained by the GC (or this if last) |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
182 |
* pending: next element in the pending-Reference list (null if last) |
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
183 |
* inactive: null |
10895 | 184 |
*/ |
50277
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
185 |
private transient Reference<T> discovered; |
2 | 186 |
|
187 |
||
188 |
/* High-priority thread to enqueue pending References |
|
189 |
*/ |
|
34716
7477a052aecc
8056152: API to create Threads that do not inherit inheritable thread-local initial values
chegar
parents:
32834
diff
changeset
|
190 |
private static class ReferenceHandler extends Thread { |
2 | 191 |
|
22615
e929c13f8d11
8022321: java/lang/ref/OOMEInReferenceHandler.java fails intermittently
plevart
parents:
19053
diff
changeset
|
192 |
private static void ensureClassInitialized(Class<?> clazz) { |
e929c13f8d11
8022321: java/lang/ref/OOMEInReferenceHandler.java fails intermittently
plevart
parents:
19053
diff
changeset
|
193 |
try { |
e929c13f8d11
8022321: java/lang/ref/OOMEInReferenceHandler.java fails intermittently
plevart
parents:
19053
diff
changeset
|
194 |
Class.forName(clazz.getName(), true, clazz.getClassLoader()); |
e929c13f8d11
8022321: java/lang/ref/OOMEInReferenceHandler.java fails intermittently
plevart
parents:
19053
diff
changeset
|
195 |
} catch (ClassNotFoundException e) { |
e929c13f8d11
8022321: java/lang/ref/OOMEInReferenceHandler.java fails intermittently
plevart
parents:
19053
diff
changeset
|
196 |
throw (Error) new NoClassDefFoundError(e.getMessage()).initCause(e); |
e929c13f8d11
8022321: java/lang/ref/OOMEInReferenceHandler.java fails intermittently
plevart
parents:
19053
diff
changeset
|
197 |
} |
e929c13f8d11
8022321: java/lang/ref/OOMEInReferenceHandler.java fails intermittently
plevart
parents:
19053
diff
changeset
|
198 |
} |
e929c13f8d11
8022321: java/lang/ref/OOMEInReferenceHandler.java fails intermittently
plevart
parents:
19053
diff
changeset
|
199 |
|
e929c13f8d11
8022321: java/lang/ref/OOMEInReferenceHandler.java fails intermittently
plevart
parents:
19053
diff
changeset
|
200 |
static { |
40954
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
35641
diff
changeset
|
201 |
// 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
|
202 |
// 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
|
203 |
// memory shortage while loading/initializing it lazily. |
22615
e929c13f8d11
8022321: java/lang/ref/OOMEInReferenceHandler.java fails intermittently
plevart
parents:
19053
diff
changeset
|
204 |
ensureClassInitialized(Cleaner.class); |
e929c13f8d11
8022321: java/lang/ref/OOMEInReferenceHandler.java fails intermittently
plevart
parents:
19053
diff
changeset
|
205 |
} |
e929c13f8d11
8022321: java/lang/ref/OOMEInReferenceHandler.java fails intermittently
plevart
parents:
19053
diff
changeset
|
206 |
|
2 | 207 |
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
|
208 |
super(g, null, name, 0, false); |
2 | 209 |
} |
210 |
||
211 |
public void run() { |
|
23009
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
22615
diff
changeset
|
212 |
while (true) { |
40954
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
35641
diff
changeset
|
213 |
processPendingReferences(); |
2 | 214 |
} |
215 |
} |
|
216 |
} |
|
217 |
||
44119
e813e246ef17
8175797: (ref) Reference::enqueue method should clear referent before enqueuing
mchung
parents:
40954
diff
changeset
|
218 |
/* |
50277
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
219 |
* Atomically get and clear (set to null) the VM's pending-Reference list. |
40954
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
35641
diff
changeset
|
220 |
*/ |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
35641
diff
changeset
|
221 |
private static native Reference<Object> getAndClearReferencePendingList(); |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
35641
diff
changeset
|
222 |
|
44119
e813e246ef17
8175797: (ref) Reference::enqueue method should clear referent before enqueuing
mchung
parents:
40954
diff
changeset
|
223 |
/* |
50277
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
224 |
* Test whether the VM's pending-Reference list contains any entries. |
40954
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
35641
diff
changeset
|
225 |
*/ |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
35641
diff
changeset
|
226 |
private static native boolean hasReferencePendingList(); |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
35641
diff
changeset
|
227 |
|
44119
e813e246ef17
8175797: (ref) Reference::enqueue method should clear referent before enqueuing
mchung
parents:
40954
diff
changeset
|
228 |
/* |
50277
f84ae8aa5d88
8203028: Simplify reference processing in light of JDK-8175797
kbarrett
parents:
49973
diff
changeset
|
229 |
* Wait until the VM's pending-Reference list may be non-null. |
23009
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
22615
diff
changeset
|
230 |
*/ |
40954
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
35641
diff
changeset
|
231 |
private static native void waitForReferencePendingList(); |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
35641
diff
changeset
|
232 |
|
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
35641
diff
changeset
|
233 |
private static final Object processPendingLock = new Object(); |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
35641
diff
changeset
|
234 |
private static boolean processPendingActive = false; |
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 |
private static void processPendingReferences() { |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
35641
diff
changeset
|
237 |
// Only the singleton reference processing thread calls |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
35641
diff
changeset
|
238 |
// waitForReferencePendingList() and getAndClearReferencePendingList(). |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
35641
diff
changeset
|
239 |
// 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
|
240 |
// that are calling waitForReferenceProcessing(). |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
35641
diff
changeset
|
241 |
waitForReferencePendingList(); |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
35641
diff
changeset
|
242 |
Reference<Object> pendingList; |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
35641
diff
changeset
|
243 |
synchronized (processPendingLock) { |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
35641
diff
changeset
|
244 |
pendingList = getAndClearReferencePendingList(); |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
35641
diff
changeset
|
245 |
processPendingActive = true; |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
35641
diff
changeset
|
246 |
} |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
35641
diff
changeset
|
247 |
while (pendingList != null) { |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
35641
diff
changeset
|
248 |
Reference<Object> ref = pendingList; |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
35641
diff
changeset
|
249 |
pendingList = ref.discovered; |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
35641
diff
changeset
|
250 |
ref.discovered = null; |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
35641
diff
changeset
|
251 |
|
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
35641
diff
changeset
|
252 |
if (ref instanceof Cleaner) { |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
35641
diff
changeset
|
253 |
((Cleaner)ref).clean(); |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
35641
diff
changeset
|
254 |
// Notify any waiters that progress has been made. |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
35641
diff
changeset
|
255 |
// This improves latency for nio.Bits waiters, which |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
35641
diff
changeset
|
256 |
// are the only important ones. |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
35641
diff
changeset
|
257 |
synchronized (processPendingLock) { |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
35641
diff
changeset
|
258 |
processPendingLock.notifyAll(); |
23009
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
22615
diff
changeset
|
259 |
} |
40954
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
35641
diff
changeset
|
260 |
} else { |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
35641
diff
changeset
|
261 |
ReferenceQueue<? super Object> q = ref.queue; |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
35641
diff
changeset
|
262 |
if (q != ReferenceQueue.NULL) q.enqueue(ref); |
23009
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
22615
diff
changeset
|
263 |
} |
40954
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
35641
diff
changeset
|
264 |
} |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
35641
diff
changeset
|
265 |
// Notify any waiters of completion of current round. |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
35641
diff
changeset
|
266 |
synchronized (processPendingLock) { |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
35641
diff
changeset
|
267 |
processPendingActive = false; |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
35641
diff
changeset
|
268 |
processPendingLock.notifyAll(); |
23009
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
22615
diff
changeset
|
269 |
} |
40954
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
35641
diff
changeset
|
270 |
} |
23009
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
22615
diff
changeset
|
271 |
|
40954
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
35641
diff
changeset
|
272 |
// Wait for progress in reference processing. |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
35641
diff
changeset
|
273 |
// |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
35641
diff
changeset
|
274 |
// 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
|
275 |
// 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
|
276 |
// references, or (2) the reference processing thread is |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
35641
diff
changeset
|
277 |
// processing references. Otherwise, returns false immediately. |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
35641
diff
changeset
|
278 |
private static boolean waitForReferenceProcessing() |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
35641
diff
changeset
|
279 |
throws InterruptedException |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
35641
diff
changeset
|
280 |
{ |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
35641
diff
changeset
|
281 |
synchronized (processPendingLock) { |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
35641
diff
changeset
|
282 |
if (processPendingActive || hasReferencePendingList()) { |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
35641
diff
changeset
|
283 |
// Wait for progress, not necessarily completion. |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
35641
diff
changeset
|
284 |
processPendingLock.wait(); |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
35641
diff
changeset
|
285 |
return true; |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
35641
diff
changeset
|
286 |
} else { |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
35641
diff
changeset
|
287 |
return false; |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
35641
diff
changeset
|
288 |
} |
23009
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
22615
diff
changeset
|
289 |
} |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
22615
diff
changeset
|
290 |
} |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
22615
diff
changeset
|
291 |
|
2 | 292 |
static { |
293 |
ThreadGroup tg = Thread.currentThread().getThreadGroup(); |
|
294 |
for (ThreadGroup tgn = tg; |
|
295 |
tgn != null; |
|
296 |
tg = tgn, tgn = tg.getParent()); |
|
297 |
Thread handler = new ReferenceHandler(tg, "Reference Handler"); |
|
298 |
/* If there were a special system-only priority greater than |
|
299 |
* MAX_PRIORITY, it would be used here |
|
300 |
*/ |
|
301 |
handler.setPriority(Thread.MAX_PRIORITY); |
|
302 |
handler.setDaemon(true); |
|
303 |
handler.start(); |
|
23009
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
22615
diff
changeset
|
304 |
|
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
22615
diff
changeset
|
305 |
// provide access in SharedSecrets |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
22615
diff
changeset
|
306 |
SharedSecrets.setJavaLangRefAccess(new JavaLangRefAccess() { |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
22615
diff
changeset
|
307 |
@Override |
40954
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
35641
diff
changeset
|
308 |
public boolean waitForReferenceProcessing() |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
35641
diff
changeset
|
309 |
throws InterruptedException |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
35641
diff
changeset
|
310 |
{ |
72ca7f63532a
8156500: Move Reference pending list into VM to prevent deadlocks
kbarrett
parents:
35641
diff
changeset
|
311 |
return Reference.waitForReferenceProcessing(); |
23009
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
22615
diff
changeset
|
312 |
} |
48922
906025796009
8198441: Replace native Runtime::runFinalization0 method with shared secrets
mchung
parents:
48360
diff
changeset
|
313 |
|
906025796009
8198441: Replace native Runtime::runFinalization0 method with shared secrets
mchung
parents:
48360
diff
changeset
|
314 |
@Override |
906025796009
8198441: Replace native Runtime::runFinalization0 method with shared secrets
mchung
parents:
48360
diff
changeset
|
315 |
public void runFinalization() { |
906025796009
8198441: Replace native Runtime::runFinalization0 method with shared secrets
mchung
parents:
48360
diff
changeset
|
316 |
Finalizer.runFinalization(); |
906025796009
8198441: Replace native Runtime::runFinalization0 method with shared secrets
mchung
parents:
48360
diff
changeset
|
317 |
} |
23009
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
22615
diff
changeset
|
318 |
}); |
2 | 319 |
} |
320 |
||
321 |
/* -- Referent accessor and setters -- */ |
|
322 |
||
323 |
/** |
|
324 |
* Returns this reference object's referent. If this reference object has |
|
325 |
* been cleared, either by the program or by the garbage collector, then |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
55351
diff
changeset
|
326 |
* this method returns {@code null}. |
2 | 327 |
* |
328 |
* @return The object to which this reference refers, or |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
55351
diff
changeset
|
329 |
* {@code null} if this reference object has been cleared |
2 | 330 |
*/ |
31671
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
29919
diff
changeset
|
331 |
@HotSpotIntrinsicCandidate |
2 | 332 |
public T get() { |
333 |
return this.referent; |
|
334 |
} |
|
335 |
||
336 |
/** |
|
337 |
* Clears this reference object. Invoking this method will not cause this |
|
338 |
* object to be enqueued. |
|
339 |
* |
|
340 |
* <p> This method is invoked only by Java code; when the garbage collector |
|
341 |
* clears references it does so directly, without invoking this method. |
|
342 |
*/ |
|
343 |
public void clear() { |
|
344 |
this.referent = null; |
|
345 |
} |
|
346 |
||
347 |
/* -- Queue operations -- */ |
|
348 |
||
349 |
/** |
|
350 |
* Tells whether or not this reference object has been enqueued, either by |
|
351 |
* the program or by the garbage collector. If this reference object was |
|
352 |
* not registered with a queue when it was created, then this method will |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
55351
diff
changeset
|
353 |
* always return {@code false}. |
2 | 354 |
* |
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
55351
diff
changeset
|
355 |
* @return {@code true} if and only if this reference object has |
2 | 356 |
* been enqueued |
357 |
*/ |
|
358 |
public boolean isEnqueued() { |
|
18815
5da35ed47cfa
8014890: (ref) Reference queues may return more entries than expected
mchung
parents:
17716
diff
changeset
|
359 |
return (this.queue == ReferenceQueue.ENQUEUED); |
2 | 360 |
} |
361 |
||
362 |
/** |
|
44119
e813e246ef17
8175797: (ref) Reference::enqueue method should clear referent before enqueuing
mchung
parents:
40954
diff
changeset
|
363 |
* 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
|
364 |
* it is registered, if any. |
2 | 365 |
* |
366 |
* <p> This method is invoked only by Java code; when the garbage collector |
|
367 |
* enqueues references it does so directly, without invoking this method. |
|
368 |
* |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
55351
diff
changeset
|
369 |
* @return {@code true} if this reference object was successfully |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
55351
diff
changeset
|
370 |
* enqueued; {@code false} if it was already enqueued or if |
2 | 371 |
* it was not registered with a queue when it was created |
372 |
*/ |
|
373 |
public boolean enqueue() { |
|
48360
2731c0ee46a9
8193780: (ref) Remove the undocumented "jdk.lang.ref.disableClearBeforeEnqueue" system property
mchung
parents:
47216
diff
changeset
|
374 |
this.referent = null; |
2 | 375 |
return this.queue.enqueue(this); |
376 |
} |
|
377 |
||
49973
e3653598e3b0
8201793: (ref) Reference object should not support cloning
mchung
parents:
49210
diff
changeset
|
378 |
/** |
e3653598e3b0
8201793: (ref) Reference object should not support cloning
mchung
parents:
49210
diff
changeset
|
379 |
* Throws {@link CloneNotSupportedException}. A {@code Reference} cannot be |
e3653598e3b0
8201793: (ref) Reference object should not support cloning
mchung
parents:
49210
diff
changeset
|
380 |
* meaningfully cloned. Construct a new {@code Reference} instead. |
e3653598e3b0
8201793: (ref) Reference object should not support cloning
mchung
parents:
49210
diff
changeset
|
381 |
* |
52510 | 382 |
* @return never returns normally |
49973
e3653598e3b0
8201793: (ref) Reference object should not support cloning
mchung
parents:
49210
diff
changeset
|
383 |
* @throws CloneNotSupportedException always |
e3653598e3b0
8201793: (ref) Reference object should not support cloning
mchung
parents:
49210
diff
changeset
|
384 |
* |
e3653598e3b0
8201793: (ref) Reference object should not support cloning
mchung
parents:
49210
diff
changeset
|
385 |
* @since 11 |
e3653598e3b0
8201793: (ref) Reference object should not support cloning
mchung
parents:
49210
diff
changeset
|
386 |
*/ |
e3653598e3b0
8201793: (ref) Reference object should not support cloning
mchung
parents:
49210
diff
changeset
|
387 |
@Override |
e3653598e3b0
8201793: (ref) Reference object should not support cloning
mchung
parents:
49210
diff
changeset
|
388 |
protected Object clone() throws CloneNotSupportedException { |
e3653598e3b0
8201793: (ref) Reference object should not support cloning
mchung
parents:
49210
diff
changeset
|
389 |
throw new CloneNotSupportedException(); |
e3653598e3b0
8201793: (ref) Reference object should not support cloning
mchung
parents:
49210
diff
changeset
|
390 |
} |
e3653598e3b0
8201793: (ref) Reference object should not support cloning
mchung
parents:
49210
diff
changeset
|
391 |
|
2 | 392 |
/* -- Constructors -- */ |
393 |
||
394 |
Reference(T referent) { |
|
395 |
this(referent, null); |
|
396 |
} |
|
397 |
||
398 |
Reference(T referent, ReferenceQueue<? super T> queue) { |
|
399 |
this.referent = referent; |
|
400 |
this.queue = (queue == null) ? ReferenceQueue.NULL : queue; |
|
401 |
} |
|
402 |
||
35254 | 403 |
/** |
404 |
* Ensures that the object referenced by the given reference remains |
|
405 |
* <a href="package-summary.html#reachability"><em>strongly reachable</em></a>, |
|
406 |
* regardless of any prior actions of the program that might otherwise cause |
|
407 |
* the object to become unreachable; thus, the referenced object is not |
|
408 |
* reclaimable by garbage collection at least until after the invocation of |
|
409 |
* this method. Invocation of this method does not itself initiate garbage |
|
410 |
* collection or finalization. |
|
411 |
* |
|
412 |
* <p> This method establishes an ordering for |
|
413 |
* <a href="package-summary.html#reachability"><em>strong reachability</em></a> |
|
414 |
* with respect to garbage collection. It controls relations that are |
|
415 |
* otherwise only implicit in a program -- the reachability conditions |
|
416 |
* triggering garbage collection. This method is designed for use in |
|
417 |
* uncommon situations of premature finalization where using |
|
418 |
* {@code synchronized} blocks or methods, or using other synchronization |
|
419 |
* facilities are not possible or do not provide the desired control. This |
|
420 |
* method is applicable only when reclamation may have visible effects, |
|
55351
8038250f9939
8225675: Outdated citation of JLS in java.lang.ref.Reference
darcy
parents:
52510
diff
changeset
|
421 |
* which is possible for objects with finalizers (See Section 12.6 |
8038250f9939
8225675: Outdated citation of JLS in java.lang.ref.Reference
darcy
parents:
52510
diff
changeset
|
422 |
* of <cite>The Java™ Language Specification</cite>) that |
8038250f9939
8225675: Outdated citation of JLS in java.lang.ref.Reference
darcy
parents:
52510
diff
changeset
|
423 |
* are implemented in ways that rely on ordering control for |
8038250f9939
8225675: Outdated citation of JLS in java.lang.ref.Reference
darcy
parents:
52510
diff
changeset
|
424 |
* correctness. |
35254 | 425 |
* |
426 |
* @apiNote |
|
427 |
* Finalization may occur whenever the virtual machine detects that no |
|
428 |
* reference to an object will ever be stored in the heap: The garbage |
|
429 |
* collector may reclaim an object even if the fields of that object are |
|
430 |
* still in use, so long as the object has otherwise become unreachable. |
|
431 |
* This may have surprising and undesirable effects in cases such as the |
|
432 |
* following example in which the bookkeeping associated with a class is |
|
433 |
* managed through array indices. Here, method {@code action} uses a |
|
434 |
* {@code reachabilityFence} to ensure that the {@code Resource} object is |
|
435 |
* not reclaimed before bookkeeping on an associated |
|
436 |
* {@code ExternalResource} has been performed; in particular here, to |
|
437 |
* ensure that the array slot holding the {@code ExternalResource} is not |
|
438 |
* nulled out in method {@link Object#finalize}, which may otherwise run |
|
439 |
* concurrently. |
|
440 |
* |
|
441 |
* <pre> {@code |
|
442 |
* class Resource { |
|
443 |
* private static ExternalResource[] externalResourceArray = ... |
|
444 |
* |
|
445 |
* int myIndex; |
|
446 |
* Resource(...) { |
|
447 |
* myIndex = ... |
|
448 |
* externalResourceArray[myIndex] = ...; |
|
449 |
* ... |
|
450 |
* } |
|
451 |
* protected void finalize() { |
|
452 |
* externalResourceArray[myIndex] = null; |
|
453 |
* ... |
|
454 |
* } |
|
455 |
* public void action() { |
|
456 |
* try { |
|
457 |
* // ... |
|
458 |
* int i = myIndex; |
|
459 |
* Resource.update(externalResourceArray[i]); |
|
460 |
* } finally { |
|
461 |
* Reference.reachabilityFence(this); |
|
462 |
* } |
|
463 |
* } |
|
464 |
* private static void update(ExternalResource ext) { |
|
465 |
* ext.status = ...; |
|
466 |
* } |
|
467 |
* }}</pre> |
|
468 |
* |
|
469 |
* Here, the invocation of {@code reachabilityFence} is nonintuitively |
|
470 |
* placed <em>after</em> the call to {@code update}, to ensure that the |
|
471 |
* array slot is not nulled out by {@link Object#finalize} before the |
|
472 |
* update, even if the call to {@code action} was the last use of this |
|
473 |
* object. This might be the case if, for example a usage in a user program |
|
474 |
* had the form {@code new Resource().action();} which retains no other |
|
475 |
* reference to this {@code Resource}. While probably overkill here, |
|
476 |
* {@code reachabilityFence} is placed in a {@code finally} block to ensure |
|
477 |
* that it is invoked across all paths in the method. In a method with more |
|
478 |
* complex control paths, you might need further precautions to ensure that |
|
479 |
* {@code reachabilityFence} is encountered along all of them. |
|
480 |
* |
|
481 |
* <p> It is sometimes possible to better encapsulate use of |
|
482 |
* {@code reachabilityFence}. Continuing the above example, if it were |
|
483 |
* acceptable for the call to method {@code update} to proceed even if the |
|
484 |
* finalizer had already executed (nulling out slot), then you could |
|
485 |
* localize use of {@code reachabilityFence}: |
|
486 |
* |
|
487 |
* <pre> {@code |
|
488 |
* public void action2() { |
|
489 |
* // ... |
|
490 |
* Resource.update(getExternalResource()); |
|
491 |
* } |
|
492 |
* private ExternalResource getExternalResource() { |
|
493 |
* ExternalResource ext = externalResourceArray[myIndex]; |
|
494 |
* Reference.reachabilityFence(this); |
|
495 |
* return ext; |
|
496 |
* }}</pre> |
|
497 |
* |
|
498 |
* <p> Method {@code reachabilityFence} is not required in constructions |
|
499 |
* that themselves ensure reachability. For example, because objects that |
|
500 |
* are locked cannot, in general, be reclaimed, it would suffice if all |
|
501 |
* accesses of the object, in all methods of class {@code Resource} |
|
502 |
* (including {@code finalize}) were enclosed in {@code synchronized (this)} |
|
503 |
* blocks. (Further, such blocks must not include infinite loops, or |
|
504 |
* themselves be unreachable, which fall into the corner case exceptions to |
|
505 |
* the "in general" disclaimer.) However, method {@code reachabilityFence} |
|
506 |
* remains a better option in cases where this approach is not as efficient, |
|
507 |
* desirable, or possible; for example because it would encounter deadlock. |
|
508 |
* |
|
509 |
* @param ref the reference. If {@code null}, this method has no effect. |
|
510 |
* @since 9 |
|
55351
8038250f9939
8225675: Outdated citation of JLS in java.lang.ref.Reference
darcy
parents:
52510
diff
changeset
|
511 |
* @jls 12.6 Finalization of Class Instances |
35254 | 512 |
*/ |
49210
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48922
diff
changeset
|
513 |
@ForceInline |
35254 | 514 |
public static void reachabilityFence(Object ref) { |
49210
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48922
diff
changeset
|
515 |
// Does nothing. This method is annotated with @ForceInline to eliminate |
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48922
diff
changeset
|
516 |
// most of the overhead that using @DontInline would cause with the |
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48922
diff
changeset
|
517 |
// HotSpot JVM, when this fence is used in a wide variety of situations. |
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48922
diff
changeset
|
518 |
// HotSpot JVM retains the ref and does not GC it before a call to |
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48922
diff
changeset
|
519 |
// this method, because the JIT-compilers do not have GC-only safepoints. |
35254 | 520 |
} |
2 | 521 |
} |