From a293b962f7ddc4e2fc0212f6a4cb9d3a4df5b32b Mon Sep 17 00:00:00 2001 From: Florian RICHER Date: Sat, 31 May 2025 12:40:19 +0200 Subject: [PATCH] use RefCell instead of RwLock - Gui and VulkanoWindows is not thread safe - It's more adapted with association with Rc --- src/core/app/context.rs | 23 +++++++++-------------- src/core/app/mod.rs | 32 +++++++++++++++----------------- 2 files changed, 24 insertions(+), 31 deletions(-) diff --git a/src/core/app/context.rs b/src/core/app/context.rs index cedd65f..d6d1647 100644 --- a/src/core/app/context.rs +++ b/src/core/app/context.rs @@ -1,4 +1,5 @@ use std::{ + cell::RefCell, rc::Rc, sync::{Arc, RwLock}, }; @@ -39,19 +40,19 @@ pub struct ApplicationContext { pub window_id: WindowId, // Données mutables partagées avec Arc> - pub vulkano_windows: Rc>, + pub vulkano_windows: Rc>, pub input_manager: Arc>, pub timer: Arc>, - pub gui: Rc>, + pub gui: Rc>, } impl ApplicationContext { pub fn new( vulkan_context: Arc, - vulkano_windows: Rc>, + vulkano_windows: Rc>, input_manager: Arc>, timer: Arc>, - gui: Rc>, + gui: Rc>, event_loop_proxy: EventLoopProxy, window_id: WindowId, ) -> Self { @@ -129,10 +130,7 @@ impl ApplicationContext { where F: FnOnce(&VulkanoWindowRenderer) -> T, { - let vulkano_windows = self - .vulkano_windows - .read() - .expect("Failed to lock vulkano_windows"); + let vulkano_windows = self.vulkano_windows.borrow_mut(); let renderer = vulkano_windows .get_renderer(self.window_id) .expect("Failed to get renderer"); @@ -144,10 +142,7 @@ impl ApplicationContext { where F: FnOnce(&mut VulkanoWindowRenderer) -> T, { - let mut vulkano_windows = self - .vulkano_windows - .write() - .expect("Failed to lock vulkano_windows"); + let mut vulkano_windows = self.vulkano_windows.borrow_mut(); let renderer = vulkano_windows .get_renderer_mut(self.window_id) .expect("Failed to get renderer"); @@ -159,7 +154,7 @@ impl ApplicationContext { where F: FnOnce(&Gui) -> T, { - let gui = self.gui.read().expect("Failed to lock gui"); + let gui = self.gui.borrow(); f(&gui) } @@ -168,7 +163,7 @@ impl ApplicationContext { where F: FnOnce(&mut Gui) -> T, { - let mut gui = self.gui.write().expect("Failed to lock gui"); + let mut gui = self.gui.borrow_mut(); f(&mut gui) } diff --git a/src/core/app/mod.rs b/src/core/app/mod.rs index c5e7acb..d936f57 100644 --- a/src/core/app/mod.rs +++ b/src/core/app/mod.rs @@ -1,3 +1,4 @@ +use std::cell::RefCell; use std::collections::HashMap; use std::rc::Rc; use std::sync::{Arc, RwLock}; @@ -28,15 +29,15 @@ pub const DEPTH_IMAGE_ID: usize = 0; pub struct App { vulkan_context: Arc, - vulkano_windows: Rc>, - gui: HashMap>>, + vulkano_windows: Rc>, + gui: HashMap>>, scene_manager: HashMap, input_manager: Arc>, timer: Arc>, event_loop_proxy: EventLoopProxy, // Context d'application partagé par fenêtre - architecture unifiée - app_contexts: HashMap>>, + app_contexts: HashMap>>, } impl App { @@ -47,7 +48,7 @@ impl App { ) -> Self { Self { vulkan_context: Arc::new(VulkanContext::new(vulkano_context)), - vulkano_windows: Rc::new(RwLock::new(VulkanoWindows::default())), + vulkano_windows: Rc::new(RefCell::new(VulkanoWindows::default())), gui: HashMap::new(), input_manager: Arc::new(RwLock::new(input_manager)), scene_manager: HashMap::new(), @@ -60,10 +61,7 @@ impl App { impl ApplicationHandler for App { fn resumed(&mut self, event_loop: &ActiveEventLoop) { - let mut vulkano_windows = self - .vulkano_windows - .write() - .expect("Failed to lock vulkano_windows"); + let mut vulkano_windows = self.vulkano_windows.borrow_mut(); let window_id = vulkano_windows.create_window( event_loop, self.vulkan_context.vulkano_context(), @@ -99,7 +97,7 @@ impl ApplicationHandler for App { }, ) }; - self.gui.insert(window_id, Rc::new(RwLock::new(gui))); + self.gui.insert(window_id, Rc::new(RefCell::new(gui))); let mut scene_manager = SceneManager::new(); scene_manager.load_scene(Box::new(MainScene::default())); @@ -120,7 +118,7 @@ impl ApplicationHandler for App { fn window_event(&mut self, event_loop: &ActiveEventLoop, id: WindowId, event: WindowEvent) { { let gui = self.gui.get_mut(&id).unwrap(); - let mut gui = gui.write().expect("Failed to lock gui"); + let mut gui = gui.borrow_mut(); if !gui.update(&event) { let mut input_manager = self.input_manager.write().unwrap(); input_manager.process_window_event(&event); @@ -133,7 +131,7 @@ impl ApplicationHandler for App { event_loop.exit(); } WindowEvent::Resized(_) | WindowEvent::ScaleFactorChanged { .. } => { - let mut vulkano_windows = self.vulkano_windows.write().unwrap(); + let mut vulkano_windows = self.vulkano_windows.borrow_mut(); vulkano_windows.get_renderer_mut(id).unwrap().resize(); } WindowEvent::RedrawRequested => { @@ -158,7 +156,7 @@ impl ApplicationHandler for App { .app_contexts .entry(id) .or_insert_with(|| { - Rc::new(RwLock::new(ApplicationContext::new( + Rc::new(RefCell::new(ApplicationContext::new( self.vulkan_context.clone(), self.vulkano_windows.clone(), self.input_manager.clone(), @@ -175,7 +173,7 @@ impl ApplicationHandler for App { // Utiliser le contexte partagé pour les scènes { let _scene_span = tracing::info_span!("scene_processing").entered(); - let mut context = app_context.write().unwrap(); + let mut context = app_context.borrow_mut(); { let _scene_span = @@ -219,7 +217,7 @@ impl ApplicationHandler for App { } fn about_to_wait(&mut self, _event_loop: &ActiveEventLoop) { - let vulkano_windows = self.vulkano_windows.read().unwrap(); + let vulkano_windows = self.vulkano_windows.borrow(); let window = vulkano_windows.get_primary_window().unwrap(); window.request_redraw(); } @@ -227,14 +225,14 @@ impl ApplicationHandler for App { fn user_event(&mut self, event_loop: &ActiveEventLoop, event: UserEvent) { match event { UserEvent::CursorGrabMode(window_id, grab) => { - let vulkano_windows = self.vulkano_windows.read().unwrap(); + let vulkano_windows = self.vulkano_windows.borrow(); let window = vulkano_windows.get_window(window_id).unwrap(); if let Err(e) = window.set_cursor_grab(grab) { tracing::error!("Failed to set cursor grab: {}", e); } } UserEvent::CursorVisible(window_id, visible) => { - let vulkano_windows = self.vulkano_windows.read().unwrap(); + let vulkano_windows = self.vulkano_windows.borrow(); let window = vulkano_windows.get_window(window_id).unwrap(); window.set_cursor_visible(visible); } @@ -244,7 +242,7 @@ impl ApplicationHandler for App { } } UserEvent::ChangeResolution(window_id, width, height) => { - let mut vulkano_windows = self.vulkano_windows.write().unwrap(); + let mut vulkano_windows = self.vulkano_windows.borrow_mut(); let window = vulkano_windows.get_window(window_id).unwrap(); let _ = window.request_inner_size(winit::dpi::LogicalSize::new(width, height)); let renderer = vulkano_windows.get_renderer_mut(window_id).unwrap();