Amanieu / thread_local-rs

Per-object thread-local storage for Rust

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Failure under loom

ibraheemdev opened this issue · comments

#[test]
#[cfg(loom)]
fn loom() {
    use thread_local::ThreadLocal;
    use loom::sync::atomic::{AtomicPtr, Ordering};
    use std::sync::Arc;

    loom::model(|| {
        let x = Arc::new(ThreadLocal::<AtomicPtr<usize>>::with_capacity(2));

        loom::thread::spawn({
            let x = x.clone();

            move || {
                for x in x.iter() {
                    unsafe { assert_eq!(*x.load(Ordering::Acquire), 1) }
                }
            }
        });

        x.get_or(|| AtomicPtr::new(Box::into_raw(Box::new(1))));
    });
}

Fails with:

thread 'tls::tests::loom' panicked at 'Causality violation: Concurrent load and mut accesses.',
/loom-0.5.4/src/rt/location.rs:115:9

Note that this is not using any loom types inside ThreadLocal.

I believe the issue is the synchronization between the creation of the AtomicPtr (which counts as a mut access), and the load. I'm not sure if this is a loom bug, or a bug in thread-local.

It seems this is a loom bug.