-
Notifications
You must be signed in to change notification settings - Fork 247
Unify lazy atomics in entropy backends #804
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
tamird
wants to merge
1
commit into
rust-random:master
Choose a base branch
from
tamird:cleanup-atomics
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.
+153
−134
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
This file was deleted.
Oops, something went wrong.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| //! Lazily caches a `bool` in an `AtomicU8`. | ||
| //! | ||
| //! Initialization is intentionally unsynchronized: concurrent callers may race | ||
| //! and run `init` more than once. Once a value is produced, it is cached and | ||
| //! reused by subsequent calls. | ||
| //! | ||
| //! Uses `Ordering::Relaxed` because this helper only publishes the cached | ||
| //! value itself. | ||
|
|
||
| use core::sync::atomic::{AtomicU8, Ordering}; | ||
|
|
||
| pub(crate) struct LazyBool(AtomicU8); | ||
|
|
||
| impl LazyBool { | ||
| const UNINIT: u8 = 2; | ||
|
|
||
| pub const fn new() -> Self { | ||
| Self(AtomicU8::new(Self::UNINIT)) | ||
| } | ||
|
|
||
| #[cold] | ||
| fn do_init(&self, init: impl FnOnce() -> bool) -> bool { | ||
| let val = u8::from(init()); | ||
| self.0.store(val, Ordering::Relaxed); | ||
| val != 0 | ||
| } | ||
|
|
||
| #[inline] | ||
| pub fn unsync_init(&self, init: impl FnOnce() -> bool) -> bool { | ||
| let val = self.0.load(Ordering::Relaxed); | ||
| if val != Self::UNINIT { | ||
| val != 0 | ||
| } else { | ||
| self.do_init(init) | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
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.
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.
Why are you casting from
NonNull<c_void>to*mut c_void?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.
That's not what is happening here. We are using
as_ptr()to get the*mut voidand casting it toGetRandomFn. This is better than casting theNonNulldirectly because it doesn't rely onNonNullbeingrepr(transparent).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.
By "casting" I meant the
fptr.as_ptr()part.I don't think it's "better".
repr(transparent)is a promise made bystdwhich forNonNullwas introduced in Rust 1.29. We can rely on this property no less than*mut c_voidbeing castable to a function pointer. For example, see rust-lang/rust#49220.Uh oh!
There was an error while loading. Please reload this page.
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.
Hm, looking into it a bit more carefully it looks like it's not as clear cut as I would've liked. I asked about it here.
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.
Seems better to sidestep this potential problem by just casting the raw pointer. Yes?
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.
Yeah, it looks like this aspect is surprisingly (in a bad way) under-specified...