signal icon indicating copy to clipboard operation
signal copied to clipboard

Pause on <C-c>

Open Yamakaky opened this issue 9 years ago • 9 comments

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?

Yamakaky avatar Oct 22 '16 18:10 Yamakaky

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.

tailhook avatar Oct 22 '16 19:10 tailhook

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

Yamakaky avatar Oct 22 '16 20:10 Yamakaky

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)

tailhook avatar Oct 22 '16 21:10 tailhook

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

Yamakaky avatar Oct 22 '16 22:10 Yamakaky

Just one more question: if an interrupt is triggered while self.step() is executing, does trap.wait catch it?

Yamakaky avatar Oct 22 '16 22:10 Yamakaky

Sure. The signal will be "pending" until wait() is called.

tailhook avatar Oct 23 '16 08:10 tailhook

Cool

Yamakaky avatar Oct 23 '16 13:10 Yamakaky

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.

Yamakaky avatar Oct 23 '16 13:10 Yamakaky

BTW, you could improve the doc by putting the trap.wait(Instant::now()) snippet here.

Yamakaky avatar Oct 23 '16 13:10 Yamakaky