Re: [PATCH v12 5/6] rust: ww_mutex: implement LockSet
From: Gary Guo
Date: Mon Oct 12 2026 - 08:05:51 EST
On Mon Oct 12, 2026 at 11:26 AM BST, Onur Özkan wrote:
> LockSet is a high-level and safe API built on top of
> ww_mutex which provides a safe and easy to use API
> while keeping the ww_mutex semantics.
>
> When EDEADLK is hit it drops all held locks and waits for the
> contended mutex. It then retries the given (by the user)
> locking algorithm with that mutex held and the same acquire
> context until it succeeds.
Do we really need this, or this can be achieved by simple retry loops?
fn retry_if_deadlock<R>(f: impl FnOnce() -> Result<R>) -> Result<R> {
loop {
match f() {
Err(EDEADLK) => (),
e => return e,
}
}
}
stack_pin_init!(let acq_ctx = AcquireCtx::new(&class));
retry_if_deadlock(|| {
let mut gaurd1 = acq_ctx.lock(&mutex1)?;
let mut guard2 = acq_ctx.lock(&mutex2)?;
acq_ctx.done();
*guard1 += 1;
*guard2 += 1;
});
I think the only difference is that the `LockSet` acquires the contending mutex
first. However, I imagine we can solve this by having a wrapped `AcquireCtx`
that provides that.
Best,
Gary
>
> Signed-off-by: Onur Özkan <work@xxxxxxxxxxxxx>
> ---
> rust/kernel/sync/lock/ww_mutex.rs | 6 +
> rust/kernel/sync/lock/ww_mutex/lock_set.rs | 449 +++++++++++++++++++++
> 2 files changed, 455 insertions(+)
> create mode 100644 rust/kernel/sync/lock/ww_mutex/lock_set.rs