Skip to content

Commit

Permalink
G_ORT_API_FOR_ENV_BUILDLocalKeyにする
Browse files Browse the repository at this point in the history
  • Loading branch information
qryxip committed Jun 29, 2024
1 parent bbf69cd commit 82c348b
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,9 @@ pub(crate) fn lib_handle() -> &'static libloading::Library {
static G_ENV_FOR_VOICEVOX: once_cell::sync::OnceCell<EnvHandle> = once_cell::sync::OnceCell::new();

#[cfg(feature = "__init-for-voicevox")]
static G_ORT_API_FOR_ENV_BUILD: std::sync::Mutex<Option<AssertSendSync<NonNull<ort_sys::OrtApi>>>> = std::sync::Mutex::new(None);
thread_local! {
static G_ORT_API_FOR_ENV_BUILD: std::cell::Cell<Option<AssertSendSync<NonNull<ort_sys::OrtApi>>>> = const { std::cell::Cell::new(None) };
}

#[cfg(feature = "__init-for-voicevox")]
#[cfg_attr(docsrs, doc(cfg(feature = "__init-for-voicevox")))]
Expand Down Expand Up @@ -304,7 +306,7 @@ fn create_env(
api: AssertSendSync<NonNull<ort_sys::OrtApi>>,
tp_options: Option<EnvironmentGlobalThreadPoolOptions>
) -> anyhow::Result<std::sync::Arc<Environment>> {
*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"));
Expand All @@ -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);
}
}
}
Expand All @@ -340,13 +340,7 @@ pub fn api() -> NonNull<ort_sys::OrtApi> {
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 {
Expand Down

0 comments on commit 82c348b

Please sign in to comment.