sigWriteNow.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2020 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
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.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "sigWriteNow.H"
27 #include "error.H"
28 #include "jobInfo.H"
29 #include "IOstreams.H"
30 #include "Time.H"
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36 
37 // Signal number to catch
38 int sigWriteNow::signal_
39 (
40  debug::optimisationSwitch("writeNowSignal", -1)
41 );
42 
43 } // End namespace Foam
44 
45 
46 Foam::Time* Foam::sigWriteNow::runTimePtr_ = nullptr;
47 struct sigaction Foam::sigWriteNow::oldAction_;
48 
49 
50 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
51 
52 void Foam::sigWriteNow::sigHandler(int)
53 {
54  Info<< "sigWriteNow :"
55  << " setting up write at end of the next iteration" << nl << endl;
56  runTimePtr_->writeOnce();
57 
59  // raise(signal_);
60 }
61 
62 
63 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
64 
66 {}
67 
68 
69 Foam::sigWriteNow::sigWriteNow(const bool verbose, Time& runTime)
70 {
71  // Store runTime
72  runTimePtr_ = &runTime;
73 
74  set(verbose);
75 }
76 
77 
78 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
79 
81 {
82  // Reset old handling
83  if (signal_ > 0)
84  {
85  if (sigaction(signal_, &oldAction_, nullptr) < 0)
86  {
88  << "Cannot reset " << signal_ << " trapping"
89  << abort(FatalError);
90  }
91  }
92 }
93 
94 
95 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
96 
97 void Foam::sigWriteNow::set(const bool verbose)
98 {
99  if (signal_ >= 0)
100  {
101  struct sigaction newAction;
102  newAction.sa_handler = sigHandler;
103  newAction.sa_flags = SA_NODEFER;
104  sigemptyset(&newAction.sa_mask);
105  if (sigaction(signal_, &newAction, &oldAction_) < 0)
106  {
108  << "Cannot set " << signal_ << " trapping"
109  << abort(FatalError);
110  }
111 
112  if (verbose)
113  {
114  Info<< "sigWriteNow :"
115  << " Enabling writing upon signal " << signal_
116  << endl;
117  }
118  }
119 }
120 
121 
123 {
124  return signal_ > 0;
125 }
126 
127 
128 // ************************************************************************* //
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:76
~sigWriteNow()
Destructor.
Definition: sigWriteNow.C:80
bool active() const
Is active?
Definition: sigWriteNow.C:122
static void set(const bool verbose)
(re)set signal catcher
Definition: sigWriteNow.C:97
sigWriteNow()
Construct null.
Definition: sigWriteNow.C:65
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
int optimisationSwitch(const char *name, const int defaultValue=0)
Lookup optimisation switch or add default value.
Definition: debug.C:255
Namespace for OpenFOAM.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:257
errorManip< error > abort(error &err)
Definition: errorManip.H:131
messageStream Info
error FatalError
static const char nl
Definition: Ostream.H:266