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

[Merged by Bors] - Make WinitWindows non send #4027

Closed
wants to merge 1 commit into from
Closed
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
10 changes: 5 additions & 5 deletions crates/bevy_winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub struct WinitPlugin;

impl Plugin for WinitPlugin {
fn build(&self, app: &mut App) {
app.init_resource::<WinitWindows>()
app.init_non_send_resource::<WinitWindows>()
.set_runner(winit_runner)
.add_system_to_stage(CoreStage::PostUpdate, change_window.exclusive_system());
let event_loop = EventLoop::new();
Expand All @@ -43,7 +43,7 @@ impl Plugin for WinitPlugin {

fn change_window(world: &mut World) {
let world = world.cell();
let winit_windows = world.get_resource::<WinitWindows>().unwrap();
let winit_windows = world.get_non_send::<WinitWindows>().unwrap();
let mut windows = world.get_resource_mut::<Windows>().unwrap();

for bevy_window in windows.iter_mut() {
Expand Down Expand Up @@ -264,7 +264,7 @@ pub fn winit_runner_with(mut app: App) {
..
} => {
let world = app.world.cell();
let winit_windows = world.get_resource_mut::<WinitWindows>().unwrap();
let winit_windows = world.get_non_send_mut::<WinitWindows>().unwrap();
let mut windows = world.get_resource_mut::<Windows>().unwrap();
let window_id =
if let Some(window_id) = winit_windows.get_window_id(winit_window_id) {
Expand Down Expand Up @@ -525,7 +525,7 @@ fn handle_create_window_events(
create_window_event_reader: &mut ManualEventReader<CreateWindow>,
) {
let world = world.cell();
let mut winit_windows = world.get_resource_mut::<WinitWindows>().unwrap();
let mut winit_windows = world.get_non_send_mut::<WinitWindows>().unwrap();
let mut windows = world.get_resource_mut::<Windows>().unwrap();
let create_window_events = world.get_resource::<Events<CreateWindow>>().unwrap();
let mut window_created_events = world.get_resource_mut::<Events<WindowCreated>>().unwrap();
Expand All @@ -544,7 +544,7 @@ fn handle_create_window_events(

fn handle_initial_window_events(world: &mut World, event_loop: &EventLoop<()>) {
let world = world.cell();
let mut winit_windows = world.get_resource_mut::<WinitWindows>().unwrap();
let mut winit_windows = world.get_non_send_mut::<WinitWindows>().unwrap();
let mut windows = world.get_resource_mut::<Windows>().unwrap();
let mut create_window_events = world.get_resource_mut::<Events<CreateWindow>>().unwrap();
let mut window_created_events = world.get_resource_mut::<Events<WindowCreated>>().unwrap();
Expand Down
4 changes: 4 additions & 0 deletions crates/bevy_winit/src/winit_windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ pub struct WinitWindows {
pub windows: HashMap<winit::window::WindowId, winit::window::Window>,
pub window_id_to_winit: HashMap<WindowId, winit::window::WindowId>,
pub winit_to_window_id: HashMap<winit::window::WindowId, WindowId>,
// Some winit functions, such as `set_window_icon` can only be used from the main thread. If
// they are used in another thread, the app will hang. This marker ensures `WinitWindows` is
// only ever accessed with bevy's non-send functions and in NonSend systems.
_not_send_sync: core::marker::PhantomData<*const ()>,
}

impl WinitWindows {
Expand Down