Skip to content

Commit 3209d2d

Browse files
committed
Correct documentation for atomic_from_ptr
* Remove duplicate alignment note that mentioned `AtomicBool` with other types * Update safety requirements about when non-atomic operations are allowed
1 parent 34bc571 commit 3209d2d

File tree

1 file changed

+36
-7
lines changed

1 file changed

+36
-7
lines changed

library/core/src/sync/atomic.rs

+36-7
Original file line numberDiff line numberDiff line change
@@ -346,9 +346,17 @@ impl AtomicBool {
346346
///
347347
/// # Safety
348348
///
349-
/// * `ptr` must be aligned to `align_of::<AtomicBool>()` (note that on some platforms this can be bigger than `align_of::<bool>()`).
349+
/// * `ptr` must be aligned to `align_of::<AtomicBool>()` (note that on some platforms this can
350+
/// be bigger than `align_of::<bool>()`).
350351
/// * `ptr` must be [valid] for both reads and writes for the whole lifetime `'a`.
351-
/// * The value behind `ptr` must not be accessed through non-atomic operations for the whole lifetime `'a`.
352+
/// * Non-atomic accesses to the value behind `ptr` must have a happens-before relationship
353+
/// with atomic accesses via the returned value (or vice-versa).
354+
/// * In other words, time periods where the value is accessed atomically may not overlap
355+
/// with periods where the value is accessed non-atomically.
356+
/// * This requirement is trivially satisfied if `ptr` is never used non-atomically for the
357+
/// duration of lifetime `'a`. Most use cases should be able to follow this guideline.
358+
/// * This requirement is also trivially satisfied if all accesses (atomic or not) are done
359+
/// from the same thread.
352360
///
353361
/// [valid]: crate::ptr#safety
354362
#[unstable(feature = "atomic_from_ptr", issue = "108652")]
@@ -1140,9 +1148,19 @@ impl<T> AtomicPtr<T> {
11401148
///
11411149
/// # Safety
11421150
///
1143-
/// * `ptr` must be aligned to `align_of::<AtomicPtr<T>>()` (note that on some platforms this can be bigger than `align_of::<*mut T>()`).
1151+
/// * `ptr` must be aligned to `align_of::<AtomicPtr<T>>()` (note that on some platforms this
1152+
/// can be bigger than `align_of::<*mut T>()`).
11441153
/// * `ptr` must be [valid] for both reads and writes for the whole lifetime `'a`.
1145-
/// * The value behind `ptr` must not be accessed through non-atomic operations for the whole lifetime `'a`.
1154+
/// * Non-atomic accesses to the value behind `ptr` must have a happens-before relationship
1155+
/// with atomic accesses via the returned value (or vice-versa).
1156+
/// * In other words, time periods where the value is accessed atomically may not overlap
1157+
/// with periods where the value is accessed non-atomically.
1158+
/// * This requirement is trivially satisfied if `ptr` is never used non-atomically for the
1159+
/// duration of lifetime `'a`. Most use cases should be able to follow this guideline.
1160+
/// * This requirement is also trivially satisfied if all accesses (atomic or not) are done
1161+
/// from the same thread.
1162+
/// * This method should not be used to create overlapping or mixed-size atomic accesses, as
1163+
/// these are not supported by the memory model.
11461164
///
11471165
/// [valid]: crate::ptr#safety
11481166
#[unstable(feature = "atomic_from_ptr", issue = "108652")]
@@ -2111,10 +2129,21 @@ macro_rules! atomic_int {
21112129
///
21122130
/// # Safety
21132131
///
2114-
/// * `ptr` must be aligned to `align_of::<AtomicBool>()` (note that on some platforms this can be bigger than `align_of::<bool>()`).
2115-
#[doc = concat!(" * `ptr` must be aligned to `align_of::<", stringify!($atomic_type), ">()` (note that on some platforms this can be bigger than `align_of::<", stringify!($int_type), ">()`).")]
2132+
#[doc = concat!(" * `ptr` must be aligned to \
2133+
`align_of::<", stringify!($atomic_type), ">()` (note that on some platforms this \
2134+
can be bigger than `align_of::<", stringify!($int_type), ">()`).")]
21162135
/// * `ptr` must be [valid] for both reads and writes for the whole lifetime `'a`.
2117-
/// * The value behind `ptr` must not be accessed through non-atomic operations for the whole lifetime `'a`.
2136+
/// * Non-atomic accesses to the value behind `ptr` must have a happens-before
2137+
/// relationship with atomic accesses via the returned value (or vice-versa).
2138+
/// * In other words, time periods where the value is accessed atomically may not
2139+
/// overlap with periods where the value is accessed non-atomically.
2140+
/// * This requirement is trivially satisfied if `ptr` is never used non-atomically
2141+
/// for the duration of lifetime `'a`. Most use cases should be able to follow
2142+
/// this guideline.
2143+
/// * This requirement is also trivially satisfied if all accesses (atomic or not) are
2144+
/// done from the same thread.
2145+
/// * This method should not be used to create overlapping or mixed-size atomic
2146+
/// accesses, as these are not supported by the memory model.
21182147
///
21192148
/// [valid]: crate::ptr#safety
21202149
#[unstable(feature = "atomic_from_ptr", issue = "108652")]

0 commit comments

Comments
 (0)