pi_async_executor/
parking.rs1use pi_async_runtime_core::task::TaskNotifier;
3use pi_async_task_queue::notify::QueueNotifier;
4use std::{
5 marker::PhantomData,
6 rc::Rc,
7 sync::{atomic::AtomicBool, Arc},
8 thread::{Thread, ThreadId},
9 time::Duration,
10};
11
12pub struct ThreadNotifier {
17 shared: Arc<ThreadNotification>,
18}
19struct ThreadNotification {
20 thread: Thread,
21 notified: AtomicBool,
22}
23impl Clone for ThreadNotifier {
24 fn clone(&self) -> Self {
25 Self {
26 shared: Arc::clone(&self.shared),
27 }
28 }
29}
30impl QueueNotifier for ThreadNotifier {
31 fn notify(&self) {
33 thread_notify_inner(&self.shared)
34 }
35}
36impl TaskNotifier for ThreadNotifier {}
37
38pub struct ThreadParker {
43 notifier: ThreadNotifier,
44 owner: ThreadId,
45 local: PhantomData<Rc<()>>,
46}
47#[derive(Clone, Copy, Debug, Eq, PartialEq)]
49pub enum ThreadParkResult {
50 Notified,
52 Recheck,
54}
55impl ThreadParker {
56 pub fn new() -> Self {
58 thread_parker_new_inner()
59 }
60 pub fn notifier(&self) -> ThreadNotifier {
62 self.notifier.clone()
63 }
64 pub fn owner_thread(&self) -> ThreadId {
66 self.owner
67 }
68 pub fn is_notified(&self) -> bool {
70 thread_is_notified_inner(&self.notifier.shared)
71 }
72 pub fn take_notification(&mut self) -> bool {
74 thread_take_notification_inner(self)
75 }
76 pub fn park_timeout(&mut self, duration: Duration) -> ThreadParkResult {
83 thread_park_inner(self, duration)
84 }
85}
86impl Default for ThreadParker {
87 fn default() -> Self {
88 Self::new()
89 }
90}
91include!("parking_inner.rs");