text: /*
   * Academic License - for use in teaching, academic research, and meeting
   * course requirements at degree granting institutions only.  Not for
   * government, commercial, or other organizational use.
   *
   * File: ert_main.c
   *
   * Code generated for Simulink model 'Mip_Balance_Demo'.
   *
   * Model version                  : 1.39
   * Simulink Coder version         : 9.0 (R2018b) 24-May-2018
   * C/C++ source code generated on : Mon Dec 24 13:12:04 2018
   *
   * Target selection: ert.tlc
   * Embedded hardware selection: ARM Compatible->ARM Cortex
   * Code generation objectives: Unspecified
   * Validation result: Not run
   */
  
  #include <stdio.h>
  #include <stdlib.h>
  #include "Mip_Balance_Demo.h"
  #include "Mip_Balance_Demo_private.h"
  #include "rtwtypes.h"
  #include "limits.h"
  #include "rt_nonfinite.h"
  #include "MW_bbblue_init.h"
  #include "linuxinitialize.h"
  #define UNUSED(x)                      x = x
  
  /* Function prototype declaration*/
  void exitFcn(int sig);
  void *terminateTask(void *arg);
  void *baseRateTask(void *arg);
  void *subrateTask(void *arg);
  volatile boolean_T stopRequested = false;
  volatile boolean_T runModel = true;
  sem_t stopSem;
  sem_t baserateTaskSem;
  pthread_t schedulerThread;
  pthread_t baseRateThread;
  unsigned long threadJoinStatus[8];
  int terminatingmodel = 0;
  void *baseRateTask(void *arg)
  {
    runModel = (rtmGetErrorStatus(Mip_Balance_Demo_M) == (NULL)) &&
      !rtmGetStopRequested(Mip_Balance_Demo_M);
    while (runModel) {
      sem_wait(&baserateTaskSem);
  
      /* External mode */
      {
        boolean_T rtmStopReq = false;
        rtExtModePauseIfNeeded(Mip_Balance_Demo_M->extModeInfo, 2, &rtmStopReq);
        if (rtmStopReq) {
          rtmSetStopRequested(Mip_Balance_Demo_M, true);
        }
  
        if (rtmGetStopRequested(Mip_Balance_Demo_M) == true) {
          rtmSetErrorStatus(Mip_Balance_Demo_M, "Simulation finished");
          break;
        }
      }
  
      /* External mode */
      {
        boolean_T rtmStopReq = false;
        rtExtModeOneStep(Mip_Balance_Demo_M->extModeInfo, 2, &rtmStopReq);
        if (rtmStopReq) {
          rtmSetStopRequested(Mip_Balance_Demo_M, true);
        }
      }
  
      Mip_Balance_Demo_step();
  
      /* Get model outputs here */
      rtExtModeCheckEndTrigger();
      stopRequested = !((rtmGetErrorStatus(Mip_Balance_Demo_M) == (NULL)) &&
                        !rtmGetStopRequested(Mip_Balance_Demo_M));
      runModel = !stopRequested;
    }
  
    runModel = 0;
    terminateTask(arg);
    pthread_exit((void *)0);
    return NULL;
  }
  
  void exitFcn(int sig)
  {
    UNUSED(sig);
    rtmSetErrorStatus(Mip_Balance_Demo_M, "stopping the model");
    runModel = 0;
  }
  
  void *terminateTask(void *arg)
  {
    UNUSED(arg);
    terminatingmodel = 1;
  
    {
      runModel = 0;
    }
  
    MW_bbblue_terminate();
  
    /* Disable rt_OneStep() here */
  
    /* Terminate model */
    Mip_Balance_Demo_terminate();
    rtExtModeShutdown(2);
    sem_post(&stopSem);
    return NULL;
  }
  
  int main(int argc, char **argv)
  {
    UNUSED(argc);
    UNUSED(argv);
    MW_bbblue_init();
    rtmSetErrorStatus(Mip_Balance_Demo_M, 0);
    rtExtModeParseArgs(argc, (const char_T **)argv, NULL);
  
    /* Initialize model */
    Mip_Balance_Demo_initialize();
  
    /* External mode */
    rtSetTFinalForExtMode(&rtmGetTFinal(Mip_Balance_Demo_M));
    rtExtModeCheckInit(2);
  
    {
      boolean_T rtmStopReq = false;
      rtExtModeWaitForStartPkt(Mip_Balance_Demo_M->extModeInfo, 2, &rtmStopReq);
      if (rtmStopReq) {
        rtmSetStopRequested(Mip_Balance_Demo_M, true);
      }
    }
  
    rtERTExtModeStartMsg();
  
    /* Call RTOS Initialization function */
    myRTOSInit(0.01, 0);
  
    /* Wait for stop semaphore */
    sem_wait(&stopSem);
  
  #if (MW_NUMBER_TIMER_DRIVEN_TASKS > 0)
  
    {
      int i;
      for (i=0; i < MW_NUMBER_TIMER_DRIVEN_TASKS; i++) {
        CHECK_STATUS(sem_destroy(&timerTaskSem[i]), 0, "sem_destroy");
      }
    }
  
  #endif
  
    return 0;
  }
  
  /*
   * File trailer for generated code.
   *
   * [EOF]
   */
  