Skip to content

Commit

Permalink
Another update to reachabilityFence() @APinote
Browse files Browse the repository at this point in the history
  • Loading branch information
bchristi-git committed Apr 5, 2024
1 parent bdac5cc commit 91d4db4
Showing 1 changed file with 14 additions and 19 deletions.
33 changes: 14 additions & 19 deletions src/java.base/share/classes/java/lang/ref/Reference.java
Original file line number Diff line number Diff line change
Expand Up @@ -569,25 +569,20 @@ protected Object clone() throws CloneNotSupportedException {
* the garbage collector clears any reference to {@code x}.
*
* @apiNote
* Reference processing or finalization may occur when the virtual machine
* detects that there is no further need for an object. The garbage collector
* may reclaim such an object even if values obtained from that object's
* fields are still in use, so long as there is no further use of the object
* itself. (From a garbage collection point of view, when an object has no
* further use in this way it is
* <a href="package-summary.html#reachability"><em>unreachable</em></a> or
* "no longer live.") Field <em>values</em> obtained from an object can
* remain live even after the object (and its fields) have become
* unreachable. The virtual machine, in the course of optimization, can
* reorder operations of an object's method; for instance, the loading of
* values from the object's fields can be moved to occur earlier. If the
* object is then no longer needed, it can become unreachable and be
* reclaimed, even while one of its methods is still running.
* This may have surprising and undesirable effects, in particular when using
* a Cleaner or finalizer for cleanup. If an object becomes unreachable while
* a method of the object is running, it can lead to a race between the
* program thread running the method and the cleanup thread running the
* Cleaner or finalizer. For instance, the cleanup thread could free a
* Reference processing or finalization can occur after an object becomes
* unreachable. An object can become unreachable when the virtual machine
* detects that there is no further need for the object (other than for
* running a finalizer). In the course of optimization, the virtual machine
* can reorder operations of an object's methods such that the object
* becomes unneeded earlier than might naively be expected &mdash;
* including while a method of the object is still running. For instance,
* the VM can move the loading of <em>values</em> from the object's fields
* to occur earlier. The object itself is then no longer needed and becomes
* unreachable, and the method can continue running using the obtained values.
* This may have surprising and undesirable effects when using a Cleaner or
* finalizer for cleanup: there is a race between the
* program thread running the method, and the cleanup thread running the
* Cleaner or finalizer. The cleanup thread could free a
* resource, followed by the program thread (still running the method)
* attempting to access the now-already-freed resource.
* Use of {@code reachabilityFence} can prevent this race by ensuring that the
Expand Down

0 comments on commit 91d4db4

Please sign in to comment.