-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Daksh14
wants to merge
17
commits into
tokio-rs:master
Choose a base branch
from
Daksh14:daksh/add-setOnce-dst
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
c4a3dc9
to
8ae6ced
Compare
SetOnce
struct
ADD-SP
reviewed
Jun 29, 2025
There was a problem hiding this 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.
Darksonn
reviewed
Jun 30, 2025
65c72ec
to
ef2ed6a
Compare
ADD-SP
reviewed
Jul 1, 2025
ADD-SP
reviewed
Jul 1, 2025
625c5ab
to
b42b3f1
Compare
ADD-SP
reviewed
Jul 3, 2025
Darksonn
reviewed
Jul 4, 2025
fc53225
to
778d958
Compare
96a4153
to
33f69a0
Compare
33f69a0
to
3f8b1c6
Compare
Darksonn
reviewed
Jul 22, 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. 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
14ecc1a
to
8bcd952
Compare
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 usinginto_inner
or by dropping it.The wait() allows you to block on a future which wakes up when the
SetOnce
is initalizedMotivation
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.