[cig-commits] [commit] master: Add signal catch for graceful quit (43f6e75)

cig_noreply at geodynamics.org cig_noreply at geodynamics.org
Wed Oct 29 16:20:22 PDT 2014


Repository : https://github.com/geodynamics/vq

On branch  : master
Link       : https://github.com/geodynamics/vq/compare/0ca7695073f8b223d815f10653613b02b6c47df8...197914f4e03e55d321cae45161deafb1a5ade706

>---------------------------------------------------------------

commit 43f6e75fabc18a174e6e52094ae3c5f9cc70850d
Author: Eric Heien <emheien at ucdavis.edu>
Date:   Tue Oct 28 14:38:28 2014 -0700

    Add signal catch for graceful quit


>---------------------------------------------------------------

43f6e75fabc18a174e6e52094ae3c5f9cc70850d
 CMakeLists.txt                  |  1 +
 config.h.in                     |  1 +
 src/simulation/GracefulQuit.cpp | 26 ++++++++++++++++++++++++--
 src/simulation/GracefulQuit.h   |  3 +++
 4 files changed, 29 insertions(+), 2 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index b2f1948..8ea6c4b 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -20,6 +20,7 @@ CHECK_INCLUDE_FILES ("math.h" VQ_HAVE_MATH_H)
 CHECK_INCLUDE_FILES ("stdlib.h" VQ_HAVE_STDLIB_H)
 CHECK_INCLUDE_FILES ("unistd.h" VQ_HAVE_UNISTD_H)
 CHECK_INCLUDE_FILES ("string.h" VQ_HAVE_STRING_H)
+CHECK_INCLUDE_FILES ("signal.h" VQ_HAVE_SIGNAL_H)
 
 # Check for functions
 INCLUDE (CheckFunctionExists)
diff --git a/config.h.in b/config.h.in
index 5fdb5ae..a7c9c4a 100644
--- a/config.h.in
+++ b/config.h.in
@@ -6,6 +6,7 @@
 #cmakedefine VQ_HAVE_STDLIB_H
 #cmakedefine VQ_HAVE_UNISTD_H
 #cmakedefine VQ_HAVE_STRING_H
+#cmakedefine VQ_HAVE_SIGNAL_H
 #cmakedefine VQ_HAVE_SLEEP_FUNC
 #cmakedefine VQ_HAVE_USLEEP_FUNC
 #cmakedefine HDF5_FOUND
diff --git a/src/simulation/GracefulQuit.cpp b/src/simulation/GracefulQuit.cpp
index a3ab48d..49d8dd4 100644
--- a/src/simulation/GracefulQuit.cpp
+++ b/src/simulation/GracefulQuit.cpp
@@ -19,7 +19,20 @@
 // DEALINGS IN THE SOFTWARE.
 
 #include "GracefulQuit.h"
+
+#ifdef VQ_HAVE_STDLIB_H
 #include <stdlib.h>
+#endif
+
+#ifdef VQ_HAVE_SIGNAL_H
+#include <signal.h>
+#endif
+
+GracefulQuit *gq_obj;
+
+void catch_quit_signal(int) {
+    gq_obj->mark_signal_quit();
+}
 
 bool GracefulQuit::quitFileExists(void) {
     FILE            *fp;
@@ -35,13 +48,18 @@ bool GracefulQuit::quitFileExists(void) {
 void GracefulQuit::initDesc(const SimFramework *_sim) const {
     const Simulation  *sim = static_cast<const Simulation *>(_sim);
     sim->console() << "# To gracefully quit, create the file "
-                   << GRACEFUL_QUIT_FILE_NAME << " in the run directory." << std::endl;
+                   << GRACEFUL_QUIT_FILE_NAME << " in the run directory";
+#ifdef VQ_HAVE_SIGNAL_H
+    sim->console() << " or use a SIGINT (Control-C)";
+#endif
+    sim->console() << "." << std::endl;
 }
 
 void GracefulQuit::init(SimFramework *_sim) {
     Simulation    *sim = static_cast<Simulation *>(_sim);
 
     next_check_event = sim->itersPerSecond();
+    signal_quit = false;
 
     if (sim->isRootNode()) {
         if (quitFileExists()) {
@@ -49,6 +67,10 @@ void GracefulQuit::init(SimFramework *_sim) {
                               " already exists. Delete this file and run again." << std::endl;
             exit(-1);
         }
+#ifdef VQ_HAVE_SIGNAL_H
+        gq_obj = this;
+        signal(SIGINT, catch_quit_signal);
+#endif
     }
 }
 
@@ -69,7 +91,7 @@ SimRequest GracefulQuit::run(SimFramework *_sim) {
         next_check_event = cur_event + sim->itersPerSecond();
 
         if (sim->isRootNode()) {
-            quit_sim = (quitFileExists() ? 1 : 0);
+            quit_sim = (quitFileExists() || signal_quit ? 1 : 0);
         }
 
         all_quit = sim->broadcastValue(quit_sim);
diff --git a/src/simulation/GracefulQuit.h b/src/simulation/GracefulQuit.h
index 7bdeb01..8e72cef 100644
--- a/src/simulation/GracefulQuit.h
+++ b/src/simulation/GracefulQuit.h
@@ -32,6 +32,7 @@
 class GracefulQuit : public SimPlugin {
     private:
         unsigned int    next_check_event;
+        bool            signal_quit;
 
         bool quitFileExists(void);
 
@@ -43,6 +44,8 @@ class GracefulQuit : public SimPlugin {
 
         virtual void init(SimFramework *_sim);
         virtual SimRequest run(SimFramework *_sim);
+    
+        void mark_signal_quit(void) { if (signal_quit) exit(1); else signal_quit = true; };
 };
 
 #endif



More information about the CIG-COMMITS mailing list