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
extern crate spin;

pub mod promise;

use std::any::Any;
use std::sync::Arc;
use std::panic::{catch_unwind, AssertUnwindSafe};
pub use promise::Promise;

#[repr(C)]
pub(crate) struct CoroutineImpl {
    _dummy: usize
}

#[repr(C)]
pub(crate) struct AnyUserData {
    _dummy: usize
}

pub(crate) type AsyncEntry = extern "C" fn (co: *const CoroutineImpl, data: *const AnyUserData);

const REQUIRED_BACKEND_VERSION: i32 = 20;

#[link(name = "unblock_hook", kind = "dylib")]
extern "C" {
    pub(crate) fn current_coroutine() -> *const CoroutineImpl;
    fn launch_co(
        entry: extern "C" fn (*const CoroutineImpl),
        user_data: *const AnyUserData
    );
    fn extract_co_user_data(
        co: *const CoroutineImpl
    ) -> *const AnyUserData;
    fn co_get_global_event_count() -> usize;

    fn coroutine_yield(
        co: *const CoroutineImpl
    );

    fn gtp_enable_work_stealing();
    fn gtp_disable_work_stealing();
    fn gtp_get_migration_count() -> i32;

    fn ubh_get_version() -> i32;

    pub(crate) fn coroutine_async_enter(
        co: *const CoroutineImpl,
        entry: AsyncEntry,
        data: *const AnyUserData
    ) -> *const AnyUserData;
    
    pub(crate) fn coroutine_async_exit(
        co: *const CoroutineImpl,
        data: *const AnyUserData
    );
}

struct CoroutineEntry<
    T: Send + 'static,
    F: FnOnce() -> T + Send + 'static,
    E: FnOnce(Result<T, Box<Any + Send>>) + Send + 'static
> {
    entry: Option<F>,
    on_exit: Option<E>
}

extern "C" fn _launch<
    T: Send + 'static,
    F: FnOnce() -> T + Send + 'static,
    E: FnOnce(Result<T, Box<Any + Send>>) + Send + 'static
>(co: *const CoroutineImpl) {
    let mut target = unsafe { Box::from_raw(
        extract_co_user_data(co) as *const CoroutineEntry<T, F, E> as *mut CoroutineEntry<T, F, E>
    ) };
    let entry = target.entry.take().unwrap();
    let ret = catch_unwind(AssertUnwindSafe(move || (entry)()));
    (target.on_exit.take().unwrap())(ret);
}

fn spawn_with_callback<
    T: Send + 'static,
    F: FnOnce() -> T + Send + 'static,
    E: FnOnce(Result<T, Box<Any + Send>>) + Send + 'static
>(entry: F, cb: E) {
    let real_version = unsafe { ubh_get_version() };
    if real_version != REQUIRED_BACKEND_VERSION {
        panic!("Backend version mismatch (expected: {}, got: {})", REQUIRED_BACKEND_VERSION, real_version);
    }

    let co = Box::new(CoroutineEntry {
        entry: Some(entry),
        on_exit: Some(cb)
    });
    unsafe {
        launch_co(
            _launch::<T, F, E>,
            Box::into_raw(co) as *const AnyUserData
        );
    }
}

/// A handle used to wait on a coroutine's termination.
pub struct JoinHandle<T: Send + 'static> {
    state: Arc<spin::Mutex<JoinHandleState<T>>>
}

impl<T: Send + 'static> JoinHandle<T> {
    fn priv_clone(&self) -> JoinHandle<T> {
        JoinHandle {
            state: self.state.clone()
        }
    }

    /// Waits for the associated coroutine to finish.
    ///
    /// If the associated coroutine has already terminated,
    /// `join` returns instantly with the result.
    /// Otherwise, `join` waits until the coroutine terminates.
    ///
    /// If the child coroutine panics, `Err` is returned with the
    /// boxed value passed to `panic`. Otherwise, `Ok` is returned
    /// with the return value of the closure executed in the coroutine.
    pub fn join(self) -> Result<T, Box<Any + Send>> {
        Promise::await(move |p| {
            let mut state = self.state.lock();
            let result = match ::std::mem::replace(&mut *state, JoinHandleState::Empty) {
                JoinHandleState::Empty => None,
                JoinHandleState::Done(v) => Some(v),
                JoinHandleState::Pending(_) => unreachable!()
            };
            if let Some(result) = result {
                drop(state);
                p.resolve(result);
            } else {
                *state = JoinHandleState::Pending(p);
            }
        })
    }
}

