Skip to content

Commit 0a731d2

Browse files
committed
Use ThreadPool for dbus
The `zbus` blocking API just wraps the async API with `block_on`, so we may as well use our own executor.
1 parent 42edf6a commit 0a731d2

File tree

5 files changed

+26
-29
lines changed

5 files changed

+26
-29
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ clap_lex = "0.7"
9090
parking_lot = "0.12.4"
9191
logind-zbus = { version = "5.3.2", optional = true }
9292
futures-executor = { version = "0.3.31", features = ["thread-pool"] }
93+
futures-util = "0.3.31"
9394

9495
[dependencies.id_tree]
9596
branch = "feature/copy_clone"

src/dbus/mod.rs

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ use crate::{
55
use anyhow::{Context, Result};
66
use calloop::{InsertError, LoopHandle, RegistrationToken};
77
use cosmic_comp_config::output::comp::OutputState;
8+
use futures_executor::{block_on, ThreadPool};
9+
use futures_util::stream::StreamExt;
810
use std::collections::HashMap;
911
use tracing::{error, warn};
1012
use zbus::blocking::{fdo::DBusProxy, Connection};
@@ -13,10 +15,13 @@ use zbus::blocking::{fdo::DBusProxy, Connection};
1315
pub mod logind;
1416
mod power;
1517

16-
pub fn init(evlh: &LoopHandle<'static, State>) -> Result<Vec<RegistrationToken>> {
18+
pub fn init(
19+
evlh: &LoopHandle<'static, State>,
20+
executor: &ThreadPool,
21+
) -> Result<Vec<RegistrationToken>> {
1722
let mut tokens = Vec::new();
1823

19-
match power::init() {
24+
match block_on(power::init()) {
2025
Ok(power_daemon) => {
2126
let (tx, rx) = calloop::channel::channel();
2227

@@ -58,29 +63,17 @@ pub fn init(evlh: &LoopHandle<'static, State>) -> Result<Vec<RegistrationToken>>
5863
.with_context(|| "Failed to add channel to event_loop")?;
5964

6065
// start helper thread
61-
let result = std::thread::Builder::new()
62-
.name("system76-power-hotplug".to_string())
63-
.spawn(move || {
64-
if let Ok(mut msg_iter) = power_daemon.receive_hot_plug_detect() {
65-
while let Some(msg) = msg_iter.next() {
66-
if tx.send(msg).is_err() {
67-
break;
68-
}
66+
executor.spawn_ok(async move {
67+
if let Ok(mut msg_iter) = power_daemon.receive_hot_plug_detect().await {
68+
while let Some(msg) = msg_iter.next().await {
69+
if tx.send(msg).is_err() {
70+
break;
6971
}
7072
}
71-
})
72-
.with_context(|| "Failed to start helper thread");
73-
74-
match result {
75-
Ok(_handle) => {
76-
tokens.push(token);
77-
// detach thread
7873
}
79-
Err(err) => {
80-
evlh.remove(token);
81-
return Err(err);
82-
}
83-
}
74+
});
75+
76+
tokens.push(token);
8477
}
8578
Err(err) => {
8679
tracing::info!(?err, "Failed to connect to com.system76.PowerDaemon");

src/dbus/power.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
//!
1919
//! …consequently `zbus-xmlgen` did not generate code for the above interfaces.
2020
21-
use zbus::blocking::Connection;
21+
use zbus::Connection;
2222

2323
#[zbus::proxy(
2424
interface = "com.system76.PowerDaemon",
@@ -79,9 +79,9 @@ pub trait PowerDaemon {
7979
fn power_profile_switch(&self, profile: &str) -> zbus::Result<()>;
8080
}
8181

82-
pub fn init() -> anyhow::Result<PowerDaemonProxyBlocking<'static>> {
83-
let conn = Connection::system()?;
84-
let proxy = PowerDaemonProxyBlocking::new(&conn)?;
85-
proxy.0.introspect()?;
82+
pub async fn init() -> anyhow::Result<PowerDaemonProxy<'static>> {
83+
let conn = Connection::system().await?;
84+
let proxy = PowerDaemonProxy::new(&conn).await?;
85+
proxy.0.introspect().await?;
8686
Ok(proxy)
8787
}

src/state.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,9 @@ impl State {
683683
);
684684
let workspace_state = WorkspaceState::new(dh, client_is_privileged);
685685

686-
if let Err(err) = crate::dbus::init(&handle) {
686+
let async_executor = ThreadPool::builder().pool_size(1).create().unwrap();
687+
688+
if let Err(err) = crate::dbus::init(&handle, &async_executor) {
687689
tracing::warn!(?err, "Failed to initialize dbus handlers");
688690
}
689691

@@ -699,7 +701,7 @@ impl State {
699701
display_handle: dh.clone(),
700702
event_loop_handle: handle,
701703
event_loop_signal: signal,
702-
async_executor: ThreadPool::builder().pool_size(1).create().unwrap(),
704+
async_executor,
703705

704706
popups: PopupManager::default(),
705707
shell,

0 commit comments

Comments
 (0)