use RefCell instead of RwLock

- Gui and VulkanoWindows is not thread safe
- It's more adapted with association with Rc
This commit is contained in:
Florian RICHER 2025-05-31 12:40:19 +02:00
parent e5d8dd58f2
commit a293b962f7
Signed by: florian.richer
GPG key ID: C73D37CBED7BFC77
2 changed files with 24 additions and 31 deletions

View file

@ -1,4 +1,5 @@
use std::{ use std::{
cell::RefCell,
rc::Rc, rc::Rc,
sync::{Arc, RwLock}, sync::{Arc, RwLock},
}; };
@ -39,19 +40,19 @@ pub struct ApplicationContext {
pub window_id: WindowId, pub window_id: WindowId,
// Données mutables partagées avec Arc<Mutex<>> // Données mutables partagées avec Arc<Mutex<>>
pub vulkano_windows: Rc<RwLock<VulkanoWindows>>, pub vulkano_windows: Rc<RefCell<VulkanoWindows>>,
pub input_manager: Arc<RwLock<InputManager>>, pub input_manager: Arc<RwLock<InputManager>>,
pub timer: Arc<RwLock<Timer>>, pub timer: Arc<RwLock<Timer>>,
pub gui: Rc<RwLock<Gui>>, pub gui: Rc<RefCell<Gui>>,
} }
impl ApplicationContext { impl ApplicationContext {
pub fn new( pub fn new(
vulkan_context: Arc<VulkanContext>, vulkan_context: Arc<VulkanContext>,
vulkano_windows: Rc<RwLock<VulkanoWindows>>, vulkano_windows: Rc<RefCell<VulkanoWindows>>,
input_manager: Arc<RwLock<InputManager>>, input_manager: Arc<RwLock<InputManager>>,
timer: Arc<RwLock<Timer>>, timer: Arc<RwLock<Timer>>,
gui: Rc<RwLock<Gui>>, gui: Rc<RefCell<Gui>>,
event_loop_proxy: EventLoopProxy<UserEvent>, event_loop_proxy: EventLoopProxy<UserEvent>,
window_id: WindowId, window_id: WindowId,
) -> Self { ) -> Self {
@ -129,10 +130,7 @@ impl ApplicationContext {
where where
F: FnOnce(&VulkanoWindowRenderer) -> T, F: FnOnce(&VulkanoWindowRenderer) -> T,
{ {
let vulkano_windows = self let vulkano_windows = self.vulkano_windows.borrow_mut();
.vulkano_windows
.read()
.expect("Failed to lock vulkano_windows");
let renderer = vulkano_windows let renderer = vulkano_windows
.get_renderer(self.window_id) .get_renderer(self.window_id)
.expect("Failed to get renderer"); .expect("Failed to get renderer");
@ -144,10 +142,7 @@ impl ApplicationContext {
where where
F: FnOnce(&mut VulkanoWindowRenderer) -> T, F: FnOnce(&mut VulkanoWindowRenderer) -> T,
{ {
let mut vulkano_windows = self let mut vulkano_windows = self.vulkano_windows.borrow_mut();
.vulkano_windows
.write()
.expect("Failed to lock vulkano_windows");
let renderer = vulkano_windows let renderer = vulkano_windows
.get_renderer_mut(self.window_id) .get_renderer_mut(self.window_id)
.expect("Failed to get renderer"); .expect("Failed to get renderer");
@ -159,7 +154,7 @@ impl ApplicationContext {
where where
F: FnOnce(&Gui) -> T, F: FnOnce(&Gui) -> T,
{ {
let gui = self.gui.read().expect("Failed to lock gui"); let gui = self.gui.borrow();
f(&gui) f(&gui)
} }
@ -168,7 +163,7 @@ impl ApplicationContext {
where where
F: FnOnce(&mut Gui) -> T, 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) f(&mut gui)
} }

View file

@ -1,3 +1,4 @@
use std::cell::RefCell;
use std::collections::HashMap; use std::collections::HashMap;
use std::rc::Rc; use std::rc::Rc;
use std::sync::{Arc, RwLock}; use std::sync::{Arc, RwLock};
@ -28,15 +29,15 @@ pub const DEPTH_IMAGE_ID: usize = 0;
pub struct App { pub struct App {
vulkan_context: Arc<VulkanContext>, vulkan_context: Arc<VulkanContext>,
vulkano_windows: Rc<RwLock<VulkanoWindows>>, vulkano_windows: Rc<RefCell<VulkanoWindows>>,
gui: HashMap<WindowId, Rc<RwLock<Gui>>>, gui: HashMap<WindowId, Rc<RefCell<Gui>>>,
scene_manager: HashMap<WindowId, SceneManager>, scene_manager: HashMap<WindowId, SceneManager>,
input_manager: Arc<RwLock<InputManager>>, input_manager: Arc<RwLock<InputManager>>,
timer: Arc<RwLock<Timer>>, timer: Arc<RwLock<Timer>>,
event_loop_proxy: EventLoopProxy<UserEvent>, event_loop_proxy: EventLoopProxy<UserEvent>,
// Context d'application partagé par fenêtre - architecture unifiée // Context d'application partagé par fenêtre - architecture unifiée
app_contexts: HashMap<WindowId, Rc<RwLock<ApplicationContext>>>, app_contexts: HashMap<WindowId, Rc<RefCell<ApplicationContext>>>,
} }
impl App { impl App {
@ -47,7 +48,7 @@ impl App {
) -> Self { ) -> Self {
Self { Self {
vulkan_context: Arc::new(VulkanContext::new(vulkano_context)), 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(), gui: HashMap::new(),
input_manager: Arc::new(RwLock::new(input_manager)), input_manager: Arc::new(RwLock::new(input_manager)),
scene_manager: HashMap::new(), scene_manager: HashMap::new(),
@ -60,10 +61,7 @@ impl App {
impl ApplicationHandler<UserEvent> for App { impl ApplicationHandler<UserEvent> for App {
fn resumed(&mut self, event_loop: &ActiveEventLoop) { fn resumed(&mut self, event_loop: &ActiveEventLoop) {
let mut vulkano_windows = self let mut vulkano_windows = self.vulkano_windows.borrow_mut();
.vulkano_windows
.write()
.expect("Failed to lock vulkano_windows");
let window_id = vulkano_windows.create_window( let window_id = vulkano_windows.create_window(
event_loop, event_loop,
self.vulkan_context.vulkano_context(), self.vulkan_context.vulkano_context(),
@ -99,7 +97,7 @@ impl ApplicationHandler<UserEvent> 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(); let mut scene_manager = SceneManager::new();
scene_manager.load_scene(Box::new(MainScene::default())); scene_manager.load_scene(Box::new(MainScene::default()));
@ -120,7 +118,7 @@ impl ApplicationHandler<UserEvent> for App {
fn window_event(&mut self, event_loop: &ActiveEventLoop, id: WindowId, event: WindowEvent) { fn window_event(&mut self, event_loop: &ActiveEventLoop, id: WindowId, event: WindowEvent) {
{ {
let gui = self.gui.get_mut(&id).unwrap(); 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) { if !gui.update(&event) {
let mut input_manager = self.input_manager.write().unwrap(); let mut input_manager = self.input_manager.write().unwrap();
input_manager.process_window_event(&event); input_manager.process_window_event(&event);
@ -133,7 +131,7 @@ impl ApplicationHandler<UserEvent> for App {
event_loop.exit(); event_loop.exit();
} }
WindowEvent::Resized(_) | WindowEvent::ScaleFactorChanged { .. } => { 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(); vulkano_windows.get_renderer_mut(id).unwrap().resize();
} }
WindowEvent::RedrawRequested => { WindowEvent::RedrawRequested => {
@ -158,7 +156,7 @@ impl ApplicationHandler<UserEvent> for App {
.app_contexts .app_contexts
.entry(id) .entry(id)
.or_insert_with(|| { .or_insert_with(|| {
Rc::new(RwLock::new(ApplicationContext::new( Rc::new(RefCell::new(ApplicationContext::new(
self.vulkan_context.clone(), self.vulkan_context.clone(),
self.vulkano_windows.clone(), self.vulkano_windows.clone(),
self.input_manager.clone(), self.input_manager.clone(),
@ -175,7 +173,7 @@ impl ApplicationHandler<UserEvent> for App {
// Utiliser le contexte partagé pour les scènes // Utiliser le contexte partagé pour les scènes
{ {
let _scene_span = tracing::info_span!("scene_processing").entered(); 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 = let _scene_span =
@ -219,7 +217,7 @@ impl ApplicationHandler<UserEvent> for App {
} }
fn about_to_wait(&mut self, _event_loop: &ActiveEventLoop) { 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(); let window = vulkano_windows.get_primary_window().unwrap();
window.request_redraw(); window.request_redraw();
} }
@ -227,14 +225,14 @@ impl ApplicationHandler<UserEvent> for App {
fn user_event(&mut self, event_loop: &ActiveEventLoop, event: UserEvent) { fn user_event(&mut self, event_loop: &ActiveEventLoop, event: UserEvent) {
match event { match event {
UserEvent::CursorGrabMode(window_id, grab) => { 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(); let window = vulkano_windows.get_window(window_id).unwrap();
if let Err(e) = window.set_cursor_grab(grab) { if let Err(e) = window.set_cursor_grab(grab) {
tracing::error!("Failed to set cursor grab: {}", e); tracing::error!("Failed to set cursor grab: {}", e);
} }
} }
UserEvent::CursorVisible(window_id, visible) => { 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(); let window = vulkano_windows.get_window(window_id).unwrap();
window.set_cursor_visible(visible); window.set_cursor_visible(visible);
} }
@ -244,7 +242,7 @@ impl ApplicationHandler<UserEvent> for App {
} }
} }
UserEvent::ChangeResolution(window_id, width, height) => { 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 = vulkano_windows.get_window(window_id).unwrap();
let _ = window.request_inner_size(winit::dpi::LogicalSize::new(width, height)); let _ = window.request_inner_size(winit::dpi::LogicalSize::new(width, height));
let renderer = vulkano_windows.get_renderer_mut(window_id).unwrap(); let renderer = vulkano_windows.get_renderer_mut(window_id).unwrap();