Loading...
Searching...
No Matches
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-2023 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::timer
29
30Description
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
50Warning
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
55Note
56 timedOut is macro because setjmp can't be in member function of timer.
57 ?something to do with stack frames.
58
59SourceFiles
60 timer.cxx
61
62\*---------------------------------------------------------------------------*/
63
64#ifndef Foam_timer_H
65#define Foam_timer_H
66
67#include <csetjmp>
68
69// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
70
71//- Check if timeout has occurred
72// keep setjmp in same stack frame so no function calls
73#undef timedOut
74#define timedOut(x) \
75 ((x).timeOut_ ? setjmp(Foam::timer::envAlarm) : false)
76
77namespace Foam
78{
79
80/*---------------------------------------------------------------------------*\
81 Class timer Declaration
82\*---------------------------------------------------------------------------*/
83
84class 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
98public:
99
100 // Public Data
101
102 //- Named/registered debug switch: 'timer'
103 static int debug;
104
105 //- The time-out value (seconds). Needed by macro timedOut
106 unsigned int timeOut_;
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 explicit timer(unsigned int seconds);
116
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// ************************************************************************* //
~timer()
Destructor. Restores the alarm and signal handler as required.
static jmp_buf envAlarm
State for setjmp. Needed by macro timedOut.
Definition timer.H:117
timer(unsigned int seconds)
Construct with specified time-out, a value of 0 makes it a no-op.
unsigned int timeOut_
The time-out value (seconds). Needed by macro timedOut.
Definition timer.H:112
static int debug
Named/registered debug switch: 'timer'.
Definition timer.H:107
Namespace for OpenFOAM.