You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Rollup merge of #108540 - WaffleLapkin:atomic_thingy_from_thingy_pointer, r=m-ou-se
Add `Atomic*::from_ptr`
This PR adds functions in the following form to all atomic types:
```rust
impl AtomicT {
pub const unsafe fn from_ptr<'a>(ptr: *mut T) -> &'a AtomicT;
}
```
r? `@m-ou-se` (we've talked about it before)
I'm not sure about docs & safety requirements, I'd appreciate some feedback on them.
/// // Create an atomic view of the allocated value
2065
+
// SAFETY: this is a doc comment, tidy, it can't hurt you (also guaranteed by the construction of `ptr` and the assert above)
2066
+
#[doc = concat!(" let atomic = unsafe {", stringify!($atomic_type),"::from_ptr(ptr) };")]
2067
+
///
2068
+
/// // Use `atomic` for atomic operations, possibly share it with other threads
2069
+
/// atomic.store(1, atomic::Ordering::Relaxed);
2070
+
/// }
2071
+
///
2072
+
/// // It's ok to non-atomically access the value behind `ptr`,
2073
+
/// // since the reference to the atomic ended its lifetime in the block above
2074
+
/// assert_eq!(unsafe { *ptr }, 1);
2075
+
///
2076
+
/// // Deallocate the value
2077
+
/// unsafe { drop(Box::from_raw(ptr)) }
2078
+
/// ```
2079
+
///
2080
+
/// # Safety
2081
+
///
2082
+
/// * `ptr` must be aligned to `align_of::<AtomicBool>()` (note that on some platforms this can be bigger than `align_of::<bool>()`).
2083
+
#[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),">()`).")]
2084
+
/// * `ptr` must be [valid] for both reads and writes for the whole lifetime `'a`.
2085
+
/// * The value behind `ptr` must not be accessed through non-atomic operations for the whole lifetime `'a`.
0 commit comments