OMCompiler/SimulationRuntime/c/simulation/solver/real_time_sync.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * This file belongs to the OpenModelica Run-Time System | ||
| 3 | * | ||
| 4 | * Copyright (c) 1998-2026, Open Source Modelica Consortium (OSMC), c/o Linköpings | ||
| 5 | * universitet, Department of Computer and Information Science, SE-58183 Linköping, Sweden. All rights | ||
| 6 | * reserved. | ||
| 7 | * | ||
| 8 | * THIS PROGRAM IS PROVIDED UNDER THE TERMS OF THE BSD NEW LICENSE OR THE | ||
| 9 | * AGPL VERSION 3 LICENSE OR THE OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.8. ANY | ||
| 10 | * USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES RECIPIENT'S | ||
| 11 | * ACCEPTANCE OF THE BSD NEW LICENSE OR THE OSMC PUBLIC LICENSE OR THE AGPL | ||
| 12 | * VERSION 3, ACCORDING TO RECIPIENTS CHOICE. | ||
| 13 | * | ||
| 14 | * The OpenModelica software and the OSMC (Open Source Modelica Consortium) Public License | ||
| 15 | * (OSMC-PL) are obtained from OSMC, either from the above address, from the URLs: | ||
| 16 | * http://www.openmodelica.org or https://github.com/OpenModelica/ or | ||
| 17 | * http://www.ida.liu.se/projects/OpenModelica, and in the OpenModelica distribution. GNU | ||
| 18 | * AGPL version 3 is obtained from: https://www.gnu.org/licenses/licenses.html#GPL. The BSD NEW | ||
| 19 | * License is obtained from: http://www.opensource.org/licenses/BSD-3-Clause. | ||
| 20 | * | ||
| 21 | * This program is distributed WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY | ||
| 23 | * SET FORTH IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF | ||
| 24 | * OSMC-PL. | ||
| 25 | * | ||
| 26 | */ | ||
| 27 | |||
| 28 | #include "real_time_sync.h" | ||
| 29 | |||
| 30 | #if defined(__linux__) || defined(__FreeBSD__) | ||
| 31 | #include <sys/mman.h> | ||
| 32 | #include <errno.h> | ||
| 33 | #endif | ||
| 34 | |||
| 35 | 1 | void omc_real_time_sync_init(threadData_t *threadData, DATA *data) | |
| 36 | { | ||
| 37 | 1 | data->real_time_sync.maxLate = INT64_MIN; | |
| 38 | |||
| 39 | 1 | omc_real_time_sync_update(data, data->real_time_sync.scaling); | |
| 40 | |||
| 41 |
1/2✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 | if (data->real_time_sync.enabled == 0) { |
| 42 | 1 | return; | |
| 43 | } | ||
| 44 | |||
| 45 | #if defined(__linux__) || defined(__FreeBSD__) | ||
| 46 | ✗ | if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1) { | |
| 47 | ✗ | warningStreamPrint(OMC_LOG_RT, 0, __FILE__ ": mlockall failed (recommended to run as root to lock memory into RAM while doing real-time simulation): %s\n", strerror(errno)); | |
| 48 | } | ||
| 49 | ✗ | struct sched_param param = {.sched_priority = 49 /* 50=interrupt handler */ }; | |
| 50 | ✗ | if(sched_setscheduler(0, SCHED_FIFO, ¶m) == -1) { | |
| 51 | ✗ | warningStreamPrint(OMC_LOG_RT, 0, __FILE__ ": sched_setscheduler failed: %s\n", strerror(errno)); | |
| 52 | } | ||
| 53 | #endif | ||
| 54 | } | ||
| 55 | |||
| 56 | 1 | void omc_real_time_sync_update(DATA *data, double scaling) | |
| 57 | { | ||
| 58 | 1 | data->real_time_sync.scaling = scaling; | |
| 59 |
1/2✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 | if (scaling == 0) { |
| 60 | 1 | data->real_time_sync.enabled = 0; | |
| 61 | 1 | return; | |
| 62 | } | ||
| 63 | ✗ | data->real_time_sync.enabled = 1; | |
| 64 | ✗ | data->real_time_sync.time = data->localData[0]->timeValue; | |
| 65 | ✗ | rt_ext_tp_tick_realtime(&data->real_time_sync.clock); | |
| 66 | } | ||
| 67 |