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
use std::os::raw::c_void;
pub use base_coroutine::*;
#[allow(dead_code)]
extern "C" {
fn init_hook();
fn coroutine_crate(
f: UserFunc<Option<&'static mut c_void>, (), Option<&'static mut c_void>>,
param: Option<&'static mut c_void>,
stack_size: usize,
) -> libc::c_int;
fn try_timed_schedule(ns_time: u64) -> libc::c_int;
fn timed_schedule(ns_time: u64) -> libc::c_int;
}
pub fn init() {
unsafe { init_hook() }
}
pub fn co(
f: UserFunc<Option<&'static mut c_void>, (), Option<&'static mut c_void>>,
param: Option<&'static mut c_void>,
stack_size: usize,
) -> bool {
unsafe { coroutine_crate(f, param, stack_size) == 0 }
}
pub fn schedule() -> bool {
unsafe { try_timed_schedule(u64::MAX) == 0 }
}
#[cfg(test)]
mod tests {
use crate::{co, init, schedule, Yielder};
use std::os::raw::c_void;
use std::time::Duration;
#[test]
fn test_link() {
init();
}
extern "C" fn f1(
_yielder: &Yielder<Option<&'static mut c_void>, (), Option<&'static mut c_void>>,
_input: Option<&'static mut c_void>,
) -> Option<&'static mut c_void> {
println!("[coroutine1] launched");
None
}
extern "C" fn f2(
_yielder: &Yielder<Option<&'static mut c_void>, (), Option<&'static mut c_void>>,
_input: Option<&'static mut c_void>,
) -> Option<&'static mut c_void> {
println!("[coroutine2] launched");
None
}
#[test]
fn simplest() {
assert!(co(f1, None, 4096));
assert!(co(f2, None, 4096));
assert!(schedule());
}
fn hook_test(secs: u64) {
assert!(co(f1, None, 4096));
assert!(co(f2, None, 4096));
std::thread::sleep(Duration::from_millis(secs))
}
#[test]
fn hook_test_schedule_timeout() {
hook_test(1)
}
#[test]
fn hook_test_schedule_normal() {
hook_test(1_000)
}
}