From 7f70a7e4d1199a9f4a061906828f123e6eb59b54 Mon Sep 17 00:00:00 2001 From: uttarayan21 Date: Sun, 22 Sep 2024 18:33:56 +0530 Subject: [PATCH] feat: Make the SessionHandle send+sync --- Cargo.lock | 134 ++++++++++++++++++++++++++++++++++++++++++++ mnn-sync/Cargo.toml | 4 ++ mnn-sync/src/lib.rs | 16 ++++-- 3 files changed, 150 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 454de34..a43dae8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -109,6 +109,12 @@ version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" +[[package]] +name = "bumpalo" +version = "3.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" + [[package]] name = "bytemuck" version = "1.18.0" @@ -237,12 +243,35 @@ dependencies = [ "rustc_version", ] +[[package]] +name = "flume" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55ac459de2512911e4b674ce33cf20befaba382d05b62b008afc1c8b57cbf181" +dependencies = [ + "nanorand", + "spin", +] + [[package]] name = "fs_extra" version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42703706b716c37f96a77aea830392ad231f44c9e9a67872fa5548707e11b11c" +[[package]] +name = "getrandom" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" +dependencies = [ + "cfg-if", + "js-sys", + "libc", + "wasi", + "wasm-bindgen", +] + [[package]] name = "glob" version = "0.3.1" @@ -279,6 +308,15 @@ dependencies = [ "libc", ] +[[package]] +name = "js-sys" +version = "0.3.70" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1868808506b929d7b0cfa8f75951347aa71bb21144b7791bae35d9bccfcfe37a" +dependencies = [ + "wasm-bindgen", +] + [[package]] name = "libc" version = "0.2.158" @@ -295,6 +333,16 @@ dependencies = [ "windows-targets", ] +[[package]] +name = "lock_api" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07af8b9cdd281b7915f413fa73f29ebd5d55d0d3f0155584dade1ff18cea1b17" +dependencies = [ + "autocfg", + "scopeguard", +] + [[package]] name = "log" version = "0.4.22" @@ -352,6 +400,7 @@ name = "mnn-sync" version = "0.1.0" dependencies = [ "error-stack", + "flume", "mnn", "oneshot", ] @@ -371,6 +420,15 @@ dependencies = [ "tap", ] +[[package]] +name = "nanorand" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a51313c5820b0b02bd422f4b44776fbf47961755c74ce64afc73bfad10226c3" +dependencies = [ + "getrandom", +] + [[package]] name = "ndarray" version = "0.16.1" @@ -543,6 +601,12 @@ dependencies = [ "semver", ] +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + [[package]] name = "semver" version = "1.0.23" @@ -555,6 +619,15 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" +[[package]] +name = "spin" +version = "0.9.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" +dependencies = [ + "lock_api", +] + [[package]] name = "strsim" version = "0.11.1" @@ -647,6 +720,67 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + +[[package]] +name = "wasm-bindgen" +version = "0.2.93" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a82edfc16a6c469f5f44dc7b571814045d60404b55a0ee849f9bcfa2e63dd9b5" +dependencies = [ + "cfg-if", + "once_cell", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.93" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9de396da306523044d3302746f1208fa71d7532227f15e347e2d93e4145dd77b" +dependencies = [ + "bumpalo", + "log", + "once_cell", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.93" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "585c4c91a46b072c92e908d99cb1dcdf95c5218eeb6f3bf1efa991ee7a68cccf" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.93" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "afc340c74d9005395cf9dd098506f7f44e38f2b4a21c6aaacf9a105ea5e1e836" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.93" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c62a0a307cb4a311d3a07867860911ca130c3494e8c2719593806c08bc5d0484" + [[package]] name = "winapi" version = "0.3.9" diff --git a/mnn-sync/Cargo.toml b/mnn-sync/Cargo.toml index e67524f..72d51fc 100644 --- a/mnn-sync/Cargo.toml +++ b/mnn-sync/Cargo.toml @@ -6,5 +6,9 @@ license.workspace = true [dependencies] error-stack = "0.5.0" +flume = { version = "0.11.0", default-features = false, features = [ + "eventual-fairness", + "nanorand", +] } mnn = { version = "0.1.0", path = ".." } oneshot = "0.1.8" diff --git a/mnn-sync/src/lib.rs b/mnn-sync/src/lib.rs index 4ca9230..6ca3033 100644 --- a/mnn-sync/src/lib.rs +++ b/mnn-sync/src/lib.rs @@ -35,6 +35,8 @@ //! When you run a closure it is sent to the thread and executed in that session and the result is //! sent back to the main thread via a [oneshot::Sender] +use flume::{Receiver, Sender}; + use error_stack::{Report, ResultExt}; use mnn::*; @@ -49,8 +51,8 @@ type CallbackSender = CallbackEnum; pub struct SessionHandle { #[allow(dead_code)] pub(crate) handle: std::thread::JoinHandle>, - pub(crate) sender: std::sync::mpsc::Sender, - pub(crate) loop_handle: std::sync::mpsc::Receiver>, + pub(crate) sender: Sender, + pub(crate) loop_handle: Receiver>, } impl Drop for SessionHandle { @@ -69,10 +71,10 @@ pub struct SessionRunner { impl SessionHandle { pub fn new(mut interpreter: Interpreter, config: ScheduleConfig) -> Result { - let (sender, receiver) = std::sync::mpsc::channel::(); + let (sender, receiver) = flume::unbounded::(); let builder = std::thread::Builder::new().name("mnn-session-thread".to_string()); - let (tx, rx) = std::sync::mpsc::channel(); + let (tx, rx) = flume::unbounded(); let handle = builder .spawn(move || -> Result<()> { let session = interpreter.create_session(config)?; @@ -291,3 +293,9 @@ pub fn test_sync_api_race() { }) .expect("Sed"); } + +#[test] +pub fn test_sync_api_is_send_sync() { + fn is_send_sync() {} + is_send_sync::(); +}