wasmer_wasix/
rewind.rs

1use std::pin::Pin;
2
3use bytes::Bytes;
4use futures::Future;
5use wasmer_wasix_types::{
6    wasi::Errno,
7    wasix::{ThreadStartType, WasiMemoryLayout},
8};
9
10use crate::os::task::thread::RewindResultType;
11
12/// Future that will be polled by asyncify methods
13#[doc(hidden)]
14pub type AsyncifyFuture = dyn Future<Output = Bytes> + Send + Sync + 'static;
15
16/// Trait that will be invoked after the rewind has finished
17/// It is possible that the process will be terminated rather
18/// than restored at this point
19pub trait RewindPostProcess {
20    /// Returns the serialized object that is returned on the rewind
21    fn finish(&mut self, res: Result<(), Errno>) -> Bytes;
22}
23
24/// The rewind state after a deep sleep
25pub struct RewindState {
26    /// Memory stack used to restore the stack trace back to where it was
27    pub memory_stack: Bytes,
28    /// Call stack used to restore the stack trace back to where it was
29    pub rewind_stack: Bytes,
30    /// All the global data stored in the store
31    pub store_data: Bytes,
32    /// Describes the type of thread start
33    pub start: ThreadStartType,
34    /// Layout of the memory,
35    pub layout: WasiMemoryLayout,
36    /// Flag that indicates if this rewind is 64-bit or 32-bit memory based
37    pub is_64bit: bool,
38}
39
40pub type RewindStateOption = Option<(RewindState, RewindResultType)>;
41
42/// Represents the work that will be done when a thread goes to deep sleep and
43/// includes the things needed to restore it again
44pub struct DeepSleepWork {
45    /// This is the work that will be performed before the thread is rewoken
46    pub trigger: Pin<Box<AsyncifyFuture>>,
47    /// State that the thread will be rewound to
48    pub rewind: RewindState,
49}
50impl std::fmt::Debug for DeepSleepWork {
51    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
52        write!(
53            f,
54            "deep-sleep-work(memory_stack_len={}, rewind_stack_len={}, store_size={})",
55            self.rewind.memory_stack.len(),
56            self.rewind.rewind_stack.len(),
57            self.rewind.store_data.len()
58        )
59    }
60}