use super::*;
use core::{marker::PhantomData, ops::Deref};
pub struct Pool(Box<TP_CALLBACK_ENVIRON_V3>);
impl Pool {
pub fn new() -> Self {
let mut e = environment(
core::ptr::null_mut(),
core::ptr::null_mut(),
Priority::Normal,
);
unsafe {
e.Pool = check_ptr(CreateThreadpool(core::ptr::null_mut()));
e.CleanupGroup = check_ptr(CreateThreadpoolCleanupGroup());
}
Self(Box::new(e))
}
pub fn with_scope<'env, F>(f: F)
where
F: for<'scope> FnOnce(&'scope Scope<'scope, 'env>),
{
let pool = Self::new();
pool.scope(f);
}
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<F: FnOnce() + Send + 'static>(&self, f: F) {
assert!(
unsafe { submit_with_environment(&*self.0, f) },
"allocation failed"
);
}
pub fn submit_with_priority<F: FnOnce() + Send + 'static>(&self, priority: Priority, f: F) {
assert!(
unsafe { submit_with_environment(&self.environment(priority), f) },
"allocation failed"
);
}
fn environment(&self, priority: Priority) -> TP_CALLBACK_ENVIRON_V3 {
environment(self.0.Pool, self.0.CleanupGroup, priority)
}
pub fn scope<'env, F>(&self, f: F)
where
F: for<'scope> FnOnce(&'scope Scope<'scope, 'env>),
{
struct DropGuard<'a>(&'a Pool);
impl Drop for DropGuard<'_> {
fn drop(&mut self) {
self.0.join();
}
}
let _guard = DropGuard(self);
let scope = Scope {
pool: self,
env: PhantomData,
scope: PhantomData,
};
f(&scope);
}
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);
}
}
}
pub struct Scope<'scope, 'env: 'scope> {
pool: &'scope Pool,
scope: PhantomData<&'scope mut &'scope ()>,
env: PhantomData<&'env mut &'env ()>,
}
impl<'scope, 'env> Scope<'scope, 'env> {
pub fn submit<F: FnOnce() + Send + 'scope>(&'scope self, f: F) {
assert!(
unsafe { submit_with_environment(&*self.pool.0, f) },
"allocation failed"
);
}
pub fn submit_with_priority<F: FnOnce() + Send + 'scope>(
&'scope self,
priority: Priority,
f: F,
) {
assert!(
unsafe { submit_with_environment(&self.pool.environment(priority), f) },
"allocation failed"
);
}
}
impl Deref for Scope<'_, '_> {
type Target = Pool;
fn deref(&self) -> &Self::Target {
self.pool
}
}