use alloc::boxed::Box;
use alloc::collections::VecDeque;
use alloc::sync::Arc;
use alloc::vec::Vec;
use std::sync::{Condvar, Mutex};
pub type Task = Option<Box<dyn FnOnce() + Send + 'static>>;
#[allow(non_camel_case_types)]
#[derive(Debug)]
pub struct TaskScheduler {
pub(crate) thread_count: u32,
pub(crate) workers: Vec<std::thread::JoinHandle<()>>,
pub(crate) tasks: Arc<TaskQueue>,
}
pub struct TaskQueue {
pub(crate) mtx: Mutex<VecDeque<Task>>,
pub(crate) cv: Condvar,
}
impl core::fmt::Debug for TaskQueue {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("TaskQueue").finish_non_exhaustive()
}
}
impl TaskQueue {
pub(crate) fn new() -> Self {
Self {
mtx: Mutex::new(VecDeque::new()),
cv: Condvar::new(),
}
}
}
impl TaskScheduler {
pub fn get_thread_count() -> u32 {
core::cmp::max(
std::thread::available_parallelism()
.map(|n| n.get() as u32)
.unwrap_or(1),
1,
)
}
}
impl Drop for TaskScheduler {
fn drop(&mut self) {
crate::methods::task_scheduler_task_scheduler_analyze_alt_b::task_scheduler_destructor(
self,
);
}
}