From 2fdadcb59b8306313e8dafcb891e0476a23c9121 Mon Sep 17 00:00:00 2001 From: Ryo Yamashita Date: Mon, 30 Oct 2023 00:25:57 +0900 Subject: [PATCH] =?UTF-8?q?`Synthesizer`=E3=81=8B=E3=82=89`Mutex`=E3=82=92?= =?UTF-8?q?=E5=A4=96=E3=81=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.lock | 2 - .../voicevox_core_java_api/src/synthesizer.rs | 102 ++++++------------ crates/voicevox_core_python_api/Cargo.toml | 2 - crates/voicevox_core_python_api/src/lib.rs | 65 ++++------- 4 files changed, 53 insertions(+), 118 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ab8dc7986..50868f63b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4361,13 +4361,11 @@ version = "0.0.0" dependencies = [ "easy-ext", "log", - "once_cell", "pyo3", "pyo3-asyncio", "pyo3-log", "serde", "serde_json", - "tokio", "uuid", "voicevox_core", ] diff --git a/crates/voicevox_core_java_api/src/synthesizer.rs b/crates/voicevox_core_java_api/src/synthesizer.rs index 3ddd1b47f..3765e93e0 100644 --- a/crates/voicevox_core_java_api/src/synthesizer.rs +++ b/crates/voicevox_core_java_api/src/synthesizer.rs @@ -8,7 +8,7 @@ use jni::{ sys::{jboolean, jint, jobject}, JNIEnv, }; -use std::sync::{Arc, Mutex}; +use std::sync::Arc; #[no_mangle] unsafe extern "system" fn Java_jp_hiroshiba_voicevoxcore_Synthesizer_rsNewWithInitialize<'local>( @@ -52,7 +52,7 @@ unsafe extern "system" fn Java_jp_hiroshiba_voicevoxcore_Synthesizer_rsNewWithIn open_jtalk, Box::leak(Box::new(options)), ))?; - env.set_rust_field(&this, "handle", Arc::new(Mutex::new(internal)))?; + env.set_rust_field(&this, "handle", Arc::new(internal))?; Ok(()) }) } @@ -68,12 +68,9 @@ unsafe extern "system" fn Java_jp_hiroshiba_voicevoxcore_Synthesizer_rsLoadVoice .get_rust_field::<_, _, Arc>(&model, "handle")? .clone(); let internal = env - .get_rust_field::<_, _, Arc>>(&this, "handle")? + .get_rust_field::<_, _, Arc>(&this, "handle")? .clone(); - { - let internal = internal.lock().unwrap(); - RUNTIME.block_on(internal.load_voice_model(&model))?; - } + RUNTIME.block_on(internal.load_voice_model(&model))?; Ok(()) }) } @@ -88,14 +85,10 @@ unsafe extern "system" fn Java_jp_hiroshiba_voicevoxcore_Synthesizer_rsUnloadVoi let model_id: String = env.get_string(&model_id)?.into(); let internal = env - .get_rust_field::<_, _, Arc>>(&this, "handle")? + .get_rust_field::<_, _, Arc>(&this, "handle")? .clone(); - { - let internal = internal.lock().unwrap(); - - internal.unload_voice_model(&voicevox_core::VoiceModelId::new(model_id))?; - } + internal.unload_voice_model(&voicevox_core::VoiceModelId::new(model_id))?; Ok(()) }) @@ -113,13 +106,10 @@ unsafe extern "system" fn Java_jp_hiroshiba_voicevoxcore_Synthesizer_rsIsLoadedV let model_id: String = env.get_string(&model_id)?.into(); let internal = env - .get_rust_field::<_, _, Arc>>(&this, "handle")? + .get_rust_field::<_, _, Arc>(&this, "handle")? .clone(); - let is_loaded = { - let internal = internal.lock().unwrap(); - internal.is_loaded_voice_model(&voicevox_core::VoiceModelId::new(model_id)) - }; + let is_loaded = internal.is_loaded_voice_model(&voicevox_core::VoiceModelId::new(model_id)); Ok(is_loaded) }) @@ -140,15 +130,12 @@ unsafe extern "system" fn Java_jp_hiroshiba_voicevoxcore_Synthesizer_rsAudioQuer let style_id = style_id as u32; let internal = env - .get_rust_field::<_, _, Arc>>(&this, "handle")? + .get_rust_field::<_, _, Arc>(&this, "handle")? .clone(); - let audio_query = { - let internal = internal.lock().unwrap(); - RUNTIME.block_on( - internal.audio_query_from_kana(&kana, voicevox_core::StyleId::new(style_id)), - )? - }; + let audio_query = RUNTIME.block_on( + internal.audio_query_from_kana(&kana, voicevox_core::StyleId::new(style_id)), + )?; let query_json = serde_json::to_string(&audio_query).expect("should not fail"); @@ -170,13 +157,11 @@ unsafe extern "system" fn Java_jp_hiroshiba_voicevoxcore_Synthesizer_rsAudioQuer let style_id = style_id as u32; let internal = env - .get_rust_field::<_, _, Arc>>(&this, "handle")? + .get_rust_field::<_, _, Arc>(&this, "handle")? .clone(); - let audio_query = { - let internal = internal.lock().unwrap(); - RUNTIME.block_on(internal.audio_query(&text, voicevox_core::StyleId::new(style_id)))? - }; + let audio_query = + RUNTIME.block_on(internal.audio_query(&text, voicevox_core::StyleId::new(style_id)))?; let query_json = serde_json::to_string(&audio_query).expect("should not fail"); @@ -200,16 +185,12 @@ unsafe extern "system" fn Java_jp_hiroshiba_voicevoxcore_Synthesizer_rsAccentPhr let style_id = style_id as u32; let internal = env - .get_rust_field::<_, _, Arc>>(&this, "handle")? + .get_rust_field::<_, _, Arc>(&this, "handle")? .clone(); - let accent_phrases = { - let internal = internal.lock().unwrap(); - RUNTIME.block_on( - internal - .create_accent_phrases_from_kana(&kana, voicevox_core::StyleId::new(style_id)), - )? - }; + let accent_phrases = RUNTIME.block_on( + internal.create_accent_phrases_from_kana(&kana, voicevox_core::StyleId::new(style_id)), + )?; let query_json = serde_json::to_string(&accent_phrases).expect("should not fail"); @@ -231,15 +212,12 @@ unsafe extern "system" fn Java_jp_hiroshiba_voicevoxcore_Synthesizer_rsAccentPhr let style_id = style_id as u32; let internal = env - .get_rust_field::<_, _, Arc>>(&this, "handle")? + .get_rust_field::<_, _, Arc>(&this, "handle")? .clone(); - let accent_phrases = { - let internal = internal.lock().unwrap(); - RUNTIME.block_on( - internal.create_accent_phrases(&text, voicevox_core::StyleId::new(style_id)), - )? - }; + let accent_phrases = RUNTIME.block_on( + internal.create_accent_phrases(&text, voicevox_core::StyleId::new(style_id)), + )?; let query_json = serde_json::to_string(&accent_phrases).expect("should not fail"); @@ -263,15 +241,12 @@ unsafe extern "system" fn Java_jp_hiroshiba_voicevoxcore_Synthesizer_rsReplaceMo let style_id = style_id as u32; let internal = env - .get_rust_field::<_, _, Arc>>(&this, "handle")? + .get_rust_field::<_, _, Arc>(&this, "handle")? .clone(); - let replaced_accent_phrases = { - let internal = internal.lock().unwrap(); - RUNTIME.block_on( - internal.replace_mora_data(&accent_phrases, voicevox_core::StyleId::new(style_id)), - )? - }; + let replaced_accent_phrases = RUNTIME.block_on( + internal.replace_mora_data(&accent_phrases, voicevox_core::StyleId::new(style_id)), + )?; let replaced_accent_phrases_json = serde_json::to_string(&replaced_accent_phrases).expect("should not fail"); @@ -296,11 +271,10 @@ unsafe extern "system" fn Java_jp_hiroshiba_voicevoxcore_Synthesizer_rsReplacePh let style_id = style_id as u32; let internal = env - .get_rust_field::<_, _, Arc>>(&this, "handle")? + .get_rust_field::<_, _, Arc>(&this, "handle")? .clone(); let replaced_accent_phrases = { - let internal = internal.lock().unwrap(); RUNTIME.block_on( internal .replace_phoneme_length(&accent_phrases, voicevox_core::StyleId::new(style_id)), @@ -328,15 +302,12 @@ unsafe extern "system" fn Java_jp_hiroshiba_voicevoxcore_Synthesizer_rsReplaceMo let style_id = style_id as u32; let internal = env - .get_rust_field::<_, _, Arc>>(&this, "handle")? + .get_rust_field::<_, _, Arc>(&this, "handle")? .clone(); - let replaced_accent_phrases = { - let internal = internal.lock().unwrap(); - RUNTIME.block_on( - internal.replace_mora_pitch(&accent_phrases, voicevox_core::StyleId::new(style_id)), - )? - }; + let replaced_accent_phrases = RUNTIME.block_on( + internal.replace_mora_pitch(&accent_phrases, voicevox_core::StyleId::new(style_id)), + )?; let replaced_accent_phrases_json = serde_json::to_string(&replaced_accent_phrases).expect("should not fail"); @@ -360,11 +331,10 @@ unsafe extern "system" fn Java_jp_hiroshiba_voicevoxcore_Synthesizer_rsSynthesis let style_id = style_id as u32; let internal = env - .get_rust_field::<_, _, Arc>>(&this, "handle")? + .get_rust_field::<_, _, Arc>(&this, "handle")? .clone(); let wave = { - let internal = internal.lock().unwrap(); let options = voicevox_core::SynthesisOptions { enable_interrogative_upspeak: enable_interrogative_upspeak != 0, // ..Default::default() @@ -395,11 +365,10 @@ unsafe extern "system" fn Java_jp_hiroshiba_voicevoxcore_Synthesizer_rsTtsFromKa let style_id = style_id as u32; let internal = env - .get_rust_field::<_, _, Arc>>(&this, "handle")? + .get_rust_field::<_, _, Arc>(&this, "handle")? .clone(); let wave = { - let internal = internal.lock().unwrap(); let options = voicevox_core::TtsOptions { enable_interrogative_upspeak: enable_interrogative_upspeak != 0, // ..Default::default() @@ -430,11 +399,10 @@ unsafe extern "system" fn Java_jp_hiroshiba_voicevoxcore_Synthesizer_rsTts<'loca let style_id = style_id as u32; let internal = env - .get_rust_field::<_, _, Arc>>(&this, "handle")? + .get_rust_field::<_, _, Arc>(&this, "handle")? .clone(); let wave = { - let internal = internal.lock().unwrap(); let options = voicevox_core::TtsOptions { enable_interrogative_upspeak: enable_interrogative_upspeak != 0, // ..Default::default() diff --git a/crates/voicevox_core_python_api/Cargo.toml b/crates/voicevox_core_python_api/Cargo.toml index fe19993ec..df9cc0eb1 100644 --- a/crates/voicevox_core_python_api/Cargo.toml +++ b/crates/voicevox_core_python_api/Cargo.toml @@ -13,12 +13,10 @@ directml = ["voicevox_core/directml"] [dependencies] easy-ext.workspace = true log = "0.4.17" -once_cell.workspace = true pyo3 = { version = "0.19.2", features = ["abi3-py38", "extension-module"] } pyo3-asyncio = { version = "0.19.0", features = ["tokio-runtime"] } pyo3-log = "0.9.0" serde.workspace = true serde_json.workspace = true -tokio.workspace = true uuid.workspace = true voicevox_core.workspace = true diff --git a/crates/voicevox_core_python_api/src/lib.rs b/crates/voicevox_core_python_api/src/lib.rs index 1a492878e..6bddfe52c 100644 --- a/crates/voicevox_core_python_api/src/lib.rs +++ b/crates/voicevox_core_python_api/src/lib.rs @@ -3,7 +3,6 @@ use std::{marker::PhantomData, sync::Arc}; mod convert; use convert::*; use log::debug; -use once_cell::sync::Lazy; use pyo3::{ create_exception, exceptions::{PyException, PyKeyError, PyValueError}, @@ -11,15 +10,12 @@ use pyo3::{ types::{IntoPyDict as _, PyBytes, PyDict, PyList, PyModule}, wrap_pyfunction, PyAny, PyObject, PyRef, PyResult, PyTypeInfo, Python, ToPyObject, }; -use tokio::{runtime::Runtime, sync::Mutex}; use uuid::Uuid; use voicevox_core::{ AccelerationMode, AudioQueryModel, InitializeOptions, StyleId, SynthesisOptions, TtsOptions, UserDictWord, VoiceModelId, }; -static RUNTIME: Lazy = Lazy::new(|| Runtime::new().unwrap()); - #[pymodule] #[pyo3(name = "_rust")] fn rust(_: Python<'_>, module: &PyModule) -> PyResult<()> { @@ -145,7 +141,7 @@ impl OpenJtalk { #[pyclass] struct Synthesizer { - synthesizer: Closable>, Self>, + synthesizer: Closable, Self>, } #[pymethods] @@ -172,9 +168,8 @@ impl Synthesizer { ) .await; let synthesizer = Python::with_gil(|py| synthesizer.into_py_result(py))?.into(); - Ok(Self { - synthesizer: Closable::new(Arc::new(synthesizer)), - }) + let synthesizer = Closable::new(synthesizer); + Ok(Self { synthesizer }) }) } @@ -199,13 +194,13 @@ impl Synthesizer { #[getter] fn is_gpu_mode(&self) -> PyResult { let synthesizer = self.synthesizer.get()?; - Ok(RUNTIME.block_on(synthesizer.lock()).is_gpu_mode()) + Ok(synthesizer.is_gpu_mode()) } #[getter] fn metas<'py>(&self, py: Python<'py>) -> PyResult> { let synthesizer = self.synthesizer.get()?; - to_pydantic_voice_model_meta(&RUNTIME.block_on(synthesizer.lock()).metas(), py) + to_pydantic_voice_model_meta(&synthesizer.metas(), py) } fn load_voice_model<'py>( @@ -216,26 +211,22 @@ impl Synthesizer { let model: VoiceModel = model.extract()?; let synthesizer = self.synthesizer.get()?.clone(); pyo3_asyncio::tokio::future_into_py(py, async move { - let synthesizer = synthesizer - .lock() - .await - .load_voice_model(&model.model) - .await; - - Python::with_gil(|py| synthesizer.into_py_result(py)) + let result = synthesizer.load_voice_model(&model.model).await; + Python::with_gil(|py| result.into_py_result(py)) }) } fn unload_voice_model(&mut self, voice_model_id: &str, py: Python<'_>) -> PyResult<()> { - RUNTIME - .block_on(self.synthesizer.get()?.lock()) + self.synthesizer + .get()? .unload_voice_model(&VoiceModelId::new(voice_model_id.to_string())) .into_py_result(py) } fn is_loaded_voice_model(&self, voice_model_id: &str) -> PyResult { - Ok(RUNTIME - .block_on(self.synthesizer.get()?.lock()) + Ok(self + .synthesizer + .get()? .is_loaded_voice_model(&VoiceModelId::new(voice_model_id.to_string()))) } @@ -252,8 +243,6 @@ impl Synthesizer { pyo3_asyncio::tokio::get_current_locals(py)?, async move { let audio_query = synthesizer - .lock() - .await .audio_query_from_kana(&kana, StyleId::new(style_id)) .await; @@ -273,11 +262,7 @@ impl Synthesizer { py, pyo3_asyncio::tokio::get_current_locals(py)?, async move { - let audio_query = synthesizer - .lock() - .await - .audio_query(&text, StyleId::new(style_id)) - .await; + let audio_query = synthesizer.audio_query(&text, StyleId::new(style_id)).await; Python::with_gil(|py| { let audio_query = audio_query.into_py_result(py)?; @@ -302,8 +287,6 @@ impl Synthesizer { pyo3_asyncio::tokio::get_current_locals(py)?, async move { let accent_phrases = synthesizer - .lock() - .await .create_accent_phrases_from_kana(&kana, StyleId::new(style_id)) .await; Python::with_gil(|py| { @@ -333,8 +316,6 @@ impl Synthesizer { pyo3_asyncio::tokio::get_current_locals(py)?, async move { let accent_phrases = synthesizer - .lock() - .await .create_accent_phrases(&text, StyleId::new(style_id)) .await; Python::with_gil(|py| { @@ -362,7 +343,7 @@ impl Synthesizer { accent_phrases, StyleId::new(style_id), py, - |a, s| async move { synthesizer.lock().await.replace_mora_data(&a, s).await }, + |a, s| async move { synthesizer.replace_mora_data(&a, s).await }, ) } @@ -377,7 +358,7 @@ impl Synthesizer { accent_phrases, StyleId::new(style_id), py, - |a, s| async move { synthesizer.lock().await.replace_phoneme_length(&a, s).await }, + |a, s| async move { synthesizer.replace_phoneme_length(&a, s).await }, ) } @@ -392,7 +373,7 @@ impl Synthesizer { accent_phrases, StyleId::new(style_id), py, - |a, s| async move { synthesizer.lock().await.replace_mora_pitch(&a, s).await }, + |a, s| async move { synthesizer.replace_mora_pitch(&a, s).await }, ) } @@ -410,8 +391,6 @@ impl Synthesizer { pyo3_asyncio::tokio::get_current_locals(py)?, async move { let wav = synthesizer - .lock() - .await .synthesis( &audio_query, StyleId::new(style_id), @@ -450,11 +429,7 @@ impl Synthesizer { py, pyo3_asyncio::tokio::get_current_locals(py)?, async move { - let wav = synthesizer - .lock() - .await - .tts_from_kana(&kana, style_id, &options) - .await; + let wav = synthesizer.tts_from_kana(&kana, style_id, &options).await; Python::with_gil(|py| { let wav = wav.into_py_result(py)?; @@ -486,11 +461,7 @@ impl Synthesizer { py, pyo3_asyncio::tokio::get_current_locals(py)?, async move { - let wav = synthesizer - .lock() - .await - .tts(&text, style_id, &options) - .await; + let wav = synthesizer.tts(&text, style_id, &options).await; Python::with_gil(|py| { let wav = wav.into_py_result(py)?;