Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pause on <C-c> #5

Open
Yamakaky opened this issue Oct 22, 2016 · 9 comments
Open

Pause on <C-c> #5

Yamakaky opened this issue Oct 22, 2016 · 9 comments

Comments

@Yamakaky
Copy link

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 to manually pause the execution. How would you do that?

@tailhook
Copy link
Owner

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.

@Yamakaky
Copy link
Author

Yamakaky commented Oct 22, 2016

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

@tailhook
Copy link
Owner

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)

@Yamakaky
Copy link
Author

Hum, for some reason my terminal window was fucked-up and was not processing correctly. Thanks!
rust-lang/cargo#2343

@Yamakaky
Copy link
Author

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

@tailhook
Copy link
Owner

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

@Yamakaky
Copy link
Author

Cool

@Yamakaky
Copy link
Author

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 Yamakaky reopened this Oct 23, 2016
@Yamakaky
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants