author | avstepan |
Thu, 06 Aug 2015 13:20:13 +0300 | |
changeset 32033 | bf24e33c7919 |
parent 25859 | 3317bb8137f4 |
child 32104 | 17bed8d40800 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
19053 | 2 |
* Copyright (c) 1997, 2013, 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 |
||
28 |
/** |
|
29 |
* Reference queues, to which registered reference objects are appended by the |
|
30 |
* garbage collector after the appropriate reachability changes are detected. |
|
31 |
* |
|
32 |
* @author Mark Reinhold |
|
33 |
* @since 1.2 |
|
34 |
*/ |
|
35 |
||
36 |
public class ReferenceQueue<T> { |
|
37 |
||
38 |
/** |
|
39 |
* Constructs a new reference-object queue. |
|
40 |
*/ |
|
41 |
public ReferenceQueue() { } |
|
42 |
||
19053 | 43 |
private static class Null<S> extends ReferenceQueue<S> { |
44 |
boolean enqueue(Reference<? extends S> r) { |
|
2 | 45 |
return false; |
46 |
} |
|
47 |
} |
|
48 |
||
19053 | 49 |
static ReferenceQueue<Object> NULL = new Null<>(); |
50 |
static ReferenceQueue<Object> ENQUEUED = new Null<>(); |
|
2 | 51 |
|
52 |
static private class Lock { }; |
|
53 |
private Lock lock = new Lock(); |
|
2442
f8bb207f9458
6666739: (ref) ReferenceQueue.poll() doesn't scale well
alanb
parents:
2
diff
changeset
|
54 |
private volatile Reference<? extends T> head = null; |
2 | 55 |
private long queueLength = 0; |
56 |
||
57 |
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
|
58 |
synchronized (lock) { |
5da35ed47cfa
8014890: (ref) Reference queues may return more entries than expected
mchung
parents:
5506
diff
changeset
|
59 |
// 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
|
60 |
// enqueued (and even then removed) |
19053 | 61 |
ReferenceQueue<?> queue = r.queue; |
18815
5da35ed47cfa
8014890: (ref) Reference queues may return more entries than expected
mchung
parents:
5506
diff
changeset
|
62 |
if ((queue == NULL) || (queue == ENQUEUED)) { |
5da35ed47cfa
8014890: (ref) Reference queues may return more entries than expected
mchung
parents:
5506
diff
changeset
|
63 |
return false; |
2 | 64 |
} |
18815
5da35ed47cfa
8014890: (ref) Reference queues may return more entries than expected
mchung
parents:
5506
diff
changeset
|
65 |
assert queue == this; |
5da35ed47cfa
8014890: (ref) Reference queues may return more entries than expected
mchung
parents:
5506
diff
changeset
|
66 |
r.queue = ENQUEUED; |
5da35ed47cfa
8014890: (ref) Reference queues may return more entries than expected
mchung
parents:
5506
diff
changeset
|
67 |
r.next = (head == null) ? r : head; |
5da35ed47cfa
8014890: (ref) Reference queues may return more entries than expected
mchung
parents:
5506
diff
changeset
|
68 |
head = r; |
5da35ed47cfa
8014890: (ref) Reference queues may return more entries than expected
mchung
parents:
5506
diff
changeset
|
69 |
queueLength++; |
5da35ed47cfa
8014890: (ref) Reference queues may return more entries than expected
mchung
parents:
5506
diff
changeset
|
70 |
if (r instanceof FinalReference) { |
5da35ed47cfa
8014890: (ref) Reference queues may return more entries than expected
mchung
parents:
5506
diff
changeset
|
71 |
sun.misc.VM.addFinalRefCount(1); |
5da35ed47cfa
8014890: (ref) Reference queues may return more entries than expected
mchung
parents:
5506
diff
changeset
|
72 |
} |
5da35ed47cfa
8014890: (ref) Reference queues may return more entries than expected
mchung
parents:
5506
diff
changeset
|
73 |
lock.notifyAll(); |
5da35ed47cfa
8014890: (ref) Reference queues may return more entries than expected
mchung
parents:
5506
diff
changeset
|
74 |
return true; |
2 | 75 |
} |
76 |
} |
|
77 |
||
19053 | 78 |
@SuppressWarnings("unchecked") |
2 | 79 |
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
|
80 |
Reference<? extends T> r = head; |
5da35ed47cfa
8014890: (ref) Reference queues may return more entries than expected
mchung
parents:
5506
diff
changeset
|
81 |
if (r != null) { |
19053 | 82 |
head = (r.next == r) ? |
83 |
null : |
|
84 |
r.next; // Unchecked due to the next field having a raw type in Reference |
|
2 | 85 |
r.queue = NULL; |
86 |
r.next = r; |
|
87 |
queueLength--; |
|
88 |
if (r instanceof FinalReference) { |
|
89 |
sun.misc.VM.addFinalRefCount(-1); |
|
90 |
} |
|
91 |
return r; |
|
92 |
} |
|
93 |
return null; |
|
94 |
} |
|
95 |
||
96 |
/** |
|
97 |
* Polls this queue to see if a reference object is available. If one is |
|
98 |
* 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
|
99 |
* returned. Otherwise this method immediately returns {@code null}. |
2 | 100 |
* |
101 |
* @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
|
102 |
* otherwise {@code null} |
2 | 103 |
*/ |
104 |
public Reference<? extends T> poll() { |
|
2442
f8bb207f9458
6666739: (ref) ReferenceQueue.poll() doesn't scale well
alanb
parents:
2
diff
changeset
|
105 |
if (head == null) |
f8bb207f9458
6666739: (ref) ReferenceQueue.poll() doesn't scale well
alanb
parents:
2
diff
changeset
|
106 |
return null; |
2 | 107 |
synchronized (lock) { |
108 |
return reallyPoll(); |
|
109 |
} |
|
110 |
} |
|
111 |
||
112 |
/** |
|
113 |
* Removes the next reference object in this queue, blocking until either |
|
114 |
* one becomes available or the given timeout period expires. |
|
115 |
* |
|
116 |
* <p> This method does not offer real-time guarantees: It schedules the |
|
117 |
* timeout as if by invoking the {@link Object#wait(long)} method. |
|
118 |
* |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
25859
diff
changeset
|
119 |
* @param timeout If positive, block for up to {@code timeout} |
2 | 120 |
* milliseconds while waiting for a reference to be |
121 |
* added to this queue. If zero, block indefinitely. |
|
122 |
* |
|
123 |
* @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
|
124 |
* timeout period, otherwise {@code null} |
2 | 125 |
* |
126 |
* @throws IllegalArgumentException |
|
127 |
* If the value of the timeout argument is negative |
|
128 |
* |
|
129 |
* @throws InterruptedException |
|
130 |
* If the timeout wait is interrupted |
|
131 |
*/ |
|
132 |
public Reference<? extends T> remove(long timeout) |
|
133 |
throws IllegalArgumentException, InterruptedException |
|
134 |
{ |
|
135 |
if (timeout < 0) { |
|
136 |
throw new IllegalArgumentException("Negative timeout value"); |
|
137 |
} |
|
138 |
synchronized (lock) { |
|
139 |
Reference<? extends T> r = reallyPoll(); |
|
140 |
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
|
141 |
long start = (timeout == 0) ? 0 : System.nanoTime(); |
2 | 142 |
for (;;) { |
143 |
lock.wait(timeout); |
|
144 |
r = reallyPoll(); |
|
145 |
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
|
146 |
if (timeout != 0) { |
07b4bf8a9026
6853696: (ref) ReferenceQueue.remove(timeout) may return null even if timeout has not expired
igerasim
parents:
19053
diff
changeset
|
147 |
long end = System.nanoTime(); |
07b4bf8a9026
6853696: (ref) ReferenceQueue.remove(timeout) may return null even if timeout has not expired
igerasim
parents:
19053
diff
changeset
|
148 |
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
|
149 |
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
|
150 |
start = end; |
07b4bf8a9026
6853696: (ref) ReferenceQueue.remove(timeout) may return null even if timeout has not expired
igerasim
parents:
19053
diff
changeset
|
151 |
} |
2 | 152 |
} |
153 |
} |
|
154 |
} |
|
155 |
||
156 |
/** |
|
157 |
* Removes the next reference object in this queue, blocking until one |
|
158 |
* becomes available. |
|
159 |
* |
|
160 |
* @return A reference object, blocking until one becomes available |
|
161 |
* @throws InterruptedException If the wait is interrupted |
|
162 |
*/ |
|
163 |
public Reference<? extends T> remove() throws InterruptedException { |
|
164 |
return remove(0); |
|
165 |
} |
|
166 |
||
167 |
} |