@@ -802,6 +802,37 @@ impl AtomicBool {
802
802
pub fn fetch_xor ( & self , val : bool , order : Ordering ) -> bool {
803
803
unsafe { atomic_xor ( self . v . get ( ) , val as u8 , order) != 0 }
804
804
}
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
+ }
805
836
}
806
837
807
838
#[ cfg( any( bootstrap, target_has_atomic_load_store = "ptr" ) ) ]
@@ -1891,6 +1922,37 @@ assert_eq!(min_foo, 12);
1891
1922
}
1892
1923
}
1893
1924
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
+ }
1894
1956
}
1895
1957
}
1896
1958
}
0 commit comments