Re: [PATCH net-next v3 7/8] rust: Add read_poll_timeout functions

From: Alice Ryhl
Date: Wed Oct 16 2024 - 04:46:02 EST


On Wed, Oct 16, 2024 at 5:54 AM FUJITA Tomonori
<fujita.tomonori@xxxxxxxxx> wrote:
> +/// Polls periodically until a condition is met or a timeout is reached.
> +///
> +/// `op` is called repeatedly until `cond` returns `true` or the timeout is
> +/// reached. The return value of `op` is passed to `cond`.
> +///
> +/// `sleep_delta` is the duration to sleep between calls to `op`.
> +/// If `sleep_delta` is less than one microsecond, the function will busy-wait.
> +///
> +/// `timeout_delta` is the maximum time to wait for `cond` to return `true`.
> +///
> +/// This macro can only be used in a nonatomic context.
> +#[macro_export]
> +macro_rules! readx_poll_timeout {
> + ($op:expr, $cond:expr, $sleep_delta:expr, $timeout_delta:expr) => {{
> + #[cfg(CONFIG_DEBUG_ATOMIC_SLEEP)]
> + if !$sleep_delta.is_zero() {
> + // SAFETY: FFI call.
> + unsafe {
> + $crate::bindings::__might_sleep(
> + ::core::file!().as_ptr() as *const i8,
> + ::core::line!() as i32,
> + )
> + }
> + }

I wonder if we can use #[track_caller] and
core::panic::Location::caller [1] to do this without having to use a
macro? I don't know whether it would work, but if it does, that would
be super cool.

#[track_caller]
fn might_sleep() {
let location = core::panic::Location::caller();
// SAFETY: FFI call.
unsafe {
$crate::bindings::__might_sleep(
location.file().as_char_ptr(),
location.line() as i32,
)
}
}

Alice

[1]: https://doc.rust-lang.org/nightly/core/panic/struct.Location.html#method.caller