RFR: 8362239: Reconcile enter_internal and reenter_internal in the ObjectMonitor code

David Holmes dholmes at openjdk.org
Thu Aug 21 12:26:18 UTC 2025


On Wed, 20 Aug 2025 11:20:38 GMT, Anton Artemov <duke at openjdk.org> wrote:

> Please consider the following changes:
> 
> The two methods of the `ObjectMonitor` class, namely `enter_internal()` and `reenter_internal()`, are reconciled. As a result, `reenter_internal()` is removed, whereas `enter_internal()` is modified. 
> 
> The modification affects the range of the code, where the thread is in the blocked state. Now, it is only a small part in the innermost loop of the method. However, suspension of the "contender" thread is not allowed on the initial entrance, as this can potentially lead to a deadlock: if the "contender" thread is suspended in the innermost loop, and the "resumer" thread is parked, no one will be making progress as the "resumer" thread cannot be woken up, as the "contender" thread has not yet exited the monitor to unpark the "resumer". 
> 
> In this sense, the initial entrance to the monitor is considered to be **atomic**, i.e. it is either completed, or not done at all. This means it should not be interrupted by a suspension request. The re-entrance to the monitor, however, allows suspension, as the monitor has been already owned by a "contender" thread.
> 
> Tested in tiers 1-5.

src/hotspot/share/runtime/objectMonitor.cpp line 981:

> 979:     // Java objects.  This would avoid awkward lifecycle and liveness issues,
> 980:     // as well as eliminate a subset of ABA issues.
> 981:     // TODO: eliminate ObjectWaiter and enqueue either Threads or Events.

Side issue: I think this commentary can be removed now.

src/hotspot/share/runtime/objectMonitor.cpp line 1903:

> 1901:     } else {
> 1902:       guarantee(v == ObjectWaiter::TS_ENTER, "invariant");
> 1903:       enter_internal(current, &node, true);

Suggestion:

      enter_internal(current, &node, true /* do_reenter */);

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

PR Review Comment: https://git.openjdk.org/jdk/pull/26858#discussion_r2290151251
PR Review Comment: https://git.openjdk.org/jdk/pull/26858#discussion_r2290144195


More information about the hotspot-runtime-dev mailing list