1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use std::{
pin::Pin,
sync::{
atomic::{AtomicBool, Ordering},
Arc,
},
task::{Context, Poll, Wake, Waker},
};
use async_component::{AsyncComponent, ComponentPollFlags};
use parking_lot::Mutex;
use winit::event_loop::EventLoopProxy;
use crate::ExecutorPollEvent;
#[derive(Debug)]
pub struct WinitExecutor {
signal: Arc<WinitSignal>,
waker: Waker,
}
impl WinitExecutor {
pub fn new(proxy: EventLoopProxy<ExecutorPollEvent>) -> Self {
let signal = Arc::new(WinitSignal {
scheduled: AtomicBool::new(false),
proxy: Mutex::new(proxy),
});
let waker = Waker::from(signal.clone());
Self { signal, waker }
}
pub fn poll_component(
&self,
component: Pin<&mut impl AsyncComponent>,
) -> Poll<ComponentPollFlags> {
let _ = self.signal.scheduled.compare_exchange(
true,
false,
Ordering::AcqRel,
Ordering::Acquire,
);
component.poll_next(&mut Context::from_waker(&self.waker))
}
}
#[derive(Debug)]
struct WinitSignal {
scheduled: AtomicBool,
proxy: Mutex<EventLoopProxy<ExecutorPollEvent>>,
}
impl Wake for WinitSignal {
fn wake(self: Arc<Self>) {
self.wake_by_ref()
}
fn wake_by_ref(self: &Arc<Self>) {
if let Ok(_) =
self.scheduled
.compare_exchange(false, true, Ordering::AcqRel, Ordering::Acquire)
{
self.proxy.lock().send_event(ExecutorPollEvent).ok();
}
}
}