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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
use std::mem::{forget, transmute};
use serde::{de, ser, Deserialize, Serialize};
use crate::channel::Receiver;
mod stdlib {
#[link(wasm_import_module = "lunatic")]
extern "C" {
pub fn spawn_with_context(
function: unsafe extern "C" fn(),
buf_ptr: *const u8,
buf_len: usize,
) -> u32;
pub fn detach_process(pid: u32);
pub fn cancel_process(pid: u32);
pub fn join(pid: u32) -> u32;
pub fn sleep_ms(millis: u64);
}
}
pub struct Process {
id: u32,
}
impl Drop for Process {
fn drop(&mut self) {
unsafe { stdlib::cancel_process(self.id) };
}
}
impl Process {
pub fn spawn_with<T>(context: T, function: fn(T)) -> Process
where
T: ser::Serialize + de::DeserializeOwned,
{
unsafe extern "C" fn spawn_with_context<'de, T>()
where
T: ser::Serialize + de::DeserializeOwned,
{
let receiver: Receiver<Context<T>> = Receiver::from(0);
let context: Context<T> = receiver.receive().unwrap();
let function: fn(T) = transmute(context.function_ptr);
function(context.context);
}
let context = Context {
function_ptr: unsafe { transmute(function) },
context: context,
};
let context_serialized = bincode::serialize(&context).unwrap();
let id = unsafe {
stdlib::spawn_with_context(
spawn_with_context::<T>,
context_serialized.as_ptr(),
context_serialized.len(),
)
};
Self { id }
}
pub fn detach(self) {
unsafe { stdlib::detach_process(self.id) };
forget(self);
}
pub fn join(self) -> Result<(), ()> {
let result = unsafe { stdlib::join(self.id) };
forget(self);
match result {
0 => Ok(()),
_ => Err(()),
}
}
pub fn sleep(milliseconds: u64) {
unsafe {
stdlib::sleep_ms(milliseconds);
};
}
}
#[derive(Serialize, Deserialize)]
struct Context<T> {
function_ptr: usize,
context: T,
}