#[cfg(feature = "std")]
use std::thread::{current, park, yield_now, Thread};
pub trait Unpark: Clone {
fn unpark(&self);
}
pub trait DefaultPark: Unpark + Sized {
type Park: Park<Self>;
fn default_park() -> Self::Park;
}
#[cfg(feature = "std")]
impl Unpark for Thread {
#[inline]
fn unpark(&self) {
self.unpark();
}
}
#[cfg(feature = "std")]
impl DefaultPark for Thread {
type Park = CurrentThread;
#[inline]
fn default_park() -> Self::Park {
CurrentThread
}
}
pub trait Park<T: Unpark> {
fn unpark_token(&self) -> T;
fn park(&self);
}
#[cfg(feature = "std")]
pub struct CurrentThread;
#[cfg(feature = "std")]
impl Park<Thread> for CurrentThread {
#[inline]
fn park(&self) {
park();
}
#[inline]
fn unpark_token(&self) -> Thread {
current()
}
}
#[derive(Clone, Copy)]
pub struct UnparkYield;
impl Unpark for UnparkYield {
#[inline]
fn unpark(&self) {}
}
pub struct ParkYield;
impl Park<UnparkYield> for ParkYield {
#[inline]
fn park(&self) {
#[cfg(feature = "std")]
yield_now();
#[cfg(not(feature = "std"))]
core::hint::spin_loop();
}
#[inline]
fn unpark_token(&self) -> UnparkYield {
UnparkYield
}
}