1use std::{
10 collections::VecDeque,
11 fmt,
12 panic::{AssertUnwindSafe, catch_unwind},
13 sync::{
14 Arc, Condvar, Mutex, MutexGuard,
15 atomic::{AtomicBool, AtomicU64, Ordering},
16 },
17 thread::{self, JoinHandle},
18};
19
20static NEXT_JOB: AtomicU64 = AtomicU64::new(0);
21
22#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
24pub struct JobId(u64);
25
26#[derive(Clone, Debug)]
28pub struct Cancellation {
29 cancelled: Arc<AtomicBool>,
30 stopping: Arc<AtomicBool>,
31}
32
33impl Cancellation {
34 pub fn is_cancelled(&self) -> bool {
36 self.cancelled.load(Ordering::Acquire) || self.stopping.load(Ordering::Acquire)
37 }
38}
39
40#[derive(Clone, Debug)]
42pub struct JobHandle {
43 id: JobId,
44 cancellation: Cancellation,
45}
46
47impl JobHandle {
48 pub fn id(&self) -> JobId {
50 self.id
51 }
52
53 pub fn cancel(&self) {
55 self.cancellation.cancelled.store(true, Ordering::Release);
56 }
57
58 pub fn is_cancelled(&self) -> bool {
60 self.cancellation.is_cancelled()
61 }
62
63 pub fn accepts<R>(&self, completion: &Completion<R>) -> bool {
66 self.id == completion.id && !self.is_cancelled()
67 }
68}
69
70#[derive(Debug)]
72pub enum JobOutcome<R> {
73 Ready(R),
75 Cancelled,
77 Panicked,
79}
80
81#[derive(Debug)]
83pub struct Completion<R> {
84 pub id: JobId,
86 pub outcome: JobOutcome<R>,
88}
89
90pub enum SubmitError<F> {
92 Full(F),
94 Stopped(F),
96 IdExhausted(F),
98}
99
100impl<F> SubmitError<F> {
101 pub fn into_inner(self) -> F {
103 match self {
104 Self::Full(work) | Self::Stopped(work) | Self::IdExhausted(work) => work,
105 }
106 }
107}
108
109impl<F> fmt::Debug for SubmitError<F> {
110 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
111 f.write_str(match self {
112 Self::Full(_) => "Full",
113 Self::Stopped(_) => "Stopped",
114 Self::IdExhausted(_) => "IdExhausted",
115 })
116 }
117}
118
119impl<F> fmt::Display for SubmitError<F> {
120 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
121 fmt::Debug::fmt(self, f)
122 }
123}
124
125impl<F> std::error::Error for SubmitError<F> {}
126
127#[derive(Debug)]
129pub enum JobPoolError {
130 InvalidConfig,
132 Spawn(std::io::Error),
134}
135
136impl fmt::Display for JobPoolError {
137 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
138 match self {
139 Self::InvalidConfig => f.write_str("workers and capacity must be positive"),
140 Self::Spawn(error) => write!(f, "worker creation failed: {error}"),
141 }
142 }
143}
144
145impl std::error::Error for JobPoolError {
146 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
147 match self {
148 Self::Spawn(error) => Some(error),
149 _ => None,
150 }
151 }
152}
153
154type Work<R> = Box<dyn FnOnce(Cancellation) -> R + Send + 'static>;
155struct Job<R> {
156 handle: JobHandle,
157 work: Work<R>,
158}
159struct Finished<R> {
160 cancellation: Cancellation,
161 completion: Completion<R>,
162}
163struct State<R> {
164 queued: VecDeque<Job<R>>,
165 completed: VecDeque<Finished<R>>,
166 outstanding: usize,
167 closed: bool,
168}
169struct Shared<R> {
170 state: Mutex<State<R>>,
171 available: Condvar,
172 stopping: Arc<AtomicBool>,
173}
174
175impl<R> Shared<R> {
176 fn lock(&self) -> MutexGuard<'_, State<R>> {
177 self.state
178 .lock()
179 .unwrap_or_else(|poisoned| poisoned.into_inner())
180 }
181}
182
183pub struct JobPool<R: Send + 'static> {
190 shared: Arc<Shared<R>>,
191 workers: Vec<JoinHandle<()>>,
192 capacity: usize,
193}
194
195impl<R: Send + 'static> JobPool<R> {
196 pub fn new(workers: usize, capacity: usize) -> Result<Self, JobPoolError> {
198 if workers == 0 || capacity == 0 {
199 return Err(JobPoolError::InvalidConfig);
200 }
201 let shared = Arc::new(Shared {
202 state: Mutex::new(State {
203 queued: VecDeque::new(),
204 completed: VecDeque::new(),
205 outstanding: 0,
206 closed: false,
207 }),
208 available: Condvar::new(),
209 stopping: Arc::new(AtomicBool::new(false)),
210 });
211 let mut pool = Self {
212 shared,
213 workers: Vec::new(),
214 capacity,
215 };
216 for index in 0..workers {
217 let shared = Arc::clone(&pool.shared);
218 let worker = thread::Builder::new()
219 .name(format!("rayengine-job-{index}"))
220 .spawn(move || worker_loop(shared))
221 .map_err(JobPoolError::Spawn)?;
222 pool.workers.push(worker);
223 }
224 Ok(pool)
225 }
226
227 pub fn capacity(&self) -> usize {
229 self.capacity
230 }
231
232 pub fn pending(&self) -> usize {
234 self.shared.lock().outstanding
235 }
236
237 pub fn try_submit<F>(&self, work: F) -> Result<JobHandle, SubmitError<F>>
239 where
240 F: FnOnce(Cancellation) -> R + Send + 'static,
241 {
242 let mut state = self.shared.lock();
243 if state.closed {
244 return Err(SubmitError::Stopped(work));
245 }
246 if state.outstanding == self.capacity {
247 return Err(SubmitError::Full(work));
248 }
249 #[allow(deprecated)]
251 let Ok(id) =
252 NEXT_JOB.fetch_update(Ordering::Relaxed, Ordering::Relaxed, |id| id.checked_add(1))
253 else {
254 return Err(SubmitError::IdExhausted(work));
255 };
256 let handle = JobHandle {
257 id: JobId(id),
258 cancellation: Cancellation {
259 cancelled: Arc::new(AtomicBool::new(false)),
260 stopping: Arc::clone(&self.shared.stopping),
261 },
262 };
263 state.queued.push_back(Job {
264 handle: handle.clone(),
265 work: Box::new(work),
266 });
267 state.outstanding += 1;
268 drop(state);
269 self.shared.available.notify_one();
270 Ok(handle)
271 }
272
273 pub fn try_recv(&self) -> Option<Completion<R>> {
277 let finished = {
278 let mut state = self.shared.lock();
279 let finished = state.completed.pop_front()?;
280 state.outstanding -= 1;
281 finished
282 };
283 let mut completion = finished.completion;
284 if finished.cancellation.is_cancelled() {
285 completion.outcome = JobOutcome::Cancelled;
286 }
287 Some(completion)
288 }
289
290 pub fn shutdown(&mut self) {
295 self.shared.stopping.store(true, Ordering::Release);
296 let discarded = {
297 let mut state = self.shared.lock();
298 state.closed = true;
299 state.outstanding = 0;
300 (
301 std::mem::take(&mut state.queued),
302 std::mem::take(&mut state.completed),
303 )
304 };
305 self.shared.available.notify_all();
306 drop(discarded);
309 for worker in self.workers.drain(..) {
310 if worker.thread().id() != thread::current().id() {
311 let _ = worker.join();
312 }
313 }
316 }
317}
318
319impl<R: Send + 'static> Drop for JobPool<R> {
320 fn drop(&mut self) {
321 self.shutdown();
322 }
323}
324
325fn worker_loop<R: Send + 'static>(shared: Arc<Shared<R>>) {
326 loop {
327 let job = {
328 let mut state = shared.lock();
329 loop {
330 if state.closed {
331 return;
332 }
333 if let Some(job) = state.queued.pop_front() {
334 break job;
335 }
336 state = shared
337 .available
338 .wait(state)
339 .unwrap_or_else(|poisoned| poisoned.into_inner());
340 }
341 };
342 let Job { handle, work } = job;
343 let cancellation = handle.cancellation;
344 let outcome = match catch_unwind(AssertUnwindSafe(|| {
345 if cancellation.is_cancelled() {
346 drop(work);
349 JobOutcome::Cancelled
350 } else {
351 JobOutcome::Ready(work(cancellation.clone()))
352 }
353 })) {
354 Ok(outcome) => outcome,
355 Err(payload) => {
356 if let Err(nested) = catch_unwind(AssertUnwindSafe(|| drop(payload))) {
359 std::mem::forget(nested);
362 }
363 JobOutcome::Panicked
364 }
365 };
366 let finished = Finished {
367 cancellation,
368 completion: Completion {
369 id: handle.id,
370 outcome,
371 },
372 };
373 let mut state = shared.lock();
374 if !state.closed {
375 state.completed.push_back(finished);
378 } else {
379 drop(state);
380 drop(finished);
381 }
382 }
383}
384
385#[cfg(test)]
386mod tests;