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