Skip to content

Commit

Permalink
Remove Arc overhead
Browse files Browse the repository at this point in the history
We could use static and share the AtomicBool with the other threads
and avoid Arc.
  • Loading branch information
psibi committed Aug 14, 2024
1 parent 9f974bb commit ba87733
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,17 @@ impl Cli {
});
}

let send_clone = send.clone();
let child_pid = i32::try_from(child.id())?;
let child_was_killed = Arc::new(AtomicBool::from(false));
let child_was_killed_clone = child_was_killed.clone();
std::thread::spawn(move || {
handle_signals(
send_clone,
nix::unistd::Pid::from_raw(child_pid),
child_was_killed_clone,
)
static CHILD_WAS_KILLED: AtomicBool = AtomicBool::new(false);
std::thread::spawn({
let send = send.clone();
move || {
handle_signals(
send,
nix::unistd::Pid::from_raw(child_pid),
&CHILD_WAS_KILLED,
)
}
});

std::thread::spawn(|| watch_child(send, child));
Expand All @@ -167,7 +168,7 @@ impl Cli {
)),
MainMessage::ChildExited(exit_status) => {
if self.can_exit && exit_status.success()
|| child_was_killed.load(Ordering::SeqCst)
|| CHILD_WAS_KILLED.load(Ordering::SeqCst)
{
eprintln!("Child exited, treating as a success case");
Ok(())
Expand Down Expand Up @@ -309,7 +310,7 @@ fn watch_child(send: SendMainMessage, mut child: Child) {
fn handle_signals(
send: SendMainMessage,
child_pid: nix::unistd::Pid,
child_was_killed: Arc<AtomicBool>,
child_was_killed: &AtomicBool,
) {
let mut signals = match signal_hook::iterator::Signals::new([SIGTERM, SIGINT])
.context("Creating new Signals value")
Expand Down

0 comments on commit ba87733

Please sign in to comment.