dont spawn a runtime in machine; inherit from outside with runtime::handle::Current

This commit is contained in:
Joshua Parkin
2023-08-03 16:22:03 +01:00
parent 3ff02da314
commit 65eb93793c
4 changed files with 24 additions and 19 deletions

View File

@@ -7,6 +7,13 @@ fn main() {
scryer_prolog::machine::INTERRUPT.store(true, Ordering::Relaxed); scryer_prolog::machine::INTERRUPT.store(true, Ordering::Relaxed);
}).unwrap(); }).unwrap();
let mut wam = machine::Machine::new(Default::default()); let runtime = tokio::runtime::Builder::new_multi_thread()
wam.run_top_level(atom!("$toplevel"), (atom!("$repl"), 1)); .enable_all()
.build()
.unwrap();
runtime.block_on(async move {
let mut wam = machine::Machine::new(Default::default());
wam.run_top_level(atom!("$toplevel"), (atom!("$repl"), 1));
});
} }

View File

@@ -69,8 +69,8 @@ mod tests {
use super::*; use super::*;
use crate::machine::{QueryMatch, Value, QueryResolution}; use crate::machine::{QueryMatch, Value, QueryResolution};
#[test] #[tokio::test]
fn programatic_query() { async fn programatic_query() {
let mut machine = Machine::new_lib(); let mut machine = Machine::new_lib();
machine.load_module_string( machine.load_module_string(
@@ -108,8 +108,8 @@ mod tests {
); );
} }
#[test] #[tokio::test]
fn failing_query() { async fn failing_query() {
let mut machine = Machine::new_lib(); let mut machine = Machine::new_lib();
let query = String::from(r#"triple("a",P,"b")."#); let query = String::from(r#"triple("a",P,"b")."#);
let output = machine.run_query(query); let output = machine.run_query(query);
@@ -119,8 +119,8 @@ mod tests {
); );
} }
#[test] #[tokio::test]
fn complex_results() { async fn complex_results() {
let mut machine = Machine::new_lib(); let mut machine = Machine::new_lib();
machine.load_module_string( machine.load_module_string(
"facts", "facts",
@@ -172,8 +172,8 @@ mod tests {
} }
#[test] #[tokio::test]
fn consult() { async fn consult() {
let mut machine = Machine::new_lib(); let mut machine = Machine::new_lib();
machine.consult_module_string( machine.consult_module_string(

View File

@@ -53,7 +53,6 @@ use std::env;
use std::io::Read; use std::io::Read;
use std::path::PathBuf; use std::path::PathBuf;
use std::sync::atomic::AtomicBool; use std::sync::atomic::AtomicBool;
use tokio::runtime::Runtime;
use self::config::MachineConfig; use self::config::MachineConfig;
use self::parsed_results::*; use self::parsed_results::*;
@@ -71,7 +70,6 @@ pub struct Machine {
pub(super) user_output: Stream, pub(super) user_output: Stream,
pub(super) user_error: Stream, pub(super) user_error: Stream,
pub(super) load_contexts: Vec<LoadContext>, pub(super) load_contexts: Vec<LoadContext>,
pub(super) runtime: Runtime,
} }
#[derive(Debug)] #[derive(Debug)]
@@ -452,8 +450,6 @@ impl Machine {
), ),
}; };
let runtime = tokio::runtime::Runtime::new().unwrap();
let mut wam = Machine { let mut wam = Machine {
machine_st, machine_st,
indices: IndexStore::new(), indices: IndexStore::new(),
@@ -462,7 +458,6 @@ impl Machine {
user_output, user_output,
user_error, user_error,
load_contexts: vec![], load_contexts: vec![],
runtime,
}; };
let mut lib_path = current_dir(); let mut lib_path = current_dir();

View File

@@ -3864,7 +3864,8 @@ impl Machine {
let address_string = address_sink.as_str(); //to_string(); let address_string = address_sink.as_str(); //to_string();
let address: Uri = address_string.parse().unwrap(); let address: Uri = address_string.parse().unwrap();
let stream = self.runtime.block_on(async { let runtime = tokio::runtime::Handle::current();
let stream = runtime.block_on(async {
let https = HttpsConnector::new(); let https = HttpsConnector::new();
let client = Client::builder() let client = Client::builder()
.build::<_, hyper::Body>(https); .build::<_, hyper::Body>(https);
@@ -3945,7 +3946,8 @@ impl Machine {
let (tx, rx) = channel(1); let (tx, rx) = channel(1);
let tx = Arc::new(Mutex::new(tx)); let tx = Arc::new(Mutex::new(tx));
let _guard = self.runtime.enter(); let runtime = tokio::runtime::Handle::current();
let _guard = runtime.enter();
let server = match Server::try_bind(&addr) { let server = match Server::try_bind(&addr) {
Ok(server) => server, Ok(server) => server,
Err(_) => { Err(_) => {
@@ -3953,7 +3955,7 @@ impl Machine {
} }
}; };
self.runtime.spawn(async move { runtime.spawn(async move {
let make_svc = make_service_fn(move |_conn| { let make_svc = make_service_fn(move |_conn| {
let tx = tx.clone(); let tx = tx.clone();
async move { Ok::<_, Infallible>(service_fn(move |req| http::serve_req(req, tx.clone()))) } async move { Ok::<_, Infallible>(service_fn(move |req| http::serve_req(req, tx.clone()))) }
@@ -4016,7 +4018,8 @@ impl Machine {
let query_cell = string_as_cstr_cell!(query_atom); let query_cell = string_as_cstr_cell!(query_atom);
let hyper_req = request.request; let hyper_req = request.request;
let buf = self.runtime.block_on(async {hyper::body::aggregate(hyper_req).await.unwrap()}); let runtime = tokio::runtime::Handle::current();
let buf = runtime.block_on(async {hyper::body::aggregate(hyper_req).await.unwrap()});
let reader = buf.reader(); let reader = buf.reader();
let mut stream = Stream::from_http_stream( let mut stream = Stream::from_http_stream(