Skip to content

Commit

Permalink
UnstoppableSingleTasked
Browse files Browse the repository at this point in the history
  • Loading branch information
qryxip committed Sep 12, 2024
1 parent ee7b593 commit 18991c1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
23 changes: 14 additions & 9 deletions crates/voicevox_core/src/asyncs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
//! [blocking]クレートで駆動する非同期処理はランタイムが無くても動作する。そのため非同期版APIを
//! もとにブロッキング版APIを構成することはできる。しかし将来WASMビルドすることを考えると、スレッド
//! がまともに扱えないため機能しなくなってしまう。そのためWASM化を見越したブロッキング版APIのため
//! に[`Unstoppable`]を用意している。
//! に[`SingleTasked`]を用意している。
//!
//! [ブロッキング版API]: crate::blocking
//! [非同期版API]: crate::tokio
Expand All @@ -27,20 +27,25 @@ pub(crate) trait Async: 'static {
async fn open_file(path: impl AsRef<Path>) -> io::Result<impl AsyncRead + AsyncSeek + Unpin>;
}

/// "async"としての責務を放棄し、すべてをブロックする
/// エグゼキュータが非同期タスクの並行実行をしないことを仮定する、[`Async`]の実装
///
/// [ブロッキング版API]用。
///
/// # Performance
///
/// `async`の中でブロッキング操作を直接行う。そのためTokioやasync-stdのような通常の非同期ランタイム
/// 上で動くべきではない。
///
/// [ブロッキング版API]: crate::blocking
pub(crate) enum Unstoppable {}
pub(crate) enum SingleTasked {}

impl Async for Unstoppable {
impl Async for SingleTasked {
async fn open_file(path: impl AsRef<Path>) -> io::Result<impl AsyncRead + AsyncSeek + Unpin> {
return std::fs::File::open(path).map(UnstoppableFile);
return std::fs::File::open(path).map(BlockingFile);

struct UnstoppableFile(std::fs::File);
struct BlockingFile(std::fs::File);

impl AsyncRead for UnstoppableFile {
impl AsyncRead for BlockingFile {
fn poll_read(
mut self: Pin<&mut Self>,
_: &mut task::Context<'_>,
Expand All @@ -50,7 +55,7 @@ impl Async for Unstoppable {
}
}

impl AsyncSeek for UnstoppableFile {
impl AsyncSeek for BlockingFile {
fn poll_seek(
mut self: Pin<&mut Self>,
_: &mut task::Context<'_>,
Expand All @@ -62,7 +67,7 @@ impl Async for Unstoppable {
}
}

/// [blocking]クレートで駆動する。
/// [blocking]クレートで駆動する[`Async`]の実装
///
/// [非同期版API]用。
///
Expand Down
4 changes: 2 additions & 2 deletions crates/voicevox_core/src/voice_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ pub(crate) mod blocking {
use uuid::Uuid;

use crate::{
asyncs::Unstoppable, error::LoadModelResult, future::FutureExt as _,
asyncs::SingleTasked, error::LoadModelResult, future::FutureExt as _,
infer::domains::InferenceDomainMap, VoiceModelMeta,
};

Expand All @@ -397,7 +397,7 @@ pub(crate) mod blocking {
/// 音声モデル。
///
/// VVMファイルと対応する。
pub struct VoiceModel(Inner<Unstoppable>);
pub struct VoiceModel(Inner<SingleTasked>);

impl self::VoiceModel {
pub(crate) fn read_inference_models(
Expand Down

0 comments on commit 18991c1

Please sign in to comment.