use std::sync::atomic::{AtomicUsize, Ordering};
use cbsk_base::log;
use cbsk_base::parking_lot::RwLock;
use crate::pool::pool_detail::PoolDetail;
pub mod pool_detail;
pub struct Pool {
pub thread_pool: RwLock<Vec<PoolDetail>>,
pub thread_pool_num: AtomicUsize,
}
impl Default for Pool {
fn default() -> Self {
let mut thread_pool = Vec::with_capacity(2);
thread_pool.push(PoolDetail::build());
Self {
thread_pool: thread_pool.into(),
thread_pool_num: AtomicUsize::new(100),
}
}
}
impl Pool {
pub fn spawn(&self, f: impl FnOnce() + Send + 'static) {
let mut thread_pool = self.thread_pool.write();
for pool in thread_pool.iter() {
if pool.is_idle() {
pool.spawn(f);
return;
}
}
let pool =
match PoolDetail::try_build() {
Ok(pool) => { pool }
Err(e) => {
log::error!("build thread fail:{e:?}");
return;
}
};
pool.spawn(f);
thread_pool.push(pool);
}
pub fn is_idle(&self) -> bool {
let thread_pool = self.thread_pool.read();
if thread_pool.len() < self.thread_pool_num.load(Ordering::Acquire) {
return true;
}
cbsk_base::match_some_return!(thread_pool.last(),false).is_idle()
}
}