-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathffi.rs
65 lines (56 loc) · 1.22 KB
/
ffi.rs
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
use crate::linger::Linger as Lingerer;
use std::ffi::c_void;
use std::process::abort;
use std::thread::Result;
#[repr(C)]
pub struct Linger {
is_complete: bool,
continuation: Lingerer<(), dyn FnMut(*mut Option<Result<()>>) + Send>,
}
#[no_mangle]
extern fn launch(fun: unsafe extern fn(*mut c_void), us: u64, args: *mut c_void) -> Linger {
use crate::force::AssertSend;
use crate::linger::launch;
let args = unsafe {
AssertSend::new(args)
};
let timed = launch(move || unsafe {
fun(*args)
}, us);
if let Ok(timed) = timed {
Linger {
is_complete: timed.is_completion(),
continuation: timed.erase(),
}
} else {
abort()
}
}
#[no_mangle]
extern fn resume(timed: Option<&mut Linger>, us: u64) {
use crate::linger::resume;
if let Some(timed) = timed {
if resume(&mut timed.continuation, us).is_err() {
abort();
}
timed.is_complete = timed.continuation.is_completion();
} else {
abort();
}
}
#[no_mangle]
extern fn cancel(timed: Option<&mut Linger>) {
if let Some(timed) = timed {
if timed.continuation.is_continuation() {
timed.continuation = Lingerer::Completion(());
timed.is_complete = true;
}
} else {
abort();
}
}
#[no_mangle]
extern fn pause() {
use crate::linger::pause;
pause();
}