#spin-lock #synchronization-primitive #thread-synchronization #mutex #sync #lock #spin

spin-sync

Synchronization primitives using spinlock. The interface resembles std::sync. The constructors of public structs are const; i.e. it is possible to declare static Mutex<T> as long as T can be build statically.

9 unstable releases (3 breaking)

0.3.2 Feb 5, 2021
0.3.1 Feb 3, 2021
0.3.0 Jan 22, 2021
0.2.1 Sep 4, 2020
0.0.1 Nov 13, 2019

#855 in Concurrency

27 downloads per month

LGPL-3.0-or-later OR Apache-2…

1MB
968 lines

Contains (WOFF font, 190KB) docs/FiraSans-Medium.woff, (WOFF font, 185KB) docs/FiraSans-Regular.woff, (WOFF font, 94KB) docs/SourceSerifPro-Bold.ttf.woff, (WOFF font, 89KB) docs/SourceSerifPro-Regular.ttf.woff, (WOFF font, 56KB) docs/SourceCodePro-Regular.woff, (WOFF font, 56KB) docs/SourceCodePro-Semibold.woff and 1 more.

Build Status Build Status

spin-sync

CircleCI Build Status

spin-sync is a module providing synchronization primitives using spinlock. (Wikipedia Spinlock)

The main features are as follows.

  • Declaring public structs Mutex , RwLock , Once , Barrier . The interfaces are resembles those of std::sync .
  • Ensuring safety as much as std::sync , including poisoning strategy and marker traits.
  • Declaring public struct Mutex8 , which behaves like a set of 8 Mutex instances except for it gives up poison strategy. It is possible to acquire 2 or more than 2 locks of 1 Mutex8 instance at once.
  • Unlike to std::sync, the constructors of the public structs are const. For example, it is possible to declare static Mutex<T> as long as T can be build statically.

Examples

Declare static spin_sync::Mutex<u64> variable and update from multi threads. It is impossible in case of std::sync::Mutex .

extern crate spin_sync;

use spin_sync::Mutex;
use std::thread;

// Declare static mut Mutex<u64> variable.
static COUNT: Mutex<u64> = Mutex::new(0);

fn main() {
    let num_thread = 10;
    let mut handles = Vec::new();

    // Create worker threads to inclement COUNT by 1.
    for _ in 0..10 {
        let handle = thread::spawn(move || {
            let mut count = COUNT.lock().unwrap();
            *count += 1;
        });

        handles.push(handle);
    }

    // Wait for all the workers.
    for handle in handles {
        handle.join().unwrap();
    }

    // Make sure the value is incremented by the worker count.
    let count = COUNT.lock().unwrap();
    assert_eq!(num_thread, *count);
}

License: LGPL-3.0-or-later OR Apache-2.0 OR BSD-2-Clause

No runtime deps