[][src]Crate spin_sync

CircleCI Build Status

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

The main features are as follows.

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);
}

Structs

Barrier

A barrier enables multiple threads to synchronize the beginning of some computation.

BarrierWaitResult
Mutex

A mutual exclusion primitive useful for protecting shared data.

Mutex8

Mutex8 is a set of mutexes. Each instance includes 8 mutexes.

Mutex8Guard

An RAII implementation of a "scoped lock(s)" of a Mutex8 .

MutexGuard

An RAII implementation of a "scoped lock" of a mutex.

Once

A synchronization primitive which can be used to run a one-time global initialization.

OnceState

State yielded to call_once_force ’s closure parameter. The state can be used to query the poison status of the Once

RwLock

A reader-writer lock.

RwLockReadGuard

An RAII implementation of a "scoped shared read lock" of a RwLock.

RwLockWriteGuard

An RAII implementation of a "scoped exclusive write lock" of a RwLock.

Type Definitions

LockResult

Alias to std::sync::LockResult.

PoisonError

Alias to std::sync::PoisonError

TryLockError

Alias to std::sync::TryLockError

TryLockResult

Alias to std::sync::TryLockResult