Re: [PATCH v2 2/2] rust: sync: Introduce LazyInit

From: lyude

Date: Mon Jun 01 2026 - 18:26:44 EST


On Mon, 2026-06-01 at 08:27 +0000, Alice Ryhl wrote:
> I'm not a big fan of using these kinds of tricks to access the
> contents
> of a mutex after unlocking it. Could we instead use a struct like
> this:
>
> struct LazyInit<T> {
>     data: UnsafeCell<MaybeUninit<T>>,
>     set: AtomicBool,
>     lock: Mutex<()>,
> }
>
> or even:
>
> struct LazyInit<T> {
>     data: SetOnce<T>,
>     lock: Mutex<()>,
> }
>
> I think this logic will be simpler for everyone.

> By the way, another option is to use a similar strategy to
> https://lore.kernel.org/rust-for-linux/20260523-upgrade-poll-v4-2-f5b4c747eac2@xxxxxxxxxx/
> where you just use SetOnce and protect calls to `populate` by another
> mutex in the structure. Then you don't need a separate LazyInit.

ooo - yeah, that definitely works for me and would be preferable. I
still will like to make one change to SetOnce that I think isn't too
crazy: writing up a `reset()` function for SetOnce that works through a
&mut like what I had here for LazyInit, since that would be quite
helpful to have in the gem shmem free_callback() function.

>
> Alice