initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / OSspecific / POSIX / timer.C
blobb61efa5ed9a032c1f9273d376a21e7e6385ac6cd
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
11     OpenFOAM is free software; you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
16     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19     for more details.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 Description
27 \*---------------------------------------------------------------------------*/
29 #include <unistd.h>
31 #include "error.H"
32 #include "timer.H"
34 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
36 defineTypeNameAndDebug(Foam::timer, 0);
38 jmp_buf Foam::timer::envAlarm;
40 struct sigaction Foam::timer::oldAction_;
42 unsigned int Foam::timer::oldTimeOut_ = 0;
44 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
46 void Foam::timer::signalHandler(int)
47
48     if (debug)
49     {
50         Info<< "Foam::timer::signalHandler(int sig) : "
51             << " timed out. Jumping."
52             << endl;
53     }
54     longjmp(envAlarm, 1);
57 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
60 // Construct from components
61 Foam::timer::timer(const unsigned int newTimeOut)
63     newTimeOut_(newTimeOut)
66     if (newTimeOut > 0)
67     {
68         // Is singleton since handler is static function
69         if (oldTimeOut_ != 0)
70         {
71             FatalErrorIn
72             (
73                 "Foam::timer::timer(const unsigned int)"
74             )   << "timer already used."
75                 << abort(FatalError);    
76         }
78         // Install alarm signal handler:
79         // - do not block any signals while in it
80         // - clear list of signals to mask
81         struct sigaction newAction;
82         newAction.sa_handler = timer::signalHandler;
83             newAction.sa_flags = SA_NODEFER;
84         sigemptyset(&newAction.sa_mask);
86         if (sigaction(SIGALRM, &newAction, &oldAction_) < 0)
87         {
88             FatalErrorIn
89             (
90                 "Foam::timer::timer(const unsigned int)"
91             )   << "sigaction(SIGALRM) error"
92                 << abort(FatalError);    
93         }
95         oldTimeOut_ = ::alarm(newTimeOut);
97         if (debug)
98         {
99             Info<< "Foam::timer::timer(const unsigned int) : "
100                 << " installing timeout " << int(newTimeOut_)
101                 << " seconds"
102                 << " (overriding old timeout " << int(oldTimeOut_)
103                 << ")." << endl;
104         }
105     }
109 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
111 Foam::timer::~timer()
113     if (newTimeOut_ > 0)
114     {
115         if (debug)
116         {
117             Info<< "Foam::timer::~timer(const unsigned int) : timeOut="
118                 << int(newTimeOut_)
119                 << " : resetting timeOut to " << int(oldTimeOut_) << endl;
120         }
122         // Reset timer
123         ::alarm(oldTimeOut_);
124         oldTimeOut_ = 0;
126         // Restore signal handler
127         if (sigaction(SIGALRM, &oldAction_, NULL) < 0)
128         {
129             FatalErrorIn
130             (
131                 "Foam::timer::~timer(const struct sigaction&"
132                 "const struct sigaction&)"
133             )   << "sigaction(SIGALRM) error"
134                 << abort(FatalError);    
135         }
136     }
139 // ************************************************************************* //