author | martin |
Tue, 15 Sep 2015 21:56:04 -0700 | |
changeset 32649 | 2ee9017c7597 |
parent 32221 | 2d0aa67d905f |
child 34774 | 03b4e6dc367b |
permissions | -rw-r--r-- |
2 | 1 |
/* |
32100
8a0db19f4e84
8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents:
25859
diff
changeset
|
2 |
* Copyright (c) 1997, 2015, 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 |
||
32100
8a0db19f4e84
8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents:
25859
diff
changeset
|
28 |
import java.util.function.Consumer; |
8a0db19f4e84
8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents:
25859
diff
changeset
|
29 |
|
2 | 30 |
/** |
31 |
* Reference queues, to which registered reference objects are appended by the |
|
32 |
* garbage collector after the appropriate reachability changes are detected. |
|
33 |
* |
|
34 |
* @author Mark Reinhold |
|
35 |
* @since 1.2 |
|
36 |
*/ |
|
37 |
||
38 |
public class ReferenceQueue<T> { |
|
39 |
||
40 |
/** |
|
41 |
* Constructs a new reference-object queue. |
|
42 |
*/ |
|
43 |
public ReferenceQueue() { } |
|
44 |
||
19053 | 45 |
private static class Null<S> extends ReferenceQueue<S> { |
46 |
boolean enqueue(Reference<? extends S> r) { |
|
2 | 47 |
return false; |
48 |
} |
|
49 |
} |
|
50 |
||
19053 | 51 |
static ReferenceQueue<Object> NULL = new Null<>(); |
52 |
static ReferenceQueue<Object> ENQUEUED = new Null<>(); |
|
2 | 53 |
|
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
32221
diff
changeset
|
54 |
private static class Lock { }; |
2 | 55 |
private Lock lock = new Lock(); |
2442
f8bb207f9458
6666739: (ref) ReferenceQueue.poll() doesn't scale well
alanb
parents:
2
diff
changeset
|
56 |
private volatile Reference<? extends T> head = null; |
2 | 57 |
private long queueLength = 0; |
58 |
||
59 |
boolean enqueue(Reference<? extends T> r) { /* Called only by Reference class */ |
|
18815
5da35ed47cfa
8014890: (ref) Reference queues may return more entries than expected
mchung
parents:
5506
diff
changeset
|
60 |
synchronized (lock) { |
5da35ed47cfa
8014890: (ref) Reference queues may return more entries than expected
mchung
parents:
5506
diff
changeset
|
61 |
// Check that since getting the lock this reference hasn't already been |
5da35ed47cfa
8014890: (ref) Reference queues may return more entries than expected
mchung
parents:
5506
diff
changeset
|
62 |
// enqueued (and even then removed) |
19053 | 63 |
ReferenceQueue<?> queue = r.queue; |
18815
5da35ed47cfa
8014890: (ref) Reference queues may return more entries than expected
mchung
parents:
5506
diff
changeset
|
64 |
if ((queue == NULL) || (queue == ENQUEUED)) { |
5da35ed47cfa
8014890: (ref) Reference queues may return more entries than expected
mchung
parents:
5506
diff
changeset
|
65 |
return false; |
2 | 66 |
} |
18815
5da35ed47cfa
8014890: (ref) Reference queues may return more entries than expected
mchung
parents:
5506
diff
changeset
|
67 |
assert queue == this; |
5da35ed47cfa
8014890: (ref) Reference queues may return more entries than expected
mchung
parents:
5506
diff
changeset
|
68 |
r.next = (head == null) ? r : head; |
5da35ed47cfa
8014890: (ref) Reference queues may return more entries than expected
mchung
parents:
5506
diff
changeset
|
69 |
head = r; |
5da35ed47cfa
8014890: (ref) Reference queues may return more entries than expected
mchung
parents:
5506
diff
changeset
|
70 |
queueLength++; |
32219
e613f4863973
8132306: java/lang/ref/ReferenceEnqueue.java fails with "RuntimeException: Error: poll() returned null; expected ref object"
kbarrett
parents:
32100
diff
changeset
|
71 |
// Update r.queue *after* adding to list, to avoid race |
e613f4863973
8132306: java/lang/ref/ReferenceEnqueue.java fails with "RuntimeException: Error: poll() returned null; expected ref object"
kbarrett
parents:
32100
diff
changeset
|
72 |
// with concurrent enqueued checks and fast-path poll(). |
e613f4863973
8132306: java/lang/ref/ReferenceEnqueue.java fails with "RuntimeException: Error: poll() returned null; expected ref object"
kbarrett
parents:
32100
diff
changeset
|
73 |
// Volatiles ensure ordering. |
e613f4863973
8132306: java/lang/ref/ReferenceEnqueue.java fails with "RuntimeException: Error: poll() returned null; expected ref object"
kbarrett
parents:
32100
diff
changeset
|
74 |
r.queue = ENQUEUED; |
18815
5da35ed47cfa
8014890: (ref) Reference queues may return more entries than expected
mchung
parents:
5506
diff
changeset
|
75 |
if (r instanceof FinalReference) { |
5da35ed47cfa
8014890: (ref) Reference queues may return more entries than expected
mchung
parents:
5506
diff
changeset
|
76 |
sun.misc.VM.addFinalRefCount(1); |
5da35ed47cfa
8014890: (ref) Reference queues may return more entries than expected
mchung
parents:
5506
diff
changeset
|
77 |
} |
5da35ed47cfa
8014890: (ref) Reference queues may return more entries than expected
mchung
parents:
5506
diff
changeset
|
78 |
lock.notifyAll(); |
5da35ed47cfa
8014890: (ref) Reference queues may return more entries than expected
mchung
parents:
5506
diff
changeset
|
79 |
return true; |
2 | 80 |
} |
81 |
} |
|
82 |
||
83 |
private Reference<? extends T> reallyPoll() { /* Must hold lock */ |
|
18815
5da35ed47cfa
8014890: (ref) Reference queues may return more entries than expected
mchung
parents:
5506
diff
changeset
|
84 |
Reference<? extends T> r = head; |
5da35ed47cfa
8014890: (ref) Reference queues may return more entries than expected
mchung
parents:
5506
diff
changeset
|
85 |
if (r != null) { |
32219
e613f4863973
8132306: java/lang/ref/ReferenceEnqueue.java fails with "RuntimeException: Error: poll() returned null; expected ref object"
kbarrett
parents:
32100
diff
changeset
|
86 |
r.queue = NULL; |
e613f4863973
8132306: java/lang/ref/ReferenceEnqueue.java fails with "RuntimeException: Error: poll() returned null; expected ref object"
kbarrett
parents:
32100
diff
changeset
|
87 |
// Update r.queue *before* removing from list, to avoid |
e613f4863973
8132306: java/lang/ref/ReferenceEnqueue.java fails with "RuntimeException: Error: poll() returned null; expected ref object"
kbarrett
parents:
32100
diff
changeset
|
88 |
// race with concurrent enqueued checks and fast-path |
e613f4863973
8132306: java/lang/ref/ReferenceEnqueue.java fails with "RuntimeException: Error: poll() returned null; expected ref object"
kbarrett
parents:
32100
diff
changeset
|
89 |
// poll(). Volatiles ensure ordering. |
32100
8a0db19f4e84
8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents:
25859
diff
changeset
|
90 |
@SuppressWarnings("unchecked") |
8a0db19f4e84
8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents:
25859
diff
changeset
|
91 |
Reference<? extends T> rn = r.next; |
8a0db19f4e84
8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents:
25859
diff
changeset
|
92 |
head = (rn == r) ? null : rn; |
2 | 93 |
r.next = r; |
94 |
queueLength--; |
|
95 |
if (r instanceof FinalReference) { |
|
96 |
sun.misc.VM.addFinalRefCount(-1); |
|
97 |
} |
|
98 |
return r; |
|
99 |
} |
|
100 |
return null; |
|
101 |
} |
|
102 |
||
103 |
/** |
|
104 |
* Polls this queue to see if a reference object is available. If one is |
|
105 |
* available without further delay then it is removed from the queue and |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
106 |
* returned. Otherwise this method immediately returns {@code null}. |
2 | 107 |
* |
108 |
* @return A reference object, if one was immediately available, |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
109 |
* otherwise {@code null} |
2 | 110 |
*/ |
111 |
public Reference<? extends T> poll() { |
|
2442
f8bb207f9458
6666739: (ref) ReferenceQueue.poll() doesn't scale well
alanb
parents:
2
diff
changeset
|
112 |
if (head == null) |
f8bb207f9458
6666739: (ref) ReferenceQueue.poll() doesn't scale well
alanb
parents:
2
diff
changeset
|
113 |
return null; |
2 | 114 |
synchronized (lock) { |
115 |
return reallyPoll(); |
|
116 |
} |
|
117 |
} |
|
118 |
||
119 |
/** |
|
120 |
* Removes the next reference object in this queue, blocking until either |
|
121 |
* one becomes available or the given timeout period expires. |
|
122 |
* |
|
123 |
* <p> This method does not offer real-time guarantees: It schedules the |
|
124 |
* timeout as if by invoking the {@link Object#wait(long)} method. |
|
125 |
* |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
126 |
* @param timeout If positive, block for up to {@code timeout} |
2 | 127 |
* milliseconds while waiting for a reference to be |
128 |
* added to this queue. If zero, block indefinitely. |
|
129 |
* |
|
130 |
* @return A reference object, if one was available within the specified |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
131 |
* timeout period, otherwise {@code null} |
2 | 132 |
* |
133 |
* @throws IllegalArgumentException |
|
134 |
* If the value of the timeout argument is negative |
|
135 |
* |
|
136 |
* @throws InterruptedException |
|
137 |
* If the timeout wait is interrupted |
|
138 |
*/ |
|
139 |
public Reference<? extends T> remove(long timeout) |
|
140 |
throws IllegalArgumentException, InterruptedException |
|
141 |
{ |
|
142 |
if (timeout < 0) { |
|
143 |
throw new IllegalArgumentException("Negative timeout value"); |
|
144 |
} |
|
145 |
synchronized (lock) { |
|
146 |
Reference<? extends T> r = reallyPoll(); |
|
147 |
if (r != null) return r; |
|
23027
07b4bf8a9026
6853696: (ref) ReferenceQueue.remove(timeout) may return null even if timeout has not expired
igerasim
parents:
19053
diff
changeset
|
148 |
long start = (timeout == 0) ? 0 : System.nanoTime(); |
2 | 149 |
for (;;) { |
150 |
lock.wait(timeout); |
|
151 |
r = reallyPoll(); |
|
152 |
if (r != null) return r; |
|
23027
07b4bf8a9026
6853696: (ref) ReferenceQueue.remove(timeout) may return null even if timeout has not expired
igerasim
parents:
19053
diff
changeset
|
153 |
if (timeout != 0) { |
07b4bf8a9026
6853696: (ref) ReferenceQueue.remove(timeout) may return null even if timeout has not expired
igerasim
parents:
19053
diff
changeset
|
154 |
long end = System.nanoTime(); |
07b4bf8a9026
6853696: (ref) ReferenceQueue.remove(timeout) may return null even if timeout has not expired
igerasim
parents:
19053
diff
changeset
|
155 |
timeout -= (end - start) / 1000_000; |
07b4bf8a9026
6853696: (ref) ReferenceQueue.remove(timeout) may return null even if timeout has not expired
igerasim
parents:
19053
diff
changeset
|
156 |
if (timeout <= 0) return null; |
07b4bf8a9026
6853696: (ref) ReferenceQueue.remove(timeout) may return null even if timeout has not expired
igerasim
parents:
19053
diff
changeset
|
157 |
start = end; |
07b4bf8a9026
6853696: (ref) ReferenceQueue.remove(timeout) may return null even if timeout has not expired
igerasim
parents:
19053
diff
changeset
|
158 |
} |
2 | 159 |
} |
160 |
} |
|
161 |
} |
|
162 |
||
163 |
/** |
|
164 |
* Removes the next reference object in this queue, blocking until one |
|
165 |
* becomes available. |
|
166 |
* |
|
167 |
* @return A reference object, blocking until one becomes available |
|
168 |
* @throws InterruptedException If the wait is interrupted |
|
169 |
*/ |
|
170 |
public Reference<? extends T> remove() throws InterruptedException { |
|
171 |
return remove(0); |
|
172 |
} |
|
173 |
||
32100
8a0db19f4e84
8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents:
25859
diff
changeset
|
174 |
/** |
8a0db19f4e84
8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents:
25859
diff
changeset
|
175 |
* Iterate queue and invoke given action with each Reference. |
8a0db19f4e84
8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents:
25859
diff
changeset
|
176 |
* Suitable for diagnostic purposes. |
8a0db19f4e84
8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents:
25859
diff
changeset
|
177 |
* WARNING: any use of this method should make sure to not |
8a0db19f4e84
8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents:
25859
diff
changeset
|
178 |
* retain the referents of iterated references (in case of |
8a0db19f4e84
8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents:
25859
diff
changeset
|
179 |
* FinalReference(s)) so that their life is not prolonged more |
8a0db19f4e84
8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents:
25859
diff
changeset
|
180 |
* than necessary. |
8a0db19f4e84
8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents:
25859
diff
changeset
|
181 |
*/ |
8a0db19f4e84
8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents:
25859
diff
changeset
|
182 |
void forEach(Consumer<? super Reference<? extends T>> action) { |
8a0db19f4e84
8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents:
25859
diff
changeset
|
183 |
for (Reference<? extends T> r = head; r != null;) { |
8a0db19f4e84
8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents:
25859
diff
changeset
|
184 |
action.accept(r); |
8a0db19f4e84
8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents:
25859
diff
changeset
|
185 |
@SuppressWarnings("unchecked") |
8a0db19f4e84
8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents:
25859
diff
changeset
|
186 |
Reference<? extends T> rn = r.next; |
8a0db19f4e84
8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents:
25859
diff
changeset
|
187 |
if (rn == r) { |
8a0db19f4e84
8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents:
25859
diff
changeset
|
188 |
if (r.queue == ENQUEUED) { |
8a0db19f4e84
8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents:
25859
diff
changeset
|
189 |
// still enqueued -> we reached end of chain |
8a0db19f4e84
8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents:
25859
diff
changeset
|
190 |
r = null; |
8a0db19f4e84
8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents:
25859
diff
changeset
|
191 |
} else { |
8a0db19f4e84
8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents:
25859
diff
changeset
|
192 |
// already dequeued: r.queue == NULL; -> |
8a0db19f4e84
8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents:
25859
diff
changeset
|
193 |
// restart from head when overtaken by queue poller(s) |
8a0db19f4e84
8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents:
25859
diff
changeset
|
194 |
r = head; |
8a0db19f4e84
8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents:
25859
diff
changeset
|
195 |
} |
8a0db19f4e84
8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents:
25859
diff
changeset
|
196 |
} else { |
8a0db19f4e84
8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents:
25859
diff
changeset
|
197 |
// next in chain |
8a0db19f4e84
8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents:
25859
diff
changeset
|
198 |
r = rn; |
8a0db19f4e84
8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents:
25859
diff
changeset
|
199 |
} |
8a0db19f4e84
8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents:
25859
diff
changeset
|
200 |
} |
8a0db19f4e84
8059036: Implement Diagnostic Commands for heap and finalizerinfo
dsamersoff
parents:
25859
diff
changeset
|
201 |
} |
2 | 202 |
} |