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 #include "MsqInterrupt.hpp"
26 #include <signal.h>
27 
28 namespace MBMesquite {
29 
30 unsigned MsqInterrupt::instanceCount = 0;
31 bool MsqInterrupt::sawInterrupt = false;
32 MsqInterrupt::InterruptMode MsqInterrupt::interruptMode = MsqInterrupt::AUTO;
33 
34 extern "C" { typedef void (*msq_sig_handler_t)(int); }
35 msq_sig_handler_t oldHandler = SIG_ERR;
36 
37 
msq_sigint_handler(int)38 extern "C" void msq_sigint_handler( int )
39 {
40   MsqInterrupt::set_interrupt();
41   if (oldHandler != SIG_DFL && oldHandler != SIG_IGN)
42     oldHandler( SIGINT );
43   MsqInterrupt::set_handler();
44 }
45 
46 
set_handler()47 void MsqInterrupt::set_handler()
48 {
49   oldHandler = signal( SIGINT, &msq_sigint_handler );
50   if (MsqInterrupt::interruptMode == MsqInterrupt::AUTO &&
51       (oldHandler == SIG_DFL || oldHandler == SIG_IGN))
52   {
53     signal( SIGINT, oldHandler );
54     oldHandler = SIG_ERR;
55   }
56 }
57 
disable(MsqError &)58 void MsqInterrupt::disable( MsqError& /*err*/ )
59 {
60   interruptMode = IGNORE;
61   if (instanceCount && SIG_ERR != oldHandler)
62   {
63     signal( SIGINT, oldHandler );
64     oldHandler = SIG_ERR;
65   }
66   sawInterrupt = false;
67 }
68 
enable(MsqError &)69 void MsqInterrupt::enable( MsqError& /*err*/ )
70 {
71   sawInterrupt = false;
72   interruptMode = CATCH;
73   if (instanceCount && SIG_ERR == oldHandler)
74     set_handler();
75 }
76 
allow(MsqError &)77 void MsqInterrupt::allow( MsqError& /*err*/ )
78 {
79   sawInterrupt = false;
80   interruptMode = AUTO;
81   if (instanceCount && SIG_ERR == oldHandler)
82     set_handler();
83 }
84 
MsqInterrupt()85 MsqInterrupt::MsqInterrupt()
86 {
87   if (!instanceCount)
88   {
89     if (IGNORE != interruptMode)
90       set_handler();
91     sawInterrupt = false;
92   }
93   ++instanceCount;
94 }
95 
~MsqInterrupt()96 MsqInterrupt::~MsqInterrupt()
97 {
98   if (!--instanceCount && SIG_ERR != oldHandler)
99   {
100     signal( SIGINT, oldHandler );
101     oldHandler = SIG_ERR;
102   }
103   sawInterrupt = false;
104 }
105 
106 } // namespace MBMesquite
107 
108 
109