diff --git a/src/OSspecific/POSIX/Make/files b/src/OSspecific/POSIX/Make/files index 06df47630ffa8349ce2acf3718d025b4e4147d17..1f91738212e947d73bac27490465839af9aa9878 100644 --- a/src/OSspecific/POSIX/Make/files +++ b/src/OSspecific/POSIX/Make/files @@ -3,14 +3,16 @@ clockValue/clockValue.C cpuInfo/cpuInfo.C cpuTime/cpuTime.C memInfo/memInfo.C + signals/sigFpe.C signals/sigSegv.C signals/sigInt.C signals/sigQuit.C signals/sigStopAtWriteNow.C signals/sigWriteNow.C +signals/timer.C + regExpPosix.C -timer.C fileStat.C POSIX.C diff --git a/src/OSspecific/POSIX/cpuTime/cpuTime.H b/src/OSspecific/POSIX/cpuTime/cpuTime.H index 3643d5332e571594f1d9f0ccf542cba80900fafd..d4be85e7f051b768750ee8704817bd9e2c994c09 100644 --- a/src/OSspecific/POSIX/cpuTime/cpuTime.H +++ b/src/OSspecific/POSIX/cpuTime/cpuTime.H @@ -41,7 +41,7 @@ SourceFiles #define cpuTime_H #include <string> -#include <time.h> +#include <ctime> #include <sys/times.h> // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // diff --git a/src/OSspecific/POSIX/fileMonitor.C b/src/OSspecific/POSIX/fileMonitor.C index c750e1633902c7247fec1946b381f006535a00ea..f8f9a46af72a1650ae827c5d8ffac8bd9789aeb0 100644 --- a/src/OSspecific/POSIX/fileMonitor.C +++ b/src/OSspecific/POSIX/fileMonitor.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | + \\ / A nd | Copyright (C) 2018 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011-2016 OpenFOAM Foundation diff --git a/src/OSspecific/POSIX/fileMonitor.H b/src/OSspecific/POSIX/fileMonitor.H index be7f8f25ea0255648b991c5b3646ce12e33759b8..9ccde7f074d3cbdacf292f8db9e1af36677f73c7 100644 --- a/src/OSspecific/POSIX/fileMonitor.H +++ b/src/OSspecific/POSIX/fileMonitor.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | + \\ / A nd | Copyright (C) 2017-2018 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011-2015 OpenFOAM Foundation @@ -54,6 +54,7 @@ SourceFiles namespace Foam { +// Forward Declarations class fileMonitor; class fileMonitorWatcher; @@ -63,7 +64,6 @@ class fileMonitorWatcher; class fileMonitor { - public: // Public data types @@ -79,6 +79,7 @@ public: static const Enum<fileState> fileStateNames_; private: + // Private data //- Whether to use inotify (requires -DFOAM_USE_INOTIFY, see above) diff --git a/src/OSspecific/POSIX/fileStat.C b/src/OSspecific/POSIX/fileStat.C index 23f97cbe9c3aab2e52acb4aa89e41cbd804fc80f..df5c8ae95f4c90969c025ff54626d2e89e06bba1 100644 --- a/src/OSspecific/POSIX/fileStat.C +++ b/src/OSspecific/POSIX/fileStat.C @@ -29,7 +29,6 @@ License #include "IOstreams.H" #include "timer.H" -#include <signal.h> #include <unistd.h> #ifndef darwin #include <sys/sysmacros.h> diff --git a/src/OSspecific/POSIX/signals/sigFpe.C b/src/OSspecific/POSIX/signals/sigFpe.C index cf8b5b161e8900eef9e48f4ba0db998e85bcc65e..abc3ae4f50ac79a456c8c6dd85c59f4afd463584 100644 --- a/src/OSspecific/POSIX/signals/sigFpe.C +++ b/src/OSspecific/POSIX/signals/sigFpe.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2016-2018 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2016-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011-2015 OpenFOAM Foundation @@ -33,6 +33,8 @@ License #include "Switch.H" #include "UList.H" +#include <limits> + #if defined(__linux__) && defined(__GNUC__) #ifndef __USE_GNU #define __USE_GNU // To use feenableexcept() @@ -47,7 +49,9 @@ License #include "feexceptErsatz.H" #endif -#include <limits> +// File-local functions +#include "signalMacros.C" + // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // @@ -57,8 +61,6 @@ bool Foam::sigFpe::switchNan_(Foam::debug::optimisationSwitch("setNaN", 0)); bool Foam::sigFpe::sigActive_ = false; bool Foam::sigFpe::nanActive_ = false; -struct sigaction Foam::sigFpe::oldAction_; - // * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * // @@ -123,17 +125,11 @@ void Foam::sigFpe::sigHandler(int) { #if (defined(__linux__) && defined(__GNUC__)) || defined(darwin) - // Reset old handling - if (sigaction(SIGFPE, &oldAction_, nullptr) < 0) - { - FatalErrorInFunction - << "Cannot reset SIGFPE trapping" - << abort(FatalError); - } + resetHandler("SIGFPE", SIGFPE); jobInfo.signalEnd(); // Update jobInfo file error::printStack(Perr); - raise(SIGFPE); // Throw signal (to old handler) + ::raise(SIGFPE); // Throw signal (to old handler) #endif // (__linux__ && __GNUC__) || darwin } @@ -203,17 +199,7 @@ void Foam::sigFpe::set(bool verbose) | FE_OVERFLOW ); - struct sigaction newAction; - newAction.sa_handler = sigHandler; - newAction.sa_flags = SA_NODEFER; - sigemptyset(&newAction.sa_mask); - - if (sigaction(SIGFPE, &newAction, &oldAction_) < 0) - { - FatalErrorInFunction - << "Cannot set SIGFPE trapping" - << abort(FatalError); - } + setHandler("SIGFPE", SIGFPE, sigHandler); sigActive_ = true; @@ -293,12 +279,7 @@ void Foam::sigFpe::unset(bool verbose) << endl; } - if (sigaction(SIGFPE, &oldAction_, nullptr) < 0) - { - FatalErrorInFunction - << "Cannot reset SIGFPE trapping" - << abort(FatalError); - } + resetHandler("SIGFPE", SIGFPE); // Reset exception raising const int oldExcept = fedisableexcept diff --git a/src/OSspecific/POSIX/signals/sigFpe.H b/src/OSspecific/POSIX/signals/sigFpe.H index a98541d720bae009804ce3043f5454884c845da1..38dd45bf352071192f80316a95ad163484941b41 100644 --- a/src/OSspecific/POSIX/signals/sigFpe.H +++ b/src/OSspecific/POSIX/signals/sigFpe.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2018 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2018-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011-2016 OpenFOAM Foundation @@ -56,7 +56,6 @@ SourceFiles #ifndef sigFpe_H #define sigFpe_H -#include <signal.h> #include "scalar.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -64,7 +63,7 @@ SourceFiles namespace Foam { -// Forward declarations +// Forward Declarations template<class T> class UList; /*---------------------------------------------------------------------------*\ @@ -73,7 +72,7 @@ template<class T> class UList; class sigFpe { - // Private data + // Private Data //- Flag that floating point trapping should be used. // Can override with FOAM_SIGFPE env variable @@ -83,20 +82,16 @@ class sigFpe // Can override with FOAM_SETNAN env variable static bool switchNan_; - //- Flag to indicate floating point trapping is currently active + //- Floating point trapping currently active? static bool sigActive_; //- Flag to indicate mallocNan is currently active static bool nanActive_; - //- Saved old signal trapping setting - static struct sigaction oldAction_; - // Private Member Functions - //- Handler for caught signals. - // Ends job and prints stack + //- Handler for caught signals - ends job and prints stack static void sigHandler(int); diff --git a/src/OSspecific/POSIX/signals/sigInt.C b/src/OSspecific/POSIX/signals/sigInt.C index f0a997a0103c173a0be9886e1c8365a04c2f358c..c66a91408b54e4e63892edf3f200a661f4bac93f 100644 --- a/src/OSspecific/POSIX/signals/sigInt.C +++ b/src/OSspecific/POSIX/signals/sigInt.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | + \\ / A nd | Copyright (C) 2018-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011-2015 OpenFOAM Foundation @@ -30,9 +30,11 @@ License #include "JobInfo.H" #include "IOstreams.H" -// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // +// File-local functions +#include "signalMacros.C" + -struct sigaction Foam::sigInt::oldAction_; +// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // bool Foam::sigInt::sigActive_ = false; @@ -41,16 +43,10 @@ bool Foam::sigInt::sigActive_ = false; void Foam::sigInt::sigHandler(int) { - // Reset old handling - if (sigaction(SIGINT, &oldAction_, nullptr) < 0) - { - FatalErrorInFunction - << "Cannot reset SIGINT trapping" - << abort(FatalError); - } + resetHandler("SIGINT", SIGINT); jobInfo.signalEnd(); // Update jobInfo file - raise(SIGINT); // Throw signal (to old handler) + ::raise(SIGINT); // Throw signal (to old handler) } @@ -74,35 +70,25 @@ Foam::sigInt::~sigInt() void Foam::sigInt::set(bool) { - if (!sigActive_) + if (sigActive_) { - struct sigaction newAction; - newAction.sa_handler = sigHandler; - newAction.sa_flags = SA_NODEFER; - sigemptyset(&newAction.sa_mask); - if (sigaction(SIGINT, &newAction, &oldAction_) < 0) - { - FatalErrorInFunction - << "Cannot call sigInt::set() more than once" - << abort(FatalError); - } - sigActive_ = true; + return; } + sigActive_ = true; + + setHandler("SIGINT", SIGINT, sigHandler); } void Foam::sigInt::unset(bool) { - if (sigActive_) + if (!sigActive_) { - if (sigaction(SIGINT, &oldAction_, nullptr) < 0) - { - FatalErrorInFunction - << "Cannot set SIGINT trapping" - << abort(FatalError); - } - sigActive_ = false; + return; } + sigActive_ = false; + + resetHandler("SIGINT", SIGINT); } diff --git a/src/OSspecific/POSIX/signals/sigInt.H b/src/OSspecific/POSIX/signals/sigInt.H index b62f52754d697dd74132a1e9eda35e410c28744b..a834362980a5012e75c57e3320462a900a901872 100644 --- a/src/OSspecific/POSIX/signals/sigInt.H +++ b/src/OSspecific/POSIX/signals/sigInt.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | + \\ / A nd | Copyright (C) 2018-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011-2016 OpenFOAM Foundation @@ -47,8 +47,6 @@ SourceFiles #ifndef sigInt_H #define sigInt_H -#include <signal.h> - // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // namespace Foam @@ -62,10 +60,7 @@ class sigInt { // Private data - //- Saved old signal trapping setting - static struct sigaction oldAction_; - - //- Flag to indicate signal trapping is enabled + //- Signal trapping enabled? static bool sigActive_; diff --git a/src/OSspecific/POSIX/signals/sigQuit.C b/src/OSspecific/POSIX/signals/sigQuit.C index 0e18a1c3ec15e7bd70ca33bc71d8b3c355dbb79d..ba4b0c6064e739a0cf6b99a95d7e78892a3ddb1d 100644 --- a/src/OSspecific/POSIX/signals/sigQuit.C +++ b/src/OSspecific/POSIX/signals/sigQuit.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | + \\ / A nd | Copyright (C) 2017-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011-2015 OpenFOAM Foundation @@ -30,9 +30,11 @@ License #include "JobInfo.H" #include "IOstreams.H" -// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // +// File-local functions +#include "signalMacros.C" + -struct sigaction Foam::sigQuit::oldAction_; +// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // bool Foam::sigQuit::sigActive_ = false; @@ -41,17 +43,11 @@ bool Foam::sigQuit::sigActive_ = false; void Foam::sigQuit::sigHandler(int) { - // Reset old handling - if (sigaction(SIGQUIT, &oldAction_, nullptr) < 0) - { - FatalErrorInFunction - << "Cannot reset SIGQUIT trapping" - << abort(FatalError); - } + resetHandler("SIGQUIT", SIGQUIT); jobInfo.signalEnd(); // Update jobInfo file error::printStack(Perr); - raise(SIGQUIT); // Throw signal (to old handler) + ::raise(SIGQUIT); // Throw signal (to old handler) } @@ -75,35 +71,25 @@ Foam::sigQuit::~sigQuit() void Foam::sigQuit::set(bool) { - if (!sigActive_) + if (sigActive_) { - struct sigaction newAction; - newAction.sa_handler = sigHandler; - newAction.sa_flags = SA_NODEFER; - sigemptyset(&newAction.sa_mask); - if (sigaction(SIGQUIT, &newAction, &oldAction_) < 0) - { - FatalErrorInFunction - << "Cannot call more than once" - << abort(FatalError); - } - sigActive_ = true; + return; } + sigActive_ = true; + + setHandler("SIGQUIT", SIGQUIT, sigHandler); } void Foam::sigQuit::unset(bool) { - if (sigActive_) + if (!sigActive_) { - if (sigaction(SIGQUIT, &oldAction_, nullptr) < 0) - { - FatalErrorInFunction - << "Cannot unset SIGQUIT trapping" - << abort(FatalError); - } - sigActive_ = false; + return; } + sigActive_ = false; + + resetHandler("SIGQUIT", SIGQUIT); } diff --git a/src/OSspecific/POSIX/signals/sigQuit.H b/src/OSspecific/POSIX/signals/sigQuit.H index 184786789170f5b0ecd36d16771ab7c33a3049a8..a12940c5009174773f5210e8a40d71385618a835 100644 --- a/src/OSspecific/POSIX/signals/sigQuit.H +++ b/src/OSspecific/POSIX/signals/sigQuit.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | + \\ / A nd | Copyright (C) 2017-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011-2016 OpenFOAM Foundation @@ -46,8 +46,6 @@ SourceFiles #ifndef sigQuit_H #define sigQuit_H -#include <signal.h> - // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // namespace Foam @@ -59,12 +57,9 @@ namespace Foam class sigQuit { - // Private data - - //- Saved old signal trapping setting - static struct sigaction oldAction_; + // Private Data - //- Flag to indicate signal trapping is enabled + //- Signal trapping enabled? static bool sigActive_; @@ -86,7 +81,7 @@ public: ~sigQuit(); - // Member functions + // Member Functions //- Activate SIGQUIT signal handler static void set(bool verbose=false); diff --git a/src/OSspecific/POSIX/signals/sigSegv.C b/src/OSspecific/POSIX/signals/sigSegv.C index 06ca476bc63536b9352ea874366b1fd3d24617e4..d3e5593df0b3be60abe5d3262ad24402260438d9 100644 --- a/src/OSspecific/POSIX/signals/sigSegv.C +++ b/src/OSspecific/POSIX/signals/sigSegv.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | + \\ / A nd | Copyright (C) 2018-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011-2015 OpenFOAM Foundation @@ -30,9 +30,11 @@ License #include "JobInfo.H" #include "IOstreams.H" -// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // +// File-local functions +#include "signalMacros.C" + -struct sigaction Foam::sigSegv::oldAction_; +// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // bool Foam::sigSegv::sigActive_ = false; @@ -41,17 +43,11 @@ bool Foam::sigSegv::sigActive_ = false; void Foam::sigSegv::sigHandler(int) { - // Reset old handling - if (sigaction(SIGSEGV, &oldAction_, nullptr) < 0) - { - FatalErrorInFunction - << "Cannot reset SIGSEGV trapping" - << abort(FatalError); - } + resetHandler("SIGSEGV", SIGSEGV); jobInfo.signalEnd(); // Update jobInfo file error::printStack(Perr); - raise(SIGSEGV); // Throw signal (to old handler) + ::raise(SIGSEGV); // Throw signal (to old handler) } @@ -75,35 +71,25 @@ Foam::sigSegv::~sigSegv() void Foam::sigSegv::set(bool) { - if (!sigActive_) + if (sigActive_) { - struct sigaction newAction; - newAction.sa_handler = sigHandler; - newAction.sa_flags = SA_NODEFER; - sigemptyset(&newAction.sa_mask); - if (sigaction(SIGSEGV, &newAction, &oldAction_) < 0) - { - FatalErrorInFunction - << "Cannot call more than once" - << abort(FatalError); - } - sigActive_ = true; + return; } + sigActive_ = true; + + setHandler("SIGSEGV", SIGSEGV, sigHandler); } void Foam::sigSegv::unset(bool) { - if (sigActive_) + if (!sigActive_) { - if (sigaction(SIGSEGV, &oldAction_, nullptr) < 0) - { - FatalErrorInFunction - << "Cannot unset SIGSEGV trapping" - << abort(FatalError); - } - sigActive_ = false; + return; } + sigActive_ = false; + + resetHandler("SIGSEGV", SIGSEGV); } diff --git a/src/OSspecific/POSIX/signals/sigSegv.H b/src/OSspecific/POSIX/signals/sigSegv.H index a5f0ae7e85f89f3056f68d0feadcb87828019e13..bbed12192d3df232449b9d73dc527f399993375b 100644 --- a/src/OSspecific/POSIX/signals/sigSegv.H +++ b/src/OSspecific/POSIX/signals/sigSegv.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | + \\ / A nd | Copyright (C) 2018-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011-2016 OpenFOAM Foundation @@ -46,8 +46,6 @@ SourceFiles #ifndef sigSegv_H #define sigSegv_H -#include <signal.h> - // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // namespace Foam @@ -59,12 +57,9 @@ namespace Foam class sigSegv { - // Private data - - //- Saved old signal trapping setting - static struct sigaction oldAction_; + // Private Data - //- Flag to indicate signal trapping is enabled + //- Signal trapping enabled? static bool sigActive_; diff --git a/src/OSspecific/POSIX/signals/sigStopAtWriteNow.C b/src/OSspecific/POSIX/signals/sigStopAtWriteNow.C index b11ba2cf45c435e5a92c7e872c9d2c92df8881ce..6d55136a2d1dbbf1cf4cc019afd7c5531e0ed72e 100644 --- a/src/OSspecific/POSIX/signals/sigStopAtWriteNow.C +++ b/src/OSspecific/POSIX/signals/sigStopAtWriteNow.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | + \\ / A nd | Copyright (C) 2016-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011-2016 OpenFOAM Foundation @@ -25,12 +25,17 @@ License \*---------------------------------------------------------------------------*/ +#include "sigWriteNow.H" #include "sigStopAtWriteNow.H" #include "error.H" #include "JobInfo.H" #include "IOstreams.H" #include "Time.H" +// File-local functions +#include "signalMacros.C" + + // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // // Signal number to catch @@ -39,21 +44,19 @@ int Foam::sigStopAtWriteNow::signal_ Foam::debug::optimisationSwitch("stopAtWriteNowSignal", -1) ); -Foam::Time const* Foam::sigStopAtWriteNow::runTimePtr_ = nullptr; +// Pointer to Time (file-local variable) +static Foam::Time const* runTimePtr_ = nullptr; -struct sigaction Foam::sigStopAtWriteNow::oldAction_; +// * * * * * * * * * * * * * * * Local Classes * * * * * * * * * * * * * * * // namespace Foam { // Register re-reader -class addstopAtWriteNowSignalToOpt +struct addstopAtWriteNowSignalToOpt : public ::Foam::simpleRegIOobject { - -public: - addstopAtWriteNowSignalToOpt(const char* name) : ::Foam::simpleRegIOobject(Foam::debug::addOptimisationObject, name) @@ -85,15 +88,9 @@ addstopAtWriteNowSignalToOpt addstopAtWriteNowSignalToOpt_ void Foam::sigStopAtWriteNow::sigHandler(int) { - // Reset old handling - if (sigaction(signal_, &oldAction_, nullptr) < 0) - { - FatalErrorInFunction - << "Cannot reset " << signal_ << " trapping" - << abort(FatalError); - } + resetHandler("stopAtWriteNow", signal_); - jobInfo.signalEnd(); // Update jobInfo file + jobInfo.signalEnd(); // Update jobInfo file if (runTimePtr_) { @@ -113,7 +110,7 @@ Foam::sigStopAtWriteNow::sigStopAtWriteNow() Foam::sigStopAtWriteNow::sigStopAtWriteNow(const Time& runTime, bool verbose) { - runTimePtr_ = &runTime; // Store runTime + runTimePtr_ = &runTime; // Store runTime set(verbose); } @@ -122,60 +119,54 @@ Foam::sigStopAtWriteNow::sigStopAtWriteNow(const Time& runTime, bool verbose) Foam::sigStopAtWriteNow::~sigStopAtWriteNow() { - // Reset old handling - if (signal_ > 0) + if (!active()) { - if (sigaction(signal_, &oldAction_, nullptr) < 0) - { - FatalErrorInFunction - << "Cannot reset " << signal_ << " trapping" - << abort(FatalError); - } + return; } + + resetHandler("stopAtWriteNow", signal_); } // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // +bool Foam::sigStopAtWriteNow::active() +{ + return signal_ > 0; +} + + +int Foam::sigStopAtWriteNow::signalNumber() +{ + return signal_; +} + + void Foam::sigStopAtWriteNow::set(bool verbose) { - if (signal_ > 0) + if (!active()) { - // Check that the signal is different from the writeNowSignal - if (sigWriteNow::signal_ == signal_) - { - FatalErrorInFunction - << "stopAtWriteNowSignal : " << signal_ - << " cannot be the same as the writeNowSignal." - << " Please change this in the etc/controlDict." - << exit(FatalError); - } - - - struct sigaction newAction; - newAction.sa_handler = sigHandler; - newAction.sa_flags = SA_NODEFER; - sigemptyset(&newAction.sa_mask); - if (sigaction(signal_, &newAction, &oldAction_) < 0) - { - FatalErrorInFunction - << "Cannot set " << signal_ << " trapping" - << abort(FatalError); - } - - if (verbose) - { - Info<< "sigStopAtWriteNow :" - << " Enabling writing and stopping upon signal " << signal_ - << endl; - } + return; } -} + // Check that the signal is different from the writeNowSignal + if (sigWriteNow::signalNumber() == signal_) + { + FatalErrorInFunction + << "stopAtWriteNowSignal : " << signal_ + << " cannot be the same as the writeNowSignal." + << " Please change this in the etc/controlDict." + << exit(FatalError); + } -bool Foam::sigStopAtWriteNow::active() const -{ - return signal_ > 0; + if (verbose) + { + Info<< "sigStopAtWriteNow :" + << " Enabling writing and stopping upon signal " << signal_ + << endl; + } + + setHandler("stopAtWriteNow", signal_, sigHandler); } diff --git a/src/OSspecific/POSIX/signals/sigStopAtWriteNow.H b/src/OSspecific/POSIX/signals/sigStopAtWriteNow.H index fb08a219fd500c54225ddcc3bae5f9a69817033f..0464a6d52a41dac277e23cbf939f7cc91e7a1995 100644 --- a/src/OSspecific/POSIX/signals/sigStopAtWriteNow.H +++ b/src/OSspecific/POSIX/signals/sigStopAtWriteNow.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | + \\ / A nd | Copyright (C) 2016-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011-2016 OpenFOAM Foundation @@ -27,10 +27,8 @@ Class Foam::sigStopAtWriteNow Description - Signal handler for interupt defined by - OptimisationSwitches::stopAtWriteNowSignal - - Write and stop the job. + Signal handler to write and stop the job. + The interrupt is defined by OptimisationSwitches::stopAtWriteNowSignal SourceFiles sigStopAtWriteNow.C @@ -40,32 +38,25 @@ SourceFiles #ifndef sigStopAtWriteNow_H #define sigStopAtWriteNow_H -#include <signal.h> - // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // namespace Foam { +// Forward Declarations class Time; /*---------------------------------------------------------------------------*\ - Class sigStopAtWriteNow Declaration + Class sigStopAtWriteNow Declaration \*---------------------------------------------------------------------------*/ class sigStopAtWriteNow { - // Private data + // Private Data - //- Number of signal to use + //- Signal number to use static int signal_; - //- Pointer to Time - static Time const* runTimePtr_; - - //- Saved old signal trapping setting - static struct sigaction oldAction_; - // Private Member Functions @@ -92,13 +83,16 @@ public: ~sigStopAtWriteNow(); - // Member functions - - //- (re)set signal catcher - static void set(bool verbose=false); + // Member Functions //- Is active? - bool active() const; + static bool active(); + + //- Signal number being used + static int signalNumber(); + + //- Set/reset signal handler + static void set(bool verbose=false); }; diff --git a/src/OSspecific/POSIX/signals/sigWriteNow.C b/src/OSspecific/POSIX/signals/sigWriteNow.C index b47fb07c8002a1e77dac6d03f13c93f3afe31c74..71d309adc5d562a16a99717858cbe1d4afab15c1 100644 --- a/src/OSspecific/POSIX/signals/sigWriteNow.C +++ b/src/OSspecific/POSIX/signals/sigWriteNow.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | + \\ / A nd | Copyright (C) 2016-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011-2016 OpenFOAM Foundation @@ -31,6 +31,10 @@ License #include "IOstreams.H" #include "Time.H" +// File-local functions +#include "signalMacros.C" + + // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // // Signal number to catch @@ -39,22 +43,20 @@ int Foam::sigWriteNow::signal_ Foam::debug::optimisationSwitch("writeNowSignal", -1) ); -Foam::Time* Foam::sigWriteNow::runTimePtr_ = nullptr; +// Pointer to Time (file-local variable) +static Foam::Time* runTimePtr_ = nullptr; -struct sigaction Foam::sigWriteNow::oldAction_; +// * * * * * * * * * * * * * * * Local Classes * * * * * * * * * * * * * * * // namespace Foam { // Register re-reader -class addwriteNowSignalToOpt +struct addwriteNowSignalToOpt : public ::Foam::simpleRegIOobject { - -public: - addwriteNowSignalToOpt(const char* name) : ::Foam::simpleRegIOobject(Foam::debug::addOptimisationObject, name) @@ -100,7 +102,7 @@ Foam::sigWriteNow::sigWriteNow() Foam::sigWriteNow::sigWriteNow(Time& runTime, bool verbose) { - runTimePtr_ = &runTime; // Store runTime + runTimePtr_ = &runTime; // Store runTime set(verbose); } @@ -109,49 +111,43 @@ Foam::sigWriteNow::sigWriteNow(Time& runTime, bool verbose) Foam::sigWriteNow::~sigWriteNow() { - // Reset old handling - if (signal_ > 0) + if (!active()) { - if (sigaction(signal_, &oldAction_, nullptr) < 0) - { - FatalErrorInFunction - << "Cannot reset " << signal_ << " trapping" - << abort(FatalError); - } + return; } + + resetHandler("writeNow", signal_); } // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // +bool Foam::sigWriteNow::active() +{ + return signal_ > 0; +} + + +int Foam::sigWriteNow::signalNumber() +{ + return signal_; +} + + void Foam::sigWriteNow::set(bool verbose) { - if (signal_ >= 0) + if (!active()) { - struct sigaction newAction; - newAction.sa_handler = sigHandler; - newAction.sa_flags = SA_NODEFER; - sigemptyset(&newAction.sa_mask); - if (sigaction(signal_, &newAction, &oldAction_) < 0) - { - FatalErrorInFunction - << "Cannot set " << signal_ << " trapping" - << abort(FatalError); - } - - if (verbose) - { - Info<< "sigWriteNow :" - << " Enabling writing upon signal " << signal_ - << endl; - } + return; } -} + if (verbose) + { + Info<< "sigWriteNow :" + << " Enabling writing upon signal " << signal_ << nl; + } -bool Foam::sigWriteNow::active() const -{ - return signal_ > 0; + setHandler("writeNow", signal_, sigHandler); } diff --git a/src/OSspecific/POSIX/signals/sigWriteNow.H b/src/OSspecific/POSIX/signals/sigWriteNow.H index eec6bc5cc7e32c851d8d2407c8e7b067d46b7a06..8ba2cedc89f0122e67d74e3e0fd6b810967de8aa 100644 --- a/src/OSspecific/POSIX/signals/sigWriteNow.H +++ b/src/OSspecific/POSIX/signals/sigWriteNow.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | + \\ / A nd | Copyright (C) 2016-2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011-2016 OpenFOAM Foundation @@ -27,9 +27,8 @@ Class Foam::sigWriteNow Description - Signal handler for interupt defined by OptimisationSwitches::writeNowSignal - - Write once and continue. + Signal handler to write once and continue. + The interrupt is defined by OptimisationSwitches::writeNowSignal SourceFiles sigWriteNow.C @@ -39,32 +38,25 @@ SourceFiles #ifndef sigWriteNow_H #define sigWriteNow_H -#include <signal.h> - // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // namespace Foam { +// Forward Declarations class Time; /*---------------------------------------------------------------------------*\ - Class sigWriteNow Declaration + Class sigWriteNow Declaration \*---------------------------------------------------------------------------*/ class sigWriteNow { - // Private data + // Private Data - //- Number of signal to use + //- Signal number to use static int signal_; - //- Pointer to Time - static Time* runTimePtr_; - - //- Saved old signal trapping setting - static struct sigaction oldAction_; - // Private Member Functions @@ -74,8 +66,6 @@ class sigWriteNow public: - friend class sigStopAtWriteNow; - //- Allow setter access to signal_ friend class addwriteNowSignalToOpt; @@ -96,9 +86,12 @@ public: // Member Functions //- Is active? - bool active() const; + static bool active(); + + //- The signal number being used + static int signalNumber(); - //- (re)set signal catcher + //- Set/reset signal handler static void set(bool verbose=false); }; diff --git a/src/OSspecific/POSIX/signals/signalMacros.C b/src/OSspecific/POSIX/signals/signalMacros.C new file mode 100644 index 0000000000000000000000000000000000000000..f1982073ba6815542b5c636a6ba23d2f19d03880 --- /dev/null +++ b/src/OSspecific/POSIX/signals/signalMacros.C @@ -0,0 +1,74 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2019 OpenCFD Ltd. + \\/ M anipulation | +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>. + +Description + File-local code for setting/resetting signal handlers. + +SourceFiles + signalMacros.C + +\*---------------------------------------------------------------------------*/ + +#include "error.H" +#include <csignal> + +// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * // + +namespace Foam +{ + +// Saved old signal trapping setting (file-local variable) +static struct sigaction oldAction_; + + +static void resetHandler(const char *what, int sigNum) +{ + if (sigaction(sigNum, &oldAction_, nullptr) < 0) + { + FatalError + << "Cannot unset " << what << " signal (" << sigNum + << ") trapping" << endl + << abort(FatalError); + } +} + + +static void setHandler(const char *what, int sigNum, void (*handler)(int)) +{ + struct sigaction newAction; + newAction.sa_handler = handler; + newAction.sa_flags = SA_NODEFER; + sigemptyset(&newAction.sa_mask); + if (sigaction(sigNum, &newAction, &oldAction_) < 0) + { + FatalError + << "Could not set " << what << " signal (" << sigNum + << ") trapping" << endl + << abort(FatalError); + } +} + +} // End namespace Foam + + +// ************************************************************************* // diff --git a/src/OSspecific/POSIX/signals/timer.C b/src/OSspecific/POSIX/signals/timer.C new file mode 100644 index 0000000000000000000000000000000000000000..182eaa2077ac633a626c3c0be31d16998f62f407 --- /dev/null +++ b/src/OSspecific/POSIX/signals/timer.C @@ -0,0 +1,114 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2004-2010, 2019 OpenCFD Ltd. + \\/ M anipulation | +------------------------------------------------------------------------------- + | Copyright (C) 2011-2016 OpenFOAM Foundation +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>. + +\*---------------------------------------------------------------------------*/ + +#include "error.H" +#include "timer.H" + +#include <unistd.h> + +// File-local functions +#include "signalMacros.C" + + +// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // + +namespace Foam +{ + defineTypeNameAndDebug(timer, 0); +} + +jmp_buf Foam::timer::envAlarm; + +unsigned int Foam::timer::oldTimeOut_ = 0; + + +// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // + +void Foam::timer::sigHandler(int) +{ + DebugInFunction<< "Timed out. Jumping." << endl; + + longjmp(envAlarm, 1); +} + + +// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // + +Foam::timer::timer(unsigned int seconds) +: + timeOut_(seconds) +{ + if (!timeOut_) + { + return; + } + + // Singleton since handler is static function + if (oldTimeOut_) + { + FatalErrorInFunction + << "timer already used." + << abort(FatalError); + } + + // Set alarm signal handler + // - do not block any signals while in it + // - clear list of signals to mask + + setHandler("SIGALRM", SIGALRM, sigHandler); + + // Set alarm timer + oldTimeOut_ = ::alarm(timeOut_); + + DebugInFunction + << "Installing timeout " << int(timeOut_) << " seconds" + << " (overriding old timeout " << int(oldTimeOut_) << ")." << endl; +} + + +// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // + +Foam::timer::~timer() +{ + if (!timeOut_) + { + return; + } + + DebugInFunction + << "timeOut=" << int(timeOut_) + << " : resetting timeOut to " << int(oldTimeOut_) << endl; + + // Reset alarm timer + ::alarm(oldTimeOut_); + oldTimeOut_ = 0; + + resetHandler("SIGALRM", SIGALRM); +} + + +// ************************************************************************* // diff --git a/src/OSspecific/POSIX/timer.H b/src/OSspecific/POSIX/signals/timer.H similarity index 88% rename from src/OSspecific/POSIX/timer.H rename to src/OSspecific/POSIX/signals/timer.H index 50ea74d8ac79a2d3bbe033d261e29b0c6825ee6b..4af28d6007a4d8598b9dde9bb93e91defdd28741 100644 --- a/src/OSspecific/POSIX/timer.H +++ b/src/OSspecific/POSIX/signals/timer.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | + \\ / A nd | Copyright (C) 2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011-2015 OpenFOAM Foundation @@ -46,14 +46,15 @@ Description Constructor set signal handler on sigalarm and alarm(). Destructor clears these. - timedOut is macro because setjmp can't be in member function of timer. - ?something to do with stack frames. - Warning The setjmp restores complete register state so including local vars held in regs. So if in blocking part something gets calced in a stack based variable make sure it is declared 'volatile'. +Note + timedOut is macro because setjmp can't be in member function of timer. + ?something to do with stack frames. + SourceFiles timer.C @@ -63,16 +64,14 @@ SourceFiles #define timer_H #include "className.H" - -#include <signal.h> -#include <setjmp.h> +#include <csetjmp> // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -//- Check it a timeout has occured +//- Check if timeout has occurred // keep setjmp in same stack frame so no function calls #define timedOut(x) \ - (((x).newTimeOut_ > 0) ? setjmp(Foam::timer::envAlarm) : false) + ((x).timeOut_ ? setjmp(Foam::timer::envAlarm) : false) namespace Foam { @@ -83,10 +82,7 @@ namespace Foam class timer { - // Private data - - //- Old signal masks - static struct sigaction oldAction_; + // Private Data //- Old alarm() value static unsigned int oldTimeOut_; @@ -95,18 +91,18 @@ class timer // Private Member Functions //- Alarm handler - static void signalHandler(int); + static void sigHandler(int); public: - // Public data + // Public Data //- Declare name of the class and its debug switch ClassName("timer"); - //- Current time out value. Needed by macro timedOut - unsigned int newTimeOut_; + //- The time-out value (seconds). Needed by macro timedOut + unsigned int timeOut_; //- State for setjmp. Needed by macro timedOut static jmp_buf envAlarm; @@ -118,7 +114,7 @@ public: explicit timer(unsigned int seconds); - //- Destructor + //- Destructor. Restores the alarm and signal handler as required. ~timer(); }; diff --git a/src/OSspecific/POSIX/timer.C b/src/OSspecific/POSIX/timer.C deleted file mode 100644 index e43047abde37199742cef2b7a2ccf3ee63876708..0000000000000000000000000000000000000000 --- a/src/OSspecific/POSIX/timer.C +++ /dev/null @@ -1,133 +0,0 @@ -/*---------------------------------------------------------------------------*\ - ========= | - \\ / F ield | OpenFOAM: The Open Source CFD Toolbox - \\ / O peration | - \\ / A nd | - \\/ M anipulation | -------------------------------------------------------------------------------- - | Copyright (C) 2011-2016 OpenFOAM Foundation -------------------------------------------------------------------------------- -License - This file is part of OpenFOAM. - - OpenFOAM is free software: you can redistribute it and/or modify it - under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - OpenFOAM is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - for more details. - - You should have received a copy of the GNU General Public License - along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>. - -\*---------------------------------------------------------------------------*/ - -#include <unistd.h> - -#include "error.H" -#include "timer.H" - -// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // - -namespace Foam -{ -defineTypeNameAndDebug(timer, 0); - -jmp_buf timer::envAlarm; - -struct sigaction timer::oldAction_; - -unsigned int timer::oldTimeOut_ = 0; -} - - -// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * // - -void Foam::timer::signalHandler(int) -{ - if (debug) - { - InfoInFunction<< "Timed out. Jumping." - << endl; - } - longjmp(envAlarm, 1); -} - -// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // - -Foam::timer::timer(const unsigned int newTimeOut) -: - newTimeOut_(newTimeOut) -{ - - if (newTimeOut > 0) - { - // Is singleton since handler is static function - if (oldTimeOut_ != 0) - { - FatalErrorInFunction - << "timer already used." - << abort(FatalError); - } - - // Install alarm signal handler: - // - do not block any signals while in it - // - clear list of signals to mask - struct sigaction newAction; - newAction.sa_handler = timer::signalHandler; - newAction.sa_flags = SA_NODEFER; - sigemptyset(&newAction.sa_mask); - - if (sigaction(SIGALRM, &newAction, &oldAction_) < 0) - { - FatalErrorInFunction - << "sigaction(SIGALRM) error" - << abort(FatalError); - } - - oldTimeOut_ = ::alarm(newTimeOut); - - if (debug) - { - InfoInFunction - << "Installing timeout " << int(newTimeOut_) - << " seconds" - << " (overriding old timeout " << int(oldTimeOut_) - << ")." << endl; - } - } -} - - -// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // - -Foam::timer::~timer() -{ - if (newTimeOut_ > 0) - { - if (debug) - { - InfoInFunction - << "timeOut=" << int(newTimeOut_) - << " : resetting timeOut to " << int(oldTimeOut_) << endl; - } - - // Reset timer - ::alarm(oldTimeOut_); - oldTimeOut_ = 0; - - // Restore signal handler - if (sigaction(SIGALRM, &oldAction_, nullptr) < 0) - { - FatalErrorInFunction - << "sigaction(SIGALRM) error" - << abort(FatalError); - } - } -} - - -// ************************************************************************* //