signal
signal copied to clipboard
Pause on <C-c>
https://github.com/Yamakaky/dcpu/blob/da059ff416b6fbefe230aa64f13b00dbfc4e3481/src/emulator/debugger/mod.rs#L298-L311
It's an the continue loop of an emulator. Basically, it executes a function until there is an error. I would like to handle <C-c> to manually pause the execution. How would you do that?
If performance is not critical you can do the following:
# somewhere before the main loop of the emulator
let trap = Trap(&[SIGINT]);
loop {
try!(self.step());
if let Some(SIGINT) = trap.wait(Instant::now()) {
# break it on signal
}
}
This isn't very performant as it requires a (pretty cheap) system call on every loop iteration.
Better idea would be to set AtomicBool in the signal handler. And check that variable, which is much cheaper, but we don't have the interface for that yet. You may send a PR or just use nix crate directly.
That's what I did, but it doesn't work, the code in the if is never triggered...
For the performance part, I'll do batching to check only like every 1/10s
Just added an example https://github.com/tailhook/signal/blob/master/examples/poll_for_signal.rs
Works fine when run as a command ./target/debug/examples/poll_for_signal. I you run it using cargo run there are some issues with it: i.e. cargo exits before the original program especially program ignores the signal)
Hum, for some reason my terminal window was fucked-up and was not processing <C-c> correctly. Thanks! https://github.com/rust-lang/cargo/issues/2343
Just one more question: if an interrupt is triggered while self.step() is executing, does trap.wait catch it?
Sure. The signal will be "pending" until wait() is called.
Cool
Hum: I just noticed that the trap doesn't work it there is the rendering thread running. The Trap is created after the thread is launched.
BTW, you could improve the doc by putting the trap.wait(Instant::now()) snippet here.