rust/tests/pass/concurrency/thread_locals.rs

67 lines
1.4 KiB
Rust
Raw Normal View History

2022-07-08 11:08:32 -05:00
//@ignore-windows: Concurrency on Windows is not supported yet.
//@compile-flags: -Zmiri-strict-provenance
2020-04-19 16:22:55 -05:00
//! The main purpose of this test is to check that if we take a pointer to
//! thread's `t1` thread-local `A` and send it to another thread `t2`,
//! dereferencing the pointer on `t2` resolves to `t1`'s thread-local. In this
//! test, we also check that thread-locals act as per-thread statics.
2020-04-01 18:28:53 -05:00
#![feature(thread_local)]
use std::thread;
#[thread_local]
static mut A: u8 = 0;
#[thread_local]
static mut B: u8 = 0;
static mut C: u8 = 0;
// Regression test for https://github.com/rust-lang/rust/issues/96191.
#[thread_local]
static READ_ONLY: u8 = 42;
2020-04-01 18:28:53 -05:00
unsafe fn get_a_ref() -> *mut u8 {
&mut A
}
struct Sender(*mut u8);
unsafe impl Send for Sender {}
2020-04-01 18:28:53 -05:00
fn main() {
let _val = READ_ONLY;
let ptr = unsafe {
2020-04-01 18:28:53 -05:00
let x = get_a_ref();
*x = 5;
assert_eq!(A, 5);
B = 15;
C = 25;
Sender(&mut A)
};
2020-04-06 18:12:25 -05:00
thread::spawn(move || unsafe {
assert_eq!(*ptr.0, 5);
assert_eq!(A, 0);
assert_eq!(B, 0);
assert_eq!(C, 25);
B = 14;
C = 24;
let y = get_a_ref();
assert_eq!(*y, 0);
*y = 4;
assert_eq!(*ptr.0, 5);
assert_eq!(A, 4);
assert_eq!(*get_a_ref(), 4);
})
.join()
.unwrap();
2020-04-01 18:28:53 -05:00
unsafe {
assert_eq!(*get_a_ref(), 5);
assert_eq!(A, 5);
assert_eq!(B, 15);
assert_eq!(C, 24);
}
}