Skip to content

Commit 3b1c742

Browse files
committed
Add as_mut_ptr method to atomic types.
1 parent 98c173a commit 3b1c742

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

src/libcore/sync/atomic.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,37 @@ impl AtomicBool {
802802
pub fn fetch_xor(&self, val: bool, order: Ordering) -> bool {
803803
unsafe { atomic_xor(self.v.get(), val as u8, order) != 0 }
804804
}
805+
806+
/// Returns a mutable pointer to the underlying [`bool`].
807+
///
808+
/// Doing non-atomic reads and writes on the resulting integer can be a data race.
809+
/// This method is mostly useful for FFI, where the function signature may use
810+
/// `*mut bool` instead of `&AtomicBool`.
811+
///
812+
/// [`bool`]: ../../../std/primitive.bool.html
813+
///
814+
/// # Examples
815+
///
816+
/// ```ignore (extern-declaration)
817+
/// # fn main() {
818+
/// use std::sync::atomic::AtomicBool;
819+
/// extern {
820+
/// fn my_atomic_op(arg: *mut bool);
821+
/// }
822+
///
823+
/// let mut atomic = AtomicBool::new(true);
824+
/// unsafe {
825+
/// my_atomic_op(atomic.as_mut_ptr());
826+
/// }
827+
/// # }
828+
/// ```
829+
#[inline]
830+
#[unstable(feature = "atomic_mut_ptr",
831+
reason = "recently added",
832+
issue = "0")]
833+
pub fn as_mut_ptr(&self) -> *mut bool {
834+
self.v.get() as *mut bool
835+
}
805836
}
806837

807838
#[cfg(any(bootstrap, target_has_atomic_load_store = "ptr"))]
@@ -1891,6 +1922,37 @@ assert_eq!(min_foo, 12);
18911922
}
18921923
}
18931924

1925+
doc_comment! {
1926+
concat!("Returns a mutable pointer to the underlying integer.
1927+
1928+
Doing non-atomic reads and writes on the resulting integer can be a data race.
1929+
This method is mostly useful for FFI, where the function signature may use
1930+
`*mut ", stringify!($int_type), "` instead of `&", stringify!($atomic_type), "`.
1931+
1932+
# Examples
1933+
1934+
```ignore (extern-declaration)
1935+
# fn main() {
1936+
", $extra_feature, "use std::sync::atomic::", stringify!($atomic_type), ";
1937+
1938+
extern {
1939+
fn my_atomic_op(arg: *mut ", stringify!($int_type), ");
1940+
}
1941+
1942+
let mut atomic = ", stringify!($atomic_type), "::new(1);
1943+
unsafe {
1944+
my_atomic_op(atomic.as_mut_ptr());
1945+
}
1946+
# }
1947+
```"),
1948+
#[inline]
1949+
#[unstable(feature = "atomic_mut_ptr",
1950+
reason = "recently added",
1951+
issue = "0")]
1952+
pub fn as_mut_ptr(&self) -> *mut $int_type {
1953+
self.v.get()
1954+
}
1955+
}
18941956
}
18951957
}
18961958
}

0 commit comments

Comments
 (0)