Skip to content

Commit 8984f63

Browse files
committed
Ensure ReferenceQueue/Reference synchronization order in enqueueImpl
Signed-off-by: Peter Shipton <[email protected]>
1 parent bcf15a7 commit 8984f63

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

jcl/src/java.base/share/classes/java/lang/ref/Reference.java

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -194,24 +194,30 @@ public boolean isEnqueued () {
194194
@NotCheckpointSafe
195195
/*[ENDIF] CRIU_SUPPORT */
196196
boolean enqueueImpl() {
197-
final ReferenceQueue tempQueue;
198-
boolean result;
199197
T tempReferent = referent;
200-
synchronized(this) {
201-
/* Static order for the following code (DO NOT CHANGE) */
202-
tempQueue = queue;
203-
queue = null;
204-
if (state == STATE_ENQUEUED || tempQueue == null) {
205-
return false;
206-
}
207-
result = tempQueue.enqueue(this);
208-
if (result) {
209-
state = STATE_ENQUEUED;
210-
if (null != tempReferent) {
211-
reprocess();
198+
while(true) {
199+
ReferenceQueue tempQueue = queue;
200+
synchronized(tempQueue) {
201+
synchronized(this) {
202+
boolean result;
203+
if (tempQueue != queue) {
204+
break;
205+
}
206+
/* Static order for the following code (DO NOT CHANGE) */
207+
queue = null;
208+
if (state == STATE_ENQUEUED || tempQueue == null) {
209+
return false;
210+
}
211+
result = tempQueue.enqueue(this);
212+
if (result) {
213+
state = STATE_ENQUEUED;
214+
if (null != tempReferent) {
215+
reprocess();
216+
}
217+
}
218+
return result;
212219
}
213220
}
214-
return result;
215221
}
216222
}
217223

0 commit comments

Comments
 (0)