Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

macos: use absolute timeouts for condition variables #4372

Closed
bnoordhuis opened this issue Mar 25, 2024 · 1 comment
Closed

macos: use absolute timeouts for condition variables #4372

bnoordhuis opened this issue Mar 25, 2024 · 1 comment

Comments

@bnoordhuis
Copy link
Member

Libuv's uv_cond_timedwait() uses pthread_cond_timedwait() with an absolute timeout on most Unices but pthread_cond_timedwait_relative_np() on macOS.

On the one hand, relative timeouts are more efficient because there's no need to query the current system time first.

On the other hand, relative timeouts block too long if the thread is preempted at an inopportune time, impacting performance:

struct timespec t = {0, 1};
// <preempted>
pthread_cond_timedwait_relative_np(cond, mutex, &t); // sleeps

With an absolute timeout, the call would have returned immediately because the deadline is already in the past.

The reason libuv uses pthread_cond_timedwait_relative_np() is because macOS at the time didn't support pthread_condattr_setclock(CLOCK_MONOTONIC) but I'm fairly sure macOS >= 11 does.

bnoordhuis added a commit to bnoordhuis/libuv that referenced this issue Mar 25, 2024
Before this commit, libuv used pthread_cond_timedwait_relative_np() on
macOS out of necessity, because that platform did not support absolute
timeouts back when uv_cond_timedwait() was added.

Absolute timeouts are oblivious to preemption. Relative timeouts on the
other hand suffer from skew when preempted between computing the timeout
and waiting, because the thread ends up waiting longer than it needs to.

Libuv's API uses relative timeouts and is therefore not immune to that
issue but this commit should make it less bad and make libuv on macOS
align more closely with other platforms.

Fixes: libuv#4372
@bnoordhuis
Copy link
Member Author

Closing for now, still doesn't seem to be supported.

@bnoordhuis bnoordhuis closed this as not planned Won't fix, can't repro, duplicate, stale Mar 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant