use super::*;
pub struct Pool(Box<TP_CALLBACK_ENVIRON_V3>);
impl Pool {
pub fn new() -> Self {
let mut e = TP_CALLBACK_ENVIRON_V3 {
Version: 3,
CallbackPriority: TP_CALLBACK_PRIORITY_NORMAL,
Size: core::mem::size_of::<TP_CALLBACK_ENVIRON_V3>() as u32,
..Default::default()
};
unsafe {
e.Pool = check(CreateThreadpool(core::ptr::null()));
e.CleanupGroup = check(CreateThreadpoolCleanupGroup());
}
Self(Box::new(e))
}
pub fn set_thread_limits(&self, min: u32, max: u32) {
unsafe {
check(SetThreadpoolThreadMinimum(self.0.Pool, min));
SetThreadpoolThreadMaximum(self.0.Pool, max);
}
}
pub fn submit<'a, F: FnOnce() + Send + 'a>(&'a self, f: F) {
unsafe {
try_submit(&*self.0, f);
}
}
pub fn join(&self) {
unsafe {
CloseThreadpoolCleanupGroupMembers(self.0.CleanupGroup, 0, core::ptr::null_mut());
}
}
}
impl Default for Pool {
fn default() -> Self {
Self::new()
}
}
unsafe impl Sync for Pool {}
unsafe impl Send for Pool {}
impl Drop for Pool {
fn drop(&mut self) {
self.join();
unsafe {
CloseThreadpoolCleanupGroup(self.0.CleanupGroup);
CloseThreadpool(self.0.Pool);
}
}
}