use pi_async_runtime_core::task::TaskNotifier;
use pi_async_task_queue::notify::QueueNotifier;
use std::{
marker::PhantomData,
rc::Rc,
sync::{atomic::AtomicBool, Arc, OnceLock},
thread::{Thread, ThreadId},
time::Duration,
};
pub struct ThreadNotifier {
shared: Arc<ThreadNotification>,
}
struct ThreadNotification {
thread: OnceLock<Thread>,
notified: AtomicBool,
}
impl Clone for ThreadNotifier {
fn clone(&self) -> Self {
Self {
shared: Arc::clone(&self.shared),
}
}
}
impl PartialEq for ThreadNotifier {
fn eq(&self, other: &Self) -> bool {
thread_notifier_eq_inner(self, other)
}
}
impl Eq for ThreadNotifier {}
impl QueueNotifier for ThreadNotifier {
fn notify(&self) {
thread_notify_inner(&self.shared)
}
}
impl TaskNotifier for ThreadNotifier {}
pub struct ThreadParker {
notifier: ThreadNotifier,
owner: ThreadId,
local: PhantomData<Rc<()>>,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ThreadParkResult {
Notified,
Recheck,
}
impl ThreadParker {
pub fn new() -> Self {
thread_parker_new_inner()
}
pub fn notifier(&self) -> ThreadNotifier {
self.notifier.clone()
}
pub fn owner_thread(&self) -> ThreadId {
self.owner
}
pub fn is_notified(&self) -> bool {
thread_is_notified_inner(&self.notifier.shared)
}
pub fn take_notification(&mut self) -> bool {
thread_take_notification_inner(self)
}
pub fn park_timeout(&mut self, duration: Duration) -> ThreadParkResult {
thread_park_inner(self, duration)
}
}
impl Default for ThreadParker {
fn default() -> Self {
Self::new()
}
}
include!("parking_inner.rs");
include!("prepared_parking.rs");