[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CAH5fLgijsRK3funsGuG6nbK26C+s6m0nO0i83RYD2cO3z7L22Q@mail.gmail.com>
Date: Fri, 8 Dec 2023 08:37:27 +0100
From: Alice Ryhl <aliceryhl@...gle.com>
To: Tiago Lam <tiagolam@...il.com>
Cc: Miguel Ojeda <ojeda@...nel.org>,
Alex Gaynor <alex.gaynor@...il.com>,
Wedson Almeida Filho <wedsonaf@...il.com>,
Boqun Feng <boqun.feng@...il.com>, Gary Guo <gary@...yguo.net>,
Björn Roy Baron <bjorn3_gh@...tonmail.com>,
Benno Lossin <benno.lossin@...ton.me>,
Andreas Hindborg <a.hindborg@...sung.com>,
Peter Zijlstra <peterz@...radead.org>,
Ingo Molnar <mingo@...hat.com>, Will Deacon <will@...nel.org>,
Waiman Long <longman@...hat.com>,
rust-for-linux@...r.kernel.org, linux-kernel@...r.kernel.org
Subject: Re: [PATCH 2/2] rust: sync: add `CondVar::wait_timeout`
On Wed, Dec 6, 2023 at 6:05 PM Tiago Lam <tiagolam@...il.com> wrote:
>
>
> On 06/12/2023 10:09, Alice Ryhl wrote:
> [...]
> > diff --git a/rust/kernel/sync/condvar.rs b/rust/kernel/sync/condvar.rs
> > index 9861c6749ad0..a6a6b6ab0c39 100644
> > --- a/rust/kernel/sync/condvar.rs
> > +++ b/rust/kernel/sync/condvar.rs
> > @@ -120,6 +120,63 @@ fn wait_internal<T: ?Sized, B: Backend>(&self, wait_state: u32, guard: &mut Guar
> > unsafe { bindings::finish_wait(self.wait_list.get(), wait.get()) };
> > }
> >
> > + /// Atomically releases the given lock (whose ownership is proven by the guard) and puts the
> > + /// thread to sleep. It wakes up when notified by [`CondVar::notify_one`] or
> > + /// [`CondVar::notify_all`], or when the thread receives a signal.
> > + ///
> > + /// Returns whether there is a signal pending.
> > + fn wait_internal_timeout<T, B>(
> > + &self,
> > + wait_state: u32,
> > + guard: &mut Guard<'_, T, B>,
> > + timeout: u64,
> > + ) -> u64
> > + where
> > + T: ?Sized,
> > + B: Backend,
> > + {
> > + let wait = Opaque::<bindings::wait_queue_entry>::uninit();
> > +
> > + // SAFETY: `wait` points to valid memory.
> > + unsafe { bindings::init_wait(wait.get()) };
> > +
> > + // SAFETY: Both `wait` and `wait_list` point to valid memory.
> > + unsafe {
> > + bindings::prepare_to_wait_exclusive(self.wait_list.get(), wait.get(), wait_state as _)
> > + };
> > +
> > + // SAFETY: Switches to another thread.
> > + let timeout =
> > + guard.do_unlocked(|| unsafe { bindings::schedule_timeout(timeout as _) as _ });
>
> It looks like `schedule_timeout()` simply calls `schedule()` when the
> timeout passed is `MAX_SCHEDULE_TIMEOUT`, so `wait_internal_timeout()`
> could be merged together with the already existing `wait_internal()`,
> where `wait_internal()` would always call `schedule_timeout()`? I may be
> missing something, so just wondering why you decided to introduce
> another method.
Ah, nice! I didn't notice that I could combine them.
> > + /// Releases the lock and waits for a notification in interruptible mode.
> > + ///
> > + /// Atomically releases the given lock (whose ownership is proven by the guard) and puts the
> > + /// thread to sleep. It wakes up when notified by [`CondVar::notify_one`] or
> > + /// [`CondVar::notify_all`], or when a timeout occurs, or when the thread receives a signal.
> > + ///
> > + /// Returns whether there is a signal pending.
> > + #[must_use = "wait_timeout returns if a signal is pending, so the caller must check the return value"]
> > + pub fn wait_timeout<T: ?Sized, B: Backend>(
> > + &self,
> > + guard: &mut Guard<'_, T, B>,
> > + jiffies: u64,
> > + ) -> CondVarTimeoutResult {
>
> Should this be called `wait_timeout_interruptable` instead, so that if
> we need to add one using the `TASK_INTERRUPTIBLE` state later we don't
> need to modfy it again? It also matches the
> `schedule_timeout_interruptible` one in the kernel (although that's not
> a reason to change it just in itself).
I don't mind changing the names, but in this patch I was just
consistent with what was already there.
> > +/// The return type of `wait_timeout`.
> > +pub enum CondVarTimeoutResult {
> > + /// The timeout was reached.
> > + Timeout,
> > + /// Somebody woke us up.
> > + Woken {
> > + /// Remaining sleep duration.
> > + jiffies: u64,
> > + },
> > + /// A signal occurred.
> > + Signal {
> > + /// Remaining sleep duration.
> > + jiffies: u64,
> > + },
> > +}
>
>
> Is `Signal` and `Woken` only going to hold a single value? Would it be
> best represented as a tuple struct instead, like so?
>
> pub enum CondVarTimeoutResult {
> /// The timeout was reached.
> Timeout,
> /// Somebody woke us up.
> Woken (u64),
> /// A signal occurred.
> Signal (u64),
> }
I could do that, but I like the explicitly named version as it makes
it clear that the unit is jiffies.
Alice
Powered by blists - more mailing lists