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

How to use PiFutex::lock_pi_until() with correct clock? #3

Closed
rtzoeller opened this issue Aug 14, 2022 · 3 comments
Closed

How to use PiFutex::lock_pi_until() with correct clock? #3

rtzoeller opened this issue Aug 14, 2022 · 3 comments

Comments

@rtzoeller
Copy link
Contributor

I'm trying to use PiFutex::lock_pi_until() and am hitting an issue with FUTEX_LOCK_PI and std::time::Instant using different clocks. It's manifesting as PiFutex::lock_pi_until() returning nearly immediately, regardless of the timeout value.

For example,

let futex = linux_futex::PiFutex::<linux_futex::Private>::new(1); // Non-zero -> locked by someone else
println!("{:?}", std::time::Instant::now());
let result = futex.lock_pi_until(std::time::Instant::now() + std::time::Duration::from_secs(2));
println!("{:?}", std::time::Instant::now());
dbg!(result);

outputs

Instant { tv_sec: 12663, tv_nsec: 14076982 }
Instant { tv_sec: 12663, tv_nsec: 14100659 }
[tests/tests.rs:144] result = Err(
    TimedOut,
)

FUTEX_LOCK_PI is documented as using CLOCK_REALTIME, while std::time::Instant (used by lock_pi_until()) is implemented with CLOCK_MONOTONIC.

  1. How is PiFutex::lock_pi_until() intended to be used? Is there a recommendation for converting between std::time::SystemTime (which uses CLOCK_REALTIME) and std::time::Instant?
  2. Have you considered adding support for FUTEX_LOCK_PI2, which allows selecting a clock and could be used with CLOCK_MONOTONIC?

Thanks!

@rtzoeller
Copy link
Contributor Author

rtzoeller commented Aug 14, 2022

It seems like modifying linux-futex to use FUTEX_LOCK_PI2 instead "just works", but there's a much higher Linux kernel requirement associated with that. I'll at least add it to rust-lang/libc (PR), since it seems to be absent.

@m-ou-se
Copy link
Owner

m-ou-se commented Aug 14, 2022

Thanks! It's fixed in 8f75c58. lock_pi_until now accepts either an SystemTime or an Instant, and picks either FUTEX_LOCK_PI or FUTEX_LOCK_PI2 depending on that.

Published as 0.1.2 to crates.io just now.

@rtzoeller
Copy link
Contributor Author

@m-ou-se thanks! This new release seems to fix it.

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

No branches or pull requests

2 participants