File: util.rs

package info (click to toggle)
linux 6.18.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,742,780 kB
  • sloc: ansic: 26,780,494; asm: 272,079; sh: 148,752; python: 79,241; makefile: 57,741; perl: 36,527; xml: 19,542; cpp: 5,911; yacc: 4,939; lex: 2,950; awk: 1,607; sed: 30; ruby: 25
file content (27 lines) | stat: -rw-r--r-- 844 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// SPDX-License-Identifier: GPL-2.0

use kernel::prelude::*;
use kernel::time::{Delta, Instant, Monotonic};

/// Wait until `cond` is true or `timeout` elapsed.
///
/// When `cond` evaluates to `Some`, its return value is returned.
///
/// `Err(ETIMEDOUT)` is returned if `timeout` has been reached without `cond` evaluating to
/// `Some`.
///
/// TODO[DLAY]: replace with `read_poll_timeout` once it is available.
/// (https://lore.kernel.org/lkml/20250220070611.214262-8-fujita.tomonori@gmail.com/)
pub(crate) fn wait_on<R, F: Fn() -> Option<R>>(timeout: Delta, cond: F) -> Result<R> {
    let start_time = Instant::<Monotonic>::now();

    loop {
        if let Some(ret) = cond() {
            return Ok(ret);
        }

        if start_time.elapsed().as_nanos() > timeout.as_nanos() {
            return Err(ETIMEDOUT);
        }
    }
}