checking for interupts
Closed this issue · 3 comments
romainfrancois commented
Client packages need a clean way to check for interupts, i.e. the documented way (as of Writing R extensions) would potentially violate C++ destructors, meaning leaks, and potentially objects that are forever protected from the GC ...
In a R-devel thread, Simon posted this code:
static void chkIntFn(void *dummy) {
R_CheckUserInterrupt();
}
// this will call the above in a top-level context so it won't longjmp-out of your context
bool checkInterrupt() {
return (R_ToplevelExec(chkIntFn, NULL) == FALSE);
}
// your code somewhere ...
if (checkInterrupt()) { // user interrupted ... }
jjallaire commented
We could have a function that throws a C++ exception in the case that checkInterrupt returns true. This could then be caught in the BEGIN/END_CPP macro construct and converted to a stock R_CheckUserInterrupt call (or Rf_jump_to_toplevel call)
romainfrancois commented
That makes sense and is inline with what we do with keeping both C++ exceptions and R conditions work together.
Although Simon says:
The problem with it is that it will eat all
errors, even if they were not yours (e.g. those resulting from events triggered the event loop), so I would
not recommend it for general use.