#atomic #generic #order #cas #atom

no-std atomig

Generic, convenient and lock-free std atomics via Atomic<T>. Can be used with many primitive types (including floats) and with custom types.

9 releases

0.4.2 Oct 18, 2024
0.4.1 Mar 4, 2024
0.4.0 Apr 9, 2022
0.3.3 Dec 30, 2021
0.1.0 Jul 24, 2019

#70 in Concurrency

Download history 81/week @ 2024-08-17 86/week @ 2024-08-24 73/week @ 2024-08-31 84/week @ 2024-09-07 74/week @ 2024-09-14 215/week @ 2024-09-21 498/week @ 2024-09-28 2081/week @ 2024-10-05 4046/week @ 2024-10-12 2305/week @ 2024-10-19 2359/week @ 2024-10-26 3590/week @ 2024-11-02 4849/week @ 2024-11-09 2993/week @ 2024-11-16 3454/week @ 2024-11-23 4502/week @ 2024-11-30

16,330 downloads per month
Used in 9 crates (3 directly)

MIT/Apache

68KB
831 lines

Atomig: generic and convenient std atomics

CI status of master Crates.io Version docs.rs

Offers Atomic<T> that can be used with primitive and custom types. However, it only works with types that can actually use atomic operations: a lock-based fallback for other types is not used! This crate is based on std's atomics and therefore does not contain any unsafe code! This crate also does not have any dependencies by default. If you enable the serde feature, then this crate will depend on serde and Serialize / Deserialize will be implemented for Atomic<T> when appropriate, using sequentially-consistent ordering.

Simple example with primitive types:

use atomig::{Atomic, Ordering};

let x = Atomic::new(27); // `Atomic<i32>`
x.store(39, Ordering::SeqCst);

This works with almost all primitive types, including f32, f64 and char, but also with types like std::ptr::NonNull and std::num::NonZero.

You can automatically derive Atom for your own enum or struct types to use them in Atomic<T>. There are some limitations, however.

// Requires the 'derive' feature:
//     atomig = { version = "_", features = ["derive"] }
use atomig::{Atom, Atomic, Ordering};

#[derive(Atom)]
#[repr(u8)]
enum Animal { Dog, Cat, Fox }

let animal = Atomic::new(Animal::Cat);
animal.store(Animal::Fox, Ordering::SeqCst);

#[derive(Atom)]
struct Port(u16);

let port = Atomic::new(Port(80));
port.store(Port(8080), Ordering::SeqCst);

For more examples and information see the documentation.



License

Licensed under either of Apache License, Version 2.0 or MIT license at your option. Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this project by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~0–290KB