open_coroutine_hooks/
task.rs

1use open_coroutine_core::common::JoinHandle;
2use open_coroutine_core::coroutine::suspender::SuspenderImpl;
3use open_coroutine_core::net::event_loop::join::TaskJoinHandleImpl;
4use open_coroutine_core::net::event_loop::{EventLoops, UserFunc};
5use std::ffi::{c_long, c_void};
6use std::time::Duration;
7
8///创建任务
9#[no_mangle]
10pub extern "C" fn task_crate(f: UserFunc, param: usize) -> TaskJoinHandleImpl {
11    EventLoops::submit(
12        move |suspender, p| {
13            #[allow(clippy::cast_ptr_alignment, clippy::ptr_as_ptr)]
14            Some(f(
15                suspender as *const _ as *const SuspenderImpl<(), ()>,
16                p.unwrap_or(0),
17            ))
18        },
19        Some(param),
20    )
21}
22
23///等待任务完成
24#[no_mangle]
25pub extern "C" fn task_join(handle: TaskJoinHandleImpl) -> c_long {
26    match handle.join() {
27        Ok(ptr) => match ptr {
28            Ok(ptr) => match ptr {
29                Some(ptr) => ptr as *mut c_void as c_long,
30                None => 0,
31            },
32            Err(_) => -1,
33        },
34        Err(_) => -1,
35    }
36}
37
38///等待任务完成
39#[no_mangle]
40pub extern "C" fn task_timeout_join(handle: &TaskJoinHandleImpl, ns_time: u64) -> c_long {
41    match handle.timeout_join(Duration::from_nanos(ns_time)) {
42        Ok(ptr) => match ptr {
43            Ok(ptr) => match ptr {
44                Some(ptr) => ptr as *mut c_void as c_long,
45                None => 0,
46            },
47            Err(_) => -1,
48        },
49        Err(_) => -1,
50    }
51}