Re: [PATCH v1 1/2] rust: Add support for calling a function exactly once

Boqun Feng
2025-11-10T16:15:00Z

On Mon, Nov 10, 2025 at 06:21:50PM +0900, FUJITA Tomonori wrote:
> On Fri, 7 Nov 2025 09:03:01 +0000
> Alice Ryhl <aliceryhl@google.com> wrote:
>
> >> That's my point (and probably also Andreas' point), we already has the
> >> type `SetOnce` to do this, no need for a `OnceLite` type if not
> >> necessary, and the fact that it can be zero'd by debugfs doesn't change
> >> it as I explained above.
> >
> > The SetOnce type doesn't do the same thing as OnceLite. SetOnce has
> > three different states, but OnceLite only needs two. I don't think we
> > should be reusing SetOnce here.

I mean 3 states should cover 2 states, right? In this case we only need
to support SetOnce<()>, so I think it's fine, see below:

>
> Yes, SetOnce provides three different states and the feature to set a
> value, which are not necessary for this case.
>
> So there are two design options: 1) to add a new minimal
> implementation that provides only the features required for this case,

Just to be clear 1) will still need to be in kernel::sync module, as per
our previous discussion.

> or 2) to use the exisiting, more complex implementation with richer
> functionality.
>

or we can just add it into `SetOnce<()>`, for exammple:

impl SetOnce<()> {
pub fn call_once<F: FnOnce()>(&self, f: F) -> bool {
// ORDERING: Relaxed is fine because we don't expect
// synchronization here.
let old = self.init.xchg(1, Relaxed);

// We are the first one here.
if old == 0 {
f();
return true;
}

return false;
}
}

Thoughts?

Regards,
Boqun

> Could we make a decision so that we can move forward?
>