All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
timer.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2019 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Class
25  Foam::timer
26 
27 Description
28  Implements a timeout mechanism via sigalarm.
29 
30  Example usage:
31  \code
32  timer myTimer(5); // 5 sec
33  ..
34  if (timedOut(myTimer))
35  {
36  // timed out
37  }
38  else
39  {
40  // do something possible blocking
41  }
42  \endcode
43 
44  Constructor set signal handler on sigalarm and alarm(). Destructor
45  clears these.
46 
47  timedOut is macro because setjmp can't be in member function of timer.
48  ?something to do with stack frames.
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 SourceFiles
56  timer.C
57 
58 \*---------------------------------------------------------------------------*/
59 
60 #ifndef timer_H
61 #define timer_H
62 
63 #include "className.H"
64 
65 #include <signal.h>
66 #include <setjmp.h>
67 
68 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
69 
70 //- Check it a timeout has occurred
71 // keep setjmp in same stack frame so no function calls
72 #define timedOut(x) \
73  (((x).newTimeOut_ > 0) ? setjmp(Foam::timer::envAlarm) : false)
74 
75 namespace Foam
76 {
77 
78 /*---------------------------------------------------------------------------*\
79  Class timer Declaration
80 \*---------------------------------------------------------------------------*/
81 
82 class timer
83 {
84  // Private Data
85 
86  //- Old signal masks
87  static struct sigaction oldAction_;
88 
89  //- Old alarm() value
90  static unsigned int oldTimeOut_;
91 
92 
93  // Private Member Functions
94 
95  //- Alarm handler
96  static void signalHandler(int);
97 
98 
99 public:
100 
101  // Public data
102 
103  //- Declare name of the class and its debug switch
104  ClassName("timer");
105 
106  //- Current time out value. Needed by macro timedOut
107  unsigned int newTimeOut_;
108 
109  //- State for setjmp. Needed by macro timedOut
110  static jmp_buf envAlarm;
111 
112 
113  // Constructors
114 
115  //- Construct from components.
116  // newTimeOut=0 makes it do nothing.
117  timer(const unsigned int newTimeOut);
118 
119 
120  //- Destructor
121  ~timer();
122 };
123 
124 
125 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
126 
127 } // End namespace Foam
128 
129 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
130 
131 #endif
132 
133 // ************************************************************************* //
timer(const unsigned int newTimeOut)
Construct from components.
Definition: timer.C:59
unsigned int newTimeOut_
Current time out value. Needed by macro timedOut.
Definition: timer.H:106
Implements a timeout mechanism via sigalarm.
Definition: timer.H:81
ClassName("timer")
Declare name of the class and its debug switch.
~timer()
Destructor.
Definition: timer.C:105
Macro definitions for declaring ClassName(), NamespaceName(), etc.
Namespace for OpenFOAM.
static jmp_buf envAlarm
State for setjmp. Needed by macro timedOut.
Definition: timer.H:109