1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
//! Defines data structure for WasmEdge async mechanism.

use fiber_for_wasmedge::{Fiber, FiberStack, Suspend};
use std::{
    future::Future,
    pin::Pin,
    ptr,
    task::{Context, Poll},
};

/// Defines a FiberFuture.
pub(crate) struct FiberFuture<'a> {
    fiber: Fiber<'a, Result<(), ()>, (), Result<(), ()>>,
    current_suspend: *mut *const Suspend<Result<(), ()>, (), Result<(), ()>>,
    current_poll_cx: *mut *mut Context<'static>,
}
impl<'a> FiberFuture<'a> {
    /// Create a fiber to execute the given function.
    ///
    /// # Arguments
    ///
    /// * `async_state` - Used to store asynchronous state at run time.
    ///
    /// * `func` - The function to execute.
    ///
    /// # Error
    ///
    /// If fail to create the fiber stack or the fiber fail to resume, then an error is returned.
    pub(crate) async fn on_fiber<R>(
        async_state: &AsyncState,
        func: impl FnOnce() -> R + Send,
    ) -> Result<R, ()> {
        let mut slot = None;
        let future = {
            let current_poll_cx = async_state.current_poll_cx.get();
            let current_suspend = async_state.current_suspend.get();

            let stack = FiberStack::new(2 << 20).map_err(|_e| ())?;
            let slot = &mut slot;
            let fiber = Fiber::new(stack, move |keep_going, suspend| {
                keep_going?;

                unsafe {
                    let _reset = Reset(current_suspend, *current_suspend);
                    *current_suspend = suspend;
                    *slot = Some(func());
                    Ok(())
                }
            })
            .map_err(|_e| ())?;

            FiberFuture {
                fiber,
                current_suspend,
                current_poll_cx,
            }
        };
        future.await?;

        Ok(slot.unwrap())
    }

    /// This is a helper function to call `resume` on the underlying
    /// fiber while correctly managing thread-local data.
    fn resume(&mut self, val: Result<(), ()>) -> Result<Result<(), ()>, ()> {
        let async_cx = AsyncCx {
            current_suspend: self.current_suspend,
            current_poll_cx: self.current_poll_cx,
        };
        ASYNC_CX.set(&async_cx, || self.fiber.resume(val))
    }
}
impl<'a> Future for FiberFuture<'a> {
    type Output = Result<(), ()>;
    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        unsafe {
            let _reset = Reset(self.current_poll_cx, *self.current_poll_cx);
            *self.current_poll_cx =
                std::mem::transmute::<&mut Context<'_>, *mut Context<'static>>(cx);

            match self.resume(Ok(())) {
                Ok(ret) => Poll::Ready(ret),
                Err(_) => Poll::Pending,
            }
        }
    }
}
unsafe impl Send for FiberFuture<'_> {}

type FiberSuspend = Suspend<Result<(), ()>, (), Result<(), ()>>;

impl Drop for FiberFuture<'_> {
    fn drop(&mut self) {
        if !self.fiber.done() {
            let result = self.resume(Err(()));
            // This resumption with an error should always complete the
            // fiber. While it's technically possible for host code to catch
            // the trap and re-resume, we'd ideally like to signal that to
            // callers that they shouldn't be doing that.
            debug_assert!(result.is_ok());
        }
    }
}

/// Defines a TimeoutFiberFuture.
pub(crate) struct TimeoutFiberFuture<'a> {
    fiber: Fiber<'a, Result<(), ()>, (), Result<(), ()>>,
    current_suspend: *mut *const Suspend<Result<(), ()>, (), Result<(), ()>>,
    current_poll_cx: *mut *mut Context<'static>,
    deadline: std::time::SystemTime,
}

impl<'a> TimeoutFiberFuture<'a> {
    /// Create a fiber to execute the given function.
    ///
    /// # Arguments
    ///
    /// * `func` - The function to execute.
    ///
    /// * `async_state` - Used to store asynchronous state at run time.
    ///
    /// * `deadline` - The deadline the function to be run.
    ///
    /// # Error
    ///
    /// If fail to create the fiber stack or the fiber fail to resume, then an error is returned.
    pub(crate) async fn on_fiber<R>(
        async_state: &AsyncState,
        func: impl FnOnce() -> R + Send,
        deadline: std::time::SystemTime,
    ) -> Result<R, ()> {
        let mut slot = None;

        let future = {
            let current_poll_cx = async_state.current_poll_cx.get();
            let current_suspend = async_state.current_suspend.get();

            let stack = FiberStack::new(2 << 20).map_err(|_e| ())?;
            let slot = &mut slot;
            let fiber = Fiber::new(stack, move |keep_going, suspend| {
                keep_going?;

                unsafe {
                    let _reset = Reset(current_suspend, *current_suspend);
                    *current_suspend = suspend;
                    *slot = Some(func());
                    Ok(())
                }
            })
            .map_err(|_e| ())?;

            TimeoutFiberFuture {
                fiber,
                current_suspend,
                current_poll_cx,
                deadline,
            }
        };

        future.await?;

        Ok(slot.unwrap())
    }
}

