use std::sync::Arc;
use std::task::Wake;
use super::notification_latch::NotificationLatch;
pub(crate) struct ThreadWaker {
thread: std::thread::Thread,
notification: NotificationLatch,
}
impl ThreadWaker {
#[must_use]
#[inline(always)]
pub(crate) fn new(thread: std::thread::Thread) -> Self {
Self {
thread,
notification: NotificationLatch::new(),
}
}
#[inline(always)]
pub(crate) fn clear_notification(&self) {
self.notification.clear_notification();
}
#[must_use]
#[inline(always)]
pub(crate) fn take_notification(&self) -> bool {
self.notification.take_notification()
}
}
impl Wake for ThreadWaker {
#[inline(always)]
fn wake(self: Arc<Self>) {
self.wake_by_ref();
}
#[inline(always)]
fn wake_by_ref(self: &Arc<Self>) {
self.notification.notify();
self.thread.unpark();
}
}