Skip to content

Commit fe163ef

Browse files
authored
0_6_4 (#51)
* Threading safe and unsafe (#50) * 0.6.4
1 parent 23d0721 commit fe163ef

File tree

6 files changed

+114
-58
lines changed

6 files changed

+114
-58
lines changed

.github/workflows/Threading.yml

Lines changed: 76 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,80 @@ jobs:
2424
run: |
2525
cargo build --features="repl" --release
2626
27-
- name: Threading
27+
- name: Thread Safe (still unsafe)
2828
run: |
29-
./target/release/3body -V
30-
./target/release/3body -c '给 cx 以 程心(); 给 星环公司 以 法则(name, y, limit) { 给 掩体纪年 以 y; 面壁 (掩体纪年 <= limit) { 冬眠(1000); 广播([name, 掩体纪年]); 掩体纪年 = 掩体纪年 + 1; } } cx.thread(星环公司, ["掩体工程", 0, 11]) 冬眠(5000) cx.thread(星环公司, ["研制曲率飞船", 5, 11]) 冬眠(6000)'
31-
./target/release/3body -c '给 cx 以 程心(); 给 星环公司 以 法则(name, y, limit) { 给 掩体纪年 以 y; 面壁 (掩体纪年 <= limit) { 冬眠(1000); 广播([name, 掩体纪年]); 掩体纪年 = 掩体纪年 + 1; } } 给 秘密研究 以 cx.thread(星环公司, ["重启光速飞船的研究", 11, 66]) cx.join(秘密研究)'
29+
./target/release/3body -c '
30+
给 cx 以 程心();
31+
给 掩体工程进展 以 0;
32+
给 研制曲率飞船进展 以 0;
33+
34+
cx.thread(法则() {
35+
给 掩体纪年 以 0;
36+
面壁 (掩体纪年 <= 11) {
37+
冬眠(1000);
38+
广播(["掩体工程进展", 掩体工程进展]);
39+
掩体工程进展 = 掩体工程进展 + 1;
40+
掩体纪年 = 掩体纪年 + 1;
41+
}
42+
}, [])
43+
44+
冬眠(5000)
45+
46+
cx.thread(法则() {
47+
给 掩体纪年 以 5;
48+
面壁 (掩体纪年 <= 11) {
49+
冬眠(1000);
50+
广播(["研制曲率飞船进展", 研制曲率飞船进展]);
51+
研制曲率飞船进展 = 研制曲率飞船进展 + 1;
52+
掩体纪年 = 掩体纪年 + 1;
53+
}
54+
}, [])
55+
56+
冬眠(6000)'
57+
58+
59+
./target/release/3body -c '
60+
给 cx 以 程心();
61+
62+
给 星环公司 以 法则(name, y, limit) {
63+
给 掩体纪年 以 y;
64+
面壁 (掩体纪年 <= limit) {
65+
冬眠(1000);
66+
广播([name, 掩体纪年]);
67+
掩体纪年 = 掩体纪年 + 1;
68+
}
69+
}
70+
71+
给 秘密研究 以 cx.thread(星环公司, ["重启光速飞船的研究", 11, 66])
72+
cx.join(秘密研究)'
73+
74+
- name: Thread Unsafe
75+
run: |
76+
./target/release/3body -c '
77+
给 cx 以 程心();
78+
给 总进展 以 0;
79+
80+
cx.thread(法则() {
81+
给 掩体纪年 以 0;
82+
面壁 (掩体纪年 <= 11) {
83+
冬眠(1000);
84+
广播(["总进展", 总进展]);
85+
总进展 = 总进展 + 1; // unsafe
86+
掩体纪年 = 掩体纪年 + 1;
87+
}
88+
}, [])
89+
90+
冬眠(5000)
91+
92+
cx.thread(法则() {
93+
给 掩体纪年 以 5;
94+
面壁 (掩体纪年 <= 11) {
95+
冬眠(1000);
96+
广播(["总进展", 总进展]);
97+
总进展 = 总进展 + 1; // unsafe
98+
掩体纪年 = 掩体纪年 + 1;
99+
}
100+
}, [])
101+
102+
冬眠(6000)
103+
广播([总进展])'

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "three_body_lang"
33
description = "Three Body Language written in Rust"
44
edition = "2021"
5-
version = "0.6.3"
5+
version = "0.6.4"
66
authors = ["meloalright", "rustq"]
77
license = "MIT"
88

@@ -12,7 +12,7 @@ members = ["interpreter"]
1212
[dependencies]
1313
rustyline = { version = "12.0.0", optional = true }
1414
rustyline-derive = { version = "0.4.0", optional = true }
15-
three_body_interpreter = { version = "0.6.3", path = "./interpreter", features = ["sophon", "threading"] }
15+
three_body_interpreter = { version = "0.6.4", path = "./interpreter", features = ["sophon", "threading"] }
1616

1717
[[bin]]
1818
name = "3body"

interpreter/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "three_body_interpreter"
33
description = "Three Body interpreter"
4-
version = "0.6.3"
4+
version = "0.6.4"
55
edition = "2021"
66
authors = ["meloalright", "rustq"]
77
license = "MIT"

interpreter/src/evaluator/builtins.rs

Lines changed: 31 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -351,66 +351,48 @@ fn three_body_sophon_engineering(args: Vec<Object>) -> Object {
351351

352352

353353
#[cfg(feature="threading")]
354-
fn three_body_threading(args: Vec<Object>) -> Object {
354+
fn three_body_threading(_: Vec<Object>) -> Object {
355355
let mut session_hash = HashMap::new();
356356
{
357357
fn three_body_thread_new(args: Vec<Object>) -> Object {
358-
match &args[0] {
359-
Object::Function(params, ast, env ) => {
360-
361-
let stmts = ast.clone();
362-
let params = params.clone();
363-
364-
let literals: Vec<crate::ast::Literal> = match &args[1] {
365-
Object::Array(arr) => {
366-
arr.iter().map(|o| match o {
367-
Object::Int(i) => ast::Literal::Int(i.clone()),
368-
Object::String(str) => ast::Literal::String(str.clone()),
369-
Object::Bool(bool) => ast::Literal::Bool(bool.clone()),
370-
_ => todo!(),
371-
}).collect()
372-
},
373-
_ => panic!()
374-
};
375-
376-
let mut handle = std::thread::spawn(move || {
377-
let local_set = tokio::task::LocalSet::new();
378-
let rt = tokio::runtime::Builder::new_current_thread()
379-
.enable_all()
380-
.build()
381-
.unwrap();
382-
383-
local_set.spawn_local(async move {
358+
let handle = std::thread::spawn(|| {
359+
let local_set = tokio::task::LocalSet::new();
360+
let rt = tokio::runtime::Builder::new_current_thread()
361+
.enable_all()
362+
.build()
363+
.unwrap();
364+
365+
local_set.spawn_local(async move {
366+
match &args[0] {
367+
Object::Function(params, stmts, env ) => {
384368
let mut ev = Evaluator {
385369
env: {
386-
let scoped_env = Rc::new(RefCell::new(Env::from(new_builtins())));
387-
388-
for (i, ident) in params.iter().enumerate() {
389-
let crate::ast::Ident(name) = ident.clone();
390-
let o = match &literals[i] {
391-
ast::Literal::Int(i) => Object::Int(i.clone()),
392-
ast::Literal::String(str) => Object::String(str.clone()),
393-
ast::Literal::Bool(bo) => Object::Bool(bo.clone()),
394-
_ => todo!(),
395-
};
396-
scoped_env.borrow_mut().set(name, o.clone());
370+
let mut scoped_env = Env::new_with_outer(Rc::clone(&env)); // still thread unsafe
371+
let list = params.iter().zip({
372+
match &args[1] {
373+
Object::Array(arr) => arr,
374+
_ => panic!()
375+
}
376+
}.iter());
377+
for (_, (ident, o)) in list.enumerate() {
378+
let ast::Ident(name) = ident.clone();
379+
scoped_env.set(name, o.clone());
397380
}
398-
399-
scoped_env
381+
Rc::new(RefCell::new(scoped_env))
400382
},
401383
};
402384
ev.eval(&stmts);
403-
});
385+
},
386+
_ => panic!()
387+
}
388+
});
404389

405-
rt.block_on(local_set);
406-
});
390+
rt.block_on(local_set);
391+
});
407392

408-
let handle = Box::leak(Box::new(handle));
409-
let handle_ptr = &mut *handle as *mut std::thread::JoinHandle<()>;
410-
Object::Native(Box::new(NativeObject::Thread(handle_ptr)))
411-
}
412-
_ => panic!()
413-
}
393+
let handle = Box::leak(Box::new(handle));
394+
let handle_ptr = &mut *handle as *mut std::thread::JoinHandle<()>;
395+
Object::Native(Box::new(NativeObject::Thread(handle_ptr)))
414396
}
415397
session_hash.insert(Object::String("thread".to_owned()), Object::Builtin(2, three_body_thread_new));
416398
}

interpreter/src/evaluator/object.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ pub enum Object {
3737
Native(Box<NativeObject>),
3838
}
3939

40+
unsafe impl Send for Object {}
41+
4042
/// This is actually repr
4143
impl fmt::Display for Object {
4244
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {

0 commit comments

Comments
 (0)