timer.C
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include <unistd.h>
00030
00031 #include "error.H"
00032 #include "timer.H"
00033
00034
00035
00036 defineTypeNameAndDebug(Foam::timer, 0);
00037
00038 jmp_buf Foam::timer::envAlarm;
00039
00040 struct sigaction Foam::timer::oldAction_;
00041
00042 unsigned int Foam::timer::oldTimeOut_ = 0;
00043
00044
00045
00046 void Foam::timer::signalHandler(int)
00047 {
00048 if (debug)
00049 {
00050 Info<< "Foam::timer::signalHandler(int sig) : "
00051 << " timed out. Jumping."
00052 << endl;
00053 }
00054 longjmp(envAlarm, 1);
00055 }
00056
00057
00058
00059
00060
00061 Foam::timer::timer(const unsigned int newTimeOut)
00062 :
00063 newTimeOut_(newTimeOut)
00064 {
00065
00066 if (newTimeOut > 0)
00067 {
00068
00069 if (oldTimeOut_ != 0)
00070 {
00071 FatalErrorIn
00072 (
00073 "Foam::timer::timer(const unsigned int)"
00074 ) << "timer already used."
00075 << abort(FatalError);
00076 }
00077
00078
00079
00080
00081 struct sigaction newAction;
00082 newAction.sa_handler = timer::signalHandler;
00083 newAction.sa_flags = SA_NODEFER;
00084 sigemptyset(&newAction.sa_mask);
00085
00086 if (sigaction(SIGALRM, &newAction, &oldAction_) < 0)
00087 {
00088 FatalErrorIn
00089 (
00090 "Foam::timer::timer(const unsigned int)"
00091 ) << "sigaction(SIGALRM) error"
00092 << abort(FatalError);
00093 }
00094
00095 oldTimeOut_ = ::alarm(newTimeOut);
00096
00097 if (debug)
00098 {
00099 Info<< "Foam::timer::timer(const unsigned int) : "
00100 << " installing timeout " << int(newTimeOut_)
00101 << " seconds"
00102 << " (overriding old timeout " << int(oldTimeOut_)
00103 << ")." << endl;
00104 }
00105 }
00106 }
00107
00108
00109
00110
00111 Foam::timer::~timer()
00112 {
00113 if (newTimeOut_ > 0)
00114 {
00115 if (debug)
00116 {
00117 Info<< "Foam::timer::~timer(const unsigned int) : timeOut="
00118 << int(newTimeOut_)
00119 << " : resetting timeOut to " << int(oldTimeOut_) << endl;
00120 }
00121
00122
00123 ::alarm(oldTimeOut_);
00124 oldTimeOut_ = 0;
00125
00126
00127 if (sigaction(SIGALRM, &oldAction_, NULL) < 0)
00128 {
00129 FatalErrorIn
00130 (
00131 "Foam::timer::~timer(const struct sigaction&"
00132 "const struct sigaction&)"
00133 ) << "sigaction(SIGALRM) error"
00134 << abort(FatalError);
00135 }
00136 }
00137 }
00138
00139
Copyright © 2000-2009 OpenCFD Ltd