Debian Codegraph @

debian/rust-vendor/crossbeam-utils/tests/atomic_cell.rs

raw
1 use std::mem;
2 use std::sync::atomic::AtomicUsize;
3 use std::sync::atomic::Ordering::SeqCst;
4
5 use crossbeam_utils::atomic::AtomicCell;
6
7 #[test]
8 fn is_lock_free() {
9 struct UsizeWrap(#[allow(dead_code)] usize);
10 struct U8Wrap(#[allow(dead_code)] bool);
11 struct I16Wrap(#[allow(dead_code)] i16);
12 #[repr(align(8))]
13 struct U64Align8(#[allow(dead_code)] u64);
14
15 assert!(AtomicCell::<usize>::is_lock_free());
16 assert!(AtomicCell::<isize>::is_lock_free());
17 assert!(AtomicCell::<UsizeWrap>::is_lock_free());
18
19 assert!(AtomicCell::<()>::is_lock_free());
20
21 assert!(AtomicCell::<u8>::is_lock_free());
22 assert!(AtomicCell::<i8>::is_lock_free());
23 assert!(AtomicCell::<bool>::is_lock_free());
24 assert!(AtomicCell::<U8Wrap>::is_lock_free());
25
26 assert!(AtomicCell::<u16>::is_lock_free());
27 assert!(AtomicCell::<i16>::is_lock_free());
28 assert!(AtomicCell::<I16Wrap>::is_lock_free());
29
30 assert!(AtomicCell::<u32>::is_lock_free());
31 assert!(AtomicCell::<i32>::is_lock_free());
32
33 // Sizes of both types must be equal, and the alignment of `u64` must be greater or equal than
34 // that of `AtomicU64`. In i686-unknown-linux-gnu, the alignment of `u64` is `4` and alignment
35 // of `AtomicU64` is `8`, so `AtomicCell<u64>` is not lock-free.
36 assert_eq!(
37 AtomicCell::<u64>::is_lock_free(),
38 cfg!(target_has_atomic = "64") && std::mem::align_of::<u64>() == 8
39 );
40 assert_eq!(mem::size_of::<U64Align8>(), 8);
41 assert_eq!(mem::align_of::<U64Align8>(), 8);
42 assert_eq!(
43 AtomicCell::<U64Align8>::is_lock_free(),
44 cfg!(target_has_atomic = "64")
45 );
46
47 // AtomicU128 is unstable
48 assert!(!AtomicCell::<u128>::is_lock_free());
49 }
50
51 #[test]
52 fn drops_unit() {
53 static CNT: AtomicUsize = AtomicUsize::new(0);
54 CNT.store(0, SeqCst);
55
56 #[derive(Debug, PartialEq, Eq)]
57 struct Foo();
58
59 impl Foo {
60 fn new() -> Foo {
61 CNT.fetch_add(1, SeqCst);
62 Foo()
63 }
64 }
65
66 impl Drop for Foo {
67 fn drop(&mut self) {
68 CNT.fetch_sub(1, SeqCst);
69 }
70 }
71
72 impl Default for Foo {
73 fn default() -> Foo {
74 Foo::new()
75 }
76 }
77
78 let a = AtomicCell::new(Foo::new());
79
80 assert_eq!(a.swap(Foo::new()), Foo::new());
81 assert_eq!(CNT.load(SeqCst), 1);
82
83 a.store(Foo::new());
84 assert_eq!(CNT.load(SeqCst), 1);
85
86 assert_eq!(a.swap(Foo::default()), Foo::new());
87 assert_eq!(CNT.load(SeqCst), 1);
88
89 drop(a);
90 assert_eq!(CNT.load(SeqCst), 0);
91 }
92
93 #[test]
94 fn drops_u8() {
95 static CNT: AtomicUsize = AtomicUsize::new(0);
96 CNT.store(0, SeqCst);
97
98 #[derive(Debug, PartialEq, Eq)]
99 struct Foo(u8);
100
101 impl Foo {
102 fn new(val: u8) -> Foo {
103 CNT.fetch_add(1, SeqCst);
104 Foo(val)
105 }
106 }
107
108 impl Drop for Foo {
109 fn drop(&mut self) {
110 CNT.fetch_sub(1, SeqCst);
111 }
112 }
113
114 impl Default for Foo {
115 fn default() -> Foo {
116 Foo::new(0)
117 }
118 }
119
120 let a = AtomicCell::new(Foo::new(5));
121
122 assert_eq!(a.swap(Foo::new(6)), Foo::new(5));
123 assert_eq!(a.swap(Foo::new(1)), Foo::new(6));
124 assert_eq!(CNT.load(SeqCst), 1);
125
126 a.store(Foo::new(2));
127 assert_eq!(CNT.load(SeqCst), 1);
128
129 assert_eq!(a.swap(Foo::default()), Foo::new(2));
130 assert_eq!(CNT.load(SeqCst), 1);
131
132 assert_eq!(a.swap(Foo::default()), Foo::new(0));
133 assert_eq!(CNT.load(SeqCst), 1);
134
135 drop(a);
136 assert_eq!(CNT.load(SeqCst), 0);
137 }
138
139 #[test]
140 fn drops_usize() {
141 static CNT: AtomicUsize = AtomicUsize::new(0);
142 CNT.store(0, SeqCst);
143
144 #[derive(Debug, PartialEq, Eq)]
145 struct Foo(usize);
146
147 impl Foo {
148 fn new(val: usize) -> Foo {
149 CNT.fetch_add(1, SeqCst);
150 Foo(val)
151 }
152 }
153
154 impl Drop for Foo {
155 fn drop(&mut self) {
156 CNT.fetch_sub(1, SeqCst);
157 }
158 }
159
160 impl Default for Foo {
161 fn default() -> Foo {
162 Foo::new(0)
163 }
164 }
165
166 let a = AtomicCell::new(Foo::new(5));
167
168 assert_eq!(a.swap(Foo::new(6)), Foo::new(5));
169 assert_eq!(a.swap(Foo::new(1)), Foo::new(6));
170 assert_eq!(CNT.load(SeqCst), 1);
171
172 a.store(Foo::new(2));
173 assert_eq!(CNT.load(SeqCst), 1);
174
175 assert_eq!(a.swap(Foo::default()), Foo::new(2));
176 assert_eq!(CNT.load(SeqCst), 1);
177
178 assert_eq!(a.swap(Foo::default()), Foo::new(0));
179 assert_eq!(CNT.load(SeqCst), 1);
180
181 drop(a);
182 assert_eq!(CNT.load(SeqCst), 0);
183 }
184
185 #[test]
186 fn modular_u8() {
187 #[derive(Clone, Copy, Eq, Debug, Default)]
188 struct Foo(u8);
189
190 impl PartialEq for Foo {
191 fn eq(&self, other: &Foo) -> bool {
192 self.0 % 5 == other.0 % 5
193 }
194 }
195
196 let a = AtomicCell::new(Foo(1));
197
198 assert_eq!(a.load(), Foo(1));
199 assert_eq!(a.swap(Foo(2)), Foo(11));
200 assert_eq!(a.load(), Foo(52));
201
202 a.store(Foo(0));
203 assert_eq!(a.compare_exchange(Foo(0), Foo(5)), Ok(Foo(100)));
204 assert_eq!(a.load().0, 5);
205 assert_eq!(a.compare_exchange(Foo(10), Foo(15)), Ok(Foo(100)));
206 assert_eq!(a.load().0, 15);
207 }
208
209 #[test]
210 fn modular_usize() {
211 #[derive(Clone, Copy, Eq, Debug, Default)]
212 struct Foo(usize);
213
214 impl PartialEq for Foo {
215 fn eq(&self, other: &Foo) -> bool {
216 self.0 % 5 == other.0 % 5
217 }
218 }
219
220 let a = AtomicCell::new(Foo(1));
221
222 assert_eq!(a.load(), Foo(1));
223 assert_eq!(a.swap(Foo(2)), Foo(11));
224 assert_eq!(a.load(), Foo(52));
225
226 a.store(Foo(0));
227 assert_eq!(a.compare_exchange(Foo(0), Foo(5)), Ok(Foo(100)));
228 assert_eq!(a.load().0, 5);
229 assert_eq!(a.compare_exchange(Foo(10), Foo(15)), Ok(Foo(100)));
230 assert_eq!(a.load().0, 15);
231 }
232
233 #[test]
234 fn garbage_padding() {
235 #[derive(Copy, Clone, Eq, PartialEq)]
236 struct Object {
237 a: i64,
238 b: i32,
239 }
240
241 let cell = AtomicCell::new(Object { a: 0, b: 0 });
242 let _garbage = [0xfe, 0xfe, 0xfe, 0xfe, 0xfe]; // Needed
243 let next = Object { a: 0, b: 0 };
244
245 let prev = cell.load();
246 assert!(cell.compare_exchange(prev, next).is_ok());
247 println!();
248 }
249
250 #[test]
251 fn const_atomic_cell() {
252 const _IS_LOCK_FREE_U: bool = AtomicCell::<usize>::is_lock_free();
253 const _IS_LOCK_FREE_I: bool = AtomicCell::<isize>::is_lock_free();
254 static INTO_INNER: usize = {
255 let v = AtomicCell::new(!0);
256 v.into_inner()
257 };
258 #[rustversion::since(1.83)]
259 static _AS_PTR: AtomicCell<usize> = {
260 let v = AtomicCell::new(!0);
261 let _p = v.as_ptr();
262 v
263 };
264 static CELL: AtomicCell<usize> = AtomicCell::new(0);
265
266 CELL.store(1);
267 assert_eq!(CELL.load(), 1);
268 assert_eq!(INTO_INNER, !0);
269 }
270
271 // https://github.com/crossbeam-rs/crossbeam/pull/767
272 macro_rules! test_arithmetic {
273 ($test_name:ident, $ty:ident) => {
274 #[test]
275 fn $test_name() {
276 let a: AtomicCell<$ty> = AtomicCell::new(7);
277
278 assert_eq!(a.fetch_add(3), 7);
279 assert_eq!(a.load(), 10);
280
281 assert_eq!(a.fetch_sub(3), 10);
282 assert_eq!(a.load(), 7);
283
284 assert_eq!(a.fetch_and(3), 7);
285 assert_eq!(a.load(), 3);
286
287 assert_eq!(a.fetch_or(16), 3);
288 assert_eq!(a.load(), 19);
289
290 assert_eq!(a.fetch_xor(2), 19);
291 assert_eq!(a.load(), 17);
292
293 assert_eq!(a.fetch_max(18), 17);
294 assert_eq!(a.load(), 18);
295
296 assert_eq!(a.fetch_min(17), 18);
297 assert_eq!(a.load(), 17);
298
299 assert_eq!(a.fetch_nand(7), 17);
300 assert_eq!(a.load(), !(17 & 7));
301 }
302 };
303 }
304 test_arithmetic!(arithmetic_u8, u8);
305 test_arithmetic!(arithmetic_i8, i8);
306 test_arithmetic!(arithmetic_u16, u16);
307 test_arithmetic!(arithmetic_i16, i16);
308 test_arithmetic!(arithmetic_u32, u32);
309 test_arithmetic!(arithmetic_i32, i32);
310 test_arithmetic!(arithmetic_u64, u64);
311 test_arithmetic!(arithmetic_i64, i64);
312 test_arithmetic!(arithmetic_u128, u128);
313 test_arithmetic!(arithmetic_i128, i128);
314
315 // https://github.com/crossbeam-rs/crossbeam/issues/748
316 #[cfg_attr(miri, ignore)] // TODO
317 #[test]
318 fn issue_748() {
319 #[allow(dead_code)]
320 #[repr(align(8))]
321 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
322 enum Test {
323 Field(u32),
324 FieldLess,
325 }
326
327 assert_eq!(mem::size_of::<Test>(), 8);
328 assert_eq!(
329 AtomicCell::<Test>::is_lock_free(),
330 cfg!(target_has_atomic = "64")
331 );
332 let x = AtomicCell::new(Test::FieldLess);
333 assert_eq!(x.load(), Test::FieldLess);
334 }
335
336 // https://github.com/crossbeam-rs/crossbeam/issues/833
337 #[test]
338 fn issue_833() {
339 use std::num::NonZeroU128;
340 use std::sync::atomic::{AtomicBool, Ordering};
341 use std::thread;
342
343 #[cfg(miri)]
344 const N: usize = 10_000;
345 #[cfg(not(miri))]
346 const N: usize = 1_000_000;
347
348 #[allow(dead_code)]
349 enum Enum {
350 NeverConstructed,
351 Cell(AtomicCell<NonZeroU128>),
352 }
353
354 static STATIC: Enum = Enum::Cell(AtomicCell::new(match NonZeroU128::new(1) {
355 Some(nonzero) => nonzero,
356 None => unreachable!(),
357 }));
358 static FINISHED: AtomicBool = AtomicBool::new(false);
359
360 let handle = thread::spawn(|| {
361 let cell = match &STATIC {
362 Enum::NeverConstructed => unreachable!(),
363 Enum::Cell(cell) => cell,
364 };
365 let x = NonZeroU128::new(0xFFFF_FFFF_FFFF_FFFF_0000_0000_0000_0000).unwrap();
366 let y = NonZeroU128::new(0x0000_0000_0000_0000_FFFF_FFFF_FFFF_FFFF).unwrap();
367 while !FINISHED.load(Ordering::Relaxed) {
368 cell.store(x);
369 cell.store(y);
370 }
371 });
372
373 for _ in 0..N {
374 if let Enum::NeverConstructed = STATIC {
375 unreachable!(":(");
376 }
377 }
378
379 FINISHED.store(true, Ordering::Relaxed);
380 handle.join().unwrap();
381 }