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 -------------------------------------------------------------------------------
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 \*---------------------------------------------------------------------------*/
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 
40 namespace Foam
41 {
42  defineTypeNameAndDebug(timer, 0);
43 }
44 
45 jmp_buf Foam::timer::envAlarm;
46 
47 unsigned int Foam::timer::oldTimeOut_ = 0;
48 
49 
50 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
51 
52 void Foam::timer::sigHandler(int)
53 {
54  DebugInFunction<< "Timed out. Jumping." << endl;
55 
56  longjmp(envAlarm, 1);
57 }
58 
59 
60 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
61 
62 Foam::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 // ************************************************************************* //
Foam::timer::~timer
~timer()
Definition: timer.C:89
Foam::resetHandler
static void resetHandler(const char *what, int sigNum)
Definition: signalMacros.C:42
Foam::endl
Ostream & endl(Ostream &os)
Definition: Ostream.H:381
error.H
DebugInFunction
#define DebugInFunction
Definition: messageStream.H:445
Foam::timer::timer
timer(unsigned int seconds)
Definition: timer.C:55
signalMacros.C
File-local code for setting/resetting signal handlers.
Foam::FatalError
error FatalError
timer.H
Foam
Definition: atmBoundaryLayer.C:26
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:139
FatalErrorInFunction
#define FatalErrorInFunction
Definition: error.H:465
Foam::setHandler
static void setHandler(const char *what, int sigNum, void(*handler)(int))
Definition: signalMacros.C:54
Foam::timer::envAlarm
static jmp_buf envAlarm
Definition: timer.H:107
Foam::timer::timeOut_
unsigned int timeOut_
Definition: timer.H:104
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)