openEMS/tools
Yifeng Li 840c9755d5 Handle SIGINT for openEMS and Python, with graceful exit support.
Currently, openEMS doesn't have any special code to handle SIGINT (which
is raised by pressing Control-C). By default, the program is terminated
without saving data. This worked okay in the past, but now its
limitations are becoming obvious.

1. When openEMS is used as a Python module, Control-C stops working
because SIGINT is now managed by Python in order to generate
KeyboardInterrupt exceptions, normally this isn't a problem, but if
we are running an external C++ (Cython) function such as openEMS, the
Python interpreter mainloop has no control until we return. As a
result, SIGINT is received but never handled. In Cython, programs are
expected to call PyErr_CheckSignals() in its blocking loop periodically
to temporally transfer control back to Python to handle signals. But
this introduces a dependency of Cython in the FDTD mainloop.

2. During a simulation, it's not possible to abort it gracefully by
pressing Control-C, this is a limitation of openEMS itself, it's
always a force exit. Currently the only supported method for graceful
exit is creating a file called "ABORT" in the simulation directory.
If we already need to implement a signal handler, adding a graceful
exit at the same time would be a good idea.

This commit installs SIGINT handlers during SetupFDTD() and RunFDTD().

1. In RunFDTD(), if SIGINT is received once, a status flag is set, which
is then checked in CheckAbortCond(), allowing a graceful exit with the
same effect of an "ABORT" file. If SIGINT is received twice, openEMS
force exit without saving data (just like the old default behavior).

2. In SetupFDTD(), if SIGINT is received, openEMS immediately force
exit without saving data, identical to the old behavior. In a huge
simulation, initializing and compressing operators may have a long
time. so we want an early exit before RunFDTD().

3. Before RunFDTD() and SetupFDTD() return, the original signal handler
for SIGINT is restored. This is important since when we're acting as
a shared library. When a program (such as the Python interpreter) calls
us, changing the SIGINT handler unilaterally may overwrite the original
handler and affect the functionality of the original program. For
example, Python would never be able to raise KeyboardInterrupt again.
Thus, we save the original handler and restore it later.

Signed-off-by: Yifeng Li <tomli@tomli.me>
2023-11-18 12:32:44 +01:00
..
AdrOp.cpp remove using namespace std from header files 2015-06-18 21:45:22 +02:00
AdrOp.h Fix various typos 2022-12-11 11:32:04 +01:00
CMakeLists.txt Handle SIGINT for openEMS and Python, with graceful exit support. 2023-11-18 12:32:44 +01:00
ErrorMsg.cpp Fix various typos 2022-12-11 11:32:04 +01:00
ErrorMsg.h Fix various typos 2022-12-11 11:32:04 +01:00
ExpenseLog.cpp Init style: applied astyle.sh 2010-12-06 15:31:11 +01:00
ExpenseLog.h Init style: applied astyle.sh 2010-12-06 15:31:11 +01:00
aligned_allocator.h Init style: applied astyle.sh 2010-12-06 15:31:11 +01:00
array_ops.cpp adaptations for win32 with MSVC 2019-12-30 17:12:51 +01:00
array_ops.h adaptations for win32 with MSVC 2019-12-30 17:12:51 +01:00
constants.h split excitation from operator 2010-05-03 18:33:14 +02:00
global.cpp new debug level 3, update to debug level 2011-11-16 11:24:25 +01:00
global.h python+MSVC: reorganize headers to reduce req. includes 2019-12-30 20:04:22 +01:00
hdf5_file_reader.cpp clean up HDF5 interface for v1.8+, hdf5readatt_octave.cc still needs to be done but has to wait for upstream Octave fix 2016-08-26 10:06:24 +00:00
hdf5_file_reader.h adaptations for win32 with MSVC 2019-12-30 17:12:51 +01:00
hdf5_file_writer.cpp Fix various typos 2022-12-11 11:32:04 +01:00
hdf5_file_writer.h hdf5 writer: allow data write for double data 2012-09-17 13:21:32 +02:00
sar_calculation.cpp Fix various typos 2022-12-11 11:32:04 +01:00
sar_calculation.h more options to setup SAR averaging method 2013-05-15 16:02:30 +02:00
signal.cpp Handle SIGINT for openEMS and Python, with graceful exit support. 2023-11-18 12:32:44 +01:00
signal.h Handle SIGINT for openEMS and Python, with graceful exit support. 2023-11-18 12:32:44 +01:00
useful.cpp Fix various typos 2022-12-11 11:32:04 +01:00
useful.h adaptations for win32 with MSVC 2019-12-30 17:12:51 +01:00
vtk_file_writer.cpp Fix various typos 2022-12-11 11:32:04 +01:00
vtk_file_writer.h reorganized vtk writer and new hdf5 file writer & reader 2012-02-02 10:20:49 +01:00