RFR: 8314480: Memory ordering spec updates in java.lang.ref

Stuart Marks smarks at openjdk.org
Wed Nov 15 22:37:34 UTC 2023


On Mon, 13 Nov 2023 22:31:16 GMT, Brent Christian <bchristi at openjdk.org> wrote:

> Classes in the `java.lang.ref` package would benefit from an update to bring the spec in line with how the VM already behaves. The changes would focus on _happens-before_ edges at some key points during reference processing.
> 
> A couple key things we want to be able to say are:
> - `Reference.reachabilityFence(x)` _happens-before_ reference processing occurs for 'x'.
> - `Cleaner.register()` _happens-before_ the Cleaner thread runs the registered cleaning action.
> 
> This will bring Cleaner in line (or close) with the memory visibility guarantees made for finalizers in [JLS 17.4.5](https://docs.oracle.com/javase/specs/jls/se18/html/jls-17.html#jls-17.4.5):
> _"There is a happens-before edge from the end of a constructor of an object to the start of a finalizer (§12.6) for that object."_

src/java.base/share/classes/java/lang/ref/Reference.java line 489:

> 487:      * <p>If this reference was already enqueued (by the garbage collector, or a
> 488:      * previous call to {@code enqueue}), this method is <b><i>not successful</i></b>,
> 489:      * and returns false.

If we're going to talk about successful and unsuccessful calls, we should define what success is first. I guess that would be something like: if this ref is registered with a queue and not already enqueued, it is enqueued successfully and the method returns true. Otherwise, not successful, and returns false.

src/java.base/share/classes/java/lang/ref/Reference.java line 495:

> 493:      * <a href="{@docRoot}/java.base/java/util/concurrent/package-summary.html#MemoryVisibility"><i>happen-before</i></a>
> 494:      * the reference is removed from the queue by {@link ReferenceQueue#poll}
> 495:      * or {@link ReferenceQueue#remove}.

Should there be an explicit statement about an unsuccessful call having no memory consistency effects?

src/java.base/share/classes/java/lang/ref/Reference.java line 505:

> 503:      * to return this reference even though the referent is still strongly
> 504:      * reachable. That is, before the referent has reached the expected
> 505:      * reachability level.

The wording here is kind of awkward. We could try to wordsmith it, but there is also the possibility that there are valid use cases for using enqueue(), which makes this caution seem misplaced. An alternative would simply be to omit this paragraph.

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/16644#discussion_r1394916147
PR Review Comment: https://git.openjdk.org/jdk/pull/16644#discussion_r1394922243
PR Review Comment: https://git.openjdk.org/jdk/pull/16644#discussion_r1394927265


More information about the core-libs-dev mailing list