#lock-free #atomic #non-blocking #no-std #replace

no-std atomic_once_cell

Thread-safe and lock-free OnceCell and Lazy

7 releases

new 0.2.0 Feb 13, 2025
0.1.6 Sep 21, 2023
0.1.5 Mar 25, 2023
0.1.3 Mar 4, 2022
0.1.2 Dec 23, 2021

#151 in Concurrency

Download history 1107/week @ 2024-10-26 1445/week @ 2024-11-02 708/week @ 2024-11-09 759/week @ 2024-11-16 608/week @ 2024-11-23 1079/week @ 2024-11-30 1519/week @ 2024-12-07 1593/week @ 2024-12-14 1079/week @ 2024-12-21 882/week @ 2024-12-28 942/week @ 2025-01-04 726/week @ 2025-01-11 771/week @ 2025-01-18 678/week @ 2025-01-25 683/week @ 2025-02-01 1255/week @ 2025-02-08

3,555 downloads per month

MPL-2.0 license

27KB
469 lines

Pipeline Crates.io API reference

atomic_once_cell

atomic_once_cell provides two new types, AtomicOnceCell and AtomicLazy, which are thread-safe and mostly lock-free drop-in replacements of core::lazy::OnceCell and core::lazy::Lazy suitable for use in #[no_std] environments.

Blocking

Because dereferencing AtomicLazy can't fail, it can't be lock-free (if you know a way, please tell me).

Both types can be used in a non-blocking way, but there are some blocking calls that should not be used from interrupt handlers or other contexts where blocking will lead to a deadlock. Blocking is based on crossbeam::utils::Backoff, and will be reduced to a spinlock in #[no_std] environments.

Examples

AtomicOnceCell

use atomic_once_cell::AtomicOnceCell;

static CELL: AtomicOnceCell<String> = AtomicOnceCell::new();

fn main() {
    CELL.set("Hello, World!".to_owned()).unwrap();

    assert_eq!(*CELL.get().unwrap(), "Hello, World!");
}

AtomicLazy

use atomic_once_cell::AtomicLazy;

static LAZY: AtomicLazy<String> = AtomicLazy::new(|| "Hello, World!".to_owned());

fn main() {
    assert_eq!(*LAZY, "Hello, World!");
}

For more details, see docs.

Usage

Add this to your Cargo.toml:

[dependencies]
atomic_once_cell = "0.2.0"

License

MPL-2.0

Dependencies

~1–24MB
~320K SLoC