#spinlock #mutex #rwlock

spin

Synchronization primitives based on spinning. They may contain data, are usable without std, and static initializers are available

25 releases

0.5.2 Aug 27, 2019
0.5.0 Jan 8, 2019
0.4.10 Oct 30, 2018
0.4.8 Apr 20, 2018
0.1.3 Jan 30, 2015

#5 in Concurrency

Download history 44976/week @ 2019-08-15 41986/week @ 2019-08-22 42612/week @ 2019-08-29 51378/week @ 2019-09-05 52662/week @ 2019-09-12 51325/week @ 2019-09-19 50915/week @ 2019-09-26 59390/week @ 2019-10-03 56755/week @ 2019-10-10 66847/week @ 2019-10-17 69154/week @ 2019-10-24 63669/week @ 2019-10-31 71609/week @ 2019-11-07 74902/week @ 2019-11-14 74145/week @ 2019-11-21

185,533 downloads per month
Used in 967 crates (61 directly)

MIT license

45KB
892 lines

spin-rs

Build Status Crates.io version docs.rs

This Rust library implements a simple spinlock, and is safe for #[no_std] environments.

Usage

Include the following code in your Cargo.toml

[dependencies.spin]
version = "0.5"

Example

When calling lock on a Mutex you will get a reference to the data. When this reference is dropped, the lock will be unlocked.

extern crate spin;

fn main()
{
    let mutex   = spin::Mutex::new(0);
    let rw_lock = spin::RwLock::new(0);

    // Modify the data
    {
      let mut data = mutex.lock();
      *data = 2;
      let mut data = rw_lock.write();
      *data = 3;
    }

    // Read the data
    let answer =
    {
      let data1 = mutex.lock();
      let data2 = rw_lock.read();
      let data3 = rw_lock.read(); // sharing
      (*data1, *data2, *data3)
    };

    println!("Answers are {:?}", answer);
}

To share the lock, an Arc<Mutex<T>> may be used.

Remarks

The behaviour of these lock is similar to their namesakes in std::sync. they differ on the following:

  • The lock will not be poisoned in case of failure;

No runtime deps