RcppCore/Rcpp — pull request #91 by jjallaire (2014-01-03 14:36:09+00:00)
Title: New checkUserInterrupt function that provides a C++ friendly implementation of R_CheckUserInterrupt

This implements a C++ friendly version of checkUserInterrupt as suggested by Simon on R-devel (see https://github.com/RcppCore/Rcpp/issues/69).

R_CheckUserInterrupt is called in a top level context and if an error occurs (i.e. Rf_jump_to_top_ex was called) then an internal C++ exception is thrown as a sentinel that an interrupt has occurred. The outer END_RCPP macro catches this exception type then calls Rf_jump_to_top (the longjmp is safe at the outer level since it's a C rather than C++ stack at that point).

Note that when called from C code R_CheckUserInterrupt attempts to respect user try handlers, invoke the browser if appropriate, etc. In this implementation these side effects don't occur because the they are eaten by the check itself. These limitations are IMHO acceptable since the alternative is to have no support whatsoever for interrupting long running C++ code.