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-2023 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 "timer.H"
30 #include "error.H"
31 #include "defineDebugSwitch.H"
32 
33 #include <unistd.h>
34 
35 // File-local functions
36 #include "signalMacros.C"
37 
38 
39 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
40 
41 namespace Foam
42 {
43  defineDebugSwitchWithName(timer, "timer", 0);
44  registerDebugSwitchWithName(timer, timer, "timer");
45 }
46 
47 jmp_buf Foam::timer::envAlarm;
48 
49 unsigned int Foam::timer::oldTimeOut_ = 0;
50 
51 
52 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
53 
54 void Foam::timer::sigHandler(int)
55 {
56  DebugInFunction<< "Timed out. Jumping." << endl;
57 
58  longjmp(envAlarm, 1);
59 }
60 
61 
62 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
63 
64 Foam::timer::timer(unsigned int seconds)
65 :
66  timeOut_(seconds)
67 {
68  if (!timeOut_)
69  {
70  return;
71  }
72 
73  // Singleton since handler is static function
74  if (oldTimeOut_)
75  {
77  << "timer already used."
78  << abort(FatalError);
79  }
80 
81  // Set alarm signal handler
82  // - do not block any signals while in it
83  // - clear list of signals to mask
84 
85  setHandler("SIGALRM", SIGALRM, sigHandler);
86 
87  // Set alarm timer
88  oldTimeOut_ = ::alarm(timeOut_);
89 
91  << "Installing timeout " << int(timeOut_) << " seconds"
92  << " (overriding old timeout " << int(oldTimeOut_) << ")." << endl;
93 }
94 
95 
96 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
97 
99 {
100  if (!timeOut_)
101  {
102  return;
103  }
104 
106  << "timeOut=" << int(timeOut_)
107  << " : resetting timeOut to " << int(oldTimeOut_) << endl;
108 
109  // Reset alarm timer
110  ::alarm(oldTimeOut_);
111  oldTimeOut_ = 0;
112 
113  resetHandler("SIGALRM", SIGALRM);
114 }
115 
116 
117 // ************************************************************************* //
static void setHandler(const char *what, int sigNum, void(*handler)(int))
Definition: signalMacros.C:54
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:598
defineDebugSwitchWithName(pointMVCWeight, "pointMVCWeight", 0)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
static void resetHandler(const char *what, int sigNum)
Definition: signalMacros.C:42
#define DebugInFunction
Report an information message using Foam::Info.
File-local code for setting/resetting signal handlers.
errorManip< error > abort(error &err)
Definition: errorManip.H:139
registerDebugSwitchWithName(fileMonitor, fileMonitor, "fileMonitor")
~timer()
Destructor. Restores the alarm and signal handler as required.
Definition: timer.C:91
Macro definitions for debug switches.
timer(unsigned int seconds)
Construct with specified time-out, a value of 0 makes it a no-op.
Definition: timer.C:57
unsigned int timeOut_
The time-out value (seconds). Needed by macro timedOut.
Definition: timer.H:112
Namespace for OpenFOAM.
static jmp_buf envAlarm
State for setjmp. Needed by macro timedOut.
Definition: timer.H:117