#top #spinlock #image #crate #abstractions

spinning_top

A simple spinlock crate based on the abstractions provided by lock_api

7 releases

0.2.4 May 13, 2021
0.2.3 Apr 1, 2021
0.2.2 Aug 24, 2020
0.2.1 Jul 7, 2020
0.1.0 Feb 12, 2020

#18 in Concurrency

Download history 14909/week @ 2021-07-02 15960/week @ 2021-07-09 17264/week @ 2021-07-16 19731/week @ 2021-07-23 18003/week @ 2021-07-30 15632/week @ 2021-08-06 15630/week @ 2021-08-13 17487/week @ 2021-08-20 12235/week @ 2021-08-27 10181/week @ 2021-09-03 13442/week @ 2021-09-10 12704/week @ 2021-09-17 10522/week @ 2021-09-24 9029/week @ 2021-10-01 9218/week @ 2021-10-08 10845/week @ 2021-10-15

73,599 downloads per month
Used in 59 crates (7 directly)

MIT/Apache

49KB
103 lines

spinning_top

image of a spinning top

Build Status Docs.rs Badge

A simple spinlock crate based on the abstractions provided by lock_api.

Example

First, import the crate as a dependency in your Cargo.toml. Then you can use it in the following way:

use spinning_top::Spinlock;

fn main() {
    // Wrap some data in a spinlock
    let data = String::from("Hello");
    let spinlock = Spinlock::new(data);
    make_uppercase(&spinlock); // only pass a shared reference
    // We have ownership of the spinlock, so we can extract the data without locking
    // Note: this consumes the spinlock
    let data = spinlock.into_inner();
    assert_eq!(data.as_str(), "HELLO");
}

fn make_uppercase(spinlock: &Spinlock<String>) {
    // Lock the spinlock to get a mutable reference to the data
    let mut locked_data = spinlock.lock();
    assert_eq!(locked_data.as_str(), "Hello");
    locked_data.make_ascii_uppercase();

    // the lock is automatically freed at the end of the scope
}

The nightly Feature

On Rust nightly, the nightly feature of this crate can be enabled to make the Spinlock::new function a const function. This makes the Spinlock type usable in statics:

use spinning_top::Spinlock;

static DATA: Spinlock<u32> = Spinlock::new(0);

fn main() {
    let mut data = DATA.lock();
    *data += 1;
    assert_eq!(*data, 1);
}

License

Licensed under either of

at your option.

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

~175KB