Skip to content

Update documentation of as_ptr function of Atomic$Int to clarify circumstances of usage #139637

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 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 9 additions & 21 deletions library/core/src/sync/atomic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1115,13 +1115,8 @@ impl AtomicBool {
///
/// Doing non-atomic reads and writes on the resulting boolean can be a data race.
/// This method is mostly useful for FFI, where the function signature may use
/// `*mut bool` instead of `&AtomicBool`.
///
/// Returning an `*mut` pointer from a shared reference to this atomic is safe because the
/// atomic types work with interior mutability. All modifications of an atomic change the value
/// through a shared reference, and can do so safely as long as they use atomic operations. Any
/// use of the returned raw pointer requires an `unsafe` block and still has to uphold the same
/// restriction: operations on it must be atomic.
/// `*mut bool` instead of `&AtomicBool`. All modifications of an atomic change the value
/// through a shared reference, and can do so safely as long as they use atomic operations.
///
/// # Examples
///
Expand All @@ -1134,6 +1129,8 @@ impl AtomicBool {
/// }
///
/// let mut atomic = AtomicBool::new(true);
///
/// // SAFETY: `my_atomic_op` only uses atomic operations so it will not lead to a data race.
/// unsafe {
/// my_atomic_op(atomic.as_ptr());
/// }
Expand Down Expand Up @@ -2308,13 +2305,8 @@ impl<T> AtomicPtr<T> {
///
/// Doing non-atomic reads and writes on the resulting pointer can be a data race.
/// This method is mostly useful for FFI, where the function signature may use
/// `*mut *mut T` instead of `&AtomicPtr<T>`.
///
/// Returning an `*mut` pointer from a shared reference to this atomic is safe because the
/// atomic types work with interior mutability. All modifications of an atomic change the value
/// through a shared reference, and can do so safely as long as they use atomic operations. Any
/// use of the returned raw pointer requires an `unsafe` block and still has to uphold the same
/// restriction: operations on it must be atomic.
/// `*mut *mut T` instead of `&AtomicPtr<T>`. All modifications of an atomic change the value
/// through a shared reference, and can do so safely as long as they use atomic operations.
///
/// # Examples
///
Expand Down Expand Up @@ -3401,12 +3393,8 @@ macro_rules! atomic_int {
/// Doing non-atomic reads and writes on the resulting integer can be a data race.
/// This method is mostly useful for FFI, where the function signature may use
#[doc = concat!("`*mut ", stringify!($int_type), "` instead of `&", stringify!($atomic_type), "`.")]
///
/// Returning an `*mut` pointer from a shared reference to this atomic is safe because the
/// atomic types work with interior mutability. All modifications of an atomic change the value
/// through a shared reference, and can do so safely as long as they use atomic operations. Any
/// use of the returned raw pointer requires an `unsafe` block and still has to uphold the same
/// restriction: operations on it must be atomic.
/// All modifications of an atomic change the value through a shared reference, and can do so safely
/// as long as they use atomic operations.
///
/// # Examples
///
Expand All @@ -3420,7 +3408,7 @@ macro_rules! atomic_int {
///
#[doc = concat!("let atomic = ", stringify!($atomic_type), "::new(1);")]
///
/// // SAFETY: Safe as long as `my_atomic_op` is atomic.
/// // SAFETY: `my_atomic_op` only uses atomic operations so it will not lead to a data race.
/// unsafe {
/// my_atomic_op(atomic.as_ptr());
/// }
Expand Down
Loading