1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
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/>.
28 Implements a timeout mechanism via sigalarm.
32 timer myTimer(5); // 5 sec
34 if (timedOut(myTimer))
40 // do something possible blocking
44 Constructor set signal handler on sigalarm and alarm(). Destructor
47 timedOut is macro because setjmp can't be in member function of timer.
48 ?something to do with stack frames.
51 The setjmp restores complete register state so including local vars
52 held in regs. So if in blocking part something gets calced in a stack
53 based variable make sure it is declared 'volatile'.
58 \*---------------------------------------------------------------------------*/
63 #include "className.H"
68 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
70 //- check it a timeout has occured
71 // keep setjmp in same stack frame so no function calls
73 (((x).newTimeOut_ > 0) ? setjmp(Foam::timer::envAlarm) : false)
78 /*---------------------------------------------------------------------------*\
79 Class timer Declaration
80 \*---------------------------------------------------------------------------*/
87 static struct sigaction oldAction_;
90 static unsigned int oldTimeOut_;
93 // Private Member Functions
96 static void signalHandler(int);
103 //- Declare name of the class and its debug switch
106 //- current time out value. Needed by macro timedOut
107 unsigned int newTimeOut_;
109 //- state for setjmp. Needed by macro timedOut
110 static jmp_buf envAlarm;
115 //- Construct from components.
116 // newTimeOut=0 makes it do nothing.
117 timer(const unsigned int newTimeOut);
125 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
127 } // End namespace Foam
129 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
133 // ************************************************************************* //