From 82c348b22bbb0bf5f3a264a210ec3ce2ee80aa9d Mon Sep 17 00:00:00 2001 From: Ryo Yamashita Date: Sun, 30 Jun 2024 02:09:54 +0900 Subject: [PATCH] =?UTF-8?q?`G=5FORT=5FAPI=5FFOR=5FENV=5FBUILD`=E3=82=92`Lo?= =?UTF-8?q?calKey`=E3=81=AB=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lib.rs | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 1a801efa..569af7ee 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -183,7 +183,9 @@ pub(crate) fn lib_handle() -> &'static libloading::Library { static G_ENV_FOR_VOICEVOX: once_cell::sync::OnceCell = once_cell::sync::OnceCell::new(); #[cfg(feature = "__init-for-voicevox")] -static G_ORT_API_FOR_ENV_BUILD: std::sync::Mutex>>> = std::sync::Mutex::new(None); +thread_local! { + static G_ORT_API_FOR_ENV_BUILD: std::cell::Cell>>> = const { std::cell::Cell::new(None) }; +} #[cfg(feature = "__init-for-voicevox")] #[cfg_attr(docsrs, doc(cfg(feature = "__init-for-voicevox")))] @@ -304,7 +306,7 @@ fn create_env( api: AssertSendSync>, tp_options: Option ) -> anyhow::Result> { - *G_ORT_API_FOR_ENV_BUILD.lock().unwrap_or_else(|e| panic!("{e}")) = Some(api); + G_ORT_API_FOR_ENV_BUILD.set(Some(api)); let _unset_api = UnsetOrtApi; let mut env = EnvironmentBuilder::default().with_name(env!("CARGO_PKG_NAME")); @@ -319,9 +321,7 @@ fn create_env( impl Drop for UnsetOrtApi { fn drop(&mut self) { - if let Ok(mut api) = G_ORT_API_FOR_ENV_BUILD.lock() { - *api = None; - } + G_ORT_API_FOR_ENV_BUILD.set(None); } } } @@ -340,13 +340,7 @@ pub fn api() -> NonNull { return G_ENV_FOR_VOICEVOX .get() .map(|&EnvHandle { api: AssertSendSync(api), .. }| api) - .or_else(|| { - G_ORT_API_FOR_ENV_BUILD - .lock() - .unwrap_or_else(|e| panic!("{e}")) - .as_ref() - .map(|&AssertSendSync(api)| api) - }) + .or_else(|| G_ORT_API_FOR_ENV_BUILD.get().map(|AssertSendSync(api)| api)) .expect("`try_init_from`または`try_init`で初期化されていなくてはなりません"); } unsafe {