Skip to content

sync: Introduce the SetOnce struct #7418

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 17 commits into
base: master
Choose a base branch
from

Conversation

Daksh14
Copy link
Contributor

@Daksh14 Daksh14 commented Jun 24, 2025

The SetOnce data structure allows you to contain a T value and wait until the initialization of that T value in the Cell. It can only be initialized once and can be destroyed using into_inner or by dropping it.

The wait() allows you to block on a future which wakes up when the SetOnce is initalized

Motivation

After #7161 failed and was messy, we needed a new type that is like OnceCell but with tigter restrictions around T
Something similar in async_io in python https://docs.python.org/3/library/asyncio-sync.html#asyncio.Event

Solution

Proposal of the implementation of SetOnce data structure.

@github-actions github-actions bot added the R-loom-sync Run loom sync tests on this PR label Jun 24, 2025
@Daksh14 Daksh14 changed the title Introduce the set_once datastructure Introduce the SetOnce struct Jun 24, 2025
@Daksh14 Daksh14 force-pushed the daksh/add-setOnce-dst branch 8 times, most recently from c4a3dc9 to 8ae6ced Compare June 24, 2025 23:34
@Daksh14 Daksh14 changed the title Introduce the SetOnce struct sync: Introduce the SetOnce struct Jun 25, 2025
@Daksh14 Daksh14 changed the title sync: Introduce the SetOnce struct sync: Introduce the SetOnce struct Jun 25, 2025
@Darksonn Darksonn added A-tokio Area: The main tokio crate M-sync Module: tokio/sync labels Jun 27, 2025
Copy link
Member

@ADD-SP ADD-SP left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't looked at entire implementation yet, so I just left interface design comment.

@ADD-SP ADD-SP added the C-enhancement Category: A PR with an enhancement or bugfix. label Jun 29, 2025
@Daksh14 Daksh14 force-pushed the daksh/add-setOnce-dst branch from 65c72ec to ef2ed6a Compare June 30, 2025 18:14
@Daksh14 Daksh14 requested a review from Darksonn June 30, 2025 18:51
@Daksh14 Daksh14 force-pushed the daksh/add-setOnce-dst branch 3 times, most recently from 625c5ab to b42b3f1 Compare July 2, 2025 18:35
@Daksh14 Daksh14 requested a review from ADD-SP July 2, 2025 19:42
@Daksh14 Daksh14 force-pushed the daksh/add-setOnce-dst branch 2 times, most recently from fc53225 to 778d958 Compare July 4, 2025 19:47
@Daksh14 Daksh14 force-pushed the daksh/add-setOnce-dst branch 7 times, most recently from 96a4153 to 33f69a0 Compare July 21, 2025 19:15
@Daksh14 Daksh14 requested review from ADD-SP and Darksonn July 21, 2025 19:59
@Daksh14 Daksh14 force-pushed the daksh/add-setOnce-dst branch from 33f69a0 to 3f8b1c6 Compare July 21, 2025 21:45
Daksh14 added 17 commits July 22, 2025 11:23
The `SetOnce` data structure allows you to contain a T value and wait
until the initialization of that T value in the Cell. It can only be
initialized once and can be destroyed using `into_inner` or by dropping
it.

This is equivalent to the `asyncioEvent` class https://docs.python.org/3/library/asyncio-sync.html#asyncio.Event`

The wait() allows you to block on a future which wakes up when the
`SetOnce` is initalized.

Added tokio/tests/sync_set_once.rs
…ingError

Document each unsafe usage with safety commit and include reasoning
Use a mutex to ensure only one caller of `set` at a time.

Change `wait()` to call `get_wait()` and panic if none
Add `get_wait()` which waits until the value is set and checks
the state again and returns None if the state is still not Set.
Lock mutex inside `wait()` when checking value_set, we also
call `self.initialized()` after locking the mutex.

We also add custom error type for SetOnce called SetError
Update a doc comment
Remove &mut reference we don't need it
Remove some trailling space too so rustfmt doesn't complain
@Daksh14 Daksh14 force-pushed the daksh/add-setOnce-dst branch from 14ecc1a to 8bcd952 Compare July 22, 2025 15:23
@Daksh14 Daksh14 requested a review from Darksonn July 22, 2025 15:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio Area: The main tokio crate C-enhancement Category: A PR with an enhancement or bugfix. M-sync Module: tokio/sync R-loom-sync Run loom sync tests on this PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants