Skip to content

Use monotonic clock for relative timed waits#17

Open
guillerodriguez wants to merge 4 commits into
masterfrom
feat/use-monotonic-clock-v4
Open

Use monotonic clock for relative timed waits#17
guillerodriguez wants to merge 4 commits into
masterfrom
feat/use-monotonic-clock-v4

Conversation

@guillerodriguez

Copy link
Copy Markdown
Contributor

No description provided.

The timedWaitVMWaitLock macro duplicates the deadline calculation
already implemented by getTimeoutRelative(). Make the macro defer to
the function instead, so relative timeout deadlines are computed in
a single place.

Signed-off-by: Guillermo Rodríguez <grodriguez@ingelabs.com>
The OpenJDK class library code probes CLOCK_MONOTONIC availability
at runtime for use by JVM_NanoTime. Move the probe to common code
(time.c), run it early in the VM initialisation sequence, and expose
it as haveMonotonicClock().

Signed-off-by: Guillermo Rodríguez <grodriguez@ingelabs.com>
Introduce initReltimeCondVar() for initialising condition variables
used with relative timeouts, and use it for all such condvars (the
per-thread wait and park condvars, and VM-internal wait locks).

A timed park (threadPark) can take either a relative timeout or an
absolute wall-clock deadline, but currently both waits share the
per-thread park condvar, and a condvar can only be bound to one clock.
Split it in two: park_cv (relative and untimed parks) and park_cv_abs
(absolute deadlines), choosing the right one when parking. Unparking
signals both; signalling a condvar nobody waits on is harmless and
cheap.

All condvars are still bound to the default realtime clock, so no
change in behaviour; this puts the infrastructure in place for
switching relative timeouts to the monotonic clock.

Signed-off-by: Guillermo Rodríguez <grodriguez@ingelabs.com>
All relative timeouts were computed against the wall clock, so
stepping the system time backward stretched them and stepping it
forward truncated them.

Fix this by binding the condvars used for relative timed waits to
CLOCK_MONOTONIC, if available, and make getTimeoutRelative() use
the same clock when computing the deadline.

If the monotonic clock cannot be used, behaviour is unchanged and
a warning is printed at startup.

Signed-off-by: Guillermo Rodríguez <grodriguez@ingelabs.com>
@guillerodriguez
guillerodriguez requested a review from phvega July 20, 2026 11:21
@guillerodriguez guillerodriguez changed the title Feat/use monotonic clock v4 Feat/use monotonic clock Jul 20, 2026
@guillerodriguez guillerodriguez changed the title Feat/use monotonic clock Use monotonic clock for relative timed waits Jul 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants