Add a safe wrapper around schedule_timeout() that takes and returns Jiffies, handling the c_long conversion internally. Signed-off-by: Danilo Krummrich --- rust/kernel/task.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/rust/kernel/task.rs b/rust/kernel/task.rs index 3336df493dec..a733816fd5dd 100644 --- a/rust/kernel/task.rs +++ b/rust/kernel/task.rs @@ -10,6 +10,7 @@ pid_namespace::PidNamespace, prelude::*, sync::aref::ARef, + time::Jiffies, types::{NotThreadSafe, Opaque}, }; use core::{ @@ -20,6 +21,21 @@ /// A sentinel value used for infinite timeouts. pub const MAX_SCHEDULE_TIMEOUT: c_long = c_long::MAX; +/// Sleeps for the given timeout in [`Jiffies`], or until woken. +/// +/// Returns the remaining time in [`Jiffies`], or zero if the timeout expired. The task state +/// should be set before calling this. +/// +/// Timeouts at or above [`MAX_SCHEDULE_TIMEOUT`] mean an indefinite wait. In that case, the return +/// value is `MAX_SCHEDULE_TIMEOUT` as [`Jiffies`]. +#[inline] +pub fn schedule_timeout(timeout: Jiffies) -> Jiffies { + let timeout = timeout.try_into().unwrap_or(MAX_SCHEDULE_TIMEOUT); + + // SAFETY: `schedule_timeout()` is always safe to call. + unsafe { bindings::schedule_timeout(timeout) as Jiffies } +} + /// Bitmask for tasks that are sleeping in an interruptible state. pub const TASK_INTERRUPTIBLE: c_int = bindings::TASK_INTERRUPTIBLE as c_int; /// Bitmask for tasks that are sleeping in an uninterruptible state. -- 2.56.0