From b6483e19d43766aa689d2bb8c11867b8b3093914 Mon Sep 17 00:00:00 2001 From: Pat Hickey Date: Thu, 27 May 2021 16:12:09 -0700 Subject: [PATCH] global module registry: switch from Mutex to RwLock @acfoltzer identified this on a code walk through wasmtime with me, and it was already noted in a comment that we could change if motivated. --- crates/wasmtime/src/module/registry.rs | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/crates/wasmtime/src/module/registry.rs b/crates/wasmtime/src/module/registry.rs index 8d6e6886ac..18bc97a1de 100644 --- a/crates/wasmtime/src/module/registry.rs +++ b/crates/wasmtime/src/module/registry.rs @@ -3,7 +3,7 @@ use crate::{signatures::SignatureCollection, Module}; use std::{ collections::BTreeMap, - sync::{Arc, Mutex}, + sync::{Arc, RwLock}, }; use wasmtime_environ::{ entity::EntityRef, @@ -15,7 +15,7 @@ use wasmtime_jit::CompiledModule; use wasmtime_runtime::{ModuleInfo, VMCallerCheckedAnyfunc, VMTrampoline}; lazy_static::lazy_static! { - static ref GLOBAL_MODULES: Mutex = Default::default(); + static ref GLOBAL_MODULES: RwLock = Default::default(); } fn func_by_pc(module: &CompiledModule, pc: usize) -> Option<(DefinedFuncIndex, u32)> { @@ -88,7 +88,7 @@ impl ModuleRegistry { ); assert!(prev.is_none()); - GLOBAL_MODULES.lock().unwrap().register(start, end, module); + GLOBAL_MODULES.write().unwrap().register(start, end, module); } /// Looks up a trampoline from an anyfunc. @@ -100,7 +100,7 @@ impl ModuleRegistry { impl Drop for ModuleRegistry { fn drop(&mut self) { - let mut info = GLOBAL_MODULES.lock().unwrap(); + let mut info = GLOBAL_MODULES.write().unwrap(); for end in self.0.keys() { info.unregister(*end); } @@ -235,7 +235,7 @@ impl GlobalModuleRegistry { /// Returns whether the `pc`, according to globally registered information, /// is a wasm trap or not. pub(crate) fn is_wasm_pc(pc: usize) -> bool { - let modules = GLOBAL_MODULES.lock().unwrap(); + let modules = GLOBAL_MODULES.read().unwrap(); match modules.module(pc) { Some(entry) => match func_by_pc(&entry.module, pc) { @@ -259,11 +259,8 @@ impl GlobalModuleRegistry { // Work with the global instance of `GlobalModuleRegistry`. Note that only // shared access is allowed, this isn't intended to mutate the contents. - // - // (right now we use a `Mutex` but if motivated we could one day use a - // `RwLock`) pub(crate) fn with(f: impl FnOnce(&GlobalModuleRegistry) -> R) -> R { - f(&GLOBAL_MODULES.lock().unwrap()) + f(&GLOBAL_MODULES.read().unwrap()) } /// Fetches frame information about a program counter in a backtrace.