[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <D8F8GNNJ52JQ.1ZXW618MPFIYD@nvidia.com>
Date: Fri, 14 Mar 2025 00:11:31 +0900
From: "Alexandre Courbot" <acourbot@...dia.com>
To: "Danilo Krummrich" <dakr@...nel.org>
Cc: "Miguel Ojeda" <ojeda@...nel.org>, "Alex Gaynor"
<alex.gaynor@...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@...nel.org>, "Alice Ryhl"
<aliceryhl@...gle.com>, "Trevor Gross" <tmgross@...ch.edu>,
<rust-for-linux@...r.kernel.org>, <linux-kernel@...r.kernel.org>
Subject: Re: [PATCH] rust/revocable: add try_with() convenience method
On Thu Mar 13, 2025 at 11:37 PM JST, Danilo Krummrich wrote:
> Hi Alex,
>
> Thanks for looking into this!
>
> On Thu, Mar 13, 2025 at 09:40:59PM +0900, Alexandre Courbot wrote:
>> Revocable::try_access() returns a guard through which the wrapped object
>> can be accessed. Code that can sleep is not allowed while the guard is
>> held ; thus, it is common that the caller will explicitly need to drop
>> it before running sleepable code, e.g:
>>
>> let b = bar.try_access()?;
>> let reg = b.readl(...);
>>
>> // Don't forget this or things could go wrong!
>> drop(b);
>>
>> something_that_might_sleep();
>>
>> let b = bar.try_access()?;
>> let reg2 = b.readl(...);
>
> Ideally, we get klint to protect us against those kind of mistakes too.
Yes, but even with klint I find it easier to delimitate the critical
sections explicitly and not having to remember about dropping the guard
when needed.
>
>> This is arguably error-prone. try_with() and try_with_ok() provides an
>> arguably safer alternative, by taking a closure that is run while the
>> guard is held, and by dropping the guard automatically after the closure
>> completes. This way, code can be organized more clearly around the
>> critical sections and the risk is forgetting to release the guard when
>> needed is considerably reduced:
>>
>> let reg = bar.try_with_ok(|b| b.readl(...))?;
>>
>> something_that_might_sleep();
>>
>> let reg2 = bar.try_with_ok(|b| b.readl(...))?;
>
> However, that's much more convenient and a great improvement.
>
> Feel free to add
>
> Acked-by: Danilo Krummrich <dakr@...nel.org>
Thanks!
>
>>
>> Unlike try_access() which returns an Option, try_with() and
>> try_with_ok() return Err(ENXIO) if the object cannot be acquired. The
>> Option returned by try_access() is typically converted to an error in
>> practice, so this saves one step for the caller.
>>
>> try_with() requires the callback itself to return a Result that is
>> passed to the caller. try_with_ok() accepts a callback that never fails.
>>
>> Signed-off-by: Alexandre Courbot <acourbot@...dia.com>
>
> Since I proposed something like that in one of the nova threads (and in Zulip),
> feel free to also add
>
> Suggested-by: Danilo Krummrich <dakr@...nel.org>
Will do. I wasn't aware of this discussion, please let me know if I have
omitted something from your suggestion (like better method names, for
instance :)).
Powered by blists - more mailing lists