// SPDX-License-Identifier: GPL-2.012use kernel::prelude::*;3use kernel::time::{Delta, Instant, Monotonic};45/// Wait until `cond` is true or `timeout` elapsed.6///7/// When `cond` evaluates to `Some`, its return value is returned.8///9/// `Err(ETIMEDOUT)` is returned if `timeout` has been reached without `cond` evaluating to10/// `Some`.11///12/// TODO[DLAY]: replace with `read_poll_timeout` once it is available.13/// (https://lore.kernel.org/lkml/[email protected]/)14pub(crate) fn wait_on<R, F: Fn() -> Option<R>>(timeout: Delta, cond: F) -> Result<R> {15let start_time = Instant::<Monotonic>::now();1617loop {18if let Some(ret) = cond() {19return Ok(ret);20}2122if start_time.elapsed().as_nanos() > timeout.as_nanos() {23return Err(ETIMEDOUT);24}25}26}272829