|
|
|
timer.HGo 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-2009 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 Foam::timer 00027 00028 Description 00029 Implements a timeout mechanism via sigalarm. 00030 00031 Example usage: 00032 @code 00033 timer myTimer(5); // 5 sec 00034 .. 00035 if (timedOut(myTimer)) 00036 { 00037 // timed out 00038 } 00039 else 00040 { 00041 // do something possible blocking 00042 } 00043 @endcode 00044 00045 Constructor set signal handler on sigalarm and alarm(). Destructor 00046 clears these. 00047 00048 timedOut is macro because setjmp can't be in member function of timer. 00049 ?something to do with stack frames. 00050 00051 Warning 00052 The setjmp restores complete register state so including local vars 00053 held in regs. So if in blocking part something gets calced in a stack 00054 based variable make sure it is declared 'volatile'. 00055 00056 SourceFiles 00057 timer.C 00058 00059 \*---------------------------------------------------------------------------*/ 00060 00061 #ifndef timer_H 00062 #define timer_H 00063 00064 #include "className.H" 00065 00066 #include <signal.h> 00067 #include <setjmp.h> 00068 00069 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // 00070 00071 //- check it a timeout has occured 00072 // keep setjmp in same stack frame so no function calls 00073 #define timedOut(x) \ 00074 (((x).newTimeOut_ > 0) ? setjmp(Foam::timer::envAlarm) : false) 00075 00076 namespace Foam 00077 { 00078 00079 /*---------------------------------------------------------------------------*\ 00080 Class timer Declaration 00081 \*---------------------------------------------------------------------------*/ 00082 00083 class timer 00084 { 00085 // Private data 00086 00087 //- old signal masks 00088 static struct sigaction oldAction_; 00089 00090 //- old alarm() value 00091 static unsigned int oldTimeOut_; 00092 00093 00094 // Private Member Functions 00095 00096 //- alarm handler 00097 static void signalHandler(int); 00098 00099 00100 public: 00101 00102 // Public data 00103 00104 //- Declare name of the class and its debug switch 00105 ClassName("timer"); 00106 00107 //- current time out value. Needed by macro timedOut 00108 unsigned int newTimeOut_; 00109 00110 //- state for setjmp. Needed by macro timedOut 00111 static jmp_buf envAlarm; 00112 00113 00114 // Constructors 00115 00116 //- Construct from components. 00117 // newTimeOut=0 makes it do nothing. 00118 timer(const unsigned int newTimeOut); 00119 00120 00121 // Destructor 00122 00123 ~timer(); 00124 }; 00125 00126 00127 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // 00128 00129 } // End namespace Foam 00130 00131 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // 00132 00133 #endif 00134 00135 // ************************************************************************* // |