sigFpe.H
Go to the documentation of this file.
1/*---------------------------------------------------------------------------*\
2 ========= |
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4 \\ / O peration |
5 \\ / A nd | www.openfoam.com
6 \\/ M anipulation |
7-------------------------------------------------------------------------------
8 Copyright (C) 2011-2016 OpenFOAM Foundation
9 Copyright (C) 2018-2019 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
12 This file is part of OpenFOAM.
13
14 OpenFOAM is free software: you can redistribute it and/or modify it
15 under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26
27Class
28 Foam::sigFpe
29
30Description
31 Set up trapping for floating point exceptions (signal FPE).
32
33 Defined by controlDict InfoSwitch entries:
34 - \par trapFpe
35 Enable floating point exception trapping.
36
37 - \par setNaN
38 Initialization all malloced memory to NaN.
39 Combined with \c trapFpe, this causes usage of uninitialized scalars
40 to trigger an abort.
41
42 Environment variables:
43 - \par FOAM_SIGFPE (true|false)
44 overrides \c trapFpe
45 - \par FOAM_SETNAN (true|false)
46 overrides \c setNaN
47
48 Note that trapping can be set/removed through the static member functions
49 or through the scope of the object (constructor sets trapping; destructor
50 restores original). The class behaves as a singleton.
51
52SourceFiles
53 sigFpe.C
54
55\*---------------------------------------------------------------------------*/
56
57#ifndef sigFpe_H
58#define sigFpe_H
59
60#include "scalar.H"
61
62// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
63
64namespace Foam
65{
66
67// Forward Declarations
68template<class T> class UList;
69
70/*---------------------------------------------------------------------------*\
71 Class sigFpe Declaration
72\*---------------------------------------------------------------------------*/
74class sigFpe
75{
76 // Private Data
77
78 //- Flag that floating point trapping should be used.
79 // Can override with FOAM_SIGFPE env variable
80 static bool switchFpe_;
81
82 //- Flag that NaN initialisation should be used.
83 // Can override with FOAM_SETNAN env variable
84 static bool switchNan_;
85
86 //- Floating point trapping currently active?
87 static bool sigActive_;
88
89 //- Flag to indicate mallocNan is currently active
90 static bool nanActive_;
91
92
93 // Private Member Functions
94
95 //- Handler for caught signals - ends job and prints stack
96 static void sigHandler(int);
97
98
99public:
100
101 // Constructors
102
103 //- Constructor calls set() to activate the FPE signal handler if it
104 //- was was not previously activate and requested() returns true.
105 sigFpe();
106
107
108 //- Destructor calls unset() to deactivate the FPE signal handler
109 //- as required.
110 ~sigFpe();
111
112
113 // Static Member Functions
114
115 //- Check if SIGFPE signals handler is to be enabled.
116 // This is controlled by the trapFpe entry or the FOAM_SIGFPE
117 // environment variable
118 static bool requested();
119
120 //- True if SIGFPE handling is currently active.
121 static inline bool active()
122 {
123 return sigActive_;
124 }
125
126 //- True if NaN memory initialisation is currently active.
127 static inline bool nanActive()
128 {
129 return nanActive_;
130 }
131
132 //- Activate SIGFPE signal handler when FOAM_SIGFPE is %set
133 // Fill memory with NaN when FOAM_SETNAN is %set
134 static void set(bool verbose=false);
135
136 //- Deactivate SIGFPE signal handler and NaN memory initialisation
137 static void unset(bool verbose=false);
138
139 //- Fill data block with NaN values
140 static void fillNan(UList<scalar>& list);
141
142
143 // Helper classes
144
145 //- Helper to locally ignore SIGFPE handling.
146 // Restores the original state of the SIGFPE handler on destruction.
147 class ignore
148 {
149 //- The signal handler state when entering
150 bool wasActive_;
151
152 //- No copy construct
153 ignore(const ignore&) = delete;
154
155 //- No copy assignment
156 void operator=(const ignore&) = delete;
157
158 //- No move construct
159 ignore(ignore&&) = delete;
160
161 //- No move assignment
162 void operator=(ignore&&) = delete;
163
164
165 public:
166
167 //- Constructor deactivates any previously active SIGFPE handler
168 ignore();
169
170 //- Destructor restores the original state of SIGFPE handler
171 ~ignore();
172
173 //- Restore the original state of SIGFPE handler
174 void restore();
175 };
176};
177
178
179// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
180
181} // End namespace Foam
182
183// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
184
185#endif
186
187// ************************************************************************* //
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: UList.H:94
Helper to locally ignore SIGFPE handling.
Definition: sigFpe.H:147
ignore()
Constructor deactivates any previously active SIGFPE handler.
Definition: sigFpe.C:105
~ignore()
Destructor restores the original state of SIGFPE handler.
Definition: sigFpe.C:124
void restore()
Restore the original state of SIGFPE handler.
Definition: sigFpe.C:132
Set up trapping for floating point exceptions (signal FPE).
Definition: sigFpe.H:74
static void fillNan(UList< scalar > &list)
Fill data block with NaN values.
Definition: sigFpe.C:225
static void set(bool verbose=false)
Activate SIGFPE signal handler when FOAM_SIGFPE is set.
Definition: sigFpe.C:148
static bool nanActive()
True if NaN memory initialisation is currently active.
Definition: sigFpe.H:126
static bool active()
True if SIGFPE handling is currently active.
Definition: sigFpe.H:120
static void unset(bool verbose=false)
Deactivate SIGFPE signal handler and NaN memory initialisation.
Definition: sigFpe.C:204
static bool requested()
Check if SIGFPE signals handler is to be enabled.
Definition: sigFpe.C:142
Namespace for OpenFOAM.