pub trait JoinHandle {
fn join(self);
}
pub trait Thread: Send + Clone + 'static {
type JoinHandle: JoinHandle;
fn spawn(&self, f: impl FnOnce() + Send + 'static) -> Self::JoinHandle;
fn yield_now(&self);
fn pin(&self, core: usize);
}
#[derive(Clone)]
pub(crate) struct DefaultThread;
impl JoinHandle for std::thread::JoinHandle<()> {
fn join(self) {
let _ = self.join();
}
}
impl Thread for DefaultThread {
type JoinHandle = std::thread::JoinHandle<()>;
fn spawn(&self, f: impl FnOnce() + Send + 'static) -> Self::JoinHandle {
std::thread::spawn(f)
}
fn yield_now(&self) {
std::thread::yield_now();
}
fn pin(&self, core: usize) {
let cores = core_affinity::get_core_ids().unwrap();
core_affinity::set_for_current(cores[core % cores.len()]);
}
}