PISM, A Parallel Ice Sheet Model  stable v2.1-1-g6902d5502 committed by Ed Bueler on 2023-12-20 08:38:27 -0800
pism_python.cc
Go to the documentation of this file.
1 // Copyright (C) 2011, 2014, 2015, 2016, 2017, 2023 David Maxwell and Constantine Khroulev
2 //
3 // This file is part of PISM.
4 //
5 // PISM is free software; you can redistribute it and/or modify it under the
6 // terms of the GNU General Public License as published by the Free Software
7 // Foundation; either version 3 of the License, or (at your option) any later
8 // version.
9 //
10 // PISM is distributed in the hope that it will be useful, but WITHOUT ANY
11 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 // details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with PISM; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 
19 #include <signal.h>
20 #include <errno.h>
21 #include <petscsys.h>
22 
23 #include "pism/pythonbindings/pism_python.hh"
24 #include "pism/util/error_handling.hh"
25 
26 namespace pism {
27 namespace python {
28 
29 void set_abort_on_sigint(bool abort) {
30  gSIGINT_is_fatal = abort;
31 }
32 
33 SigInstaller::SigInstaller(int sig, void (*new_handler)(int)) {
34  m_old_handler = signal(sig, new_handler);
35  m_sig = sig;
36 }
37 
39  if (m_old_handler != nullptr) {
40  signal(m_sig, m_old_handler);
41  }
42  m_old_handler = NULL;
43 }
44 
46  release();
47 }
48 
49 int gSignalSet = 0;
51 
52 int check_signal() {
53  int rv = gSignalSet;
54  if (rv != 0) {
55  gSignalSet = 0;
56  }
57  return 0;
58 }
59 
60 void sigint_handler(int sig) {
61  if (sig == SIGINT) {
62  if (gSIGINT_is_fatal) {
63  throw pism::RuntimeError(ErrorLocation(), "caught signal SIGTERM.");
64  }
65 
66  PetscPrintf(PETSC_COMM_WORLD, "\nCaught signal SIGTERM, waiting to exit.\n");
67  gSignalSet = SIGINT;
68  }
69 }
70 
71 } // end of namespace python
72 } // end of namespace pism
void release()
Restores the signal handler to its previous value.
Definition: pism_python.cc:38
SigInstaller(int sig, void(*new_handler)(int))
Installs handle new_handler for signal sig.
Definition: pism_python.cc:33
void sigint_handler(int sig)
Definition: pism_python.cc:60
void set_abort_on_sigint(bool abort)
Definition: pism_python.cc:29
bool gSIGINT_is_fatal
Definition: pism_python.cc:50
int check_signal()
Definition: pism_python.cc:52