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