yomi_core/vm/interrupt.rs
1//! The token to interrupt a running VM from another thread.
2
3use std::sync::Arc;
4use std::sync::atomic::{AtomicBool, Ordering};
5
6/// The token to interrupt a `run` from another thread (`spec/host/01-execution.md`, 1.9a).
7///
8/// Cloning it gives another handle to the same token, so one token can stop many VMs. An interrupt stays requested until
9/// [`InterruptHandle::reset`] is called.
10#[derive(Debug, Clone, Default)]
11pub struct InterruptHandle {
12 flag: Arc<AtomicBool>,
13}
14
15impl InterruptHandle {
16 /// Makes a token with no interrupt requested.
17 pub fn new() -> Self {
18 Self::default()
19 }
20
21 /// Requests an interrupt. The VMs that watch this token return `Interrupted` at their next check point.
22 pub fn interrupt(&self) {
23 self.flag.store(true, Ordering::Relaxed);
24 }
25
26 /// Whether an interrupt is requested.
27 pub fn is_interrupted(&self) -> bool {
28 self.flag.load(Ordering::Relaxed)
29 }
30
31 /// Cancels the request.
32 pub fn reset(&self) {
33 self.flag.store(false, Ordering::Relaxed);
34 }
35}