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

View file

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