@@ -90,6 +90,8 @@ use default::Default;
90
90
use fmt;
91
91
92
92
/// A boolean type which can be safely shared between threads.
93
+ ///
94
+ /// This type has the same in-memory representation as a `bool`.
93
95
#[ cfg( target_has_atomic = "8" ) ]
94
96
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
95
97
pub struct AtomicBool {
@@ -110,6 +112,8 @@ impl Default for AtomicBool {
110
112
unsafe impl Sync for AtomicBool { }
111
113
112
114
/// A raw pointer type which can be safely shared between threads.
115
+ ///
116
+ /// This type has the same in-memory representation as a `*mut T`.
113
117
#[ cfg( target_has_atomic = "ptr" ) ]
114
118
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
115
119
pub struct AtomicPtr < T > {
@@ -191,6 +195,48 @@ impl AtomicBool {
191
195
AtomicBool { v : UnsafeCell :: new ( v as u8 ) }
192
196
}
193
197
198
+ /// Returns a mutable reference to the underlying `bool`.
199
+ ///
200
+ /// This is safe because the mutable reference guarantees that no other threads are
201
+ /// concurrently accessing the atomic data.
202
+ ///
203
+ /// # Examples
204
+ ///
205
+ /// ```
206
+ /// #![feature(atomic_access)]
207
+ /// use std::sync::atomic::{AtomicBool, Ordering};
208
+ ///
209
+ /// let mut some_bool = AtomicBool::new(true);
210
+ /// assert_eq!(*some_bool.get_mut(), true);
211
+ /// *some_bool.get_mut() = false;
212
+ /// assert_eq!(some_bool.load(Ordering::SeqCst), false);
213
+ /// ```
214
+ #[ inline]
215
+ #[ unstable( feature = "atomic_access" , issue = "35603" ) ]
216
+ pub fn get_mut ( & mut self ) -> & mut bool {
217
+ unsafe { & mut * ( self . v . get ( ) as * mut bool ) }
218
+ }
219
+
220
+ /// Consumes the atomic and returns the contained value.
221
+ ///
222
+ /// This is safe because passing `self` by value guarantees that no other threads are
223
+ /// concurrently accessing the atomic data.
224
+ ///
225
+ /// # Examples
226
+ ///
227
+ /// ```
228
+ /// #![feature(atomic_access)]
229
+ /// use std::sync::atomic::AtomicBool;
230
+ ///
231
+ /// let some_bool = AtomicBool::new(true);
232
+ /// assert_eq!(some_bool.into_inner(), true);
233
+ /// ```
234
+ #[ inline]
235
+ #[ unstable( feature = "atomic_access" , issue = "35603" ) ]
236
+ pub fn into_inner ( self ) -> bool {
237
+ unsafe { self . v . into_inner ( ) != 0 }
238
+ }
239
+
194
240
/// Loads a value from the bool.
195
241
///
196
242
/// `load` takes an `Ordering` argument which describes the memory ordering of this operation.
@@ -528,6 +574,47 @@ impl<T> AtomicPtr<T> {
528
574
AtomicPtr { p : UnsafeCell :: new ( p) }
529
575
}
530
576
577
+ /// Returns a mutable reference to the underlying pointer.
578
+ ///
579
+ /// This is safe because the mutable reference guarantees that no other threads are
580
+ /// concurrently accessing the atomic data.
581
+ ///
582
+ /// # Examples
583
+ ///
584
+ /// ```
585
+ /// #![feature(atomic_access)]
586
+ /// use std::sync::atomic::{AtomicPtr, Ordering};
587
+ ///
588
+ /// let mut atomic_ptr = AtomicPtr::new(&mut 10);
589
+ /// *atomic_ptr.get_mut() = &mut 5;
590
+ /// assert_eq!(unsafe { *atomic_ptr.load(Ordering::SeqCst) }, 5);
591
+ /// ```
592
+ #[ inline]
593
+ #[ unstable( feature = "atomic_access" , issue = "35603" ) ]
594
+ pub fn get_mut ( & mut self ) -> & mut * mut T {
595
+ unsafe { & mut * self . p . get ( ) }
596
+ }
597
+
598
+ /// Consumes the atomic and returns the contained value.
599
+ ///
600
+ /// This is safe because passing `self` by value guarantees that no other threads are
601
+ /// concurrently accessing the atomic data.
602
+ ///
603
+ /// # Examples
604
+ ///
605
+ /// ```
606
+ /// #![feature(atomic_access)]
607
+ /// use std::sync::atomic::AtomicPtr;
608
+ ///
609
+ /// let atomic_ptr = AtomicPtr::new(&mut 5);
610
+ /// assert_eq!(unsafe { *atomic_ptr.into_inner() }, 5);
611
+ /// ```
612
+ #[ inline]
613
+ #[ unstable( feature = "atomic_access" , issue = "35603" ) ]
614
+ pub fn into_inner ( self ) -> * mut T {
615
+ unsafe { self . p . into_inner ( ) }
616
+ }
617
+
531
618
/// Loads a value from the pointer.
532
619
///
533
620
/// `load` takes an `Ordering` argument which describes the memory ordering of this operation.
@@ -730,8 +817,11 @@ macro_rules! atomic_int {
730
817
( $stable: meta,
731
818
$stable_cxchg: meta,
732
819
$stable_debug: meta,
820
+ $stable_access: meta,
733
821
$int_type: ident $atomic_type: ident $atomic_init: ident) => {
734
822
/// An integer type which can be safely shared between threads.
823
+ ///
824
+ /// This type has the same in-memory representation as the underlying integer type.
735
825
#[ $stable]
736
826
pub struct $atomic_type {
737
827
v: UnsafeCell <$int_type>,
@@ -777,6 +867,48 @@ macro_rules! atomic_int {
777
867
$atomic_type { v: UnsafeCell :: new( v) }
778
868
}
779
869
870
+ /// Returns a mutable reference to the underlying integer.
871
+ ///
872
+ /// This is safe because the mutable reference guarantees that no other threads are
873
+ /// concurrently accessing the atomic data.
874
+ ///
875
+ /// # Examples
876
+ ///
877
+ /// ```
878
+ /// #![feature(atomic_access)]
879
+ /// use std::sync::atomic::{AtomicIsize, Ordering};
880
+ ///
881
+ /// let mut some_isize = AtomicIsize::new(10);
882
+ /// assert_eq!(*some_isize.get_mut(), 10);
883
+ /// *some_isize.get_mut() = 5;
884
+ /// assert_eq!(some_isize.load(Ordering::SeqCst), 5);
885
+ /// ```
886
+ #[ inline]
887
+ #[ $stable_access]
888
+ pub fn get_mut( & mut self ) -> & mut $int_type {
889
+ unsafe { & mut * self . v. get( ) }
890
+ }
891
+
892
+ /// Consumes the atomic and returns the contained value.
893
+ ///
894
+ /// This is safe because passing `self` by value guarantees that no other threads are
895
+ /// concurrently accessing the atomic data.
896
+ ///
897
+ /// # Examples
898
+ ///
899
+ /// ```
900
+ /// #![feature(atomic_access)]
901
+ /// use std::sync::atomic::AtomicIsize;
902
+ ///
903
+ /// let some_isize = AtomicIsize::new(5);
904
+ /// assert_eq!(some_isize.into_inner(), 5);
905
+ /// ```
906
+ #[ inline]
907
+ #[ $stable_access]
908
+ pub fn into_inner( self ) -> $int_type {
909
+ unsafe { self . v. into_inner( ) }
910
+ }
911
+
780
912
/// Loads a value from the atomic integer.
781
913
///
782
914
/// `load` takes an `Ordering` argument which describes the memory ordering of this
@@ -1057,69 +1189,79 @@ atomic_int! {
1057
1189
unstable( feature = "integer_atomics" , issue = "32976" ) ,
1058
1190
unstable( feature = "integer_atomics" , issue = "32976" ) ,
1059
1191
unstable( feature = "integer_atomics" , issue = "32976" ) ,
1192
+ unstable( feature = "integer_atomics" , issue = "32976" ) ,
1060
1193
i8 AtomicI8 ATOMIC_I8_INIT
1061
1194
}
1062
1195
#[ cfg( target_has_atomic = "8" ) ]
1063
1196
atomic_int ! {
1064
1197
unstable( feature = "integer_atomics" , issue = "32976" ) ,
1065
1198
unstable( feature = "integer_atomics" , issue = "32976" ) ,
1066
1199
unstable( feature = "integer_atomics" , issue = "32976" ) ,
1200
+ unstable( feature = "integer_atomics" , issue = "32976" ) ,
1067
1201
u8 AtomicU8 ATOMIC_U8_INIT
1068
1202
}
1069
1203
#[ cfg( target_has_atomic = "16" ) ]
1070
1204
atomic_int ! {
1071
1205
unstable( feature = "integer_atomics" , issue = "32976" ) ,
1072
1206
unstable( feature = "integer_atomics" , issue = "32976" ) ,
1073
1207
unstable( feature = "integer_atomics" , issue = "32976" ) ,
1208
+ unstable( feature = "integer_atomics" , issue = "32976" ) ,
1074
1209
i16 AtomicI16 ATOMIC_I16_INIT
1075
1210
}
1076
1211
#[ cfg( target_has_atomic = "16" ) ]
1077
1212
atomic_int ! {
1078
1213
unstable( feature = "integer_atomics" , issue = "32976" ) ,
1079
1214
unstable( feature = "integer_atomics" , issue = "32976" ) ,
1080
1215
unstable( feature = "integer_atomics" , issue = "32976" ) ,
1216
+ unstable( feature = "integer_atomics" , issue = "32976" ) ,
1081
1217
u16 AtomicU16 ATOMIC_U16_INIT
1082
1218
}
1083
1219
#[ cfg( target_has_atomic = "32" ) ]
1084
1220
atomic_int ! {
1085
1221
unstable( feature = "integer_atomics" , issue = "32976" ) ,
1086
1222
unstable( feature = "integer_atomics" , issue = "32976" ) ,
1087
1223
unstable( feature = "integer_atomics" , issue = "32976" ) ,
1224
+ unstable( feature = "integer_atomics" , issue = "32976" ) ,
1088
1225
i32 AtomicI32 ATOMIC_I32_INIT
1089
1226
}
1090
1227
#[ cfg( target_has_atomic = "32" ) ]
1091
1228
atomic_int ! {
1092
1229
unstable( feature = "integer_atomics" , issue = "32976" ) ,
1093
1230
unstable( feature = "integer_atomics" , issue = "32976" ) ,
1094
1231
unstable( feature = "integer_atomics" , issue = "32976" ) ,
1232
+ unstable( feature = "integer_atomics" , issue = "32976" ) ,
1095
1233
u32 AtomicU32 ATOMIC_U32_INIT
1096
1234
}
1097
1235
#[ cfg( target_has_atomic = "64" ) ]
1098
1236
atomic_int ! {
1099
1237
unstable( feature = "integer_atomics" , issue = "32976" ) ,
1100
1238
unstable( feature = "integer_atomics" , issue = "32976" ) ,
1101
1239
unstable( feature = "integer_atomics" , issue = "32976" ) ,
1240
+ unstable( feature = "integer_atomics" , issue = "32976" ) ,
1102
1241
i64 AtomicI64 ATOMIC_I64_INIT
1103
1242
}
1104
1243
#[ cfg( target_has_atomic = "64" ) ]
1105
1244
atomic_int ! {
1106
1245
unstable( feature = "integer_atomics" , issue = "32976" ) ,
1107
1246
unstable( feature = "integer_atomics" , issue = "32976" ) ,
1108
1247
unstable( feature = "integer_atomics" , issue = "32976" ) ,
1248
+ unstable( feature = "integer_atomics" , issue = "32976" ) ,
1109
1249
u64 AtomicU64 ATOMIC_U64_INIT
1110
1250
}
1111
1251
#[ cfg( target_has_atomic = "ptr" ) ]
1112
1252
atomic_int ! {
1113
1253
stable( feature = "rust1" , since = "1.0.0" ) ,
1114
1254
stable( feature = "extended_compare_and_swap" , since = "1.10.0" ) ,
1115
1255
stable( feature = "atomic_debug" , since = "1.3.0" ) ,
1256
+ unstable( feature = "atomic_access" , issue = "35603" ) ,
1116
1257
isize AtomicIsize ATOMIC_ISIZE_INIT
1117
1258
}
1118
1259
#[ cfg( target_has_atomic = "ptr" ) ]
1119
1260
atomic_int ! {
1120
1261
stable( feature = "rust1" , since = "1.0.0" ) ,
1121
1262
stable( feature = "extended_compare_and_swap" , since = "1.10.0" ) ,
1122
1263
stable( feature = "atomic_debug" , since = "1.3.0" ) ,
1264
+ unstable( feature = "atomic_access" , issue = "35603" ) ,
1123
1265
usize AtomicUsize ATOMIC_USIZE_INIT
1124
1266
}
1125
1267
0 commit comments