]> rtime.felk.cvut.cz Git - ert_linux.git/commitdiff
Include SIGINT handler to properly stop model when aborted by CTRL+C.
authorPavel Pisa <pisa@cmp.felk.cvut.cz>
Mon, 20 Mar 2017 01:38:41 +0000 (02:38 +0100)
committerPavel Pisa <pisa@cmp.felk.cvut.cz>
Mon, 20 Mar 2017 01:38:41 +0000 (02:38 +0100)
Enhancement based on change done by Constantin Wiesener
in daqbeagle project.

Signed-off-by: Pavel Pisa <pisa@cmp.felk.cvut.cz>
ert_linux/ert_linux_main.tlc

index 418d20c0bf84f722c49e6e6e03109fd3294f61fb..30943c29281a07efef2698a2d95563f2b945c2a5 100644 (file)
@@ -32,6 +32,7 @@
   #include <stdbool.h>
   #include <unistd.h>
   #include <sys/mman.h>                  /* For mlockall() */
+  #include <signal.h>
   #include "%<modelName>.h"              /* Model's header file */
   #include "rtwtypes.h"                  /* MathWorks types */
   %if extMode == 1
     return NULL;
   }
 
+  /**
+   * Signal handler for ABORT during simulation
+   */
+  void abort_signal_handler(int sig) {
+    fprintf(stderr, "Simulation aborted by pressing CTRL+C\n");
+    rtmSetStopRequested(%<modelName>_M, 1);
+  }
+
   /**
    * This is the main function of the model.
    * Multirate - Multitasking case main file
     %if extMode == 1
       /* External mode */
       CHECKE(sem_wait(&ext_mode_ready));
+
+      signal(SIGINT, abort_signal_handler); /* important for letting the destructor be called. */
+
       while(rtmGetErrorStatus(%<modelName>_M) == NULL && !rtmGetStopRequested(%<modelName>_M)) {
         rtExtModeOneStep(rtmGetRTWExtModeInfo(RT_MDL), NUMST, (boolean_T *)&rtmGetStopRequested(RT_MDL));
        usleep(%<FEVAL("uint32", fundamentalStepSize * 1000000)>);