]> git.lizzy.rs Git - rust.git/blob - library/std/src/sys/windows/thread_local_key/tests.rs
Rollup merge of #102857 - saethlin:derived-enum-hash-test, r=Mark-Simulacrum
[rust.git] / library / std / src / sys / windows / thread_local_key / tests.rs
1 use super::StaticKey;
2 use crate::ptr;
3
4 #[test]
5 fn smoke() {
6     static K1: StaticKey = StaticKey::new(None);
7     static K2: StaticKey = StaticKey::new(None);
8
9     unsafe {
10         assert!(K1.get().is_null());
11         assert!(K2.get().is_null());
12         K1.set(ptr::invalid_mut(1));
13         K2.set(ptr::invalid_mut(2));
14         assert_eq!(K1.get() as usize, 1);
15         assert_eq!(K2.get() as usize, 2);
16     }
17 }
18
19 #[test]
20 fn destructors() {
21     use crate::mem::ManuallyDrop;
22     use crate::sync::Arc;
23     use crate::thread;
24
25     unsafe extern "C" fn destruct(ptr: *mut u8) {
26         drop(Arc::from_raw(ptr as *const ()));
27     }
28
29     static KEY: StaticKey = StaticKey::new(Some(destruct));
30
31     let shared1 = Arc::new(());
32     let shared2 = Arc::clone(&shared1);
33
34     unsafe {
35         assert!(KEY.get().is_null());
36         KEY.set(Arc::into_raw(shared1) as *mut u8);
37     }
38
39     thread::spawn(move || unsafe {
40         assert!(KEY.get().is_null());
41         KEY.set(Arc::into_raw(shared2) as *mut u8);
42     })
43     .join()
44     .unwrap();
45
46     // Leak the Arc, let the TLS destructor clean it up.
47     let shared1 = unsafe { ManuallyDrop::new(Arc::from_raw(KEY.get() as *const ())) };
48     assert_eq!(
49         Arc::strong_count(&shared1),
50         1,
51         "destructor should have dropped the other reference on thread exit"
52     );
53 }