Skip to content

Commit

Permalink
reduce size of unsafe block in signal handler
Browse files Browse the repository at this point in the history
I guess it's generally good to keep unsafe blocks small, so it's
easier for the reader to guess which part is unsafe.
  • Loading branch information
martinvonz committed Dec 8, 2023
1 parent ff1542c commit d77bc0e
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions gix/src/interrupt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,21 +110,22 @@ mod init {
// * we only set atomics or call functions that do
// * there is no use of the heap
let interrupt = interrupt.clone();
let action = move || {
static INTERRUPT_COUNT: AtomicUsize = AtomicUsize::new(0);
if !super::is_triggered() {
INTERRUPT_COUNT.store(0, Ordering::SeqCst);
}
let msg_idx = INTERRUPT_COUNT.fetch_add(1, Ordering::SeqCst);
if msg_idx == grace_count {
gix_tempfile::registry::cleanup_tempfiles_signal_safe();
signal_hook::low_level::emulate_default_handler(*sig).ok();
}
interrupt();
super::trigger();
};
#[allow(unsafe_code)]
unsafe {
let hook_id = signal_hook::low_level::register(*sig, move || {
static INTERRUPT_COUNT: AtomicUsize = AtomicUsize::new(0);
if !super::is_triggered() {
INTERRUPT_COUNT.store(0, Ordering::SeqCst);
}
let msg_idx = INTERRUPT_COUNT.fetch_add(1, Ordering::SeqCst);
if msg_idx == grace_count {
gix_tempfile::registry::cleanup_tempfiles_signal_safe();
signal_hook::low_level::emulate_default_handler(*sig).ok();
}
interrupt();
super::trigger();
})?;
let hook_id = signal_hook::low_level::register(*sig, action)?;
hooks.push((*sig, hook_id));
}
}
Expand Down

0 comments on commit d77bc0e

Please sign in to comment.