impl<'a> Future for TimeoutFiberFuture<'a> {
    type Output = Result<(), ()>;
    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        unsafe {
            crate::executor::init_signal_listen();

            let _reset = Reset(self.current_poll_cx, *self.current_poll_cx);
            *self.current_poll_cx =
                std::mem::transmute::<&mut Context<'_>, *mut Context<'static>>(cx);
            let async_cx = AsyncCx {
                current_suspend: self.current_suspend,
                current_poll_cx: self.current_poll_cx,
            };

            ASYNC_CX.set(&async_cx, || {
                let mut self_thread = libc::pthread_self();
                let mut timerid: libc::timer_t = std::mem::zeroed();
                let mut sev: libc::sigevent = std::mem::zeroed();
                sev.sigev_notify = libc::SIGEV_SIGNAL;
                sev.sigev_signo = crate::executor::timeout_signo();
                sev.sigev_value.sival_ptr = &mut self_thread as *mut _ as *mut libc::c_void;

                if libc::timer_create(libc::CLOCK_REALTIME, &mut sev, &mut timerid) < 0 {
                    return Poll::Ready(Err(()));
                }

                let timeout = match self.deadline.duration_since(std::time::SystemTime::now()) {
                    Ok(timeout) => timeout.max(std::time::Duration::from_millis(100)),
                    Err(_) => return Poll::Ready(Err(())),
                };

                let mut value: libc::itimerspec = std::mem::zeroed();
                value.it_value.tv_sec = timeout.as_secs() as _;
                value.it_value.tv_nsec = timeout.subsec_nanos() as _;
                if libc::timer_settime(timerid, 0, &value, std::ptr::null_mut()) < 0 {
                    libc::timer_delete(timerid);
                    return Poll::Ready(Err(()));
                }

                let mut env: setjmp::sigjmp_buf = std::mem::zeroed();
                let jmp_state = crate::executor::JmpState {
                    sigjmp_buf: &mut env,
                };

                crate::executor::JMP_BUF.set(&jmp_state, || {
                    if setjmp::sigsetjmp(&mut env, 1) == 0 {
                        let r = match self.as_ref().fiber.resume(Ok(())) {
                            Ok(ret) => Poll::Ready(ret),
                            Err(_) => Poll::Pending,
                        };
                        libc::timer_delete(timerid);
                        r
                    } else {
                        libc::timer_delete(timerid);
                        Poll::Ready(Err(()))
                    }
                })
            })
        }
    }
}
unsafe impl Send for TimeoutFiberFuture<'_> {}

impl Drop for TimeoutFiberFuture<'_> {
    fn drop(&mut self) {
        if !self.fiber.done() {
            let async_cx = AsyncCx {
                current_suspend: self.current_suspend,
                current_poll_cx: self.current_poll_cx,
            };
            let result = ASYNC_CX.set(&async_cx, || self.fiber.resume(Err(())));
            // This resumption with an error should always complete the
            // fiber. While it's technically possible for host code to catch
            // the trap and re-resume, we'd ideally like to signal that to
            // callers that they shouldn't be doing that.
            debug_assert!(result.is_ok());
        }
    }
}

scoped_tls::scoped_thread_local!(static ASYNC_CX: AsyncCx);

/// Defines a async state that contains the pointer to current poll context and current suspend.
#[derive(Debug)]
pub struct AsyncState {
    current_suspend: std::cell::UnsafeCell<*const FiberSuspend>,
    current_poll_cx: std::cell::UnsafeCell<*mut Context<'static>>,
}
impl Default for AsyncState {
    fn default() -> Self {
        Self::new()
    }
}
impl AsyncState {
    /// Creates a new async state.
    pub fn new() -> Self {
        AsyncState {
            current_suspend: std::cell::UnsafeCell::new(std::ptr::null()),
            current_poll_cx: std::cell::UnsafeCell::new(std::ptr::null_mut()),
        }
    }

    /// Returns an async execution context.
    ///
    /// If the pointer of poll context is null, then None is returned.
    pub fn async_cx(&self) -> Option<AsyncCx> {
        let poll_cx_box_ptr = self.current_poll_cx.get();
        if poll_cx_box_ptr.is_null() {
            return None;
        }
        let poll_cx_inner_ptr = unsafe { *poll_cx_box_ptr };
        if poll_cx_inner_ptr.is_null() {
            return None;
        }

        Some(AsyncCx {
            current_suspend: self.current_suspend.get(),
            current_poll_cx: poll_cx_box_ptr,
        })
    }
}
unsafe impl Send for AsyncState {}
unsafe impl Sync for AsyncState {}

/// Defines an async execution context.
#[derive(Debug, Clone, Copy)]
pub struct AsyncCx {
    current_suspend: *mut *const Suspend<Result<(), ()>, (), Result<(), ()>>,
    current_poll_cx: *mut *mut Context<'static>,
}
impl Default for AsyncCx {
    fn default() -> Self {
        Self::new()
    }
}
impl AsyncCx {
    /// Creates a new async execution context.
    pub fn new() -> Self {
        ASYNC_CX.with(|async_cx| *async_cx)
    }

    /// Runs a future to completion.
    ///
    /// # Arguments
    ///
    /// * `future` - The future to run.
    ///
    /// # Error
    ///
    /// If fail to run, then an error is returned.
    pub(crate) unsafe fn block_on<U>(
        &self,
        mut future: Pin<&mut (dyn Future<Output = U> + Send)>,
    ) -> Result<U, ()> {
        let suspend = *self.current_suspend;
        let _reset = Reset(self.current_suspend, suspend);
        *self.current_suspend = ptr::null();
        assert!(!suspend.is_null());

        loop {
            let future_result = {
                let poll_cx = *self.current_poll_cx;
                let _reset = Reset(self.current_poll_cx, poll_cx);
                *self.current_poll_cx = ptr::null_mut();
                assert!(!poll_cx.is_null());
                future.as_mut().poll(&mut *poll_cx)
            };

            match future_result {
                Poll::Ready(t) => break Ok(t),
                Poll::Pending => {}
            }
            let res = (*suspend).suspend(());
            res?;
        }
    }
}

struct Reset<T: Copy>(*mut T, T);
impl<T: Copy> Drop for Reset<T> {
    fn drop(&mut self) {
        unsafe {
            *self.0 = self.1;
        }
    }
}