|
| 1 | +// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*- |
| 2 | +// |
| 3 | +// Interrupt.h: Rcpp R/C++ interface class library -- check for interrupts |
| 4 | +// |
| 5 | +// Copyright (C) 2009 - 2013 Dirk Eddelbuettel and Romain Francois |
| 6 | +// |
| 7 | +// This file is part of Rcpp. |
| 8 | +// |
| 9 | +// Rcpp is free software: you can redistribute it and/or modify it |
| 10 | +// under the terms of the GNU General Public License as published by |
| 11 | +// the Free Software Foundation, either version 2 of the License, or |
| 12 | +// (at your option) any later version. |
| 13 | +// |
| 14 | +// Rcpp is distributed in the hope that it will be useful, but |
| 15 | +// WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | +// GNU General Public License for more details. |
| 18 | +// |
| 19 | +// You should have received a copy of the GNU General Public License |
| 20 | +// along with Rcpp. If not, see <http://www.gnu.org/licenses/>. |
| 21 | + |
| 22 | +#ifndef Rcpp_Interrupt_h |
| 23 | +#define Rcpp_Interrupt_h |
| 24 | + |
| 25 | +#include <Rinterface.h> |
| 26 | + |
| 27 | +namespace Rcpp { |
| 28 | + |
| 29 | + // Internal functions used in the implementation of checkUserInterrupt |
| 30 | + namespace internal { |
| 31 | + |
| 32 | + // Sentinel class for communicating interrupts to the top-level END_RCPP macro |
| 33 | + class InterruptedException {}; |
| 34 | + |
| 35 | + // Sentinel object of class "interrupted-error" which is used for |
| 36 | + // communicating interrupts accross module boundaries without an |
| 37 | + // exception (which would crash on windows). This is identical to |
| 38 | + // the existing "try-error" sentinel object used for communicating |
| 39 | + // errors accross module boundaries. |
| 40 | + inline SEXP interruptedError() { |
| 41 | + Rcpp::Shield<SEXP> interruptedError( Rf_mkString("") ); |
| 42 | + Rf_setAttrib( interruptedError, R_ClassSymbol, Rf_mkString("interrupted-error") ) ; |
| 43 | + return interruptedError; |
| 44 | + } |
| 45 | + |
| 46 | + // Interrupt R's execution by jumping to the top-level |
| 47 | + inline void jumpToTop() { |
| 48 | + Rf_jump_to_toplevel(); |
| 49 | + } |
| 50 | + |
| 51 | + } // namespace internal |
| 52 | + |
| 53 | + // Helper function to check for interrupts. This is invoked within |
| 54 | + // R_ToplevelExec so it doesn't longjmp |
| 55 | + namespace { |
| 56 | + |
| 57 | + inline void checkInterruptFn(void *dummy) { |
| 58 | + R_CheckUserInterrupt(); |
| 59 | + } |
| 60 | + |
| 61 | + } // anonymous namespace |
| 62 | + |
| 63 | + // Check for interrupts and throw the sentinel exception if one is pending |
| 64 | + inline void checkUserInterrupt() { |
| 65 | + if (R_ToplevelExec(checkInterruptFn, NULL) == FALSE) |
| 66 | + throw internal::InterruptedException(); |
| 67 | + } |
| 68 | + |
| 69 | +} // namespace Rcpp |
| 70 | + |
| 71 | +#endif |
0 commit comments