A spinlock for rust
Switch branches/tags
Nothing to show
Clone or download
mvdnes Merge pull request #60 from Ericson2314/patch-1
Update my email in the authors field
Latest commit 7301fe0 Dec 21, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
examples Remove example as it would fail the builds Sep 10, 2016
script Rename repo to spin-rs May 13, 2016
src Turns out `once` works fine in stable as well Dec 7, 2018
.gitignore Improve .gitignore Jan 20, 2015
.travis.yml Test stable and beta on Travis May 14, 2016
Cargo.toml Update my email Dec 9, 2018
LICENSE Initial commit Jul 10, 2014
README.md Remove need for const_fn feature Dec 7, 2018

README.md

spin-rs

Build Status Crates.io version

Documentation

This Rust library implements a simple spinlock.

Usage

By default this crate only works on nightly but you can disable the default features if you want to run on stable. Nightly is more efficient than stable currently.

Include the following code in your Cargo.toml

[dependencies.spin]
version = "0.4"
# If you want to run on stable you will need to add the following:
# default-features = false

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;