FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

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-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 Description
00025 
00026 \*---------------------------------------------------------------------------*/
00027 
00028 #include <unistd.h>
00029 
00030 #include <OpenFOAM/error.H>
00031 #include "timer.H"
00032 
00033 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00034 
00035 defineTypeNameAndDebug(Foam::timer, 0);
00036 
00037 jmp_buf Foam::timer::envAlarm;
00038 
00039 struct sigaction Foam::timer::oldAction_;
00040 
00041 unsigned int Foam::timer::oldTimeOut_ = 0;
00042 
00043 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
00044 
00045 void Foam::timer::signalHandler(int)
00046 { 
00047     if (debug)
00048     {
00049         Info<< "Foam::timer::signalHandler(int sig) : "
00050             << " timed out. Jumping."
00051             << endl;
00052     }
00053     longjmp(envAlarm, 1);
00054 }
00055 
00056 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00057 
00058 
00059 // Construct from components
00060 Foam::timer::timer(const unsigned int newTimeOut)
00061 :
00062     newTimeOut_(newTimeOut)
00063 {
00064 
00065     if (newTimeOut > 0)
00066     {
00067         // Is singleton since handler is static function
00068         if (oldTimeOut_ != 0)
00069         {
00070             FatalErrorIn
00071             (
00072                 "Foam::timer::timer(const unsigned int)"
00073             )   << "timer already used."
00074                 << abort(FatalError);    
00075         }
00076 
00077         // Install alarm signal handler:
00078         // - do not block any signals while in it
00079         // - clear list of signals to mask
00080         struct sigaction newAction;
00081         newAction.sa_handler = timer::signalHandler;
00082         newAction.sa_flags = SA_NODEFER;
00083         sigemptyset(&newAction.sa_mask);
00084 
00085         if (sigaction(SIGALRM, &newAction, &oldAction_) < 0)
00086         {
00087             FatalErrorIn
00088             (
00089                 "Foam::timer::timer(const unsigned int)"
00090             )   << "sigaction(SIGALRM) error"
00091                 << abort(FatalError);    
00092         }
00093 
00094         oldTimeOut_ = ::alarm(newTimeOut);
00095 
00096         if (debug)
00097         {
00098             Info<< "Foam::timer::timer(const unsigned int) : "
00099                 << " installing timeout " << int(newTimeOut_)
00100                 << " seconds"
00101                 << " (overriding old timeout " << int(oldTimeOut_)
00102                 << ")." << endl;
00103         }
00104     }
00105 }
00106 
00107 
00108 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00109 
00110 Foam::timer::~timer()
00111 {
00112     if (newTimeOut_ > 0)
00113     {
00114         if (debug)
00115         {
00116             Info<< "Foam::timer::~timer(const unsigned int) : timeOut="
00117                 << int(newTimeOut_)
00118                 << " : resetting timeOut to " << int(oldTimeOut_) << endl;
00119         }
00120 
00121         // Reset timer
00122         ::alarm(oldTimeOut_);
00123         oldTimeOut_ = 0;
00124 
00125         // Restore signal handler
00126         if (sigaction(SIGALRM, &oldAction_, NULL) < 0)
00127         {
00128             FatalErrorIn
00129             (
00130                 "Foam::timer::~timer(const struct sigaction&"
00131                 "const struct sigaction&)"
00132             )   << "sigaction(SIGALRM) error"
00133                 << abort(FatalError);    
00134         }
00135     }
00136 }
00137 
00138 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines