LCOV - code coverage report
Current view: top level - src/mesquite/Misc - MsqInterrupt.hpp (source / functions) Hit Total Coverage
Test: coverage_sk.info Lines: 2 5 40.0 %
Date: 2020-07-18 00:09:26 Functions: 1 2 50.0 %
Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : /* *****************************************************************
       2                 :            :     MESQUITE -- The Mesh Quality Improvement Toolkit
       3                 :            : 
       4                 :            :     Copyright 2004 Sandia Corporation and Argonne National
       5                 :            :     Laboratory.  Under the terms of Contract DE-AC04-94AL85000
       6                 :            :     with Sandia Corporation, the U.S. Government retains certain
       7                 :            :     rights in this software.
       8                 :            : 
       9                 :            :     This library is free software; you can redistribute it and/or
      10                 :            :     modify it under the terms of the GNU Lesser General Public
      11                 :            :     License as published by the Free Software Foundation; either
      12                 :            :     version 2.1 of the License, or (at your option) any later version.
      13                 :            : 
      14                 :            :     This library is distributed in the hope that it will be useful,
      15                 :            :     but WITHOUT ANY WARRANTY; without even the implied warranty of
      16                 :            :     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      17                 :            :     Lesser General Public License for more details.
      18                 :            : 
      19                 :            :     You should have received a copy of the GNU Lesser General Public License
      20                 :            :     (lgpl.txt) along with this library; if not, write to the Free Software
      21                 :            :     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
      22                 :            : 
      23                 :            :   ***************************************************************** */
      24                 :            : 
      25                 :            : #ifndef MSQ_INTERRUPT_HPP
      26                 :            : #define MSQ_INTERRUPT_HPP
      27                 :            : 
      28                 :            : #include "MsqError.hpp"
      29                 :            : 
      30                 :            : namespace MBMesquite
      31                 :            : {
      32                 :            : 
      33                 :            : /** \brief Class to watch for user-interrupt (SIGINT, ctrl-C)
      34                 :            :  *
      35                 :            :  * A class to watch for SIGINT.
      36                 :            :  *
      37                 :            :  * Creating an instance of the class ensures that the Mesquite
      38                 :            :  * handler for SIGINT is registered.  When all instances are
      39                 :            :  * destroyed, the Mesquite handler is removed.  The intent is
      40                 :            :  * that each interruptable API declare an instance on the stack.
      41                 :            :  * This way the handler is automatically unregistered when the
      42                 :            :  * API returns.  For example:
      43                 :            :  * <code>
      44                 :            :  * void my_api( MsqError& err )
      45                 :            :  * {
      46                 :            :  *   MsqInterrupt interrupt;
      47                 :            :  *   ... //do stuff
      48                 :            :  *   return;
      49                 :            :  * }
      50                 :            :  * </code>
      51                 :            :  */
      52                 :            : class MsqInterrupt
      53                 :            : {
      54                 :            :   public:
      55                 :            :     /**\brief Disable Mesquite's SIGINT handler */
      56                 :            :     static void disable( MsqError& err );
      57                 :            :     /**\brief Allow Mesquite to register a SIGINT handler */
      58                 :            :     static void allow( MsqError& err );
      59                 :            :     /**\brief Force Mesquite to register SIGINT handler */
      60                 :            :     static void enable( MsqError& err );
      61                 :            : 
      62                 :            :     /**\brief Check if an interrupt was seen */
      63                 :     939994 :     static bool interrupt()
      64                 :            :     {
      65                 :     939994 :         return sawInterrupt;
      66                 :            :     }
      67                 :            :     /**\brief Clear the interrupt flag */
      68                 :            :     static void clear()
      69                 :            :     {
      70                 :            :         sawInterrupt = false;
      71                 :            :     }
      72                 :            :     /**\brief Set the interrupt flag */
      73                 :          0 :     static void set_interrupt()
      74                 :            :     {
      75                 :          0 :         sawInterrupt = true;
      76                 :          0 :     }
      77                 :            : 
      78                 :            :     /** Constructor, increment instance count. If
      79                 :            :      * instance count was zero, register SIGINT handler */
      80                 :            :     MsqInterrupt();
      81                 :            :     /** Constructor, decrement instance count. If
      82                 :            :      * instance count goes to zero, remove SIGINT handler */
      83                 :            :     ~MsqInterrupt();
      84                 :            : 
      85                 :            :     static void set_handler();
      86                 :            : 
      87                 :            :   private:
      88                 :            :     enum InterruptMode
      89                 :            :     {
      90                 :            :         CATCH,
      91                 :            :         IGNORE,
      92                 :            :         AUTO
      93                 :            :     };
      94                 :            : 
      95                 :            :     static InterruptMode interruptMode;
      96                 :            :     static unsigned instanceCount;
      97                 :            :     static bool sawInterrupt;
      98                 :            : 
      99                 :            :     // Don't allow any of this stuff (make them private)
     100                 :            :     void* operator new( size_t size );
     101                 :            :     MsqInterrupt( const MsqInterrupt& );
     102                 :            :     MsqInterrupt& operator=( const MsqInterrupt& );
     103                 :            : };
     104                 :            : }  // namespace MBMesquite
     105                 :            : 
     106                 :            : #endif

Generated by: LCOV version 1.11