Skip to content
This repository was archived by the owner on Nov 3, 2022. It is now read-only.

Commit 2127c3a

Browse files
author
Dilawar Singh
committed
1 parent 31f2b8f commit 2127c3a

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

pybind11/helper.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include <memory>
1818
#include <stdexcept>
19+
#include <csignal>
1920

2021
#include "../external/pybind11/include/pybind11/functional.h"
2122
#include "../external/pybind11/include/pybind11/numpy.h"
@@ -88,6 +89,20 @@ Id initShell(void)
8889
return shellId;
8990
}
9091

92+
/**
93+
* @brief Handle signal raised by user during simulation.
94+
*
95+
* @param signum
96+
*/
97+
void handleKeyboardInterrupts(int signum)
98+
{
99+
LOG(moose::info, "Interrupt signal (" << signum << ") received.");
100+
// Get the shell and cleanup.
101+
Shell *shell = getShellPtr();
102+
shell->cleanSimulation();
103+
exit(signum);
104+
}
105+
91106
/**
92107
Utility function to create objects from full path, dimensions
93108
and classname.
@@ -315,8 +330,22 @@ void mooseReinit()
315330
getShellPtr()->doReinit();
316331
}
317332

333+
/* --------------------------------------------------------------------------*/
334+
/**
335+
* @Synopsis Register and signal handler and start the simulation. When ctrl+c
336+
* is pressed, stop, cleanup and exit.
337+
*
338+
* @Param runtime
339+
* @Param notify
340+
*/
341+
/* ----------------------------------------------------------------------------*/
318342
void mooseStart(double runtime, bool notify = false)
319343
{
344+
struct sigaction sigHandler;
345+
sigHandler.sa_handler = handleKeyboardInterrupts;
346+
sigemptyset(&sigHandler.sa_mask);
347+
sigHandler.sa_flags = 0;
348+
sigaction(SIGINT, &sigHandler, NULL);
320349
getShellPtr()->doStart(runtime, notify);
321350
}
322351

pybind11/helper.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ map<string, string> mooseGetFieldDict(const string& className,
173173

174174
void mooseReinit();
175175

176+
void handleKeyboardInterrupts(int signum);
177+
176178
void mooseStart(double runtime, bool notify);
177179

178180
void mooseStop();

pybind11/pymoose.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ py::object getFieldGeneric(const ObjId &oid, const string &fieldName)
157157
return pybind11::none();
158158
}
159159

160+
161+
160162
/* --------------------------------------------------------------------------*/
161163
/**
162164
* @Synopsis MOOSE extension module _moose.so.

0 commit comments

Comments
 (0)