Files
linux-cryptodev-2.6/drivers/gpu/nova-core/util.rs
Alexandre Courbot 4092e1b412 gpu: nova-core: replace Duration with Delta
The kernel's `Delta` type was not available when the `wait_on` function
was introduced. Now that it is, switch to it as it is more compact than
`Duration` and cannot panic.

Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
Link: https://lore.kernel.org/r/20250624-nova-delta-v1-1-b37d75a593ac@nvidia.com
Signed-off-by: Danilo Krummrich <dakr@kernel.org>
2025-06-25 20:13:32 +02:00

48 lines
1.3 KiB
Rust

// SPDX-License-Identifier: GPL-2.0
use kernel::prelude::*;
use kernel::time::{Delta, Instant};
pub(crate) const fn to_lowercase_bytes<const N: usize>(s: &str) -> [u8; N] {
let src = s.as_bytes();
let mut dst = [0; N];
let mut i = 0;
while i < src.len() && i < N {
dst[i] = (src[i] as char).to_ascii_lowercase() as u8;
i += 1;
}
dst
}
pub(crate) const fn const_bytes_to_str(bytes: &[u8]) -> &str {
match core::str::from_utf8(bytes) {
Ok(string) => string,
Err(_) => kernel::build_error!("Bytes are not valid UTF-8."),
}
}
/// 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::now();
loop {
if let Some(ret) = cond() {
return Ok(ret);
}
if start_time.elapsed().as_nanos() > timeout.as_nanos() {
return Err(ETIMEDOUT);
}
}
}