Skip to content

Commit

Permalink
Added async support
Browse files Browse the repository at this point in the history
  • Loading branch information
bvssvni committed Feb 28, 2024
1 parent 6c871fc commit 55c1881
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 10 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ read_token = "1.0.0"
lazy_static = "1.0.0"
vecmath = "1.0.0"
advancedresearch-tree_mem_sort = "0.2.0"
tokio = {version = "1.34.0", features = ["full"], optional = true}

[target.'cfg(not(target_family = "wasm"))'.dependencies.reqwest]
version = "0.9.22"
Expand All @@ -43,3 +44,4 @@ http = ["reqwest"]
file = []
threading = []
stdio = []
async = ["tokio"]
2 changes: 1 addition & 1 deletion src/dyon_std/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1774,7 +1774,7 @@ pub(crate) fn join__thread(rt: &mut Runtime) -> Result<Variable, String> {
let handle_res = Thread::invalidate_handle(rt, thread);
Ok(Variable::Result({
match handle_res {
Ok(handle) => match handle.join() {
Ok(handle) => match join!(rt.tokio_runtime, handle) {
Ok(res) => match res {
Ok(res) => Ok(Box::new(res)),
Err(err) => Err(Box::new(Error {
Expand Down
11 changes: 5 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use std::collections::HashMap;
use std::fmt;
use std::sync::{Arc, Mutex};
#[cfg(all(not(target_family = "wasm"), feature = "threading"))]
use std::thread::JoinHandle;
use threading::JoinHandle;

pub mod ast;
pub mod embed;
Expand All @@ -40,6 +40,8 @@ mod mat4;
mod module;
mod prelude;
pub mod runtime;
#[cfg(all(not(target_family = "wasm"), feature = "threading"))]
pub mod threading;
mod ty;
mod vec4;
mod write;
Expand Down Expand Up @@ -623,10 +625,7 @@ pub(crate) fn check_str(
/// - module - The module to load the source
pub fn load_str(source: &str, d: Arc<String>, module: &mut Module) -> Result<(), String> {
#[cfg(all(not(target_family = "wasm"), feature = "threading"))]
use std::thread;

#[cfg(all(not(target_family = "wasm"), feature = "threading"))]
struct MaybeThread<T>(JoinHandle<T>);
struct MaybeThread<T>(std::thread::JoinHandle<T>);

#[cfg(all(not(target_family = "wasm"), feature = "threading"))]
impl<T> MaybeThread<T>
Expand All @@ -637,7 +636,7 @@ pub fn load_str(source: &str, d: Arc<String>, module: &mut Module) -> Result<(),
where
F: FnOnce() -> T + Send + 'static,
{
Self(thread::spawn(func))
Self(std::thread::spawn(func))
}
fn join(self) -> T {
self.0.join().unwrap()
Expand Down
14 changes: 11 additions & 3 deletions src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ pub struct Runtime {
pub module_resolver: fn(source: &str, target: &mut String) -> Result<(), String>,
/// External functions can choose to report an error on an argument.
pub arg_err_index: Cell<Option<usize>>,
/// Tokio runtime handle.
#[cfg(feature = "async")]
pub tokio_runtime: Arc<tokio::runtime::Runtime>,
}

impl Default for Runtime {
Expand Down Expand Up @@ -377,6 +380,8 @@ impl Runtime {
#[cfg(feature = "dynload")]
module_resolver: file_resolve_module,
arg_err_index: Cell::new(None),
#[cfg(feature = "async")]
tokio_runtime: Arc::new(tokio::runtime::Runtime::new().unwrap()),
}
}

Expand Down Expand Up @@ -952,8 +957,9 @@ impl Runtime {
/// Start a new thread and return the handle.
#[cfg(all(not(target_family = "wasm"), feature = "threading"))]
pub fn go(&mut self, go: &ast::Go) -> FlowResult {
use std::thread::{self, JoinHandle};
use crate::Thread;
use crate::threading::JoinHandle;
use crate::spawn;

let n = go.call.args.len();
let mut stack = vec![];
Expand Down Expand Up @@ -1007,8 +1013,10 @@ impl Runtime {
}],
rng: self.rng.clone(),
arg_err_index: Cell::new(None),
#[cfg(feature = "async")]
tokio_runtime: self.tokio_runtime.clone(),
};
let handle: JoinHandle<Result<Variable, String>> = thread::spawn(move || {
let handle: JoinHandle<Result<Variable, String>> = spawn!(self.tokio_runtime,
let mut new_rt = new_rt;
let fake_call = fake_call;
let loader = false;
Expand All @@ -1018,7 +1026,7 @@ impl Runtime {
Ok((Some(x), _)) => x,
}
.deep_clone(&new_rt.stack))
});
);
Ok((Some(Variable::Thread(Thread::new(handle))), Flow::Continue))
}

Expand Down
47 changes: 47 additions & 0 deletions src/threading.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//! Wrapper code for co-routines.

#[cfg(not(feature = "async"))]
pub use std::thread::JoinHandle;

#[cfg(feature = "async")]
pub use tokio::task::JoinHandle;

/// Spawns new thread.
#[cfg(not(feature = "async"))]
#[macro_export]
macro_rules! spawn {
($rt:expr, $($e:tt)*) => {
std::thread::spawn(move || {
$($e)*
})
};
}

/// Spawn new thread.
#[cfg(feature = "async")]
#[macro_export]
macro_rules! spawn {
($rt:expr, $($e:tt)*) => {
$rt.spawn(async move {
$($e)*
})
};
}

/// Joins thread.
#[cfg(not(feature = "async"))]
#[macro_export]
macro_rules! join {
($rt:expr, $e:expr) => {
$e.join()
}
}

/// Joins thread.
#[cfg(feature = "async")]
#[macro_export]
macro_rules! join {
($rt:expr, $e:expr) => {
$rt.block_on($e)
}
}

0 comments on commit 55c1881

Please sign in to comment.