Skip to content

Commit 2a7c2df

Browse files
committed
Auto merge of #115719 - tgross35:atomic-from-ptr, r=dtolnay
Stabilize `atomic_from_ptr` This stabilizes `atomic_from_ptr` and moves the const gate to `const_atomic_from_ptr`. Const stability is blocked on `const_mut_refs`. Tracking issue: #108652 Newly stable API: ```rust // core::atomic impl AtomicBool { pub unsafe fn from_ptr<'a>(ptr: *mut bool) -> &'a AtomicBool; } impl<T> AtomicPtr<T> { pub unsafe fn from_ptr<'a>(ptr: *mut *mut T) -> &'a AtomicPtr<T>; } impl AtomicU8 { pub unsafe fn from_ptr<'a>(ptr: *mut u8) -> &'a AtomicU8; } impl AtomicU16 { pub unsafe fn from_ptr<'a>(ptr: *mut u16) -> &'a AtomicU16; } impl AtomicU32 { pub unsafe fn from_ptr<'a>(ptr: *mut u32) -> &'a AtomicU32; } impl AtomicU64 { pub unsafe fn from_ptr<'a>(ptr: *mut u64) -> &'a AtomicU64; } impl AtomicUsize { pub unsafe fn from_ptr<'a>(ptr: *mut usize) -> &'a AtomicUsize; } impl AtomicI8 { pub unsafe fn from_ptr<'a>(ptr: *mut i8) -> &'a AtomicI8; } impl AtomicI16 { pub unsafe fn from_ptr<'a>(ptr: *mut i16) -> &'a AtomicI16; } impl AtomicI32 { pub unsafe fn from_ptr<'a>(ptr: *mut i32) -> &'a AtomicI32; } impl AtomicI64 { pub unsafe fn from_ptr<'a>(ptr: *mut i64) -> &'a AtomicI64; } impl AtomicIsize { pub unsafe fn from_ptr<'a>(ptr: *mut isize) -> &'a AtomicIsize; } ```
2 parents 75a5dd0 + 227c844 commit 2a7c2df

File tree

1 file changed

+45
-16
lines changed

1 file changed

+45
-16
lines changed

Diff for: library/core/src/sync/atomic.rs

+45-16
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ impl AtomicBool {
319319
/// # Examples
320320
///
321321
/// ```
322-
/// #![feature(atomic_from_ptr, pointer_is_aligned)]
322+
/// #![feature(pointer_is_aligned)]
323323
/// use std::sync::atomic::{self, AtomicBool};
324324
/// use std::mem::align_of;
325325
///
@@ -346,13 +346,21 @@ 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
354-
#[unstable(feature = "atomic_from_ptr", issue = "108652")]
355-
#[rustc_const_unstable(feature = "atomic_from_ptr", issue = "108652")]
362+
#[stable(feature = "atomic_from_ptr", since = "CURRENT_RUSTC_VERSION")]
363+
#[rustc_const_unstable(feature = "const_atomic_from_ptr", issue = "108652")]
356364
pub const unsafe fn from_ptr<'a>(ptr: *mut bool) -> &'a AtomicBool {
357365
// SAFETY: guaranteed by the caller
358366
unsafe { &*ptr.cast() }
@@ -1113,7 +1121,7 @@ impl<T> AtomicPtr<T> {
11131121
/// # Examples
11141122
///
11151123
/// ```
1116-
/// #![feature(atomic_from_ptr, pointer_is_aligned)]
1124+
/// #![feature(pointer_is_aligned)]
11171125
/// use std::sync::atomic::{self, AtomicPtr};
11181126
/// use std::mem::align_of;
11191127
///
@@ -1140,13 +1148,23 @@ 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
1148-
#[unstable(feature = "atomic_from_ptr", issue = "108652")]
1149-
#[rustc_const_unstable(feature = "atomic_from_ptr", issue = "108652")]
1166+
#[stable(feature = "atomic_from_ptr", since = "CURRENT_RUSTC_VERSION")]
1167+
#[rustc_const_unstable(feature = "const_atomic_from_ptr", issue = "108652")]
11501168
pub const unsafe fn from_ptr<'a>(ptr: *mut *mut T) -> &'a AtomicPtr<T> {
11511169
// SAFETY: guaranteed by the caller
11521170
unsafe { &*ptr.cast() }
@@ -2083,7 +2101,7 @@ macro_rules! atomic_int {
20832101
/// # Examples
20842102
///
20852103
/// ```
2086-
/// #![feature(atomic_from_ptr, pointer_is_aligned)]
2104+
/// #![feature(pointer_is_aligned)]
20872105
#[doc = concat!($extra_feature, "use std::sync::atomic::{self, ", stringify!($atomic_type), "};")]
20882106
/// use std::mem::align_of;
20892107
///
@@ -2111,14 +2129,25 @@ 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
2120-
#[unstable(feature = "atomic_from_ptr", issue = "108652")]
2121-
#[rustc_const_unstable(feature = "atomic_from_ptr", issue = "108652")]
2149+
#[stable(feature = "atomic_from_ptr", since = "CURRENT_RUSTC_VERSION")]
2150+
#[rustc_const_unstable(feature = "const_atomic_from_ptr", issue = "108652")]
21222151
pub const unsafe fn from_ptr<'a>(ptr: *mut $int_type) -> &'a $atomic_type {
21232152
// SAFETY: guaranteed by the caller
21242153
unsafe { &*ptr.cast() }

0 commit comments

Comments
 (0)