Move command spawning to a thread

It was showing up on profiles causing dropped frames.
This commit is contained in:
Ivan Molodetskikh
2023-11-24 22:43:15 +04:00
parent 9f24b48d8f
commit ae47f2116d
3 changed files with 25 additions and 10 deletions
+1 -3
View File
@@ -118,9 +118,7 @@ impl State {
self.backend.toggle_debug_tint();
}
Action::Spawn(command) => {
if let Some((command, args)) = command.split_first() {
spawn(command, args);
}
spawn(command);
}
Action::ScreenshotScreen => {
let active = self.niri.layout.active_output().cloned();
+2 -6
View File
@@ -164,14 +164,10 @@ fn main() {
};
// Spawn commands from cli and auto-start.
if let Some((command, args)) = cli.command.split_first() {
spawn(command, args);
}
spawn(cli.command);
for elem in spawn_at_startup {
if let Some((command, args)) = elem.command.split_first() {
spawn(command, args);
}
spawn(elem.command);
}
// Run the compositor.
+22 -1
View File
@@ -6,6 +6,7 @@ use std::path::PathBuf;
use std::process::{Command, Stdio};
use std::ptr::null_mut;
use std::sync::atomic::{AtomicBool, Ordering};
use std::thread;
use std::time::Duration;
use anyhow::{ensure, Context};
@@ -58,7 +59,27 @@ pub static REMOVE_ENV_RUST_BACKTRACE: AtomicBool = AtomicBool::new(false);
pub static REMOVE_ENV_RUST_LIB_BACKTRACE: AtomicBool = AtomicBool::new(false);
/// Spawns the command to run independently of the compositor.
pub fn spawn(command: impl AsRef<OsStr>, args: impl IntoIterator<Item = impl AsRef<OsStr>>) {
pub fn spawn<T: AsRef<OsStr> + Send + 'static>(command: Vec<T>) {
let _span = tracy_client::span!();
if command.is_empty() {
return;
}
// Spawning and waiting takes some milliseconds, so do it in a thread.
let res = thread::Builder::new()
.name("Command Spawner".to_owned())
.spawn(move || {
let (command, args) = command.split_first().unwrap();
spawn_sync(command, args);
});
if let Err(err) = res {
warn!("error spawning a thread to spawn the command: {err:?}");
}
}
fn spawn_sync(command: impl AsRef<OsStr>, args: impl IntoIterator<Item = impl AsRef<OsStr>>) {
let _span = tracy_client::span!();
let command = command.as_ref();