Change Mutex by RwLock
Some checks failed
Build legacy Nix package on Ubuntu / build (push) Failing after 10m35s

This commit is contained in:
Florian RICHER 2025-05-30 22:04:54 +02:00
parent bc42892d39
commit b1458785e5
Signed by: florian.richer
GPG key ID: C73D37CBED7BFC77
2 changed files with 44 additions and 38 deletions

View file

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

View file

@ -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<VulkanContext>,
vulkano_windows: Arc<Mutex<VulkanoWindows>>,
gui: HashMap<WindowId, Arc<Mutex<Gui>>>,
vulkano_windows: Arc<RwLock<VulkanoWindows>>,
gui: HashMap<WindowId, Arc<RwLock<Gui>>>,
scene_manager: HashMap<WindowId, SceneManager>,
input_manager: Arc<Mutex<InputManager>>,
timer: Arc<Mutex<Timer>>,
input_manager: Arc<RwLock<InputManager>>,
timer: Arc<RwLock<Timer>>,
event_loop_proxy: EventLoopProxy<UserEvent>,
// Context d'application partagé par fenêtre - architecture unifiée
app_contexts: HashMap<WindowId, Arc<Mutex<ApplicationContext>>>,
app_contexts: HashMap<WindowId, Arc<RwLock<ApplicationContext>>>,
}
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<UserEvent> 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<UserEvent> 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<UserEvent> 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<UserEvent> 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<UserEvent> 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<UserEvent> 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<UserEvent> 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<UserEvent> 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<UserEvent> 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();