Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0
2
3use kernel::prelude::*;
4use kernel::time::{Delta, Instant, Monotonic};
5
6/// Wait until `cond` is true or `timeout` elapsed.
7///
8/// When `cond` evaluates to `Some`, its return value is returned.
9///
10/// `Err(ETIMEDOUT)` is returned if `timeout` has been reached without `cond` evaluating to
11/// `Some`.
12///
13/// TODO[DLAY]: replace with `read_poll_timeout` once it is available.
14/// (https://lore.kernel.org/lkml/20250220070611.214262-8-fujita.tomonori@gmail.com/)
15pub(crate) fn wait_on<R, F: Fn() -> Option<R>>(timeout: Delta, cond: F) -> Result<R> {
16 let start_time = Instant::<Monotonic>::now();
17
18 loop {
19 if let Some(ret) = cond() {
20 return Ok(ret);
21 }
22
23 if start_time.elapsed().as_nanos() > timeout.as_nanos() {
24 return Err(ETIMEDOUT);
25 }
26 }
27}