Skip to content

Conversation

pchilano
Copy link
Contributor

@pchilano pchilano commented Oct 1, 2025

Please review the following fix. When blocking in ObjectMonitor::enter_internal we currently use timed-park for pinned virtual threads. This is done to alleviate some potential deadlocks cases where the successor is an unmounted virtual thread that cannot run. In particular this could happen during class loading/initialization if all other carriers are blocked waiting for the same class to be loaded/initialized.
This mechanism should be extended to cover ObjectMonitor::reenter_internal used in Object.wait (notification case). Also, the criteria to decide whether to do a timed-park should be based on whether there are unmounted vthreads already in the _entry_list, and not just if this is a pinned virtual thread. This covers mixed usages of the same ObjectMonitor between virtual threads and platform threads. This will become more relevant once we bring the changes currently in the fibers branch to preempt virtual threads during klass initialization.

These changes have been running in the loom pipeline for a couple of months already. I also added a new test case to test/jdk/java/lang/Thread/virtual/MonitorWaitNotify.java which deadlocks without these changes.

Thanks,
Patricio


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8369019: Improve timed-park mechanism in ObjectMonitor for virtual thread support (Enhancement - P4)

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/27597/head:pull/27597
$ git checkout pull/27597

Update a local copy of the PR:
$ git checkout pull/27597
$ git pull https://git.openjdk.org/jdk.git pull/27597/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 27597

View PR using the GUI difftool:
$ git pr show -t 27597

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/27597.diff

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Oct 1, 2025

👋 Welcome back pchilanomate! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Oct 1, 2025

❗ This change is not yet ready to be integrated.
See the Progress checklist in the description for automated requirements.

@openjdk
Copy link

openjdk bot commented Oct 1, 2025

@pchilano The following labels will be automatically applied to this pull request:

  • core-libs
  • hotspot-runtime

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing lists. If you would like to change these labels, use the /label pull request command.

@pchilano
Copy link
Contributor Author

pchilano commented Oct 1, 2025

/label remove core-libs

@openjdk
Copy link

openjdk bot commented Oct 1, 2025

@pchilano
The core-libs label was successfully removed.

@pchilano pchilano marked this pull request as ready for review October 1, 2025 23:33
@openjdk openjdk bot added the rfr Pull request is ready for review label Oct 1, 2025
@mlbridge
Copy link

mlbridge bot commented Oct 1, 2025

Webrevs

// all other carriers have a vthread pinned to it waiting for said class
// to be loaded/initialized.
// If there are unmounted virtual threads ahead in the _entry_list we want
// to do a timed-park instead to alleviate some deadlocks cases where one
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// to do a timed-park instead to alleviate some deadlocks cases where one
// to do a timed-park instead to alleviate some deadlock cases where one

assert_mark_word_consistency();

// If there are unmounted virtual threads ahead in the _entry_list we want
// to do a timed-park instead to alleviate some deadlocks cases where one
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// to do a timed-park instead to alleviate some deadlocks cases where one
// to do a timed-park instead to alleviate some deadlock cases where one

// _entry_list uses Atomic::cmpxchg() which already provides a fence that
// prevents this load from floating up previous store.
// Note that we can have false positives where timed-park is not necessary.
bool do_timed_parked = has_unmounted_vthreads();
Copy link
Member

Choose a reason for hiding this comment

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

Don't we still only need the timed-park if the current thread is a pinned vthread?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, except if the monitor is also used in the context of a carrier thread. Currently there are only very few such cases and we disable preemption for them (e.g. interruptLock), so it’s likely not needed. With the upcoming changes to preempt on klass initialization, we could also have this situation if a class can be initialized both in the context of a carrier and a vthread. Since code executed in the context of the carriers is limited to library code there will also be very few cases of this, but I’ve seen at least one such case with LockSupport.

// the notifier in notify_internal.
// Note that we can have false positives where timed-park is not necessary.
bool do_timed_parked = has_unmounted_vthreads();
static int MAX_RECHECK_INTERVAL = 1000;
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this a constant? This is the same as the enter case, should there be only one?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I moved it to a global static.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hotspot-runtime [email protected] rfr Pull request is ready for review
Development

Successfully merging this pull request may close these issues.

3 participants