]> Repositorios git - scryer-prolog.git/commitdiff
dont spawn a runtime in machine; inherit from outside with runtime::handle::Current
authorJoshua Parkin <[email protected]>
Thu, 3 Aug 2023 15:22:03 +0000 (16:22 +0100)
committerJoshua Parkin <[email protected]>
Thu, 3 Aug 2023 15:22:03 +0000 (16:22 +0100)
src/bin/scryer-prolog.rs
src/machine/lib_machine.rs
src/machine/mod.rs
src/machine/system_calls.rs

index f1f0b705dba77e5de3733680166a84eaab645a47..4dcc05c26bf9718a28f5d97774de9e3e1d118c62 100644 (file)
@@ -7,6 +7,13 @@ fn main() {
         scryer_prolog::machine::INTERRUPT.store(true, Ordering::Relaxed);
     }).unwrap();
 
-    let mut wam = machine::Machine::new(Default::default());
-    wam.run_top_level(atom!("$toplevel"), (atom!("$repl"), 1));
+    let runtime = tokio::runtime::Builder::new_multi_thread()
+        .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));
+    });
 }
index 831ca2009d2f683550e0b9df1d1db2e3569cd99f..074731e9b4e65eacac6b063d3abc1f4dea942904 100644 (file)
@@ -69,8 +69,8 @@ mod tests {
     use super::*;
     use crate::machine::{QueryMatch, Value, QueryResolution};
 
-    #[test]
-    fn programatic_query() {
+    #[tokio::test]
+    async fn programatic_query() {
         let mut machine = Machine::new_lib();
 
         machine.load_module_string(
@@ -108,8 +108,8 @@ mod tests {
         );
     }
 
-    #[test]
-    fn failing_query() {
+    #[tokio::test]
+    async fn failing_query() {
         let mut machine = Machine::new_lib();
         let query = String::from(r#"triple("a",P,"b")."#);
         let output = machine.run_query(query);
@@ -119,8 +119,8 @@ mod tests {
         );
     }
 
-    #[test]
-    fn complex_results() {
+    #[tokio::test]
+    async fn complex_results() {
         let mut machine = Machine::new_lib();
         machine.load_module_string(
             "facts",
@@ -172,8 +172,8 @@ mod tests {
     }
 
 
-    #[test]
-    fn consult() {
+    #[tokio::test]
+    async fn consult() {
         let mut machine = Machine::new_lib();
 
         machine.consult_module_string(
index c77272c9023a1e8994f4734e4038804c3ee4a174..03ac6492519124e66175a1859bffc0cffa7302a0 100644 (file)
@@ -53,7 +53,6 @@ use std::env;
 use std::io::Read;
 use std::path::PathBuf;
 use std::sync::atomic::AtomicBool;
-use tokio::runtime::Runtime;
 
 use self::config::MachineConfig;
 use self::parsed_results::*;
@@ -71,7 +70,6 @@ pub struct Machine {
     pub(super) user_output: Stream,
     pub(super) user_error: Stream,
     pub(super) load_contexts: Vec<LoadContext>,
-    pub(super) runtime: Runtime,
 }
 
 #[derive(Debug)]
@@ -452,8 +450,6 @@ impl Machine {
             ),
         };
 
-        let runtime = tokio::runtime::Runtime::new().unwrap();
-
         let mut wam = Machine {
             machine_st,
             indices: IndexStore::new(),
@@ -462,7 +458,6 @@ impl Machine {
             user_output,
             user_error,
             load_contexts: vec![],
-            runtime,
         };
 
         let mut lib_path = current_dir();
index b154e266a591657356376cd515f72e343e719d78..1160c1cdb372a6e2a1f43a8c95247aa76899921e 100644 (file)
@@ -3864,7 +3864,8 @@ impl Machine {
             let address_string = address_sink.as_str(); //to_string();
             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 client = Client::builder()
                     .build::<_, hyper::Body>(https);
@@ -3945,7 +3946,8 @@ impl Machine {
            let (tx, rx) = channel(1);
            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) {
                Ok(server) => server,
                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 tx = 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 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 mut stream = Stream::from_http_stream(