From 170d27b04cff70c34d8ff4876a9cdd618ca452bd Mon Sep 17 00:00:00 2001 From: Isaac Clayton Date: Tue, 12 Jul 2022 16:32:41 +0200 Subject: [PATCH] Start working on plugin epoch async yield --- crates/plugin_runtime/build.rs | 2 +- crates/plugin_runtime/src/plugin.rs | 21 ++++++++++++++++----- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/crates/plugin_runtime/build.rs b/crates/plugin_runtime/build.rs index 542be4fb5f41be91ec045a598095eb78d7a65271..3371b0ab391b22f6c03a62546e8eed78d7e8cc8b 100644 --- a/crates/plugin_runtime/build.rs +++ b/crates/plugin_runtime/build.rs @@ -60,7 +60,7 @@ fn main() { fn create_default_engine() -> Engine { let mut config = Config::default(); config.async_support(true); - // config.epoch_interruption(true); + config.epoch_interruption(true); Engine::new(&config).expect("Could not create engine") } diff --git a/crates/plugin_runtime/src/plugin.rs b/crates/plugin_runtime/src/plugin.rs index d2570410f9437015037fe50d82f36b4198aec39d..6e0764d1a73a548ff7bec99b2c1a02efa677d3a2 100644 --- a/crates/plugin_runtime/src/plugin.rs +++ b/crates/plugin_runtime/src/plugin.rs @@ -71,7 +71,7 @@ pub struct PluginBuilder { pub fn create_default_engine() -> Result { let mut config = Config::default(); config.async_support(true); - // config.epoch_interruption(true); + config.epoch_interruption(true); Engine::new(&config) } @@ -303,11 +303,12 @@ impl Plugin { println!(); } - async fn init( + async fn init + Send + 'static>( precompiled: bool, module: Vec, plugin: PluginBuilder, - ) -> Result { + spawn_incrementer: impl Fn(F) -> T, + ) -> Result<(Self, T), Error> { // initialize the WebAssembly System Interface context let engine = plugin.engine; let mut linker = plugin.linker; @@ -322,7 +323,8 @@ impl Plugin { alloc: None, }, ); - // store.epoch_deadline_async_yield_and_update(todo!()); + store.epoch_deadline_async_yield_and_update(1); + let module = if precompiled { unsafe { Module::deserialize(&engine, module)? } } else { @@ -342,7 +344,16 @@ impl Plugin { free_buffer, }); - Ok(Plugin { store, instance }) + let plugin = Plugin { store, instance }; + let incrementer = spawn_incrementer(async move { + loop { + smol::Timer::after(std::time::Duration::from_millis(100)).await; + + engine.increment_epoch(); + } + }); + + Ok((plugin, incrementer)) } /// Attaches a file or directory the the given system path to the runtime.