xref: /linux/drivers/gpu/nova-core/util.rs (revision 07fdad3a93756b872da7b53647715c48d0f4a2d0)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 use kernel::prelude::*;
4 use 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/)
15 pub(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 }
28