Skip to main content

extern_executor/
ffi.rs

1use crate::{UserData, BoxedPoll, Box, global};
2
3/// Raw external task handle
4pub type ExternTask = UserData;
5
6/// Raw external data
7pub type ExternData = UserData;
8
9/// Rust internal task handle
10pub type InternTask = UserData;
11
12/// Raw poll function
13///
14/// This function must be called to poll future on each wake event
15pub type TaskPoll = extern "C" fn(InternTask) -> bool;
16
17/// Raw C drop function
18///
19/// This function must be called to cleanup either pending or completed future
20pub type TaskDrop = extern "C" fn(InternTask);
21
22/// C function which can create new tasks
23pub type TaskNew = extern "C" fn(ExternData) -> ExternTask;
24
25/// C function which can run created tasks
26pub type TaskRun = extern "C" fn(ExternTask, InternTask);
27
28/// C function which can wake created task
29///
30/// This function will be called when pending future need to be polled again
31pub type TaskWake = extern "C" fn(ExternTask);
32
33/// Initialize async executor by providing task API calls
34#[export_name = "rust_async_executor_init"]
35pub extern "C" fn loop_init(task_new: TaskNew, task_run: TaskRun, task_wake: TaskWake, task_data: ExternData) {
36    use global::*;
37
38    unsafe {
39        TASK_NEW = task_new as _;
40        TASK_RUN = task_run as _;
41        TASK_WAKE = task_wake as _;
42        TASK_DATA = task_data as _;
43    }
44}
45
46/// Task poll function which should be called to resume task
47#[export_name = "rust_async_executor_poll"]
48pub extern "C" fn task_poll(data: InternTask) -> bool {
49    let poll = unsafe { &mut *(data as *mut BoxedPoll) };
50    poll()
51}
52
53/// Task drop function which should be called to delete task
54#[export_name = "rust_async_executor_drop"]
55pub extern "C" fn task_drop(data: InternTask) {
56    let _poll = unsafe { Box::from_raw(data as *mut BoxedPoll) };
57}