From dcc9de9230f3de8735357cc668208f3f48ff423d Mon Sep 17 00:00:00 2001 From: DashieTM Date: Wed, 5 Jun 2024 00:43:23 +0200 Subject: [PATCH] startup: Add spinloop until deamon is ready --- Cargo.lock | 5 ++--- Cargo.toml | 3 ++- src/main.rs | 16 ++++++++-------- src/tests.rs | 15 +++++++++++---- 4 files changed, 23 insertions(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1a5bc88..93534f7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -968,9 +968,8 @@ dependencies = [ [[package]] name = "reset_daemon" -version = "2.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8cf329c1f14918f24d8d8f1674a16f5103208682f97b5d2bf1621b09c563c9cb" +version = "2.1.1" +source = "git+https://github.com/Xetibo/ReSet-Daemon?branch=dev#ea5e866068fa991a1ca05c42c0707b93f8701413" dependencies = [ "crossbeam", "dbus", diff --git a/Cargo.toml b/Cargo.toml index f3a9f30..0286389 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,8 @@ repository = "https://github.com/Xetibo/ReSet" license = "GPL-3.0-or-later" [dependencies] -reset_daemon = "2.1.0" +# reset_daemon = "2.1.1" +reset_daemon = { git = "https://github.com/Xetibo/ReSet-Daemon", branch = "dev" } re_set-lib = "5.2.1" # re_set-lib = { git = "https://github.com/Xetibo/ReSet-Lib" } adw = { version = "0.6.0", package = "libadwaita", features = ["v1_4"] } diff --git a/src/main.rs b/src/main.rs index 4b9cfdd..d1f6f63 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,7 +24,11 @@ pub const VERSION: &str = env!("CARGO_PKG_VERSION"); #[tokio::main] async fn main() { - tokio::task::spawn(daemon_check()); + let ready = Arc::new(AtomicBool::new(false)); + tokio::task::spawn(daemon_check(ready.clone())); + while !ready.load(std::sync::atomic::Ordering::SeqCst) { + hint::spin_loop(); + } gio::resources_register_include!("src.templates.gresource") .expect("Failed to register resources."); gio::resources_register_include!("src.icons.gresource").expect("Failed to register resources."); @@ -67,19 +71,15 @@ fn shutdown(_: &Application) { }); } -async fn daemon_check() { - let handle = thread::spawn(|| { +async fn daemon_check(ready: Arc) { + let handle = thread::spawn(move || { let conn = Connection::new_session().unwrap(); let proxy = conn.with_proxy(BASE, DBUS_PATH, Duration::from_millis(100)); let res: Result<(), Error> = proxy.method_call(BASE, "RegisterClient", ("ReSet",)); res }); - let ready = Arc::new(AtomicBool::new(false)); let res = handle.join(); if res.unwrap().is_err() { - run_daemon(Some(ready.clone())).await; - } - while !ready.load(std::sync::atomic::Ordering::SeqCst) { - hint::spin_loop(); + run_daemon(Some(ready)).await; } } diff --git a/src/tests.rs b/src/tests.rs index 83b6e88..12dadc6 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -4,15 +4,22 @@ async fn test_plugins() { use crate::daemon_check; use re_set_lib::utils::plugin::plugin_tests; use re_set_lib::utils::plugin_setup::FRONTEND_PLUGINS; - use std::thread; - use std::time::Duration; - tokio::task::spawn(daemon_check()); - thread::sleep(Duration::from_millis(2000)); + use std::hint; + use std::sync::atomic::AtomicBool; + use std::sync::Arc; + let ready = Arc::new(AtomicBool::new(false)); + let rc = tokio::runtime::Runtime::new().expect("Failed to create runtime"); + rc.spawn(daemon_check(ready.clone())); + while !ready.load(std::sync::atomic::Ordering::SeqCst) { + hint::spin_loop(); + } unsafe { + println!("pang"); for plugin in FRONTEND_PLUGINS.iter() { let name = (plugin.frontend_name)(); let tests = (plugin.frontend_tests)(); plugin_tests(name, tests); } } + rc.shutdown_background(); }