tpism_python.cc - pism - [fork] customized build of PISM, the parallel ice sheet model (tillflux branch)
HTML git clone git://src.adamsgaard.dk/pism
DIR Log
DIR Files
DIR Refs
DIR LICENSE
---
tpism_python.cc (1859B)
---
1 // Copyright (C) 2011, 2014, 2015, 2016, 2017 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_python.hh"
24 #include "util/pism_utilities.hh"
25 #include "util/error_handling.hh"
26
27 namespace pism {
28 namespace python {
29
30 void set_abort_on_sigint(bool abort) {
31 gSIGINT_is_fatal = abort;
32 }
33
34 SigInstaller::SigInstaller(int sig, void (*new_handler)(int) ) {
35 m_old_handler = signal(sig, new_handler);
36 m_sig = sig;
37 }
38
39 void SigInstaller::release() {
40 if (m_old_handler) {
41 signal(m_sig, m_old_handler);
42 }
43 m_old_handler = NULL;
44 }
45
46 SigInstaller::~SigInstaller() {
47 release();
48 }
49
50 int gSignalSet = 0;
51 bool gSIGINT_is_fatal;
52
53 int check_signal() {
54 int rv = gSignalSet;
55 if (rv) {
56 gSignalSet = 0;
57 }
58 return 0;
59 }
60
61 void sigint_handler(int sig) {
62 if (sig == SIGINT) {
63 if (gSIGINT_is_fatal) {
64 throw pism::RuntimeError(ErrorLocation(), "caught signal SIGTERM.");
65 } else {
66 PetscPrintf(PETSC_COMM_WORLD, "\nCaught signal SIGTERM, waiting to exit.\n");
67 gSignalSet = SIGINT;
68 }
69 }
70 }
71
72 } // end of namespace python
73 } // end of namespace pism