Skip to content

Commit

Permalink
feat: make threading can work with join
Browse files Browse the repository at this point in the history
  • Loading branch information
meloalright committed Oct 7, 2024
1 parent 3f28886 commit ea21a1a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 47 deletions.
101 changes: 54 additions & 47 deletions interpreter/src/evaluator/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ pub fn new_builtins() -> HashMap<String, Object> {
);
#[cfg(feature="threading")] // threading
builtins.insert(
String::from("饱和式救援"),
Object::Builtin(1, three_body_threading),
String::from("程心"),
Object::Builtin(0, three_body_threading),
);
builtins
}
Expand Down Expand Up @@ -361,56 +361,63 @@ fn eval(input: &str) -> Option<Object> {

#[cfg(feature="threading")]
fn three_body_threading(args: Vec<Object>) -> Object {
match &args[0] {
Object::Int(o) => {
async fn local_task(id: i64) {
println!("Local task {} is running!", id);
tokio::time::sleep(Duration::from_secs(1)).await;
println!("Local task {} completed!", id);
let mut session_hash = HashMap::new();
{
fn three_body_thread_new(args: Vec<Object>) -> Object {
match &args[0] {
Object::String(input) => {
let input = (*input).clone();

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();

// 在 LocalSet 中安排任务
local_set.spawn_local(async move { eval(&input) });

// 运行 LocalSet 直到其中的任务完成
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!()
}
}
session_hash.insert(Object::String("thread".to_owned()), Object::Builtin(1, three_body_thread_new));
}

let o = (*o).clone();

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();

// 在 LocalSet 中安排任务
local_set.spawn_local(local_task(o));

// 运行 LocalSet 直到其中的任务完成
rt.block_on(local_set);
});
Object::Null
},
Object::String(input) => {
// async fn local_task(stmt: &BlockStmt) {
// println!("Local task {} is running!", id);
// tokio::time::sleep(Duration::from_secs(1)).await;
// // println!("Local task {} completed!", id);
// }
let input = (*input).clone();

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();

// 在 LocalSet 中安排任务
local_set.spawn_local(async move { eval(&input) });

// 运行 LocalSet 直到其中的任务完成
rt.block_on(local_set);
});
Object::Null
},
_ => Object::Null,
{
fn three_body_thread_join(args: Vec<Object>) -> Object {
match &args[0] {
Object::Native(ptr) => {
let handle_ptr = match **ptr {
NativeObject::Thread(handle_ptr) => {
handle_ptr.clone()
}
_ => panic!()
};
// let model = unsafe { & *model_ptr };
unsafe { Box::from_raw(handle_ptr) }.join();
// std::mem::drop(model);
Object::Null
},
_ => panic!()
}
}
session_hash.insert(Object::String("join".to_owned()), Object::Builtin(1, three_body_thread_join));
}


Object::Hash(session_hash)

}

#[cfg(test)]
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 @@ -16,6 +16,8 @@ pub type BuiltinFunc = fn(Vec<Object>) -> Object;
pub enum NativeObject {
#[cfg(feature="sophon")]
LLMModel(*mut dyn llm::Model),
#[cfg(feature="threading")]
Thread(*mut std::thread::JoinHandle<()>),
}

#[derive(PartialEq, Clone, Debug)]
Expand Down

0 comments on commit ea21a1a

Please sign in to comment.