FreeFOAM The Cross-Platform CFD Toolkit
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) 1991-2010 OpenCFD Ltd.
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 Description
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include <unistd.h>
29 
30 #include <OpenFOAM/error.H>
31 #include "timer.H"
32 
33 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 
36 
37 jmp_buf Foam::timer::envAlarm;
38 
39 struct sigaction Foam::timer::oldAction_;
40 
41 unsigned int Foam::timer::oldTimeOut_ = 0;
42 
43 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
44 
45 void Foam::timer::signalHandler(int)
46 {
47  if (debug)
48  {
49  Info<< "Foam::timer::signalHandler(int sig) : "
50  << " timed out. Jumping."
51  << endl;
52  }
53  longjmp(envAlarm, 1);
54 }
55 
56 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
57 
58 
59 // Construct from components
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  (
72  "Foam::timer::timer(const unsigned int)"
73  ) << "timer already used."
74  << abort(FatalError);
75  }
76 
77  // Install alarm signal handler:
78  // - do not block any signals while in it
79  // - clear list of signals to mask
80  struct sigaction newAction;
81  newAction.sa_handler = timer::signalHandler;
82  newAction.sa_flags = SA_NODEFER;
83  sigemptyset(&newAction.sa_mask);
84 
85  if (sigaction(SIGALRM, &newAction, &oldAction_) < 0)
86  {
88  (
89  "Foam::timer::timer(const unsigned int)"
90  ) << "sigaction(SIGALRM) error"
91  << abort(FatalError);
92  }
93 
94  oldTimeOut_ = ::alarm(newTimeOut);
95 
96  if (debug)
97  {
98  Info<< "Foam::timer::timer(const unsigned int) : "
99  << " installing timeout " << int(newTimeOut_)
100  << " seconds"
101  << " (overriding old timeout " << int(oldTimeOut_)
102  << ")." << endl;
103  }
104  }
105 }
106 
107 
108 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
109 
111 {
112  if (newTimeOut_ > 0)
113  {
114  if (debug)
115  {
116  Info<< "Foam::timer::~timer(const unsigned int) : timeOut="
117  << int(newTimeOut_)
118  << " : resetting timeOut to " << int(oldTimeOut_) << endl;
119  }
120 
121  // Reset timer
122  ::alarm(oldTimeOut_);
123  oldTimeOut_ = 0;
124 
125  // Restore signal handler
126  if (sigaction(SIGALRM, &oldAction_, NULL) < 0)
127  {
129  (
130  "Foam::timer::~timer(const struct sigaction&"
131  "const struct sigaction&)"
132  ) << "sigaction(SIGALRM) error"
133  << abort(FatalError);
134  }
135  }
136 }
137 
138 // ************************ vim: set sw=4 sts=4 et: ************************ //