use std::{
collections::VecDeque,
fmt,
panic::{AssertUnwindSafe, catch_unwind},
sync::{
Arc, Condvar, Mutex, MutexGuard,
atomic::{AtomicBool, AtomicU64, Ordering},
},
thread::{self, JoinHandle},
};
static NEXT_JOB: AtomicU64 = AtomicU64::new(0);
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct JobId(u64);
#[derive(Clone, Debug)]
pub struct Cancellation {
cancelled: Arc<AtomicBool>,
stopping: Arc<AtomicBool>,
}
impl Cancellation {
pub fn is_cancelled(&self) -> bool {
self.cancelled.load(Ordering::Acquire) || self.stopping.load(Ordering::Acquire)
}
}
#[derive(Clone, Debug)]
pub struct JobHandle {
id: JobId,
cancellation: Cancellation,
}
impl JobHandle {
pub fn id(&self) -> JobId {
self.id
}
pub fn cancel(&self) {
self.cancellation.cancelled.store(true, Ordering::Release);
}
pub fn is_cancelled(&self) -> bool {
self.cancellation.is_cancelled()
}
pub fn accepts<R>(&self, completion: &Completion<R>) -> bool {
self.id == completion.id && !self.is_cancelled()
}
}
#[derive(Debug)]
pub enum JobOutcome<R> {
Ready(R),
Cancelled,
Panicked,
}
#[derive(Debug)]
pub struct Completion<R> {
pub id: JobId,
pub outcome: JobOutcome<R>,
}
pub enum SubmitError<F> {
Full(F),
Stopped(F),
IdExhausted(F),
}
impl<F> SubmitError<F> {
pub fn into_inner(self) -> F {
match self {
Self::Full(work) | Self::Stopped(work) | Self::IdExhausted(work) => work,
}
}
}
impl<F> fmt::Debug for SubmitError<F> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::Full(_) => "Full",
Self::Stopped(_) => "Stopped",
Self::IdExhausted(_) => "IdExhausted",
})
}
}
impl<F> fmt::Display for SubmitError<F> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(self, f)
}
}
impl<F> std::error::Error for SubmitError<F> {}
#[derive(Debug)]
pub enum JobPoolError {
InvalidConfig,
Spawn(std::io::Error),
}
impl fmt::Display for JobPoolError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidConfig => f.write_str("workers and capacity must be positive"),
Self::Spawn(error) => write!(f, "worker creation failed: {error}"),
}
}
}
impl std::error::Error for JobPoolError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Spawn(error) => Some(error),
_ => None,
}
}
}
type Work<R> = Box<dyn FnOnce(Cancellation) -> R + Send + 'static>;
struct Job<R> {
handle: JobHandle,
work: Work<R>,
}
struct Finished<R> {
cancellation: Cancellation,
completion: Completion<R>,
}
struct State<R> {
queued: VecDeque<Job<R>>,
completed: VecDeque<Finished<R>>,
outstanding: usize,
closed: bool,
}
struct Shared<R> {
state: Mutex<State<R>>,
available: Condvar,
stopping: Arc<AtomicBool>,
}
impl<R> Shared<R> {
fn lock(&self) -> MutexGuard<'_, State<R>> {
self.state
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner())
}
}
pub struct JobPool<R: Send + 'static> {
shared: Arc<Shared<R>>,
workers: Vec<JoinHandle<()>>,
capacity: usize,
}
impl<R: Send + 'static> JobPool<R> {
pub fn new(workers: usize, capacity: usize) -> Result<Self, JobPoolError> {
if workers == 0 || capacity == 0 {
return Err(JobPoolError::InvalidConfig);
}
let shared = Arc::new(Shared {
state: Mutex::new(State {
queued: VecDeque::new(),
completed: VecDeque::new(),
outstanding: 0,
closed: false,
}),
available: Condvar::new(),
stopping: Arc::new(AtomicBool::new(false)),
});
let mut pool = Self {
shared,
workers: Vec::new(),
capacity,
};
for index in 0..workers {
let shared = Arc::clone(&pool.shared);
let worker = thread::Builder::new()
.name(format!("rayengine-job-{index}"))
.spawn(move || worker_loop(shared))
.map_err(JobPoolError::Spawn)?;
pool.workers.push(worker);
}
Ok(pool)
}
pub fn capacity(&self) -> usize {
self.capacity
}
pub fn pending(&self) -> usize {
self.shared.lock().outstanding
}
pub fn try_submit<F>(&self, work: F) -> Result<JobHandle, SubmitError<F>>
where
F: FnOnce(Cancellation) -> R + Send + 'static,
{
let mut state = self.shared.lock();
if state.closed {
return Err(SubmitError::Stopped(work));
}
if state.outstanding == self.capacity {
return Err(SubmitError::Full(work));
}
#[allow(deprecated)]
let Ok(id) =
NEXT_JOB.fetch_update(Ordering::Relaxed, Ordering::Relaxed, |id| id.checked_add(1))
else {
return Err(SubmitError::IdExhausted(work));
};
let handle = JobHandle {
id: JobId(id),
cancellation: Cancellation {
cancelled: Arc::new(AtomicBool::new(false)),
stopping: Arc::clone(&self.shared.stopping),
},
};
state.queued.push_back(Job {
handle: handle.clone(),
work: Box::new(work),
});
state.outstanding += 1;
drop(state);
self.shared.available.notify_one();
Ok(handle)
}
pub fn try_recv(&self) -> Option<Completion<R>> {
let finished = {
let mut state = self.shared.lock();
let finished = state.completed.pop_front()?;
state.outstanding -= 1;
finished
};
let mut completion = finished.completion;
if finished.cancellation.is_cancelled() {
completion.outcome = JobOutcome::Cancelled;
}
Some(completion)
}
pub fn shutdown(&mut self) {
self.shared.stopping.store(true, Ordering::Release);
let discarded = {
let mut state = self.shared.lock();
state.closed = true;
state.outstanding = 0;
(
std::mem::take(&mut state.queued),
std::mem::take(&mut state.completed),
)
};
self.shared.available.notify_all();
drop(discarded);
for worker in self.workers.drain(..) {
if worker.thread().id() != thread::current().id() {
let _ = worker.join();
}
}
}
}
impl<R: Send + 'static> Drop for JobPool<R> {
fn drop(&mut self) {
self.shutdown();
}
}
fn worker_loop<R: Send + 'static>(shared: Arc<Shared<R>>) {
loop {
let job = {
let mut state = shared.lock();
loop {
if state.closed {
return;
}
if let Some(job) = state.queued.pop_front() {
break job;
}
state = shared
.available
.wait(state)
.unwrap_or_else(|poisoned| poisoned.into_inner());
}
};
let Job { handle, work } = job;
let cancellation = handle.cancellation;
let outcome = match catch_unwind(AssertUnwindSafe(|| {
if cancellation.is_cancelled() {
drop(work);
JobOutcome::Cancelled
} else {
JobOutcome::Ready(work(cancellation.clone()))
}
})) {
Ok(outcome) => outcome,
Err(payload) => {
if let Err(nested) = catch_unwind(AssertUnwindSafe(|| drop(payload))) {
std::mem::forget(nested);
}
JobOutcome::Panicked
}
};
let finished = Finished {
cancellation,
completion: Completion {
id: handle.id,
outcome,
},
};
let mut state = shared.lock();
if !state.closed {
state.completed.push_back(finished);
} else {
drop(state);
drop(finished);
}
}
}
#[cfg(test)]
mod tests;