use crate::config::config;
use crate::coroutine_impl::CoroutineImpl;
use crossbeam::queue::ArrayQueue as Queue;
use generator::Gn;
pub struct CoroutinePool {
pool: Queue<CoroutineImpl>,
}
impl CoroutinePool {
fn create_dummy_coroutine() -> CoroutineImpl {
Gn::new_opt(config().get_stack_size(), move || {
unreachable!("dummy coroutine should never be called");
})
}
pub fn new() -> Self {
let capacity = config().get_pool_capacity();
let pool = Queue::new(capacity);
for _ in 0..capacity {
let co = Self::create_dummy_coroutine();
pool.push(co).unwrap();
}
CoroutinePool { pool }
}
#[inline]
pub fn get(&self) -> CoroutineImpl {
match self.pool.pop() {
Some(co) => co,
None => Self::create_dummy_coroutine(),
}
}
#[inline]
pub fn put(&self, co: CoroutineImpl) {
self.pool.push(co).ok();
}
}