RFR: 8355646: Optimize ObjectMonitor::exit
Coleen Phillimore
coleenp at openjdk.org
Tue May 6 14:02:17 UTC 2025
On Mon, 28 Apr 2025 15:29:12 GMT, Fredrik Bredberg <fbredberg at openjdk.org> wrote:
> Today ObjectMonitor::exit always releases the lock before checking if it needs to wake up a waiting thread that is queued on the entry_list. In order to unpark a thread from the entry_list it needs to re-acquire the lock again.
>
> If we (before releasing the lock) check if there is no assigned successor but there is a waiting thread in the entry_list we can immediately unpark the thread, thus saving us the trouble of releasing and re-acquiring the lock.
>
> If there already is an assigned successor (spinning and wanting to acquire the lock) we still want to release the lock as soon as possible so that we don't lengthen the critical section thereby delaying any spinning threads from getting the lock in a high contention scenario.
>
> This PR is first and foremost an optimization if the lock is lightly contended. It also removes some source reading confusion, as to why we release and then immediately re-acquire the lock.
>
> When running performance tests we see that the number of test that has improved outnumber the tests that has not.
>
> Passes tier1-7 with no added problems.
Besides the typo, this makes sense. Instead of releasing the lock and reacquiring it to find the heir-apparent, you do that before releasing the lock if there's no successor. So the difference is that if a successor shows up after you check that there's none and assign an heir, the heir will get the lock next and not the successor.
Avoiding a CAS seems worth it for the short window where this might happen.
src/hotspot/share/runtime/objectMonitor.cpp line 1513:
> 1511:
> 1512: // If there is a successor we should release the lock as soon as
> 1513: // posible, so that the successor can aquire the look. If there is
typo: possible
-------------
Marked as reviewed by coleenp (Reviewer).
PR Review: https://git.openjdk.org/jdk/pull/24927#pullrequestreview-2818374608
PR Review Comment: https://git.openjdk.org/jdk/pull/24927#discussion_r2075529295
More information about the hotspot-runtime-dev
mailing list