-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparallel.rs
More file actions
64 lines (59 loc) · 1.94 KB
/
Copy pathparallel.rs
File metadata and controls
64 lines (59 loc) · 1.94 KB
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
use crate::{StdFunction, StdlibModule, StdlibRegistry};
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
use std::thread;
use techscript_runtime::{error::RuntimeError, value::RuntimeValue};
impl StdlibRegistry {
pub fn register_parallel(&mut self) {
let mut exports: HashMap<String, Rc<dyn techscript_runtime::function::Callable>> =
HashMap::new();
exports.insert(
"run".to_string(),
Rc::new(StdFunction {
name: "run".to_string(),
arity: 1,
callback: |_ctx, args| {
let _task = args[0].to_string();
thread::spawn(move || {});
Ok(RuntimeValue::Null)
},
}),
);
exports.insert(
"sleep".to_string(),
Rc::new(StdFunction {
name: "sleep".to_string(),
arity: 1,
callback: |_ctx, args| {
let ms = args[0].try_into_int().unwrap_or(0) as u64;
thread::sleep(std::time::Duration::from_millis(ms));
Ok(RuntimeValue::Null)
},
}),
);
exports.insert(
"num_cpus".to_string(),
Rc::new(StdFunction {
name: "num_cpus".to_string(),
arity: 0,
callback: |_ctx, _args| {
Ok(RuntimeValue::Int(
std::thread::available_parallelism()
.map(|n| n.get() as i64)
.unwrap_or(1),
))
},
}),
);
self.register_module(
"std.parallel",
StdlibModule {
name: "std.parallel".to_string(),
version: "1.0.0".to_string(),
exports,
required_capabilities: Vec::new(),
},
);
}
}