From 414befaf3f18461daa3343c3effc11007cc06b61 Mon Sep 17 00:00:00 2001 From: "Carson M." Date: Thu, 21 Mar 2024 19:00:00 -0500 Subject: [PATCH] refactor: do not lock dylib handle --- src/lib.rs | 41 +++++++++++++++++++---------------------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 10189696..6cb38ae7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -85,7 +85,7 @@ pub const MINOR_VERSION: u32 = ort_sys::ORT_API_VERSION; #[cfg(feature = "load-dynamic")] pub(crate) static G_ORT_DYLIB_PATH: OnceLock> = OnceLock::new(); #[cfg(feature = "load-dynamic")] -pub(crate) static G_ORT_LIB: OnceLock>> = OnceLock::new(); +pub(crate) static G_ORT_LIB: OnceLock> = OnceLock::new(); #[cfg(feature = "load-dynamic")] pub(crate) fn dylib_path() -> &'static String { @@ -104,27 +104,24 @@ pub(crate) fn dylib_path() -> &'static String { } #[cfg(feature = "load-dynamic")] -pub(crate) fn lib_handle() -> MutexGuard<'static, libloading::Library> { - G_ORT_LIB - .get_or_init(|| { - // resolve path relative to executable - let path: std::path::PathBuf = dylib_path().into(); - let absolute_path = if path.is_absolute() { - path - } else { - let relative = std::env::current_exe() - .expect("could not get current executable path") - .parent() - .unwrap() - .join(&path); - if relative.exists() { relative } else { path } - }; - let lib = unsafe { libloading::Library::new(&absolute_path) } - .unwrap_or_else(|e| panic!("An error occurred while attempting to load the ONNX Runtime binary at `{}`: {e}", absolute_path.display())); - Arc::new(Mutex::new(lib)) - }) - .lock() - .expect("failed to acquire ONNX Runtime dylib lock; another thread panicked?") +pub(crate) fn lib_handle() -> &'static libloading::Library { + G_ORT_LIB.get_or_init(|| { + // resolve path relative to executable + let path: std::path::PathBuf = dylib_path().into(); + let absolute_path = if path.is_absolute() { + path + } else { + let relative = std::env::current_exe() + .expect("could not get current executable path") + .parent() + .unwrap() + .join(&path); + if relative.exists() { relative } else { path } + }; + let lib = unsafe { libloading::Library::new(&absolute_path) } + .unwrap_or_else(|e| panic!("An error occurred while attempting to load the ONNX Runtime binary at `{}`: {e}", absolute_path.display())); + Arc::new(lib) + }) } pub(crate) static G_ORT_API: OnceLock> = OnceLock::new();