sparking-lot-core

Crates.iosparking-lot-core
lib.rssparking-lot-core
version0.1.3
sourcesrc
created_at2023-11-20 20:17:53.992192
updated_at2023-11-30 22:24:54.477635
descriptionA simple implementation of parking on addresses.
homepage
repositoryhttps://github.com/JuliusEmperorOfRome/sparking-lot-core
max_upload_size
id1042799
size72,637
(JuliusEmperorOfRome)

documentation

README

sparking-lot-core

s(implified-)parking-lot-core is a simplified version of parking_lot_core, the backend of parking_lot. It doesn't include timeouts and park or unpark tokens, and doesn't readjust based on thread count, so going above certain thread counts (96 by default, 384 with the more-concurrency feature), will lead to worse scaling than parking_lot_core. However, it has static memory usage and, most importantly, sparking-lot-core has loom 0.7 support with --cfg loom for concurrency testing.

Usage

First, add this to your Cargo.toml:

[dependencies]
sparking-lot-core = "0.1"

Then use it:

use sparking_lot_core::{park, unpark_one};
use std::sync::atomic::{AtomicBool, Ordering::Relaxed};
use std::thread;

fn main() {
    static WAKE_UP: AtomicBool = AtomicBool::new(false);
    let t = thread::spawn(|| {
        unsafe{
            park(&WAKE_UP as *const _ as *const _, || {
                !WAKE_UP.load(Relaxed)
            });
        };
    });
    /* Since Relaxed stores/loads are used, this wouldn't guarantee
     * the ordering of loads/stores to other variables.
     * 
     * But this is guaranteed to exit.
     */
    WAKE_UP.store(true, Relaxed);
    unpark_one(&WAKE_UP as *const _ as *const _);
    t.join().unwrap();
}

loom

loom is enabled with --cfg loom. When running loom tests, it's recommended to enable the loom-test feature, as the default test implementation is severely limited. The old behaviour is described in the docs.

License

This project is licensed under the MIT LICENSE

Commit count: 30

cargo fmt