-
Notifications
You must be signed in to change notification settings - Fork 1
/
lib.rs
49 lines (40 loc) · 1.32 KB
/
lib.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
#![allow(non_camel_case_types, non_upper_case_globals)]
#![cfg_attr(test, allow(unknown_lints, deref_nullptr))]
pub mod mprompt;
#[cfg(feature = "mpeff")]
pub mod mpeff;
#[cfg(test)]
mod tests {
use super::mprompt::*;
use std::ffi::c_void;
pub const N: usize = 1000;
pub const M: usize = 1000000;
unsafe extern "C" fn await_result(r: *mut mp_resume_t, _arg: *mut c_void) -> *mut c_void {
return r as _;
}
unsafe extern "C" fn async_worker(parent: *mut mp_prompt_t, _arg: *mut c_void) -> *mut c_void {
let mut partial_result : usize = 0;
mp_yield(parent, Some(await_result), 0 as _);
partial_result += 1;
return partial_result as _;
}
#[test]
fn async_workers() {
unsafe {
let mut workers = Vec::new();
workers.resize(N, std::ptr::null_mut());
let mut count = 0_usize;
for i in 0..M + N {
let j = i % N;
if workers[j] as usize != 0_usize {
count += mp_resume(workers[j], 0 as _) as usize;
workers[j] = 0 as _;
}
if i < M {
workers[j] = mp_prompt(Some(async_worker), 0 as _) as _;
}
println!("ran {} workers", count);
}
}
}
}