1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | foam-extend: Open Source CFD
4 \\ / O peration | Version: 3.2
5 \\ / A nd | Web: http://www.foam-extend.org
6 \\/ M anipulation | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
9 This file is part of foam-extend.
11 foam-extend 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 3 of the License, or (at your
14 option) any later version.
16 foam-extend is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with foam-extend. 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);
126 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
128 } // End namespace Foam
130 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
134 // ************************************************************************* //