use std::sync::atomic::{AtomicUsize, Ordering};
const DEFAULT_STACK_SIZE: usize = 0x1000;
const DEFAULT_POOL_CAPACITY: usize = 1000;
static WORKERS: AtomicUsize = AtomicUsize::new(0);
static STACK_SIZE: AtomicUsize = AtomicUsize::new(DEFAULT_STACK_SIZE);
static POOL_CAPACITY: AtomicUsize = AtomicUsize::new(DEFAULT_POOL_CAPACITY);
pub struct Config;
pub fn config() -> Config {
Config
}
impl Config {
pub fn set_workers(&self, workers: usize) -> &Self {
info!("set workers={:?}", workers);
WORKERS.store(workers, Ordering::Relaxed);
self
}
pub fn get_workers(&self) -> usize {
let workers = WORKERS.load(Ordering::Relaxed);
if workers != 0 {
workers
} else {
let num = num_cpus::get();
WORKERS.store(num, Ordering::Relaxed);
num
}
}
#[deprecated(since = "0.3.13", note = "use `set_workers` only")]
pub fn set_io_workers(&self, _workers: usize) -> &Self {
self
}
pub fn set_pool_capacity(&self, capacity: usize) -> &Self {
info!("set pool capacity={:?}", capacity);
POOL_CAPACITY.store(capacity, Ordering::Release);
self
}
pub fn get_pool_capacity(&self) -> usize {
let size = POOL_CAPACITY.load(Ordering::Acquire);
if size != 0 {
size
} else {
DEFAULT_POOL_CAPACITY
}
}
pub fn set_stack_size(&self, size: usize) -> &Self {
info!("set stack size={:?}", size);
STACK_SIZE.store(size, Ordering::Release);
self
}
pub fn get_stack_size(&self) -> usize {
STACK_SIZE.load(Ordering::Acquire)
}
}