All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
timer.C
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-2018 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 \*---------------------------------------------------------------------------*/
25 
26 #include <unistd.h>
27 
28 #include "error.H"
29 #include "timer.H"
30 
31 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
32 
33 namespace Foam
34 {
35 defineTypeNameAndDebug(timer, 0);
36 
37 jmp_buf timer::envAlarm;
38 
39 struct sigaction timer::oldAction_;
40 
41 unsigned int timer::oldTimeOut_ = 0;
42 }
43 
44 
45 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
46 
47 void Foam::timer::signalHandler(int)
48 {
49  if (debug)
50  {
51  InfoInFunction<< "Timed out. Jumping."
52  << endl;
53  }
54  longjmp(envAlarm, 1);
55 }
56 
57 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
58 
59 Foam::timer::timer(const unsigned int newTimeOut)
60 :
61  newTimeOut_(newTimeOut)
62 {
63 
64  if (newTimeOut > 0)
65  {
66  // Is singleton since handler is static function
67  if (oldTimeOut_ != 0)
68  {
70  << "timer already used."
71  << abort(FatalError);
72  }
73 
74  // Install alarm signal handler:
75  // - do not block any signals while in it
76  // - clear list of signals to mask
77  struct sigaction newAction;
78  newAction.sa_handler = timer::signalHandler;
79  newAction.sa_flags = SA_NODEFER;
80  sigemptyset(&newAction.sa_mask);
81 
82  if (sigaction(SIGALRM, &newAction, &oldAction_) < 0)
83  {
85  << "sigaction(SIGALRM) error"
86  << abort(FatalError);
87  }
88 
89  oldTimeOut_ = ::alarm(newTimeOut);
90 
91  if (debug)
92  {
94  << "Installing timeout " << int(newTimeOut_)
95  << " seconds"
96  << " (overriding old timeout " << int(oldTimeOut_)
97  << ")." << endl;
98  }
99  }
100 }
101 
102 
103 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
104 
106 {
107  if (newTimeOut_ > 0)
108  {
109  if (debug)
110  {
112  << "timeOut=" << int(newTimeOut_)
113  << " : resetting timeOut to " << int(oldTimeOut_) << endl;
114  }
115 
116  // Reset timer
117  ::alarm(oldTimeOut_);
118  oldTimeOut_ = 0;
119 
120  // Restore signal handler
121  if (sigaction(SIGALRM, &oldAction_, nullptr) < 0)
122  {
124  << "sigaction(SIGALRM) error"
125  << abort(FatalError);
126  }
127  }
128 }
129 
130 
131 // ************************************************************************* //
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
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
errorManip< error > abort(error &err)
Definition: errorManip.H:131
defineTypeNameAndDebug(combustionModel, 0)
~timer()
Destructor.
Definition: timer.C:105
Namespace for OpenFOAM.
#define InfoInFunction
Report an information message using Foam::Info.
static jmp_buf envAlarm
State for setjmp. Needed by macro timedOut.
Definition: timer.H:109