diff options
| author | Paul Dicker <pitdicker@gmail.com> | 2019-11-23 17:07:31 +0100 |
|---|---|---|
| committer | Paul Dicker <pitdicker@gmail.com> | 2019-11-23 17:24:14 +0100 |
| commit | 3b1c742e23648260e60f87d67aa47e1f43d0cc0d (patch) | |
| tree | 245c4063fc5e4883364d65b85e282e495cb9a0af | |
| parent | 98c173afe4b7019d0b739151b7d4d4343a85ea2b (diff) | |
| download | rust-3b1c742e23648260e60f87d67aa47e1f43d0cc0d.tar.gz rust-3b1c742e23648260e60f87d67aa47e1f43d0cc0d.zip | |
Add as_mut_ptr method to atomic types.
| -rw-r--r-- | src/libcore/sync/atomic.rs | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/src/libcore/sync/atomic.rs b/src/libcore/sync/atomic.rs index d311cb16b64..5d6972bf75c 100644 --- a/src/libcore/sync/atomic.rs +++ b/src/libcore/sync/atomic.rs @@ -802,6 +802,37 @@ impl AtomicBool { pub fn fetch_xor(&self, val: bool, order: Ordering) -> bool { unsafe { atomic_xor(self.v.get(), val as u8, order) != 0 } } + + /// Returns a mutable pointer to the underlying [`bool`]. + /// + /// 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 + /// `*mut bool` instead of `&AtomicBool`. + /// + /// [`bool`]: ../../../std/primitive.bool.html + /// + /// # Examples + /// + /// ```ignore (extern-declaration) + /// # fn main() { + /// use std::sync::atomic::AtomicBool; + /// extern { + /// fn my_atomic_op(arg: *mut bool); + /// } + /// + /// let mut atomic = AtomicBool::new(true); + /// unsafe { + /// my_atomic_op(atomic.as_mut_ptr()); + /// } + /// # } + /// ``` + #[inline] + #[unstable(feature = "atomic_mut_ptr", + reason = "recently added", + issue = "0")] + pub fn as_mut_ptr(&self) -> *mut bool { + self.v.get() as *mut bool + } } #[cfg(any(bootstrap, target_has_atomic_load_store = "ptr"))] @@ -1891,6 +1922,37 @@ assert_eq!(min_foo, 12); } } + doc_comment! { + concat!("Returns a mutable pointer to the underlying integer. + +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 +`*mut ", stringify!($int_type), "` instead of `&", stringify!($atomic_type), "`. + +# Examples + +```ignore (extern-declaration) +# fn main() { +", $extra_feature, "use std::sync::atomic::", stringify!($atomic_type), "; + +extern { + fn my_atomic_op(arg: *mut ", stringify!($int_type), "); +} + +let mut atomic = ", stringify!($atomic_type), "::new(1); +unsafe { + my_atomic_op(atomic.as_mut_ptr()); +} +# } +```"), + #[inline] + #[unstable(feature = "atomic_mut_ptr", + reason = "recently added", + issue = "0")] + pub fn as_mut_ptr(&self) -> *mut $int_type { + self.v.get() + } + } } } } |
