-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpmt1-rust-wasi.rs
82 lines (70 loc) · 2.09 KB
/
mpmt1-rust-wasi.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// mpmt1.rs:
//
//
// License:
// Apache License, Version 2.0
// History:
// * 2021/12/31 v0.1 Initial version.
// Author:
// Masanori Itoh <masanori.itoh@gmail.com>
// TODO:
#![feature(rustc_private)]
extern crate getopts;
use getopts::Options;
use std::env;
use std::thread;
use std::time::SystemTime;
use std::convert::TryInto;
fn worker(id: i32, duration: i32) {
let ts_save = SystemTime::now();
let max: u128 = (duration * 1000 * 1000).try_into().unwrap();
println!("worker: {} started. duration: {:?} (us)", id, max);
let mut diff: u128;
loop {
let ts = SystemTime::now();
diff = ts.duration_since(ts_save).unwrap().as_micros();
if diff >= max {
break;
}
}
println!("worker: {} exiting... duration: {:?} (us)", id, diff);
}
fn main() {
let args: Vec<String> = env::args().collect();
let mut opts = Options::new();
opts.optmulti("n", "", "number of contexts", "NUM_CONTEXT");
opts.optmulti("d", "", "duration", "DURATION");
opts.optmulti("m", "", "mode", "t(hread)");
let matches = opts.parse(&args[1..]).unwrap_or_else(|f| panic!("{}", f.to_string()));
let num_context: i32 = if matches.opt_present("n") {
matches.opt_strs("n")[0].parse::<i32>().unwrap()
} else {
3
};
let duration: i32 = if matches.opt_present("d") {
matches.opt_strs("d")[0].parse::<i32>().unwrap()
} else {
5
};
let mode = if matches.opt_present("m") {
&matches.opt_strs("m")[0]
} else {
"t"
};
println!("num_context: {}, duration: {}, mode: {}", num_context, duration, mode);
if mode == "t" {
let mut handles: Vec<thread::JoinHandle<()>> = Vec::new();
for i in 0..num_context {
println!("main: creating {} th thread.", i);
let thread = thread::spawn(move || {
worker(i, duration)
});
handles.push(thread);
}
for thread in handles.into_iter() {
thread.join().unwrap();
}
} else {
println!("process mode not supported.");
}
}