spincell 0.1.0

A small, lightweight thread-safe cell implementation targeting no_std environments.
Documentation
  • Coverage
  • 0%
    0 out of 6 items documented0 out of 5 items with examples
  • Size
  • Source code size: 8.37 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 769.68 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 13s Average build duration of successful builds.
  • all releases: 11s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • ushiba0/spincell
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • ushiba0

spincell

A small, lightweight thread-safe cell implementation targeting no_std environments.

Features

  • Small and lightweight: designed to be minimal and easy to audit.
  • Targets no_std environments: uses core primitives (AtomicBool, UnsafeCell, MaybeUninit).
  • Provides a Lazy/Delayed initialization API similar to LazyLock.
  • Uses a spinlock to implement a thread-safe cell.

Usage

fn main() {
    // SpinCell behaves like a normal cell.
    let one = SpinCell::new(1usize);
    assert_eq!(1, *one);

    // Supports lazy initialization.
    let lazy_init_one = SpinCell::uninit();
    // try_initialize accepts a closure that constructs T, returns Err(()) if
    // the cell was already initialized.
    lazy_init_one.try_initialize(|| 1).unwrap();
    assert_eq!(1, *lazy_init_one);

    // Supports containing structs.
    let message = SpinCell::new(String::from("Hi!"));
    assert_eq!(*message, "Hi!");

    // Accessing an uninitialized cell will panic.
    let uninit: SpinCell<usize> = SpinCell::uninit();
    let _ = *uninit; // Panic!
}

Future work / TODO

  • Improve TOCTOU behavior during initialization. Current try_initialize / force_initialize interactions can lead to races where a thread observes the cell as uninitialized and another thread initializes it concurrently. Possible fixes include using an atomic compare-exchange to acquire initialization rights, using Arc-based publish-swap, or employing an epoch/RCU-style reclamation mechanism.
  • Support for pluggable lock implementations (a lock_api-like abstraction). Allow users to choose different locking strategies (e.g., spin vs parking vs OS mutex) for performance and platform constraints.