1
Fork 0

Add as_mut_ptr method to atomic types.

This commit is contained in:
Paul Dicker 2019-11-23 17:07:31 +01:00
parent 98c173afe4
commit 3b1c742e23

View file

@ -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()
}
}
}
}
}