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
29 Implements a timeout mechanism via sigalarm.
33 timer myTimer(5); // 5 sec
35 if (timedOut(myTimer))
41 // do something possible blocking
45 Constructor set signal handler on sigalarm and alarm(). Destructor
48 timedOut is macro because setjmp can't be in member function of timer.
49 ?something to do with stack frames.
52 The setjmp restores complete register state so including local vars
53 held in regs. So if in blocking part something gets calced in a stack
54 based variable make sure it is declared 'volatile'.
59 \*---------------------------------------------------------------------------*/
64 #include "className.H"
69 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
71 //- check it a timeout has occured
72 // keep setjmp in same stack frame so no function calls
74 (((x).newTimeOut_ > 0) ? setjmp(Foam::timer::envAlarm) : false)
79 /*---------------------------------------------------------------------------*\
80 Class timer Declaration
81 \*---------------------------------------------------------------------------*/
88 static struct sigaction oldAction_;
91 static unsigned int oldTimeOut_;
94 // Private Member Functions
97 static void signalHandler(int);
104 //- Declare name of the class and its debug switch
107 //- current time out value. Needed by macro timedOut
108 unsigned int newTimeOut_;
110 //- state for setjmp. Needed by macro timedOut
111 static jmp_buf envAlarm;
116 //- Construct from components.
117 // newTimeOut=0 makes it do nothing.
118 timer(const unsigned int newTimeOut);
127 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
129 } // End namespace Foam
131 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
135 // ************************************************************************* //