-
Notifications
You must be signed in to change notification settings - Fork 139
[atomic] Initial commit of Atomic<T>
#2959
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
base: main
Are you sure you want to change the base?
Conversation
Summary of ChangesHello @joshlf, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the Highlights
π§ New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with π and π on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
|
This is an alternate design to #2956, this one anchored on |
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.
Code Review
This pull request introduces a generic Atomic<T> wrapper, which is a useful abstraction for atomic operations on various types. The overall approach using a Kl trait to select the appropriate underlying standard atomic type is clever.
However, as an initial commit, there are several areas that need attention before this can be considered complete and safe. The most critical issue is the pervasive use of // SAFETY: TODO comments throughout the new src/atomic.rs file. Given the heavy use of unsafe code, providing detailed safety justifications is paramount for ensuring the soundness of this abstraction.
Additionally, there are several TODO comments for documentation and missing trait implementations (like Debug, Copy, Clone) on Atomic<T> that should be addressed. Some of the tests also appear to be using methods (swap, compare_exchange, etc.) that are not yet implemented on Atomic<T>, causing them to fail compilation.
I've left specific comments on these points.
| // SAFETY: TODO | ||
| unsafe { u.prim } |
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.
This unsafe block lacks a safety justification. Reading from a union field that was not the last one written to is undefined behavior unless the types are layout-compatible. Please provide a detailed safety comment explaining why it's safe to transmute t into <T::Atomic as AtomicOps>::Value. This likely relies on T being IntoBytes (which implies no padding) and having a size that fits within the primitive type.
| // SAFETY: TODO | ||
| ManuallyDrop::into_inner(unsafe { u.t }) |
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.
src/atomic.rs
Outdated
| // SAFETY: TODO | ||
| unsafe { mu.assume_init() } |
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.
This is a critical unsafe block that lacks a safety justification. The call to mu.assume_init() is only sound if the bytes read from the atomic operation correspond to a valid instance of T.
The current implementation of load has a bound of T: Kl, but Kl does not require T: FromBytes. This means a user could implement Kl for a type with validity invariants (like bool), and calling load would be unsound.
To ensure soundness, you should either:
- Add a
T: FromBytesbound toload. - Make
Klanunsafetrait that requires implementors to guarantee thatTisFromBytes. - If you intend to support types that are not
FromBytes,loadwould need to perform validation (e.g., requireT: TryFromBytesand checkis_bit_valid).
Please add a detailed safety comment explaining how the invariants of Atomic<T> guarantee the soundness of this operation.
src/atomic.rs
Outdated
| /// # Safety | ||
| /// | ||
| /// TODO | ||
| #[doc(hidden)] | ||
| pub unsafe trait AtomicSelector<const N: usize> { | ||
| type AtomicType; | ||
| const PADDING: usize; | ||
| } | ||
|
|
||
| /// # Safety | ||
| /// | ||
| /// TODO | ||
| #[doc(hidden)] |
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.
src/atomic.rs
Outdated
| macro_rules! impl_atomic_selector { | ||
| ($atomic:ty [$value:ty]; $($size:expr),+) => { | ||
| // SAFETY: TODO | ||
| unsafe impl AtomicOps for $atomic { | ||
| type Value = $value; | ||
|
|
||
| #[inline(always)] | ||
| fn new(value: Self::Value) -> Self { | ||
| Self::new(value) | ||
| } | ||
|
|
||
| #[inline(always)] | ||
| fn load(&self, ordering: Ordering) -> Self::Value { | ||
| self.load(ordering) | ||
| } | ||
|
|
||
| #[inline(always)] | ||
| fn store(&self, value: Self::Value, ordering: Ordering) { | ||
| self.store(value, ordering) | ||
| } | ||
| } | ||
|
|
||
| $( | ||
| // SAFETY: TODO | ||
| unsafe impl AtomicSelector<$size> for () { | ||
| type AtomicType = $atomic; | ||
| const PADDING: usize = core::mem::size_of::<$atomic>() - $size; | ||
| } | ||
| )+ | ||
| }; | ||
| } |
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.
| fn test_atomic_u32() { | ||
| let a = Atomic::new(10u32); | ||
| assert_eq!(a.load(Ordering::SeqCst), 10); | ||
| a.store(20, Ordering::SeqCst); | ||
| assert_eq!(a.load(Ordering::SeqCst), 20); | ||
| assert_eq!(a.swap(30, Ordering::SeqCst), 20); | ||
| assert_eq!(a.load(Ordering::SeqCst), 30); | ||
| } |
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.
src/atomic.rs
Outdated
| ManuallyDrop::into_inner(unsafe { u.t }) | ||
| } | ||
|
|
||
| trait Kl { |
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.
src/atomic.rs
Outdated
| /// TODO | ||
| #[repr(transparent)] | ||
| #[allow(missing_copy_implementations, missing_debug_implementations)] | ||
| pub struct Atomic<T: Kl> { | ||
| // INVARIANT: The first `size_of::<T>()` bytes of `atomic` have the same | ||
| // validity as `T`, and logically own a `T`. | ||
| atomic: T::Atomic, | ||
| _marker: PhantomData<T>, | ||
| } | ||
|
|
||
| // TODO: Impl traits (Copy, Clone, Debug, Send, Sync, etc). |
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.
dd6dc84 to
89ac12e
Compare
gherrit-pr-id: Ge6a504780e973e00e15ad398613d1473ad57ae64
89ac12e to
f46f756
Compare
Atomic<T>Β #2959Latest Update: v3 β Compare vs v2
π Full Patch History
Links show the diff between the row version and the column version.
Stacked PRs enabled by GHerrit.