Skip to content

Commit 8458808

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 49e2317 commit 8458808

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
@@ -67,6 +67,7 @@ reis = { version = "0.5", features = ["calloop"] }
6767
clap_lex = "0.7"
6868
parking_lot = "0.12.3"
6969
futures-executor = { version = "0.3.31", features = ["thread-pool"] }
70+
futures-util = "0.3.31"
7071

7172
[dependencies.id_tree]
7273
branch = "feature/copy_clone"

src/dbus/mod.rs

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
use crate::state::{BackendData, Common, State};
22
use anyhow::{Context, Result};
33
use calloop::{InsertError, LoopHandle, RegistrationToken};
4+
use futures_executor::{block_on, ThreadPool};
5+
use futures_util::stream::StreamExt;
46
use std::collections::HashMap;
57
use zbus::blocking::{fdo::DBusProxy, Connection};
68

79
mod power;
810

9-
pub fn init(evlh: &LoopHandle<'static, State>) -> Result<Vec<RegistrationToken>> {
11+
pub fn init(
12+
evlh: &LoopHandle<'static, State>,
13+
executor: &ThreadPool,
14+
) -> Result<Vec<RegistrationToken>> {
1015
let mut tokens = Vec::new();
1116

12-
match power::init() {
17+
match block_on(power::init()) {
1318
Ok(power_daemon) => {
1419
let (tx, rx) = calloop::channel::channel();
1520

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

3742
// start helper thread
38-
let result = std::thread::Builder::new()
39-
.name("system76-power-hotplug".to_string())
40-
.spawn(move || {
41-
if let Ok(mut msg_iter) = power_daemon.receive_hot_plug_detect() {
42-
while let Some(msg) = msg_iter.next() {
43-
if tx.send(msg).is_err() {
44-
break;
45-
}
43+
executor.spawn_ok(async move {
44+
if let Ok(mut msg_iter) = power_daemon.receive_hot_plug_detect().await {
45+
while let Some(msg) = msg_iter.next().await {
46+
if tx.send(msg).is_err() {
47+
break;
4648
}
4749
}
48-
})
49-
.with_context(|| "Failed to start helper thread");
50-
51-
match result {
52-
Ok(_handle) => {
53-
tokens.push(token);
54-
// detach thread
5550
}
56-
Err(err) => {
57-
evlh.remove(token);
58-
return Err(err);
59-
}
60-
}
51+
});
52+
53+
tokens.push(token);
6154
}
6255
Err(err) => {
6356
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
@@ -592,7 +592,9 @@ impl State {
592592
);
593593
let workspace_state = WorkspaceState::new(dh, client_is_privileged);
594594

595-
if let Err(err) = crate::dbus::init(&handle) {
595+
let async_executor = ThreadPool::builder().pool_size(1).create().unwrap();
596+
597+
if let Err(err) = crate::dbus::init(&handle, &async_executor) {
596598
tracing::warn!(?err, "Failed to initialize dbus handlers");
597599
}
598600

@@ -608,7 +610,7 @@ impl State {
608610
display_handle: dh.clone(),
609611
event_loop_handle: handle,
610612
event_loop_signal: signal,
611-
async_executor: ThreadPool::builder().pool_size(1).create().unwrap(),
613+
async_executor,
612614

613615
popups: PopupManager::default(),
614616
shell,

0 commit comments

Comments
 (0)