Re: [PATCH v5 0/3] rust: add `ww_mutex` support
From: Onur Özkan
Date: Wed Jul 30 2025 - 06:25:20 EST
On Tue, 29 Jul 2025 19:15:12 +0200
"Benno Lossin" <lossin@xxxxxxxxxx> wrote:
> > - The second note is about how EDEADLK is handled. On the C side, it
> > looks like some code paths may not release all the previously locked
> > mutexes or have a special/custom logic when locking returns EDEADLK
> > (see [3]). So, handling EDEADLK automatically (pointed
> > in [1]) can be quite useful for most cases, but that could also be a
> > limitation in certain scenarios.
> >
> > I was thinking we could provide an alternative version of each
> > `lock*` function that accepts a closure which is called on the
> > EDEADLK error. This way, we can support both auto-release locks and
> > custom logic for handling EDEADLK scenarios.
> >
> > Something like this (just a dummy code for demonstration):
> >
> > ctx.lock_and_handle_edeadlk(|active_locks| {
> > // user-defined handling here
> > });
>
> But this function wouldn't be locking any additional locks, right?
>
> I think the closure makes sense to give as a way to allow custom code.
> But we definitely should try to get the common use-cases closure-free
> (except of course they run completely custom code to their specific
> use-case).
>
> We can also try to invent a custom return type that is used instead of
> `Result`. So for example:
>
> let a: WwMutex<'_, A>;
> let b: WwMutex<'_, B>;
> let ctx: WwAcquireCtx<'_>;
>
> ctx.enter() // EnteredContext<'_, ()>
> .lock(a) // LockAttempt<'_, A, ()>
> .or_err(a)? // EnteredContext<'_, (A,)>
> .lock(b) // LockAttempt<'_, B, (A,)>
> .or_lock_slow(a, b) // Result<EnteredContext<'_, (A, B,)>>
> ?.finish() // (WwMutexGuard<'_, A>, WwMutexGuard<'_,
> B>)
>
> But no idea if this is actually useful...
That wouldn't work if the user wants to lock `a` and `b` in separate
calls, right? If user wants to lock `a` right away and lock `b` later
under certain conditions (still in the same context as `a`), then to
automatically release `a`, we have to keep the locked mutexes in some
dynamic list inside `EnteredContext` so we can access all the locked
mutexes when we want to unlock them on EDEADLK.
>
>
> What I think would be a good way forward would be to convert some
> existing C uses of `WwMutex` to the intended Rust API and see how it
> looks. Best to cover several different kinds of uses.
Good idea. I will try find sometime to do that during next week.
Regards,
Onur