timer.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-2015 OpenFOAM Foundation
9  Copyright (C) 2019 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
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 
27 Class
28  Foam::timer
29 
30 Description
31  Implements a timeout mechanism via sigalarm.
32 
33  Example usage:
34  \code
35  timer myTimer(5); // 5 sec
36  ..
37  if (timedOut(myTimer))
38  {
39  // timed out
40  }
41  else
42  {
43  // do something possible blocking
44  }
45  \endcode
46 
47  Constructor set signal handler on sigalarm and alarm(). Destructor
48  clears these.
49 
50 Warning
51  The setjmp restores complete register state so including local vars
52  held in regs. So if in blocking part something gets calced in a stack
53  based variable make sure it is declared 'volatile'.
54 
55 Note
56  timedOut is macro because setjmp can't be in member function of timer.
57  ?something to do with stack frames.
58 
59 SourceFiles
60  timer.C
61 
62 \*---------------------------------------------------------------------------*/
63 
64 #ifndef timer_H
65 #define timer_H
66 
67 #include "className.H"
68 #include <csetjmp>
69 
70 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
71 
72 //- Check if timeout has occurred
73 // keep setjmp in same stack frame so no function calls
74 #define timedOut(x) \
75  ((x).timeOut_ ? setjmp(Foam::timer::envAlarm) : false)
76 
77 namespace Foam
78 {
79 
80 /*---------------------------------------------------------------------------*\
81  Class timer Declaration
82 \*---------------------------------------------------------------------------*/
83 
84 class timer
85 {
86  // Private Data
87 
88  //- Old alarm() value
89  static unsigned int oldTimeOut_;
90 
91 
92  // Private Member Functions
93 
94  //- Alarm handler
95  static void sigHandler(int);
96 
97 
98 public:
99 
100  // Public Data
101 
102  //- Declare name of the class and its debug switch
103  ClassName("timer");
104 
105  //- The time-out value (seconds). Needed by macro timedOut
106  unsigned int timeOut_;
107 
108  //- State for setjmp. Needed by macro timedOut
109  static jmp_buf envAlarm;
110 
111 
112  // Constructors
113 
114  //- Construct with specified time-out, a value of 0 makes it a no-op.
115  timer(unsigned int seconds);
116 
117 
118  //- Destructor. Restores the alarm and signal handler as required.
119  ~timer();
120 };
121 
122 
123 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
124 
125 } // End namespace Foam
126 
127 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
128 
129 #endif
130 
131 // ************************************************************************* //
Foam::timer::~timer
~timer()
Destructor. Restores the alarm and signal handler as required.
Definition: timer.C:129
timedOut
#define timedOut(x)
Check if timeout has occurred.
Definition: timer.H:73
Foam::timer::timer
timer(unsigned int seconds)
Construct with specified time-out, a value of 0 makes it a no-op.
Definition: timer.C:80
className.H
Macro definitions for declaring ClassName(), NamespaceName(), etc.
Foam::timer::envAlarm
static jmp_buf envAlarm
State for setjmp. Needed by macro timedOut.
Definition: timer.H:108
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::timer::ClassName
ClassName("timer")
Declare name of the class and its debug switch.
ClassName
#define ClassName(TypeNameString)
Add typeName information from argument TypeNameString to a class.
Definition: className.H:67
Foam::timer
Implements a timeout mechanism via sigalarm.
Definition: timer.H:83
x
x
Definition: LISASMDCalcMethod2.H:52
Foam::timer::timeOut_
unsigned int timeOut_
The time-out value (seconds). Needed by macro timedOut.
Definition: timer.H:105