| 36 | } |
| 37 | |
| 38 | int main() { |
| 39 | // Enable interruptible code via `Config` and then create an interrupt |
| 40 | // handle which we'll use later to interrupt running code. |
| 41 | Config config; |
| 42 | config.epoch_interruption(true); |
| 43 | Engine engine(std::move(config)); |
| 44 | Store store(engine); |
| 45 | store.context().set_epoch_deadline(1); |
| 46 | |
| 47 | // Compile and instantiate a small example with an infinite loop. |
| 48 | auto wat = readFile("examples/interrupt.wat"); |
| 49 | Module module = Module::compile(engine, wat).unwrap(); |
| 50 | Instance instance = Instance::create(store, module, {}).unwrap(); |
| 51 | Func run = std::get<Func>(*instance.get(store, "run")); |
| 52 | |
| 53 | // Spin up a thread to send us an interrupt in a second |
| 54 | std::thread t([engine{std::move(engine)}]() { |
| 55 | std::this_thread::sleep_for(std::chrono::seconds(1)); |
| 56 | std::cout << "Interrupting!\n"; |
| 57 | engine.increment_epoch(); |
| 58 | }); |
| 59 | |
| 60 | std::cout << "Entering infinite loop ...\n"; |
| 61 | auto err = run.call(store, {}).err(); |
| 62 | auto &trap = std::get<Trap>(err.data); |
| 63 | |
| 64 | std::cout << "trap: " << trap.message() << "\n"; |
| 65 | t.join(); |
| 66 | } |
nothing calls this directly
no test coverage detected