enum JoinHandleState<T: Send + 'static> {
    Empty,
    Done(Result<T, Box<Any + Send>>),
    Pending(Promise<Result<T, Box<Any + Send>>>)
}

/// Spawns a coroutine without building a `JoinHandle`.
///
/// This may be faster than `spawn` in some cases.
pub fn fast_spawn<T: Send + 'static, F: FnOnce() -> T + Send + 'static>(entry: F) {
    spawn_with_callback(entry, |_| {});
}

/// Spawns a coroutine and returns its `JoinHandle`.
pub fn spawn<T: Send + 'static, F: FnOnce() -> T + Send + 'static>(entry: F) -> JoinHandle<T> {
    let handle = JoinHandle {
        state: Arc::new(spin::Mutex::new(JoinHandleState::Empty as JoinHandleState<T>))
    };
    let handle2 = handle.priv_clone();
    spawn_with_callback(entry, move |ret| {
        let mut ret = Some(ret);
        let mut resolve_target: Option<Promise<Result<T, Box<Any + Send>>>> = None;

        let mut state = handle2.state.lock();
        let new_state = match ::std::mem::replace(&mut *state, JoinHandleState::Empty) {
            JoinHandleState::Empty => JoinHandleState::Done(ret.take().unwrap()),
            JoinHandleState::Pending(p) => {
                resolve_target = Some(p);
                JoinHandleState::Empty
            },
            JoinHandleState::Done(_) => unreachable!()
        };
        *state = new_state;
        drop(state);

        if let Some(p) = resolve_target {
            p.resolve(ret.take().unwrap());
        }
    });
    handle
}

/// Deprecated.
///
/// Spawns another coroutine if called inside a coroutine,
/// or an OS thread otherwise.
pub fn spawn_inherit<T: Send + 'static, F: FnOnce() -> T + Send + 'static>(entry: F) {
    if unsafe { current_coroutine() }.is_null() {
        ::std::thread::spawn(entry);
    } else {
        spawn(entry);
    }
}

/// Yields out of the current coroutine and allows the scheduler
/// to execute other coroutines.
pub fn yield_now() {
    let co = unsafe { current_coroutine() };
    if !co.is_null() {
        unsafe {
            coroutine_yield(co);
        }
    }
}

/// Returns the global event count.
pub fn global_event_count() -> usize {
    unsafe {
        co_get_global_event_count()
    }
}

/// Enable / disable coroutine migration ("work stealing")
/// globally. (disabled by default)
///
/// This should be used with care because migrating values of
/// non-Send types might break Rust's safety guarantee.
pub unsafe fn set_work_stealing(enabled: bool) {
    if enabled {
        gtp_enable_work_stealing();
    } else {
        gtp_disable_work_stealing();
    }
}

/// Returns the current global migration count.
pub fn migration_count() -> usize {
    unsafe {
        gtp_get_migration_count() as usize
    }
}

#[cfg(test)]
mod tests {
    use std::time::Duration;

    #[test]
    fn test_spawn_join_instant() {
        super::spawn(move || {
            let handle = super::spawn(|| {
                42
            });
            ::std::thread::sleep(Duration::from_millis(50));
            let v: i32 = handle.join().unwrap();
            assert!(v == 42);
        }).join().unwrap();
    }

    #[test]
    fn test_spawn_join_deferred() {
        super::spawn(move || {
            let handle = super::spawn(|| {
                ::std::thread::sleep(Duration::from_millis(50));
                42
            });
            let v: i32 = handle.join().unwrap();
            assert!(v == 42);
        }).join().unwrap();
    }
}