use crossbeam_deque::{
Steal,
Stealer,
};
use crate::PoolJob;
pub struct ThreadPoolWorkerQueue {
worker_index: usize,
stealer: Stealer<PoolJob>,
}
impl ThreadPoolWorkerQueue {
pub fn new(worker_index: usize, stealer: Stealer<PoolJob>) -> Self {
Self {
worker_index,
stealer,
}
}
#[inline]
pub fn worker_index(&self) -> usize {
self.worker_index
}
pub fn steal_back(&self) -> Option<PoolJob> {
self.steal_one()
}
pub fn drain(&self) -> Vec<PoolJob> {
let mut jobs = Vec::new();
while let Some(job) = self.steal_one() {
jobs.push(job);
}
jobs
}
fn steal_one(&self) -> Option<PoolJob> {
loop {
match self.stealer.steal() {
Steal::Success(job) => return Some(job),
Steal::Empty => return None,
Steal::Retry => continue,
}
}
}
}