1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 1991-2008 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 the
13 Free Software Foundation; either version 2 of the License, or (at your
14 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, write to the Free Software Foundation,
23 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 \*---------------------------------------------------------------------------*/
34 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
36 defineTypeNameAndDebug(Foam::timer, 0);
38 jmp_buf Foam::timer::envAlarm;
40 struct sigaction Foam::timer::oldAction_;
42 unsigned int Foam::timer::oldTimeOut_ = 0;
44 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
46 void Foam::timer::signalHandler(int)
50 Info<< "Foam::timer::signalHandler(int sig) : "
51 << " timed out. Jumping."
57 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
60 // Construct from components
61 Foam::timer::timer(const unsigned int newTimeOut)
63 newTimeOut_(newTimeOut)
68 // Is singleton since handler is static function
73 "Foam::timer::timer(const unsigned int)"
74 ) << "timer already used."
78 // Install alarm signal handler:
79 // - do not block any signals while in it
80 // - clear list of signals to mask
81 struct sigaction newAction;
82 newAction.sa_handler = timer::signalHandler;
83 newAction.sa_flags = SA_NODEFER;
84 sigemptyset(&newAction.sa_mask);
86 if (sigaction(SIGALRM, &newAction, &oldAction_) < 0)
90 "Foam::timer::timer(const unsigned int)"
91 ) << "sigaction(SIGALRM) error"
95 oldTimeOut_ = ::alarm(newTimeOut);
99 Info<< "Foam::timer::timer(const unsigned int) : "
100 << " installing timeout " << int(newTimeOut_)
102 << " (overriding old timeout " << int(oldTimeOut_)
109 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
111 Foam::timer::~timer()
117 Info<< "Foam::timer::~timer(const unsigned int) : timeOut="
119 << " : resetting timeOut to " << int(oldTimeOut_) << endl;
123 ::alarm(oldTimeOut_);
126 // Restore signal handler
127 if (sigaction(SIGALRM, &oldAction_, NULL) < 0)
131 "Foam::timer::~timer(const struct sigaction&"
132 "const struct sigaction&)"
133 ) << "sigaction(SIGALRM) error"
134 << abort(FatalError);
139 // ************************************************************************* //