Skip to content

Commit

Permalink
web: make EventLoopProxy less generalized
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda committed Nov 12, 2024
1 parent 8bd65c7 commit af7c8d9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 51 deletions.
80 changes: 35 additions & 45 deletions src/platform_impl/web/event_loop/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,54 +8,47 @@ use crate::event_loop::EventLoopProxyProvider;
use crate::platform_impl::web::event_loop::runner::WeakShared;
use crate::platform_impl::web::r#async::{AtomicWaker, Wrapper};

pub struct EventLoopProxy(Wrapper<Handler, Sender, ()>);
pub struct EventLoopProxy(Wrapper<WeakShared, Arc<State>, ()>);

struct Handler {
execution: WeakShared,
handler: fn(&WeakShared, bool),
struct State {
awoken: AtomicBool,
waker: AtomicWaker,
closed: AtomicBool,
}

#[derive(Clone)]
struct Sender(Arc<Inner>);

impl EventLoopProxy {
pub fn new(
main_thread: MainThreadMarker,
execution: WeakShared,
handler: fn(&WeakShared, bool),
) -> Self {
let inner = Arc::new(Inner {
pub fn new(main_thread: MainThreadMarker, runner: WeakShared) -> Self {
let state = Arc::new(State {
awoken: AtomicBool::new(false),
waker: AtomicWaker::new(),
closed: AtomicBool::new(false),
});

let handler = Handler { execution, handler };

let sender = Sender(Arc::clone(&inner));

Self(Wrapper::new(
main_thread,
handler,
|handler, _| {
let handler = handler.borrow();
let handler = handler.as_ref().unwrap();
(handler.handler)(&handler.execution, true);
runner,
|runner, _| {
let runner = runner.borrow();
let runner = runner.as_ref().unwrap();

if let Some(runner) = runner.upgrade() {
runner.send_proxy_wake_up(true);
}
},
{
let inner = Arc::clone(&inner);
let state = Arc::clone(&state);

move |handler| async move {
move |runner| async move {
while future::poll_fn(|cx| {
if inner.awoken.swap(false, Ordering::Relaxed) {
if state.awoken.swap(false, Ordering::Relaxed) {
Poll::Ready(true)
} else {
inner.waker.register(cx.waker());
state.waker.register(cx.waker());

if inner.awoken.swap(false, Ordering::Relaxed) {
if state.awoken.swap(false, Ordering::Relaxed) {
Poll::Ready(true)
} else {
if inner.closed.load(Ordering::Relaxed) {
if state.closed.load(Ordering::Relaxed) {
return Poll::Ready(false);
}

Expand All @@ -65,16 +58,19 @@ impl EventLoopProxy {
})
.await
{
let handler = handler.borrow();
let handler = handler.as_ref().unwrap();
(handler.handler)(&handler.execution, false);
let runner = runner.borrow();
let runner = runner.as_ref().unwrap();

if let Some(runner) = runner.upgrade() {
runner.send_proxy_wake_up(false);
}
}
}
},
sender,
|inner, _| {
inner.0.awoken.store(true, Ordering::Relaxed);
inner.0.waker.wake();
state,
|state, _| {
state.awoken.store(true, Ordering::Relaxed);
state.waker.wake();
},
))
}
Expand All @@ -85,15 +81,15 @@ impl EventLoopProxy {
"this should only be called from the main thread"
);

self.0.with_sender_data(|inner| inner.0.awoken.swap(false, Ordering::Relaxed))
self.0.with_sender_data(|state| state.awoken.swap(false, Ordering::Relaxed))
}
}

impl Drop for EventLoopProxy {
fn drop(&mut self) {
self.0.with_sender_data(|inner| {
inner.0.closed.store(true, Ordering::Relaxed);
inner.0.waker.wake();
self.0.with_sender_data(|state| {
state.closed.store(true, Ordering::Relaxed);
state.waker.wake();
});
}
}
Expand All @@ -103,9 +99,3 @@ impl EventLoopProxyProvider for EventLoopProxy {
self.0.send(())
}
}

struct Inner {
awoken: AtomicBool,
waker: AtomicWaker,
closed: AtomicBool,
}
7 changes: 1 addition & 6 deletions src/platform_impl/web/event_loop/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,7 @@ impl Shared {
let document = window.document().expect("Failed to obtain document");

Shared(Rc::<Execution>::new_cyclic(|weak| {
let proxy_spawner =
EventLoopProxy::new(main_thread, WeakShared(weak.clone()), |runner, local| {
if let Some(runner) = runner.upgrade() {
runner.send_proxy_wake_up(local);
}
});
let proxy_spawner = EventLoopProxy::new(main_thread, WeakShared(weak.clone()));

let monitor = MonitorHandler::new(
main_thread,
Expand Down

0 comments on commit af7c8d9

Please sign in to comment.