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