From 98cc88df5c00ae2bdfd7df0148ee40758a677747 Mon Sep 17 00:00:00 2001 From: iceiix <43691553+iceiix@users.noreply.github.com> Date: Sun, 6 Jan 2019 19:56:34 -0800 Subject: [PATCH] Workaround resource manager RwLock deadlock (#78). Closes https://github.com/iceiix/steven/issues/75 game.resource_manager, an Arc>, sometimes hangs on write(), apparently a deadlock. Switch to using try_write() and continue the main game loop iteration if it fails, using the previous resource version and allow main loop tick if can't obtain a write lock. No warning is logged since this error is recoverable and seemingly intermittent. May not be the best fix, but unblocking iterative development. --- src/main.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 22ba351..c87e912 100644 --- a/src/main.rs +++ b/src/main.rs @@ -240,6 +240,7 @@ fn main() { }; game.renderer.camera.pos = cgmath::Point3::new(0.5, 13.2, 0.5); + let mut last_resource_version = 0; while !game.should_close { let now = Instant::now(); @@ -250,10 +251,18 @@ fn main() { let (physical_width, physical_height) = window.get_inner_size().unwrap().to_physical(game.dpi_factor).into(); let version = { - let mut res = game.resource_manager.write().unwrap(); - res.tick(&mut resui, &mut ui_container, delta); - res.version() + let try_res = game.resource_manager.try_write(); + if try_res.is_ok() { + let mut res = try_res.unwrap(); + res.tick(&mut resui, &mut ui_container, delta); + res.version() + } else { + // TODO: why does game.resource_manager.write() sometimes deadlock? + //warn!("Failed to obtain mutable reference to resource manager!"); + last_resource_version + } }; + last_resource_version = version; let vsync_changed = *game.vars.get(settings::R_VSYNC); if vsync != vsync_changed {