dbus: Use ThreadPool instead of zbus blocking API

The `zbus` blocking API just wraps the async API with `block_on`, so we
may as well use our own executor.
This commit is contained in:
Ian Douglas Scott 2025-06-17 17:50:30 -07:00 committed by Ian Douglas Scott
parent 8455869439
commit 55401b1e53
5 changed files with 26 additions and 29 deletions

1
Cargo.lock generated
View file

@ -834,6 +834,7 @@ dependencies = [
"egui", "egui",
"egui_plot", "egui_plot",
"futures-executor", "futures-executor",
"futures-util",
"i18n-embed", "i18n-embed",
"i18n-embed-fl", "i18n-embed-fl",
"iced_tiny_skia", "iced_tiny_skia",

View file

@ -88,6 +88,7 @@ clap_lex = "0.7"
parking_lot = "0.12.5" parking_lot = "0.12.5"
logind-zbus = { version = "5.3.2", optional = true } logind-zbus = { version = "5.3.2", optional = true }
futures-executor = { version = "0.3.31", features = ["thread-pool"] } futures-executor = { version = "0.3.31", features = ["thread-pool"] }
futures-util = "0.3.31"
[dependencies.id_tree] [dependencies.id_tree]
branch = "feature/copy_clone" branch = "feature/copy_clone"

View file

@ -5,6 +5,8 @@ use crate::{
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use calloop::{InsertError, LoopHandle, RegistrationToken}; use calloop::{InsertError, LoopHandle, RegistrationToken};
use cosmic_comp_config::output::comp::OutputState; use cosmic_comp_config::output::comp::OutputState;
use futures_executor::{ThreadPool, block_on};
use futures_util::stream::StreamExt;
use std::collections::HashMap; use std::collections::HashMap;
use tracing::{error, warn}; use tracing::{error, warn};
use zbus::blocking::{Connection, fdo::DBusProxy}; use zbus::blocking::{Connection, fdo::DBusProxy};
@ -13,10 +15,13 @@ use zbus::blocking::{Connection, fdo::DBusProxy};
pub mod logind; pub mod logind;
mod power; mod power;
pub fn init(evlh: &LoopHandle<'static, State>) -> Result<Vec<RegistrationToken>> { pub fn init(
evlh: &LoopHandle<'static, State>,
executor: &ThreadPool,
) -> Result<Vec<RegistrationToken>> {
let mut tokens = Vec::new(); let mut tokens = Vec::new();
match power::init() { match block_on(power::init()) {
Ok(power_daemon) => { Ok(power_daemon) => {
let (tx, rx) = calloop::channel::channel(); let (tx, rx) = calloop::channel::channel();
@ -56,29 +61,17 @@ pub fn init(evlh: &LoopHandle<'static, State>) -> Result<Vec<RegistrationToken>>
.with_context(|| "Failed to add channel to event_loop")?; .with_context(|| "Failed to add channel to event_loop")?;
// start helper thread // start helper thread
let result = std::thread::Builder::new() executor.spawn_ok(async move {
.name("system76-power-hotplug".to_string()) if let Ok(mut msg_iter) = power_daemon.receive_hot_plug_detect().await {
.spawn(move || { while let Some(msg) = msg_iter.next().await {
if let Ok(msg_iter) = power_daemon.receive_hot_plug_detect() { if tx.send(msg).is_err() {
for msg in msg_iter { break;
if tx.send(msg).is_err() {
break;
}
} }
} }
}) }
.with_context(|| "Failed to start helper thread"); });
match result { tokens.push(token);
Ok(_handle) => {
tokens.push(token);
// detach thread
}
Err(err) => {
evlh.remove(token);
return Err(err);
}
}
} }
Err(err) => { Err(err) => {
tracing::info!(?err, "Failed to connect to com.system76.PowerDaemon"); tracing::info!(?err, "Failed to connect to com.system76.PowerDaemon");

View file

@ -18,7 +18,7 @@
//! //!
//! …consequently `zbus-xmlgen` did not generate code for the above interfaces. //! …consequently `zbus-xmlgen` did not generate code for the above interfaces.
use zbus::blocking::Connection; use zbus::Connection;
#[zbus::proxy( #[zbus::proxy(
interface = "com.system76.PowerDaemon", interface = "com.system76.PowerDaemon",
@ -79,9 +79,9 @@ pub trait PowerDaemon {
fn power_profile_switch(&self, profile: &str) -> zbus::Result<()>; fn power_profile_switch(&self, profile: &str) -> zbus::Result<()>;
} }
pub fn init() -> anyhow::Result<PowerDaemonProxyBlocking<'static>> { pub async fn init() -> anyhow::Result<PowerDaemonProxy<'static>> {
let conn = Connection::system()?; let conn = Connection::system().await?;
let proxy = PowerDaemonProxyBlocking::new(&conn)?; let proxy = PowerDaemonProxy::new(&conn).await?;
proxy.0.introspect()?; proxy.0.introspect().await?;
Ok(proxy) Ok(proxy)
} }

View file

@ -696,7 +696,9 @@ impl State {
); );
let workspace_state = WorkspaceState::new(dh, client_not_sandboxed); let workspace_state = WorkspaceState::new(dh, client_not_sandboxed);
if let Err(err) = crate::dbus::init(&handle) { let async_executor = ThreadPool::builder().pool_size(1).create().unwrap();
if let Err(err) = crate::dbus::init(&handle, &async_executor) {
tracing::warn!(?err, "Failed to initialize dbus handlers"); tracing::warn!(?err, "Failed to initialize dbus handlers");
} }
@ -712,7 +714,7 @@ impl State {
display_handle: dh.clone(), display_handle: dh.clone(),
event_loop_handle: handle, event_loop_handle: handle,
event_loop_signal: signal, event_loop_signal: signal,
async_executor: ThreadPool::builder().pool_size(1).create().unwrap(), async_executor,
popups: PopupManager::default(), popups: PopupManager::default(),
shell, shell,