Skip to content

Commit

Permalink
feat: try set up the threading safe and unsafe for 3body threading
Browse files Browse the repository at this point in the history
  • Loading branch information
meloalright committed Nov 4, 2024
1 parent 23d0721 commit 6f97356
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 37 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/Threading.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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; 广播(掩体纪年); }'
62 changes: 25 additions & 37 deletions interpreter/src/evaluator/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,55 +351,43 @@ fn three_body_sophon_engineering(args: Vec<Object>) -> Object {


#[cfg(feature="threading")]
fn three_body_threading(args: Vec<Object>) -> Object {
fn three_body_threading(_: Vec<Object>) -> Object {
let mut session_hash = HashMap::new();
{
fn three_body_thread_new(args: Vec<Object>) -> Object {
match &args[0] {
Object::Function(params, ast, env ) => {

let stmts = ast.clone();
let params = params.clone();

let literals: Vec<crate::ast::Literal> = 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()
.build()
.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);
Expand Down
2 changes: 2 additions & 0 deletions interpreter/src/evaluator/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ pub enum Object {
Native(Box<NativeObject>),
}

unsafe impl Send for Object {}

/// This is actually repr
impl fmt::Display for Object {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Expand Down

0 comments on commit 6f97356

Please sign in to comment.