Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
8175797: (ref) Reference::enqueue method should clear the reference o…
…bject before enqueuing

8178832: (ref) jdk.lang.ref.disableClearBeforeEnqueue property is ignored
8193780: (ref) Remove the undocumented "jdk.lang.ref.disableClearBeforeEnqueue" system property

Reviewed-by: mchung, dholmes, iris, andrew
Backport-of: 330d63d2f9e18ba069e11868d4381059c66f480f
  • Loading branch information
Poonam Bajaj committed Aug 22, 2022
1 parent 5323ef6 commit e633df1
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 9 deletions.
7 changes: 6 additions & 1 deletion jdk/src/share/classes/java/lang/ref/FinalReference.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -33,4 +33,9 @@
public FinalReference(T referent, ReferenceQueue<? super T> q) {
super(referent, q);
}

@Override
public boolean enqueue() {
throw new InternalError("should never reach here");
}
}
8 changes: 3 additions & 5 deletions jdk/src/share/classes/java/lang/ref/Reference.java
Expand Up @@ -265,7 +265,6 @@ public void clear() {
this.referent = null;
}


/* -- Queue operations -- */

/**
Expand All @@ -282,8 +281,8 @@ public boolean isEnqueued() {
}

/**
* Adds this reference object to the queue with which it is registered,
* if any.
* Clears this reference object and adds it to the queue with which
* it is registered, if any.
*
* <p> This method is invoked only by Java code; when the garbage collector
* enqueues references it does so directly, without invoking this method.
Expand All @@ -293,10 +292,10 @@ public boolean isEnqueued() {
* it was not registered with a queue when it was created
*/
public boolean enqueue() {
this.referent = null;
return this.queue.enqueue(this);
}


/* -- Constructors -- */

Reference(T referent) {
Expand All @@ -307,5 +306,4 @@ public boolean enqueue() {
this.referent = referent;
this.queue = (queue == null) ? ReferenceQueue.NULL : queue;
}

}
52 changes: 49 additions & 3 deletions jdk/test/java/lang/ref/ReferenceEnqueue.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -22,17 +22,22 @@
*/

/* @test
* @bug 4268317
* @bug 4268317 8175797
* @summary Test if Reference.enqueue() works properly with GC
* @run main ReferenceEnqueue
*/

import java.lang.ref.*;
import java.util.ArrayList;
import java.util.List;

public class ReferenceEnqueue {

public static void main(String args[]) throws Exception {
for (int i=0; i < 5; i++)
for (int i=0; i < 5; i++) {
new WeakRef().run();
new ExplicitEnqueue().run();
}
System.out.println("Test passed.");
}

Expand Down Expand Up @@ -76,4 +81,45 @@ void run() throws InterruptedException {
}
}
}

static class ExplicitEnqueue {
final ReferenceQueue<Object> queue = new ReferenceQueue<>();
final List<Reference<Object>> refs = new ArrayList<>();
final int iterations = 1000;

ExplicitEnqueue() {
this.refs.add(new SoftReference<>(new Object(), queue));
this.refs.add(new WeakReference<>(new Object(), queue));
// Can't test PhantomReference because get() always returns null.
}

void run() throws InterruptedException {
for (Reference<Object> ref : refs) {
if (ref.enqueue() == false) {
throw new RuntimeException("Error: enqueue failed");
}
if (ref.get() != null) {
throw new RuntimeException("Error: referent must be cleared");
}
}

System.gc();
for (int i = 0; refs.size() > 0 && i < iterations; i++) {
Reference<Object> ref = (Reference<Object>)queue.poll();
if (ref == null) {
System.gc();
Thread.sleep(100);
continue;
}

if (refs.remove(ref) == false) {
throw new RuntimeException("Error: unknown reference " + ref);
}
}

if (!refs.isEmpty()) {
throw new RuntimeException("Error: not all references are removed");
}
}
}
}

1 comment on commit e633df1

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.