initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / OSspecific / POSIX / timer.H
blobdf493c326dab8e777ae4f2d94746696ce5c222db
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
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
19     for more details.
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
25 Class
26     Foam::timer
28 Description
29     Implements a timeout mechanism via sigalarm.
31     Example usage:
32     @code
33         timer myTimer(5);     // 5 sec
34         ..
35         if (timedOut(myTimer))
36         {
37             // timed out
38         }
39         else
40         {
41             // do something possible blocking
42         }
43     @endcode
45     Constructor set signal handler on sigalarm and alarm(). Destructor
46     clears these.
48     timedOut is macro because setjmp can't be in member function of timer.
49     ?something to do with stack frames.
51 Warning
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'.
56 SourceFiles
57     timer.C
59 \*---------------------------------------------------------------------------*/
61 #ifndef timer_H
62 #define timer_H
64 #include "className.H"
66 #include <signal.h>
67 #include <setjmp.h>
69 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
71 //- check it a timeout has occured
72 //  keep setjmp in same stack frame so no function calls
73 #define timedOut(x) \
74     (((x).newTimeOut_ > 0) ? setjmp(Foam::timer::envAlarm) : false)
76 namespace Foam
79 /*---------------------------------------------------------------------------*\
80                            Class timer Declaration
81 \*---------------------------------------------------------------------------*/
83 class timer
85     // Private data
87         //- old signal masks
88         static struct sigaction oldAction_;
90         //- old alarm() value
91         static unsigned int oldTimeOut_;
94     // Private Member Functions
96         //- alarm handler
97         static void signalHandler(int);
100 public:
102     // Public data
104         //- Declare name of the class and its debug switch
105         ClassName("timer");
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;
114     // Constructors
116         //- Construct from components.
117         //  newTimeOut=0 makes it do nothing.
118         timer(const unsigned int newTimeOut);
121     // Destructor
123         ~timer();
127 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
129 } // End namespace Foam
131 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
133 #endif
135 // ************************************************************************* //