Skip to content

JuliusEmperorOfRome/sparking-lot-core

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 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