#spin-lock #abstraction #top #reference #reference-data #lock-api #spinning

spinning_top

A simple spinlock crate based on the abstractions provided by lock_api

9 releases

0.3.0 Oct 19, 2023
0.2.5 Feb 24, 2023
0.2.4 May 13, 2021
0.2.3 Apr 1, 2021
0.1.0 Feb 12, 2020

#155 in Concurrency

Download history 69152/week @ 2024-07-29 71887/week @ 2024-08-05 85206/week @ 2024-08-12 93331/week @ 2024-08-19 104813/week @ 2024-08-26 90748/week @ 2024-09-02 96242/week @ 2024-09-09 89441/week @ 2024-09-16 107137/week @ 2024-09-23 114194/week @ 2024-09-30 108379/week @ 2024-10-07 126085/week @ 2024-10-14 125627/week @ 2024-10-21 123249/week @ 2024-10-28 114926/week @ 2024-11-04 139953/week @ 2024-11-11

509,878 downloads per month
Used in 327 crates (12 directly)

MIT/Apache

53KB
552 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
}

Spinlock::new is 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

~170KB