Added scopeguard to protect against unwind

This commit is contained in:
ccgauche 2021-02-23 13:12:44 +01:00 committed by Marcel Märtens
parent 74ec5c652a
commit 4b1c033f6e
3 changed files with 8 additions and 4 deletions

1
Cargo.lock generated
View File

@ -5637,6 +5637,7 @@ dependencies = [
"indexmap",
"rand 0.8.3",
"rayon",
"scopeguard",
"serde",
"slab",
"specs",

View File

@ -35,6 +35,7 @@ serde = { version = "1.0.110", features = ["derive"] }
tracy-client = { version = "0.10.0", optional = true }
# Plugins
scopeguard = "1.1.0"
toml = { version = "0.5.7", optional = true }
tar = { version = "0.4.30", optional = true }
wasmer = { version = "1.0.0", optional = true, default-features = false, features = ["wat", "default-cranelift", "default-jit"] }

View File

@ -23,12 +23,14 @@ impl EcsAccessManager {
// This function take a World reference and a function to execute ensuring the
// pointer will never be corrupted during the execution of the function!
pub fn execute_with<T>(&self, world: &World, func: impl FnOnce() -> T) -> T {
let _guard = scopeguard::guard((), |_| {
// ensure the pointer is cleared in any case
self.ecs_pointer
.store(std::ptr::null_mut(), Ordering::Relaxed);
});
self.ecs_pointer
.store(world as *const _ as *mut _, Ordering::Relaxed);
let out = func();
self.ecs_pointer
.store(std::ptr::null_mut(), Ordering::Relaxed);
out
func()
}
/// This unsafe function returns a reference to the Ecs World