Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,13 @@ jobs:
- run: cargo fmt -- --check
- run: cargo test
- run: cargo bench
miri:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install Rust
run: rustup toolchain install nightly --component miri && rustup default nightly
- run: cargo miri test
env:
MIRIFLAGS: -Zmiri-strict-provenance -Zmiri-symbolic-alignment-check -Zmiri-disable-isolation
RUSTFLAGS: ${{ env.RUSTFLAGS }} -Z randomize-layout
25 changes: 23 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,7 @@ impl<T: Send> ThreadLocal<T> {
}
unsafe {
let entry = &*bucket_ptr.add(thread.index);
// Read without atomic operations as only this thread can set the value.
if (&entry.present as *const _ as *const bool).read() {
if entry.present.load(Ordering::Relaxed) {
Some(&*(&*entry.value.get()).as_ptr())
} else {
None
Expand Down Expand Up @@ -610,6 +609,28 @@ mod tests {
assert_eq!(vec![1, 2, 3], v);
}

#[test]
fn miri_iter_soundness_check() {
let tls = Arc::new(ThreadLocal::new());
let _local = tls.get_or(|| Box::new(1));

let tls2 = tls.clone();
let join_1 = thread::spawn(move || {
let _tls = tls2.get_or(|| Box::new(2));
let iter = tls2.iter();
for item in iter {
println!("{:?}", item);
}
});

let iter = tls.iter();
for item in iter {
println!("{:?}", item);
}

join_1.join().ok();
}

#[test]
fn test_drop() {
let local = ThreadLocal::new();
Expand Down