OpenFOAM logo
Open Source CFD Toolkit

timer.H

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2005 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 Class
00026     timer
00027 
00028 Description
00029     Implements a timeout mechanism. Usage is as following:
00030 
00031     timer myTimer(5);     // 5 sec
00032     ..
00033     if (timedOut(myTimer))
00034     {
00035         // timed out
00036     }
00037     else
00038     {
00039         // do something possible blocking
00040     }
00041 
00042     Constructor set signal handler on sigalarm and alarm(). Destructor
00043     clears these.
00044 
00045     timedOut is macro because setjmp can't be in member function of timer.
00046     ?something to do with stack frames.
00047 
00048     WARNING: setjmp restores complete register state so including local vars
00049     held in regs. So if in blocking part something gets calced in a stack
00050     based variable make sure it is declared 'volatile'.
00051 
00052 SourceFiles
00053     timer.C
00054 
00055 \*---------------------------------------------------------------------------*/
00056 
00057 #ifndef timer_H
00058 #define timer_H
00059 
00060 #include "className.H"
00061 
00062 #include <signal.h>
00063 #include <setjmp.h>
00064 
00065 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00066 
00067 // keep setjmp in same stack frame so no function calls
00068 #define timedOut(x) \
00069     (((x).newTimeOut_ > 0) ? setjmp(Foam::timer::envAlarm) : false)
00070 
00071 namespace Foam
00072 {
00073 
00074 /*---------------------------------------------------------------------------*\
00075                            Class timer Declaration
00076 \*---------------------------------------------------------------------------*/
00077 
00078 class timer
00079 {
00080     // Private data
00081 
00082         //- old signal masks
00083         static struct sigaction oldAction_;
00084 
00085         //- old alarm() value
00086         static unsigned int oldTimeOut_;
00087 
00088 
00089     // Private Member Functions
00090 
00091         //- alarm handler
00092         static void signalHandler(int);
00093 
00094 
00095 public:
00096 
00097     // Public data
00098 
00099         //- Declare name of the class and it's debug switch
00100         ClassName("timer");
00101 
00102         //- current time out value. Needed by macro timedOut
00103         unsigned int newTimeOut_;
00104 
00105         //- state for setjmp. Needed by macro timedOut
00106         static jmp_buf envAlarm;
00107 
00108 
00109     // Constructors
00110 
00111         //- Construct from components.
00112         //  newTimeOut=0 makes it do nothing.
00113         timer(const unsigned int newTimeOut);
00114 
00115 
00116     // Destructor
00117 
00118         ~timer();
00119 };
00120 
00121 
00122 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00123 
00124 } // End namespace Foam
00125 
00126 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00127 
00128 #endif
00129 
00130 // ************************************************************************* //
For further information go to www.openfoam.org