1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 2004-2010 OpenCFD Ltd.
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
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.
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
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/>.
24 \*---------------------------------------------------------------------------*/
31 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 defineTypeNameAndDebug(Foam::timer, 0);
35 jmp_buf Foam::timer::envAlarm;
37 struct sigaction Foam::timer::oldAction_;
39 unsigned int Foam::timer::oldTimeOut_ = 0;
41 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
43 void Foam::timer::signalHandler(int)
47 Info<< "Foam::timer::signalHandler(int sig) : "
48 << " timed out. Jumping."
54 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
56 Foam::timer::timer(const unsigned int newTimeOut)
58 newTimeOut_(newTimeOut)
63 // Is singleton since handler is static function
68 "Foam::timer::timer(const unsigned int)"
69 ) << "timer already used."
73 // Install alarm signal handler:
74 // - do not block any signals while in it
75 // - clear list of signals to mask
76 struct sigaction newAction;
77 newAction.sa_handler = timer::signalHandler;
78 newAction.sa_flags = SA_NODEFER;
79 sigemptyset(&newAction.sa_mask);
81 if (sigaction(SIGALRM, &newAction, &oldAction_) < 0)
85 "Foam::timer::timer(const unsigned int)"
86 ) << "sigaction(SIGALRM) error"
90 oldTimeOut_ = ::alarm(newTimeOut);
94 Info<< "Foam::timer::timer(const unsigned int) : "
95 << " installing timeout " << int(newTimeOut_)
97 << " (overriding old timeout " << int(oldTimeOut_)
104 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
106 Foam::timer::~timer()
112 Info<< "Foam::timer::~timer(const unsigned int) : timeOut="
114 << " : resetting timeOut to " << int(oldTimeOut_) << endl;
118 ::alarm(oldTimeOut_);
121 // Restore signal handler
122 if (sigaction(SIGALRM, &oldAction_, NULL) < 0)
126 "Foam::timer::~timer(const struct sigaction&"
127 "const struct sigaction&)"
128 ) << "sigaction(SIGALRM) error"
129 << abort(FatalError);
134 // ************************************************************************* //