Re: [PATCH v5 2/3] implement ww_mutex abstraction for the Rust tree

From: Onur
Date: Mon Jul 07 2025 - 14:13:22 EST


On Mon, 07 Jul 2025 17:31:10 +0200
"Benno Lossin" <lossin@xxxxxxxxxx> wrote:

> On Mon Jul 7, 2025 at 3:39 PM CEST, Onur wrote:
> > On Mon, 23 Jun 2025 17:14:37 +0200
> > "Benno Lossin" <lossin@xxxxxxxxxx> wrote:
> >
> >> > We also need to take into consideration that the user want to
> >> > drop any lock in the sequence? E.g. the user acquires a, b and
> >> > c, and then drop b, and then acquires d. Which I think is
> >> > possible for ww_mutex.
> >>
> >> Hmm what about adding this to the above idea?:
> >>
> >> impl<'a, Locks> WwActiveCtx<'a, Locks>
> >> where
> >> Locks: Tuple
> >> {
> >> fn custom<L2>(self, action: impl FnOnce(Locks) -> L2) ->
> >> WwActiveCtx<'a, L2>; }
> >>
> >> Then you can do:
> >>
> >> let (a, c, d) = ctx.begin()
> >> .lock(a)
> >> .lock(b)
> >> .lock(c)
> >> .custom(|(a, _, c)| (a, c))
> >> .lock(d)
> >> .finish();
> >
> >
> > Instead of `begin` and `custom`, why not something like this:
> >
> > let (a, c, d) = ctx.init()
> > .lock(a)
> > .lock(b)
> > .lock(c)
> > .unlock(b)
> > .lock(d)
> > .finish();
> >
> > Instead of `begin`, `init` would be better naming to imply `fini`
> > on the C side, and `unlock` instead of `custom` would make the
> > intent clearer when dropping locks mid chain.
>
> I don't think that this `unlock` operation will work. How would you
> implement it?


We could link mutexes to locks using some unique value, so that we can
access locks by passing mutexes (though that sounds a bit odd).

Another option would be to unlock by the index, e.g.,:

let (a, c) = ctx.init()
.lock(a)
.lock(b)
.unlock::<1>()
.lock(c)
.finish();

The index approach would require us to write something very similar
to `Tuple` (with macro obviously) you proposed sometime ago.

We could also just go back to your `custom` but find a better name
for it (such as `retain`).

Regards,
Onur