OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

timer.C

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-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 Description
00026 
00027 \*---------------------------------------------------------------------------*/
00028 
00029 #include <unistd.h>
00030 
00031 #include "error.H"
00032 #include "timer.H"
00033 
00034 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
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 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
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 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00058 
00059 
00060 // Construct from components
00061 Foam::timer::timer(const unsigned int newTimeOut)
00062 :
00063     newTimeOut_(newTimeOut)
00064 {
00065 
00066     if (newTimeOut > 0)
00067     {
00068         // Is singleton since handler is static function
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         // Install alarm signal handler:
00079         // - do not block any signals while in it
00080         // - clear list of signals to mask
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 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
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         // Reset timer
00123         ::alarm(oldTimeOut_);
00124         oldTimeOut_ = 0;
00125 
00126         // Restore signal handler
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