From b1458785e51efe651b283eaa23f454ca21e23543 Mon Sep 17 00:00:00 2001 From: Florian RICHER Date: Fri, 30 May 2025 22:04:54 +0200 Subject: [PATCH] Change Mutex by RwLock --- src/core/app/context.rs | 33 ++++++++++++++------------- src/core/app/mod.rs | 49 ++++++++++++++++++++++------------------- 2 files changed, 44 insertions(+), 38 deletions(-) diff --git a/src/core/app/context.rs b/src/core/app/context.rs index ce01c7c..f796563 100644 --- a/src/core/app/context.rs +++ b/src/core/app/context.rs @@ -1,4 +1,4 @@ -use std::sync::{Arc, Mutex}; +use std::sync::{Arc, RwLock}; use egui_winit_vulkano::Gui; use vulkano::{ @@ -35,19 +35,19 @@ pub struct ApplicationContext { pub window_id: WindowId, // Données mutables partagées avec Arc> - pub vulkano_windows: Arc>, - pub input_manager: Arc>, - pub timer: Arc>, - pub gui: Arc>, + pub vulkano_windows: Arc>, + pub input_manager: Arc>, + pub timer: Arc>, + pub gui: Arc>, } impl ApplicationContext { pub fn new( vulkan_context: Arc, - vulkano_windows: Arc>, - input_manager: Arc>, - timer: Arc>, - gui: Arc>, + vulkano_windows: Arc>, + input_manager: Arc>, + timer: Arc>, + gui: Arc>, event_loop_proxy: EventLoopProxy, window_id: WindowId, ) -> Self { @@ -172,7 +172,7 @@ impl ApplicationContext { { let vulkano_windows = self .vulkano_windows - .lock() + .read() .expect("Failed to lock vulkano_windows"); let renderer = vulkano_windows .get_renderer(self.window_id) @@ -187,7 +187,7 @@ impl ApplicationContext { { let mut vulkano_windows = self .vulkano_windows - .lock() + .write() .expect("Failed to lock vulkano_windows"); let renderer = vulkano_windows .get_renderer_mut(self.window_id) @@ -200,7 +200,7 @@ impl ApplicationContext { where F: FnOnce(&Gui) -> T, { - let gui = self.gui.lock().unwrap(); + let gui = self.gui.read().expect("Failed to lock gui"); f(&gui) } @@ -209,7 +209,7 @@ impl ApplicationContext { where F: FnOnce(&mut Gui) -> T, { - let mut gui = self.gui.lock().unwrap(); + let mut gui = self.gui.write().expect("Failed to lock gui"); f(&mut gui) } @@ -218,7 +218,10 @@ impl ApplicationContext { where F: FnOnce(&InputManager) -> T, { - let input_manager = self.input_manager.lock().unwrap(); + let input_manager = self + .input_manager + .read() + .expect("Failed to lock input_manager"); f(&input_manager) } @@ -227,7 +230,7 @@ impl ApplicationContext { where F: FnOnce(&Timer) -> T, { - let timer = self.timer.lock().unwrap(); + let timer = self.timer.read().expect("Failed to lock timer"); f(&timer) } } diff --git a/src/core/app/mod.rs b/src/core/app/mod.rs index 140dee1..d17e84d 100644 --- a/src/core/app/mod.rs +++ b/src/core/app/mod.rs @@ -1,5 +1,5 @@ use std::collections::HashMap; -use std::sync::{Arc, Mutex}; +use std::sync::{Arc, RwLock}; use super::render::vulkan_context::VulkanContext; use crate::core::input::InputManager; @@ -27,15 +27,15 @@ pub const DEPTH_IMAGE_ID: usize = 0; pub struct App { vulkan_context: Arc, - vulkano_windows: Arc>, - gui: HashMap>>, + vulkano_windows: Arc>, + gui: HashMap>>, scene_manager: HashMap, - input_manager: Arc>, - timer: Arc>, + 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 { @@ -46,11 +46,11 @@ impl App { ) -> Self { Self { vulkan_context: Arc::new(VulkanContext::new(vulkano_context)), - vulkano_windows: Arc::new(Mutex::new(VulkanoWindows::default())), + vulkano_windows: Arc::new(RwLock::new(VulkanoWindows::default())), gui: HashMap::new(), - input_manager: Arc::new(Mutex::new(input_manager)), + input_manager: Arc::new(RwLock::new(input_manager)), scene_manager: HashMap::new(), - timer: Arc::new(Mutex::new(Timer::new())), + timer: Arc::new(RwLock::new(Timer::new())), event_loop_proxy, app_contexts: HashMap::new(), } @@ -59,7 +59,10 @@ impl App { impl ApplicationHandler for App { fn resumed(&mut self, event_loop: &ActiveEventLoop) { - let mut vulkano_windows = self.vulkano_windows.lock().unwrap(); + let mut vulkano_windows = self + .vulkano_windows + .write() + .expect("Failed to lock vulkano_windows"); let window_id = vulkano_windows.create_window( event_loop, &self.vulkan_context.vulkano_context(), @@ -95,7 +98,7 @@ impl ApplicationHandler for App { }, ) }; - self.gui.insert(window_id, Arc::new(Mutex::new(gui))); + self.gui.insert(window_id, Arc::new(RwLock::new(gui))); let mut scene_manager = SceneManager::new(); scene_manager.load_scene(Box::new(MainScene::default())); @@ -109,16 +112,16 @@ impl ApplicationHandler for App { _device_id: winit::event::DeviceId, event: winit::event::DeviceEvent, ) { - let mut input_manager = self.input_manager.lock().unwrap(); + let mut input_manager = self.input_manager.write().unwrap(); input_manager.process_device_event(&event); } fn window_event(&mut self, event_loop: &ActiveEventLoop, id: WindowId, event: WindowEvent) { { let gui = self.gui.get_mut(&id).unwrap(); - let mut gui = gui.lock().unwrap(); + let mut gui = gui.write().expect("Failed to lock gui"); if !gui.update(&event) { - let mut input_manager = self.input_manager.lock().unwrap(); + let mut input_manager = self.input_manager.write().unwrap(); input_manager.process_window_event(&event); } } @@ -129,19 +132,19 @@ impl ApplicationHandler for App { event_loop.exit(); } WindowEvent::Resized(_) | WindowEvent::ScaleFactorChanged { .. } => { - let mut vulkano_windows = self.vulkano_windows.lock().unwrap(); + let mut vulkano_windows = self.vulkano_windows.write().unwrap(); vulkano_windows.get_renderer_mut(id).unwrap().resize(); } WindowEvent::RedrawRequested => { { let mut input_manager = self .input_manager - .lock() + .write() .expect("Failed to lock input manager"); input_manager.update(); } { - let mut timer = self.timer.lock().expect("Failed to lock timer"); + let mut timer = self.timer.write().expect("Failed to lock timer"); timer.update(); } @@ -150,7 +153,7 @@ impl ApplicationHandler for App { .app_contexts .entry(id) .or_insert_with(|| { - Arc::new(Mutex::new(ApplicationContext::new( + Arc::new(RwLock::new(ApplicationContext::new( self.vulkan_context.clone(), self.vulkano_windows.clone(), self.input_manager.clone(), @@ -166,7 +169,7 @@ impl ApplicationHandler for App { // Utiliser le contexte partagé pour les scènes { - let mut context = app_context.lock().unwrap(); + let mut context = app_context.write().unwrap(); scene_manager .load_scene_if_not_loaded(&mut context) @@ -193,7 +196,7 @@ impl ApplicationHandler for App { } fn about_to_wait(&mut self, _event_loop: &ActiveEventLoop) { - let vulkano_windows = self.vulkano_windows.lock().unwrap(); + let vulkano_windows = self.vulkano_windows.read().unwrap(); let window = vulkano_windows.get_primary_window().unwrap(); window.request_redraw(); } @@ -201,14 +204,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.lock().unwrap(); + let vulkano_windows = self.vulkano_windows.read().unwrap(); let window = vulkano_windows.get_window(window_id).unwrap(); if let Err(e) = window.set_cursor_grab(grab) { log::error!("Failed to set cursor grab: {}", e); } } UserEvent::CursorVisible(window_id, visible) => { - let vulkano_windows = self.vulkano_windows.lock().unwrap(); + let vulkano_windows = self.vulkano_windows.read().unwrap(); let window = vulkano_windows.get_window(window_id).unwrap(); window.set_cursor_visible(visible); } @@ -218,7 +221,7 @@ impl ApplicationHandler for App { } } UserEvent::ChangeResolution(window_id, width, height) => { - let mut vulkano_windows = self.vulkano_windows.lock().unwrap(); + let mut vulkano_windows = self.vulkano_windows.write().unwrap(); 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();