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 | Copyright (C) 2011-2015 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 
48 {
49  if (debug)
50  {
51  Info<< "Foam::timer::signalHandler(int sig) : "
52  << " timed out. Jumping."
53  << endl;
54  }
55  longjmp(envAlarm, 1);
56 }
57 
58 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
59 
60 Foam::timer::timer(const unsigned int newTimeOut)
61 :
62  newTimeOut_(newTimeOut)
63 {
64 
65  if (newTimeOut > 0)
66  {
67  // Is singleton since handler is static function
68  if (oldTimeOut_ != 0)
69  {
71  << "timer already used."
72  << abort(FatalError);
73  }
74 
75  // Install alarm signal handler:
76  // - do not block any signals while in it
77  // - clear list of signals to mask
78  struct sigaction newAction;
79  newAction.sa_handler = timer::signalHandler;
80  newAction.sa_flags = SA_NODEFER;
81  sigemptyset(&newAction.sa_mask);
82 
83  if (sigaction(SIGALRM, &newAction, &oldAction_) < 0)
84  {
86  << "sigaction(SIGALRM) error"
87  << abort(FatalError);
88  }
89 
90  oldTimeOut_ = ::alarm(newTimeOut);
91 
92  if (debug)
93  {
94  Info<< "Foam::timer::timer(const unsigned int) : "
95  << " installing timeout " << int(newTimeOut_)
96  << " seconds"
97  << " (overriding old timeout " << int(oldTimeOut_)
98  << ")." << endl;
99  }
100  }
101 }
102 
103 
104 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
105 
107 {
108  if (newTimeOut_ > 0)
109  {
110  if (debug)
111  {
112  Info<< "Foam::timer::~timer(const unsigned int) : timeOut="
113  << int(newTimeOut_)
114  << " : resetting timeOut to " << int(oldTimeOut_) << endl;
115  }
116 
117  // Reset timer
118  ::alarm(oldTimeOut_);
119  oldTimeOut_ = 0;
120 
121  // Restore signal handler
122  if (sigaction(SIGALRM, &oldAction_, NULL) < 0)
123  {
125  << "sigaction(SIGALRM) error"
126  << abort(FatalError);
127  }
128  }
129 }
130 
131 // ************************************************************************* //
Foam::timer::signalHandler
static void signalHandler(int)
Alarm handler.
Definition: timer.C:47
Foam::timer::~timer
~timer()
Destructor.
Definition: timer.C:106
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::timer::oldTimeOut_
static unsigned int oldTimeOut_
Old alarm() value.
Definition: timer.H:89
error.H
Foam::Info
messageStream Info
Foam::FatalError
error FatalError
timer.H
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:131
Foam::timer::newTimeOut_
unsigned int newTimeOut_
Current time out value. Needed by macro timedOut.
Definition: timer.H:106
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:318
Foam::timer::envAlarm
static jmp_buf envAlarm
State for setjmp. Needed by macro timedOut.
Definition: timer.H:109
Foam::timer::oldAction_
static struct sigaction oldAction_
Old signal masks.
Definition: timer.H:86
Foam::timer::timer
timer(const unsigned int newTimeOut)
Construct from components.
Definition: timer.C:60
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)