From 6f973563c5933faed30025ce8f5b48accdcb3c9a Mon Sep 17 00:00:00 2001 From: meloalright Date: Mon, 4 Nov 2024 22:37:23 +0800 Subject: [PATCH] feat: try set up the threading safe and unsafe for 3body threading --- .github/workflows/Threading.yml | 8 ++++ interpreter/src/evaluator/builtins.rs | 62 +++++++++++---------------- interpreter/src/evaluator/object.rs | 2 + 3 files changed, 35 insertions(+), 37 deletions(-) diff --git a/.github/workflows/Threading.yml b/.github/workflows/Threading.yml index 28c15c7..e6c8112 100644 --- a/.github/workflows/Threading.yml +++ b/.github/workflows/Threading.yml @@ -29,3 +29,11 @@ jobs: ./target/release/3body -V ./target/release/3body -c '给 cx 以 程心(); 给 星环公司 以 法则(name, y, limit) { 给 掩体纪年 以 y; 面壁 (掩体纪年 <= limit) { 冬眠(1000); 广播([name, 掩体纪年]); 掩体纪年 = 掩体纪年 + 1; } } cx.thread(星环公司, ["掩体工程", 0, 11]) 冬眠(5000) cx.thread(星环公司, ["研制曲率飞船", 5, 11]) 冬眠(6000)' ./target/release/3body -c '给 cx 以 程心(); 给 星环公司 以 法则(name, y, limit) { 给 掩体纪年 以 y; 面壁 (掩体纪年 <= limit) { 冬眠(1000); 广播([name, 掩体纪年]); 掩体纪年 = 掩体纪年 + 1; } } 给 秘密研究 以 cx.thread(星环公司, ["重启光速飞船的研究", 11, 66]) cx.join(秘密研究)' + + - name: Threading Safe + run: | + ./target/release/3body -c '给 cx 以 程心(); 给 执剑人 以 "程心女士"; 给 星环公司 以 法则(name, y, limit) { 给 掩体纪年 以 y; 面壁 (掩体纪年 <= limit) { 冬眠(1000); 广播([name, 掩体纪年, 执剑人]); 掩体纪年 = 掩体纪年 + 1; } } cx.thread(星环公司, ["掩体工程", 0, 11]) 冬眠(5000) cx.thread(星环公司, ["研制曲率飞船", 5, 11]) 冬眠(6000)' + + - name: Threading Unsafe + run: | + ./target/release/3body -c '给 cx 以 程心(); 给 掩体纪年 以 0; 给 星环公司 以 法则(name, limit) { 面壁 (掩体纪年 <= limit) { 冬眠(1000); 广播([name, 掩体纪年]); }} cx.thread(星环公司, ["掩体工程", 11]); 面壁 (掩体纪年 < 5) { 冬眠(1000); 掩体纪年 = 掩体纪年 + 1; 广播(掩体纪年); } cx.thread(星环公司, ["研制曲率飞船", 11]); 面壁 (掩体纪年 <= 11) { 冬眠(1000); 掩体纪年 = 掩体纪年 + 1; 广播(掩体纪年); }' diff --git a/interpreter/src/evaluator/builtins.rs b/interpreter/src/evaluator/builtins.rs index e850700..28349d3 100644 --- a/interpreter/src/evaluator/builtins.rs +++ b/interpreter/src/evaluator/builtins.rs @@ -351,29 +351,13 @@ fn three_body_sophon_engineering(args: Vec) -> Object { #[cfg(feature="threading")] -fn three_body_threading(args: Vec) -> Object { +fn three_body_threading(_: Vec) -> Object { let mut session_hash = HashMap::new(); { fn three_body_thread_new(args: Vec) -> Object { match &args[0] { - Object::Function(params, ast, env ) => { - - let stmts = ast.clone(); - let params = params.clone(); - - let literals: Vec = match &args[1] { - Object::Array(arr) => { - arr.iter().map(|o| match o { - Object::Int(i) => ast::Literal::Int(i.clone()), - Object::String(str) => ast::Literal::String(str.clone()), - Object::Bool(bool) => ast::Literal::Bool(bool.clone()), - _ => todo!(), - }).collect() - }, - _ => panic!() - }; - - let mut handle = std::thread::spawn(move || { + Object::Function(_, _, _) => { + let handle = std::thread::spawn(|| { let local_set = tokio::task::LocalSet::new(); let rt = tokio::runtime::Builder::new_current_thread() .enable_all() @@ -381,25 +365,29 @@ fn three_body_threading(args: Vec) -> Object { .unwrap(); local_set.spawn_local(async move { - let mut ev = Evaluator { - env: { - let scoped_env = Rc::new(RefCell::new(Env::from(new_builtins()))); - - for (i, ident) in params.iter().enumerate() { - let crate::ast::Ident(name) = ident.clone(); - let o = match &literals[i] { - ast::Literal::Int(i) => Object::Int(i.clone()), - ast::Literal::String(str) => Object::String(str.clone()), - ast::Literal::Bool(bo) => Object::Bool(bo.clone()), - _ => todo!(), - }; - scoped_env.borrow_mut().set(name, o.clone()); - } - - scoped_env + match &args[0] { + Object::Function(params, stmts, env ) => { + let mut ev = Evaluator { + env: { + let scoped_env = env.clone(); + let list = params.iter().zip({ + match &args[1] { + Object::Array(arr) => arr, + _ => panic!() + } + }.iter()); + let mut scoped_env_mut = scoped_env.borrow_mut(); + for (_, (ident, o)) in list.enumerate() { + let ast::Ident(name) = ident.clone(); + scoped_env_mut.set(name, o.clone()); + } + scoped_env.clone() + }, + }; + ev.eval(&stmts); }, - }; - ev.eval(&stmts); + _ => panic!() + } }); rt.block_on(local_set); diff --git a/interpreter/src/evaluator/object.rs b/interpreter/src/evaluator/object.rs index 3a0234d..d8fd6f7 100644 --- a/interpreter/src/evaluator/object.rs +++ b/interpreter/src/evaluator/object.rs @@ -37,6 +37,8 @@ pub enum Object { Native(Box), } +unsafe impl Send for Object {} + /// This is actually repr impl fmt::Display for Object { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {