move stream width related into unit

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-12-24 14:37:26 +00:00 committed by strawberry
parent a407909d09
commit 3b95af9a18
2 changed files with 28 additions and 29 deletions

View file

@ -0,0 +1,26 @@
use std::sync::atomic::{AtomicUsize, Ordering};
/// Stream concurrency factor; this is a live value.
static WIDTH: AtomicUsize = AtomicUsize::new(32);
/// Practicable limits on the stream width
pub const WIDTH_LIMIT: (usize, usize) = (1, 1024);
/// Sets the live concurrency factor. The first return value is the previous
/// width which was replaced. The second return value is the value which was set
/// after any applied limits.
pub fn set_width(width: usize) -> (usize, usize) {
let width = width.clamp(WIDTH_LIMIT.0, WIDTH_LIMIT.1);
(WIDTH.swap(width, Ordering::Relaxed), width)
}
/// Used by stream operations where the concurrency factor hasn't been manually
/// supplied by the caller (most uses). Instead we provide a default value which
/// is adjusted at startup for the specific system and also dynamically.
#[inline]
pub fn automatic_width() -> usize {
let width = WIDTH.load(Ordering::Relaxed);
debug_assert!(width >= WIDTH_LIMIT.0, "WIDTH should not be zero");
debug_assert!(width <= WIDTH_LIMIT.1, "WIDTH is probably too large");
width
}

View file

@ -1,3 +1,4 @@
mod band;
mod broadband; mod broadband;
mod cloned; mod cloned;
mod expect; mod expect;
@ -9,6 +10,7 @@ mod try_broadband;
mod try_ready; mod try_ready;
mod wideband; mod wideband;
pub use band::{automatic_width, set_width, WIDTH_LIMIT};
pub use broadband::BroadbandExt; pub use broadband::BroadbandExt;
pub use cloned::Cloned; pub use cloned::Cloned;
pub use expect::TryExpect; pub use expect::TryExpect;
@ -19,32 +21,3 @@ pub use tools::Tools;
pub use try_broadband::TryBroadbandExt; pub use try_broadband::TryBroadbandExt;
pub use try_ready::TryReadyExt; pub use try_ready::TryReadyExt;
pub use wideband::WidebandExt; pub use wideband::WidebandExt;
/// Stream concurrency factor; this is a live value.
static WIDTH: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(32);
/// Practicable limits on the stream width
pub const WIDTH_LIMIT: (usize, usize) = (1, 1024);
/// Sets the live concurrency factor. The first return value is the previous
/// width which was replaced. The second return value is the value which was set
/// after any applied limits.
pub fn set_width(width: usize) -> (usize, usize) {
use std::sync::atomic::Ordering;
let width = width.clamp(WIDTH_LIMIT.0, WIDTH_LIMIT.1);
(WIDTH.swap(width, Ordering::Relaxed), width)
}
/// Used by stream operations where the concurrency factor hasn't been manually
/// supplied by the caller (most uses). Instead we provide a default value which
/// is adjusted at startup for the specific system and also dynamically.
#[inline]
pub fn automatic_width() -> usize {
use std::sync::atomic::Ordering;
let width = WIDTH.load(Ordering::Relaxed);
debug_assert!(width >= WIDTH_LIMIT.0, "WIDTH should not be zero");
debug_assert!(width <= WIDTH_LIMIT.1, "WIDTH is probably too large");
width
}