#atomic

atomic-shim

Atomic types for unsupported platforms

2 unstable releases

0.2.0 Sep 12, 2021
0.1.0 May 20, 2020

#12 in #atomic

Download history 13564/week @ 2021-06-07 12889/week @ 2021-06-14 14364/week @ 2021-06-21 14499/week @ 2021-06-28 11725/week @ 2021-07-05 11324/week @ 2021-07-12 12455/week @ 2021-07-19 13711/week @ 2021-07-26 12519/week @ 2021-08-02 13233/week @ 2021-08-09 12214/week @ 2021-08-16 13897/week @ 2021-08-23 13663/week @ 2021-08-30 12809/week @ 2021-09-06 17657/week @ 2021-09-13 15521/week @ 2021-09-20

55,469 downloads per month
Used in 35 crates (3 directly)

MIT/Apache

32KB
216 lines

atomic-shim

Atomic types shims for unsupported architectures.

This crate provides shims for std::sync::AtomicU64 and std::sync::AtomicI64 for mips and powerpc.

The std primitives are not available on all platforms, and that makes it tricky to write code for mips, such as OpenWRT Routers. This crate provides it's own AtomicU64 and AtomicI64, which can directly replace the std::sync structs.

The crate does target detection and on supported architectures it will use std::sync structures. When it detects it is running on unsupported platforms, it fallbacks to the shim implementation, using crossbeam Mutex.

For testing purposes, and for other reasons, you can replace the default implementation with the Mutex implementation by using the features = ["mutex"]

Usage

Replace any imports of use std::sync::AtomicU64; with use atomic_shim::Atomic64;

Installation

Add the dependency to your Cargo.toml, and optionally, exposes the mutex feature to test without cross-compiling:

[dependencies]
atomic-shim = "*"

# Optional
#[features]
#mutex = ["atomic-shim/mutex"]

Test

To run tests, it is important to enable the --features mutex.

cargo test --features mutex

Examples

A simple spinlock:

use std::sync::Arc;
use std::sync::atomic::Ordering;
use std::thread;
use atomic_shim::AtomicU64;

fn main() {
    let spinlock = Arc::new(AtomicU64::new(1));

    let spinlock_clone = spinlock.clone();
    let thread = thread::spawn(move|| {
        spinlock_clone.store(0, Ordering::SeqCst);
    });

    // Wait for the other thread to release the lock
    while spinlock.load(Ordering::SeqCst) != 0 {}

    if let Err(panic) = thread.join() {
        println!("Thread had an error: {:?}", panic);
    }
}

Keep a global count of live threads:

use std::sync::atomic::Ordering;
use atomic_shim::AtomicU64;

let global_thread_count = AtomicU64::new(0);

let old_thread_count = global_thread_count.fetch_add(1, Ordering::SeqCst);
println!("live threads: {}", old_thread_count + 1);

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~30KB