use std::task::Poll;
use crate::{
driver::schedule::ready::task::{self, pool, topology},
executor::task::Fiber,
executor::task::batch,
executor::task::context::{PollCall, wake},
};
pub struct Pool<'d, const N: usize>(pub(super) task::raw::Scheduler<'d, N>);
impl<'d, const N: usize> Pool<'d, N> {
pub fn try_new(parent: wake::RootWaker<'d>) -> Result<Self, batch::Error> {
let parent: wake::Waker<'d> = parent.into();
let ready = topology::Endpoint::try_new(parent.0).map_err(batch::Error::Task)?;
let tasks = pool::Pool::try_new(parent.0).map_err(batch::Error::Task)?;
Ok(Self(task::raw::Scheduler::new(tasks, ready)))
}
pub fn acquire() -> impl Fiber<'d, Output = Result<Self, batch::Error>> {
Acquire::<N>
}
}
struct Acquire<const N: usize>;
impl<'d, const N: usize> Fiber<'d> for Acquire<N> {
type Output = Result<Pool<'d, N>, batch::Error>;
fn poll(call: PollCall<'_, '_, 'd, Self>) -> Poll<Self::Output> {
Poll::Ready(Pool::try_new(call.into_context().root_waker()))
}
}
const _: () =
assert!(core::mem::size_of::<Pool<'static, 1>>() == 5 * core::mem::size_of::<usize>());
const _: () = assert!(
core::mem::size_of::<Pool<'static, 4096>>() == core::mem::size_of::<Pool<'static, 1>>()
);