//! Example of interrupting a WebAssembly function's runtime via epoch1//! changes ("epoch interruption") in a synchronous context. To see2//! an example of setup for asynchronous usage, see3//! `tests/all/epoch_interruption.rs`45use anyhow::Error;6use std::sync::Arc;7use wasmtime::{Config, Engine, Instance, Module, Store};89fn main() -> Result<(), Error> {10// Set up an engine configured with epoch interruption enabled.11let mut config = Config::new();12config.epoch_interruption(true);13let engine = Arc::new(Engine::new(&config)?);1415let mut store = Store::new(&engine, ());16// Configure the store to trap on reaching the epoch deadline.17// This is the default, but we do it explicitly here to18// demonstrate.19store.epoch_deadline_trap();20// Configure the store to have an initial epoch deadline one tick21// in the future.22store.set_epoch_deadline(1);2324// Reuse the fibonacci function from the Fuel example. This is a25// long-running function that we will want to interrupt.26let module = Module::from_file(store.engine(), "examples/fuel.wat")?;27let instance = Instance::new(&mut store, &module, &[])?;2829// Start a thread that will bump the epoch after 1 second.30let engine_clone = engine.clone();31std::thread::spawn(move || {32std::thread::sleep(std::time::Duration::from_secs(1));33engine_clone.increment_epoch();34});3536// Invoke `fibonacci` with a large argument such that a normal37// invocation would take many seconds to complete.38let fibonacci = instance.get_typed_func::<i32, i32>(&mut store, "fibonacci")?;39match fibonacci.call(&mut store, 100) {40Ok(_) => panic!("Somehow we computed recursive fib(100) in less than a second!"),41Err(_) => {42println!("Trapped out of fib(100) after epoch increment");43}44};4546Ok(())47}484950