async_component_winit/executor/
signal.rs

1use std::sync::atomic::{AtomicBool, Ordering};
2
3use parking_lot::Mutex;
4use winit::event_loop::EventLoopProxy;
5
6use super::ExecutorPollEvent;
7
8/// Signal [`winit:EventLoop`] using [`ExecutorPollEvent`] user event with [`EventLoopProxy`]
9#[derive(Debug)]
10pub struct WinitSignal {
11    pub scheduled: AtomicBool,
12    proxy: Mutex<EventLoopProxy<ExecutorPollEvent>>,
13}
14
15impl WinitSignal {
16    /// Create new [`WinitSignal`] with given [`EventLoopProxy`]
17    pub const fn new(proxy: EventLoopProxy<ExecutorPollEvent>) -> Self {
18        Self {
19            scheduled: AtomicBool::new(true),
20            proxy: Mutex::new(proxy),
21        }
22    }
23}
24
25impl WinitSignal {
26    pub fn wake_by_ref(&self) {
27        if self
28            .scheduled
29            .compare_exchange(false, true, Ordering::AcqRel, Ordering::Acquire)
30            .is_ok()
31        {
32            self.proxy.lock().send_event(ExecutorPollEvent).ok();
33        }
34    }
35}