hook/
common.rs

1use base_coroutine::coroutine::UserFunc;
2use base_coroutine::{EventLoop, JoinHandle};
3use std::os::raw::c_void;
4
5#[no_mangle]
6pub extern "C" fn init_hook() {
7    //啥都不做,只是为了保证hook的函数能够被重定向到
8    //主要为了防止有的程序压根不调用coroutine_crate的情况
9}
10
11///创建协程
12#[no_mangle]
13pub extern "C" fn coroutine_crate(
14    f: UserFunc<&'static mut c_void, (), &'static mut c_void>,
15    param: &'static mut c_void,
16    stack_size: usize,
17) -> JoinHandle {
18    match EventLoop::submit(f, param, stack_size) {
19        Ok(handle) => handle,
20        Err(_) => {
21            //we had handled NULL in JoinHandle
22            #[allow(invalid_value)]
23            JoinHandle(unsafe { std::mem::transmute(0usize) })
24        }
25    }
26}
27
28///等待协程完成
29#[no_mangle]
30pub extern "C" fn coroutine_join(handle: JoinHandle) -> libc::c_long {
31    match handle.join() {
32        Ok(ptr) => ptr as libc::c_long,
33        Err(_) => -1,
34    }
35}
36
37///等待协程完成
38#[no_mangle]
39pub extern "C" fn coroutine_timeout_join(handle: &JoinHandle, ns_time: u64) -> libc::c_long {
40    match handle.timeout_join(std::time::Duration::from_nanos(ns_time)) {
41        Ok(ptr) => ptr as libc::c_long,
42        Err(_) => -1,
43    }
44}
45
46///轮询协程
47#[no_mangle]
48pub extern "C" fn try_timed_schedule(ns_time: u64) -> libc::c_int {
49    let timeout_time = timer_utils::add_timeout_time(ns_time);
50    match EventLoop::round_robin_timeout_schedule(timeout_time) {
51        Ok(_) => 0,
52        Err(_) => -1,
53    }
54}
55
56#[no_mangle]
57pub extern "C" fn timed_schedule(ns_time: u64) -> libc::c_int {
58    let timeout_time = timer_utils::add_timeout_time(ns_time);
59    match EventLoop::round_robin_timed_schedule(timeout_time) {
60        Ok(_) => 0,
61        Err(_) => -1,
62    }
63}