Crate spinning_top

source ·
Expand description

Provides a simple spinlock based on the abstractions provided by the lock_api crate.

Usage Example

use spinning_top::Spinlock;

fn main() {
    let data = String::from("Hello");
    // Wrap some data in a spinlock
    let spinlock = Spinlock::new(data);

    // Lock the spinlock to get a mutex guard for the data
    let mut locked_data = spinlock.lock();
    // The guard implements the `Deref` trait, so we can use it like a `&String`
    assert_eq!(locked_data.as_str(), "Hello");
    // It also implements `DerefMut` so mutation is possible too. This is safe
    // because the spinlock ensures mutual exclusion
    assert_eq!(locked_data.as_str(), "HELLO");

    // the guard automatically frees the lock at the end of the scope



  • Provides mutual exclusion based on spinning on an AtomicBool.


Type Definitions

  • A RAII guard returned by SpinlockGuard::map.
  • A mutual exclusion (Mutex) type based on busy-waiting.
  • A RAII guard that frees the spinlock when it goes out of scope.