timer.C
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) 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
27\*---------------------------------------------------------------------------*/
28
29#include "error.H"
30#include "timer.H"
31
32#include <unistd.h>
33
34// File-local functions
35#include "signalMacros.C"
36
37
38// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
39
40namespace Foam
41{
42 defineTypeNameAndDebug(timer, 0);
43}
44
46
47unsigned int Foam::timer::oldTimeOut_ = 0;
48
49
50// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
51
52void Foam::timer::sigHandler(int)
53{
54 DebugInFunction<< "Timed out. Jumping." << endl;
55
56 longjmp(envAlarm, 1);
57}
58
59
60// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
61
62Foam::timer::timer(unsigned int seconds)
63:
64 timeOut_(seconds)
65{
66 if (!timeOut_)
67 {
68 return;
69 }
70
71 // Singleton since handler is static function
72 if (oldTimeOut_)
73 {
75 << "timer already used."
76 << abort(FatalError);
77 }
78
79 // Set alarm signal handler
80 // - do not block any signals while in it
81 // - clear list of signals to mask
82
83 setHandler("SIGALRM", SIGALRM, sigHandler);
84
85 // Set alarm timer
86 oldTimeOut_ = ::alarm(timeOut_);
87
89 << "Installing timeout " << int(timeOut_) << " seconds"
90 << " (overriding old timeout " << int(oldTimeOut_) << ")." << endl;
91}
92
93
94// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
95
97{
98 if (!timeOut_)
99 {
100 return;
101 }
102
104 << "timeOut=" << int(timeOut_)
105 << " : resetting timeOut to " << int(oldTimeOut_) << endl;
106
107 // Reset alarm timer
108 ::alarm(oldTimeOut_);
109 oldTimeOut_ = 0;
110
111 resetHandler("SIGALRM", SIGALRM);
112}
113
114
115// ************************************************************************* //
#define SIGALRM
Definition: timer.C:43
File-local code for setting/resetting signal handlers.
Implements a timeout mechanism via sigalarm.
Definition: timer.H:84
~timer()
Destructor. Restores the alarm and signal handler as required.
Definition: timer.C:129
static jmp_buf envAlarm
State for setjmp. Needed by macro timedOut.
Definition: timer.H:108
unsigned int timeOut_
The time-out value (seconds). Needed by macro timedOut.
Definition: timer.H:105
#define defineTypeNameAndDebug(Type, DebugSwitch)
Define the typeName and debug information.
Definition: className.H:121
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
#define DebugInFunction
Report an information message using Foam::Info.
Namespace for OpenFOAM.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
errorManip< error > abort(error &err)
Definition: errorManip.H:144
static void resetHandler(const char *what, int sigNum)
Definition: signalMacros.C:46