Move command spawning to a thread
Browse files at this commitae47f2116decc046836c9e02880af79bf5f507eeParent: 9f24b483 changed files +25 -10
- src/input.rs +1 -3
- src/main.rs +2 -6
- src/utils.rs +22 -1
src/input.rsmodified+1 -3
| @@ -118,9 +118,7 @@ impl State { | |||
| 118 | self.backend.toggle_debug_tint(); | 118 | self.backend.toggle_debug_tint(); |
| 119 | } | 119 | } |
| 120 | Action::Spawn(command) => { | 120 | Action::Spawn(command) => { |
| 121 | - if let Some((command, args)) = command.split_first() { | 121 | + spawn(command); |
| 122 | - spawn(command, args); | ||
| 123 | - } | ||
| 124 | } | 122 | } |
| 125 | Action::ScreenshotScreen => { | 123 | Action::ScreenshotScreen => { |
| 126 | let active = self.niri.layout.active_output().cloned(); | 124 | let active = self.niri.layout.active_output().cloned(); |
src/main.rsmodified+2 -6
| @@ -164,14 +164,10 @@ fn main() { | |||
| 164 | }; | 164 | }; |
| 165 | 165 | ||
| 166 | // Spawn commands from cli and auto-start. | 166 | // Spawn commands from cli and auto-start. |
| 167 | - if let Some((command, args)) = cli.command.split_first() { | 167 | + spawn(cli.command); |
| 168 | - spawn(command, args); | ||
| 169 | - } | ||
| 170 | 168 | ||
| 171 | for elem in spawn_at_startup { | 169 | for elem in spawn_at_startup { |
| 172 | - if let Some((command, args)) = elem.command.split_first() { | 170 | + spawn(elem.command); |
| 173 | - spawn(command, args); | ||
| 174 | - } | ||
| 175 | } | 171 | } |
| 176 | 172 | ||
| 177 | // Run the compositor. | 173 | // Run the compositor. |
src/utils.rsmodified+22 -1
| @@ -6,6 +6,7 @@ use std::path::PathBuf; | |||
| 6 | use std::process::{Command, Stdio}; | 6 | use std::process::{Command, Stdio}; |
| 7 | use std::ptr::null_mut; | 7 | use std::ptr::null_mut; |
| 8 | use std::sync::atomic::{AtomicBool, Ordering}; | 8 | use std::sync::atomic::{AtomicBool, Ordering}; |
| 9 | +use std::thread; | ||
| 9 | use std::time::Duration; | 10 | use std::time::Duration; |
| 10 | 11 | ||
| 11 | use anyhow::{ensure, Context}; | 12 | use anyhow::{ensure, Context}; |
| @@ -58,7 +59,27 @@ pub static REMOVE_ENV_RUST_BACKTRACE: AtomicBool = AtomicBool::new(false); | |||
| 58 | pub static REMOVE_ENV_RUST_LIB_BACKTRACE: AtomicBool = AtomicBool::new(false); | 59 | pub static REMOVE_ENV_RUST_LIB_BACKTRACE: AtomicBool = AtomicBool::new(false); |
| 59 | 60 | ||
| 60 | /// Spawns the command to run independently of the compositor. | 61 | /// Spawns the command to run independently of the compositor. |
| 61 | -pub fn spawn(command: impl AsRef<OsStr>, args: impl IntoIterator<Item = impl AsRef<OsStr>>) { | 62 | +pub fn spawn<T: AsRef<OsStr> + Send + 'static>(command: Vec<T>) { |
| 63 | + let _span = tracy_client::span!(); | ||
| 64 | + | ||
| 65 | + if command.is_empty() { | ||
| 66 | + return; | ||
| 67 | + } | ||
| 68 | + | ||
| 69 | + // Spawning and waiting takes some milliseconds, so do it in a thread. | ||
| 70 | + let res = thread::Builder::new() | ||
| 71 | + .name("Command Spawner".to_owned()) | ||
| 72 | + .spawn(move || { | ||
| 73 | + let (command, args) = command.split_first().unwrap(); | ||
| 74 | + spawn_sync(command, args); | ||
| 75 | + }); | ||
| 76 | + | ||
| 77 | + if let Err(err) = res { | ||
| 78 | + warn!("error spawning a thread to spawn the command: {err:?}"); | ||
| 79 | + } | ||
| 80 | +} | ||
| 81 | + | ||
| 82 | +fn spawn_sync(command: impl AsRef<OsStr>, args: impl IntoIterator<Item = impl AsRef<OsStr>>) { | ||
| 62 | let _span = tracy_client::span!(); | 83 | let _span = tracy_client::span!(); |
| 63 | 84 | ||
| 64 | let command = command.as_ref(); | 85 | let command = command.as_ref(); |