Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't allow event loops to run in parallel #2907

Merged
merged 1 commit into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions src/platform_impl/web/event_loop/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ pub use self::window_target::EventLoopWindowTarget;

use super::{backend, device, window};
use crate::event::Event;
use crate::event_loop::{
ControlFlow, EventLoopBuilder, EventLoopWindowTarget as RootEventLoopWindowTarget,
};
use crate::event_loop::{ControlFlow, EventLoopWindowTarget as RootEventLoopWindowTarget};

use std::marker::PhantomData;

Expand All @@ -35,7 +33,7 @@ impl<T> EventLoop<T> {
where
F: 'static + FnMut(Event<'_, T>, &RootEventLoopWindowTarget<T>, &mut ControlFlow),
{
self.spawn_inner(event_handler);
self.spawn_inner(event_handler, false);

// Throw an exception to break out of Rust execution and use unreachable to tell the
// compiler this function won't return, giving it a return type of '!'
Expand All @@ -50,11 +48,10 @@ impl<T> EventLoop<T> {
where
F: 'static + FnMut(Event<'_, T>, &RootEventLoopWindowTarget<T>, &mut ControlFlow),
{
EventLoopBuilder::<T>::allow_event_loop_recreation();
self.spawn_inner(event_handler);
self.spawn_inner(event_handler, true);
}

fn spawn_inner<F>(self, mut event_handler: F)
fn spawn_inner<F>(self, mut event_handler: F, event_loop_recreation: bool)
where
F: 'static + FnMut(Event<'_, T>, &RootEventLoopWindowTarget<T>, &mut ControlFlow),
{
Expand All @@ -63,9 +60,10 @@ impl<T> EventLoop<T> {
_marker: PhantomData,
};

self.elw.p.run(Box::new(move |event, flow| {
event_handler(event, &target, flow)
}));
self.elw.p.run(
Box::new(move |event, flow| event_handler(event, &target, flow)),
event_loop_recreation,
);
}

pub fn create_proxy(&self) -> EventLoopProxy<T> {
Expand Down
9 changes: 9 additions & 0 deletions src/platform_impl/web/event_loop/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type OnEventHandle<T> = RefCell<Option<EventListenerHandle<dyn FnMut(T)>>>;

pub struct Execution<T: 'static> {
runner: RefCell<RunnerEnum<T>>,
event_loop_recreation: Cell<bool>,
events: RefCell<VecDeque<EventWrapper<T>>>,
id: RefCell<u32>,
window: web_sys::Window,
Expand Down Expand Up @@ -139,6 +140,7 @@ impl<T: 'static> Shared<T> {
pub fn new() -> Self {
Shared(Rc::new(Execution {
runner: RefCell::new(RunnerEnum::Pending),
event_loop_recreation: Cell::new(false),
events: RefCell::new(VecDeque::new()),
#[allow(clippy::disallowed_methods)]
window: web_sys::window().expect("only callable from inside the `Window`"),
Expand Down Expand Up @@ -633,6 +635,9 @@ impl<T: 'static> Shared<T> {
// * For each undropped `Window`:
// * The `register_redraw_request` closure.
// * The `destroy_fn` closure.
if self.0.event_loop_recreation.get() {
crate::event_loop::EventLoopBuilder::<T>::allow_event_loop_recreation();
}
}

// Check if the event loop is currently closed
Expand Down Expand Up @@ -675,6 +680,10 @@ impl<T: 'static> Shared<T> {
DeviceEvents::Never => false,
}
}

pub fn event_loop_recreation(&self, allow: bool) {
self.0.event_loop_recreation.set(allow)
}
}

pub(crate) enum EventWrapper<T: 'static> {
Expand Down
3 changes: 2 additions & 1 deletion src/platform_impl/web/event_loop/window_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ impl<T> EventLoopWindowTarget<T> {
EventLoopProxy::new(self.runner.clone())
}

pub fn run(&self, event_handler: Box<runner::EventHandler<T>>) {
pub fn run(&self, event_handler: Box<runner::EventHandler<T>>, event_loop_recreation: bool) {
self.runner.event_loop_recreation(event_loop_recreation);
self.runner.set_listener(event_handler);
}

Expand Down