#thread-local-storage #os #primitive #storage-api #lazy-evaluation #per-object #os-backed

os-thread-local

OS-backed thread-local storage. This crate provides a ThreadLocal type as an alternative to std::thread_local! that allows per-object thread-local storage, while providing a similar API. It always uses the thread-local storage primitives provided by the OS.

4 releases

0.1.3 Dec 19, 2019
0.1.2 Oct 25, 2019
0.1.1 Oct 25, 2019
0.1.0 Oct 25, 2019

#954 in Rust patterns

Download history 475/week @ 2024-01-06 3411/week @ 2024-01-13 11445/week @ 2024-01-20 12777/week @ 2024-01-27 12486/week @ 2024-02-03 12919/week @ 2024-02-10 13178/week @ 2024-02-17 13131/week @ 2024-02-24 12832/week @ 2024-03-02 13327/week @ 2024-03-09 13146/week @ 2024-03-16 15334/week @ 2024-03-23 13010/week @ 2024-03-30 12944/week @ 2024-04-06 11630/week @ 2024-04-13 11195/week @ 2024-04-20

50,635 downloads per month
Used in 4 crates (3 directly)

Apache-2.0/MIT

27KB
390 lines

OS-backed thread-local storage

This crate provides a ThreadLocal type as an alternative to std::thread_local! that allows per-object thread-local storage, while providing a similar API. It always uses the thread-local storage primitives provided by the OS.

On Unix systems, pthread-based thread-local storage is used.

On Windows, fiber-local storage is used. This acts like thread-local storage when fibers are unused, but also provides per-fiber values after fibers are created with e.g. winapi::um::winbase::CreateFiber.

The thread_local crate is an example of another crate that provides per-object thread-local storage, with a different API, and different features, but with more performance overhead than this one.

Examples

This is the same as the example in the std::thread::LocalKey documentation, but adjusted to use ThreadLocal instead. To use it in a static context, a lazy initializer, such as once_cell::sync::Lazy or lazy_static! is required.

use std::cell::RefCell;
use std::thread;
use once_cell::sync::Lazy;
use os_thread_local::ThreadLocal;

static FOO: Lazy<ThreadLocal<RefCell<u32>>> =
    Lazy::new(|| ThreadLocal::new(|| RefCell::new(1)));

FOO.with(|f| {
    assert_eq!(*f.borrow(), 1);
    *f.borrow_mut() = 2;
});

// each thread starts out with the initial value of 1
let t = thread::spawn(move || {
    FOO.with(|f| {
        assert_eq!(*f.borrow(), 1);
        *f.borrow_mut() = 3;
    });
});

// wait for the thread to complete and bail out on panic
t.join().unwrap();

// we retain our original value of 2 despite the child thread
FOO.with(|f| {
    assert_eq!(*f.borrow(), 2);
});

A variation of the same with scoped threads and per-object thread-local storage:

use std::cell::RefCell;
use crossbeam_utils::thread::scope;
use os_thread_local::ThreadLocal;

struct Foo {
    data: u32,
    tls: ThreadLocal<RefCell<u32>>,
}

let foo = Foo {
    data: 0,
    tls: ThreadLocal::new(|| RefCell::new(1)),
};

foo.tls.with(|f| {
    assert_eq!(*f.borrow(), 1);
    *f.borrow_mut() = 2;
});

scope(|s| {
    // each thread starts out with the initial value of 1
    let foo2 = &foo;
    let t = s.spawn(move |_| {
        foo2.tls.with(|f| {
            assert_eq!(*f.borrow(), 1);
            *f.borrow_mut() = 3;
        });
    });

    // wait for the thread to complete and bail out on panic
    t.join().unwrap();

    // we retain our original value of 2 despite the child thread
    foo.tls.with(|f| {
        assert_eq!(*f.borrow(), 2);
    });
}).unwrap();

Dependencies

~215KB