Detegr/rust-ctrlc

Please support using signals

Closed this issue · 1 comments

The fix for #6 switched to a spinloop, which wakes up periodically. This wastes power by waking up the CPU and preventing it from reaching deep sleep states; in addition, this delays responsiveness to Ctrl-C.

Please consider switching back to signals. This would not require a large number of changes, just a few simple ones:

  • Create a pipe (libc::pipe or something like the os_pipe crate), and mark both ends as CLOEXEC.
  • Set the signal handler and spawn the thread, as you do now.
  • In the signal handler, write one byte to the write end of the pipe. You can safely call write() from a signal handler.
  • In the thread, do a blocking read of a byte from the pipe. When that read completes, set the condition variable.

This eliminates the spinloop, and allows the thread to sleep forever waiting for a signal rather than waking up periodically.

Optionally (not required), you could add an additional function that just sets up the signal handler and the pipe, but not the thread, for users who instead want to put the read end of the pipe into an event loop.

Fixed with PR #13.