From fe163ef65358e1079a247ac2d52c702b04eab8fa Mon Sep 17 00:00:00 2001 From: MeloAlright Date: Tue, 5 Nov 2024 01:58:09 +0800 Subject: [PATCH] 0_6_4 (#51) * Threading safe and unsafe (#50) * 0.6.4 --- .github/workflows/Threading.yml | 80 +++++++++++++++++++++++++-- Cargo.lock | 4 +- Cargo.toml | 4 +- interpreter/Cargo.toml | 2 +- interpreter/src/evaluator/builtins.rs | 80 +++++++++++---------------- interpreter/src/evaluator/object.rs | 2 + 6 files changed, 114 insertions(+), 58 deletions(-) diff --git a/.github/workflows/Threading.yml b/.github/workflows/Threading.yml index 28c15c7..6fb1378 100644 --- a/.github/workflows/Threading.yml +++ b/.github/workflows/Threading.yml @@ -24,8 +24,80 @@ jobs: run: | cargo build --features="repl" --release - - name: Threading + - name: Thread Safe (still unsafe) run: | - ./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(秘密研究)' + ./target/release/3body -c ' + 给 cx 以 程心(); + 给 掩体工程进展 以 0; + 给 研制曲率飞船进展 以 0; + + cx.thread(法则() { + 给 掩体纪年 以 0; + 面壁 (掩体纪年 <= 11) { + 冬眠(1000); + 广播(["掩体工程进展", 掩体工程进展]); + 掩体工程进展 = 掩体工程进展 + 1; + 掩体纪年 = 掩体纪年 + 1; + } + }, []) + + 冬眠(5000) + + cx.thread(法则() { + 给 掩体纪年 以 5; + 面壁 (掩体纪年 <= 11) { + 冬眠(1000); + 广播(["研制曲率飞船进展", 研制曲率飞船进展]); + 研制曲率飞船进展 = 研制曲率飞船进展 + 1; + 掩体纪年 = 掩体纪年 + 1; + } + }, []) + + 冬眠(6000)' + + + ./target/release/3body -c ' + 给 cx 以 程心(); + + 给 星环公司 以 法则(name, y, limit) { + 给 掩体纪年 以 y; + 面壁 (掩体纪年 <= limit) { + 冬眠(1000); + 广播([name, 掩体纪年]); + 掩体纪年 = 掩体纪年 + 1; + } + } + + 给 秘密研究 以 cx.thread(星环公司, ["重启光速飞船的研究", 11, 66]) + cx.join(秘密研究)' + + - name: Thread Unsafe + run: | + ./target/release/3body -c ' + 给 cx 以 程心(); + 给 总进展 以 0; + + cx.thread(法则() { + 给 掩体纪年 以 0; + 面壁 (掩体纪年 <= 11) { + 冬眠(1000); + 广播(["总进展", 总进展]); + 总进展 = 总进展 + 1; // unsafe + 掩体纪年 = 掩体纪年 + 1; + } + }, []) + + 冬眠(5000) + + cx.thread(法则() { + 给 掩体纪年 以 5; + 面壁 (掩体纪年 <= 11) { + 冬眠(1000); + 广播(["总进展", 总进展]); + 总进展 = 总进展 + 1; // unsafe + 掩体纪年 = 掩体纪年 + 1; + } + }, []) + + 冬眠(6000) + 广播([总进展])' \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index b67f0b3..558717a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -776,7 +776,7 @@ dependencies = [ [[package]] name = "three_body_interpreter" -version = "0.6.3" +version = "0.6.4" dependencies = [ "llm", "llm-base", @@ -788,7 +788,7 @@ dependencies = [ [[package]] name = "three_body_lang" -version = "0.6.3" +version = "0.6.4" dependencies = [ "rustyline", "rustyline-derive", diff --git a/Cargo.toml b/Cargo.toml index 7cfcba7..e9fa2df 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "three_body_lang" description = "Three Body Language written in Rust" edition = "2021" -version = "0.6.3" +version = "0.6.4" authors = ["meloalright", "rustq"] license = "MIT" @@ -12,7 +12,7 @@ members = ["interpreter"] [dependencies] rustyline = { version = "12.0.0", optional = true } rustyline-derive = { version = "0.4.0", optional = true } -three_body_interpreter = { version = "0.6.3", path = "./interpreter", features = ["sophon", "threading"] } +three_body_interpreter = { version = "0.6.4", path = "./interpreter", features = ["sophon", "threading"] } [[bin]] name = "3body" diff --git a/interpreter/Cargo.toml b/interpreter/Cargo.toml index 46aca3f..b58a5ce 100644 --- a/interpreter/Cargo.toml +++ b/interpreter/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "three_body_interpreter" description = "Three Body interpreter" -version = "0.6.3" +version = "0.6.4" edition = "2021" authors = ["meloalright", "rustq"] license = "MIT" diff --git a/interpreter/src/evaluator/builtins.rs b/interpreter/src/evaluator/builtins.rs index e850700..07777f5 100644 --- a/interpreter/src/evaluator/builtins.rs +++ b/interpreter/src/evaluator/builtins.rs @@ -351,66 +351,48 @@ 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 || { - let local_set = tokio::task::LocalSet::new(); - let rt = tokio::runtime::Builder::new_current_thread() - .enable_all() - .build() - .unwrap(); - - local_set.spawn_local(async move { + let handle = std::thread::spawn(|| { + let local_set = tokio::task::LocalSet::new(); + let rt = tokio::runtime::Builder::new_current_thread() + .enable_all() + .build() + .unwrap(); + + local_set.spawn_local(async move { + match &args[0] { + Object::Function(params, stmts, env ) => { 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()); + let mut scoped_env = Env::new_with_outer(Rc::clone(&env)); // still thread unsafe + let list = params.iter().zip({ + match &args[1] { + Object::Array(arr) => arr, + _ => panic!() + } + }.iter()); + for (_, (ident, o)) in list.enumerate() { + let ast::Ident(name) = ident.clone(); + scoped_env.set(name, o.clone()); } - - scoped_env + Rc::new(RefCell::new(scoped_env)) }, }; ev.eval(&stmts); - }); + }, + _ => panic!() + } + }); - rt.block_on(local_set); - }); + rt.block_on(local_set); + }); - let handle = Box::leak(Box::new(handle)); - let handle_ptr = &mut *handle as *mut std::thread::JoinHandle<()>; - Object::Native(Box::new(NativeObject::Thread(handle_ptr))) - } - _ => panic!() - } + let handle = Box::leak(Box::new(handle)); + let handle_ptr = &mut *handle as *mut std::thread::JoinHandle<()>; + Object::Native(Box::new(NativeObject::Thread(handle_ptr))) } session_hash.insert(Object::String("thread".to_owned()), Object::Builtin(2, three_body_thread_new)); } 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 {