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
#![allow(missing_docs)]
#![allow(clippy::needless_lifetimes)]

use core::future::Future;
use core::pin::Pin;
use core::task::{Context, Poll, Waker};
use ockam_core::compat::io;
use ockam_core::compat::sync::{Arc, Mutex};

use crate::executor;

/// execute
pub fn execute<'r, F>(_runtime: &'r Runtime, future: F) -> <F as Future>::Output
where
    F: Future<Output = ()> + Send,
    F::Output: Send,
{
    executor::current().block_on(future)
}

/// block_future
pub fn block_future<'r, F>(_runtime: &'r Runtime, _future: F) -> <F as Future>::Output
where
    F: Future + Send,
    F::Output: Send,
{
    // task::block_in_place(move || {
    //     let local = task::LocalSet::new();
    //     local.block_on(rt, f)
    // })
    unimplemented!();
}

/// spawn
pub fn spawn<F: 'static>(_future: F)
where
    F: Future + Send,
    F::Output: Send,
{
    // task::spawn(f)
    unimplemented!();
}

/// Runtime
pub struct Runtime {
    handle: Handle,
}

impl Runtime {
    pub fn new() -> io::Result<Runtime> {
        Ok(Self { handle: Handle(()) })
    }

    pub fn handle(&self) -> &Handle {
        &self.handle
    }

    /// Spawn a future onto the runtime.
    pub fn spawn<F>(&self, future: F) -> JoinHandle<F::Output>
    where
        F: Future + Send + 'static,
        F::Output: Send + 'static,
    {
        executor::current().spawn(future);
        JoinHandle::new()
    }
}

/// Runtime handle
#[derive(Clone)]
pub struct Handle(());

impl Handle {
    pub fn spawn<F>(&self, future: F) -> JoinHandle<F::Output>
    where
        F: Future + Send + 'static,
        F::Output: Send + 'static,
    {
        executor::current().spawn(future);
        JoinHandle::new()
    }
}

/// SharedJoinHandle
pub struct SharedJoinHandle<T> {
    pub value: Option<T>,
    pub waker: Option<Waker>,
}

/// JoinHandle
pub struct JoinHandle<T>(pub Arc<Mutex<SharedJoinHandle<T>>>);

impl<T: Send> Default for SharedJoinHandle<T> {
    fn default() -> SharedJoinHandle<T> {
        Self {
            value: None,
            waker: None,
        }
    }
}

impl<T: Send> Future for JoinHandle<T> {
    type Output = T;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<T> {
        let mut guard = self.0.lock().unwrap();
        if let Some(value) = guard.value.take() {
            return Poll::Ready(value);
        }
        guard.waker = Some(cx.waker().clone());
        Poll::Pending
    }
}

impl<T: Send> JoinHandle<T> {
    pub fn new() -> JoinHandle<T> {
        let inner = Arc::new(Mutex::new(SharedJoinHandle::default()));
        JoinHandle(inner)
    }
}

impl<T: Send> Default for JoinHandle<T> {
    fn default() -> Self {
        Self::new()
    }
}

/// yield_now
pub async fn yield_now() {
    #[allow(dead_code)]
    struct YieldNow {
        yielded: bool,
    }

    impl Future for YieldNow {
        type Output = ();

        fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
            if self.yielded {
                Poll::Ready(())
            } else {
                self.yielded = true;
                cx.waker().wake_by_ref();
                Poll::Pending
            }
        }
    }

    YieldNow { yielded: false }.await
}