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::{
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<Mutex<>>
pub vulkano_windows: Rc<RwLock<VulkanoWindows>>,
pub vulkano_windows: Rc<RefCell<VulkanoWindows>>,
pub input_manager: Arc<RwLock<InputManager>>,
pub timer: Arc<RwLock<Timer>>,
pub gui: Rc<RwLock<Gui>>,
pub gui: Rc<RefCell<Gui>>,
}
impl ApplicationContext {
pub fn new(
vulkan_context: Arc<VulkanContext>,
vulkano_windows: Rc<RwLock<VulkanoWindows>>,
vulkano_windows: Rc<RefCell<VulkanoWindows>>,
input_manager: Arc<RwLock<InputManager>>,
timer: Arc<RwLock<Timer>>,
gui: Rc<RwLock<Gui>>,
gui: Rc<RefCell<Gui>>,
event_loop_proxy: EventLoopProxy<UserEvent>,
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)
}