From 5929abca4a45e2ba441602b77deff3f38120c389 Mon Sep 17 00:00:00 2001 From: erlingrj Date: Mon, 9 Dec 2024 11:10:54 -0800 Subject: [PATCH] Factor setting and waiting for start time into a separate function --- core/reactor.c | 13 +++---------- core/reactor_common.c | 12 ++++++++++++ core/threaded/reactor_threaded.c | 12 +++--------- include/core/reactor_common.h | 6 ++++++ 4 files changed, 24 insertions(+), 19 deletions(-) diff --git a/core/reactor.c b/core/reactor.c index d044a6ce6..71e58377f 100644 --- a/core/reactor.c +++ b/core/reactor.c @@ -328,16 +328,9 @@ int lf_reactor_c_main(int argc, const char* argv[]) { LF_PRINT_DEBUG("Initializing."); initialize_global(); - // Set start time - if (!start_time_specified) { - start_time = lf_time_physical(); - } else { - instant_t now = lf_time_physical(); - if (now < start_time) { - LF_PRINT_LOG("Sleeping " PRINTF_TIME " ns until start time", start_time - now); - lf_sleep(start_time - now); - } - } + // Set the start time of the program (if it is not set from the command line). Possibly wait for + // it to arrive also. + _lf_set_and_wait_for_start_time(); #ifndef FEDERATED lf_tracing_set_start_time(start_time); #endif diff --git a/core/reactor_common.c b/core/reactor_common.c index ca38cbaf3..55fc3772d 100644 --- a/core/reactor_common.c +++ b/core/reactor_common.c @@ -1261,3 +1261,15 @@ index_t lf_combine_deadline_and_level(interval_t deadline, int level) { else return (deadline << 16) | level; } + +void _lf_set_and_wait_for_start_time() { + if (!start_time_specified) { + start_time = lf_time_physical(); + } else { + instant_t now = lf_time_physical(); + if (!fast && now < start_time) { + lf_print("Sleeping " PRINTF_TIME " ns until specified start time", start_time - now); + lf_sleep(start_time - now); + } + } +} \ No newline at end of file diff --git a/core/threaded/reactor_threaded.c b/core/threaded/reactor_threaded.c index 5bc797ab0..d61973549 100644 --- a/core/threaded/reactor_threaded.c +++ b/core/threaded/reactor_threaded.c @@ -1017,15 +1017,9 @@ int lf_reactor_c_main(int argc, const char* argv[]) { // Initialize the clock through the platform API. No reading of physical time before this. _lf_initialize_clock(); - if (!start_time_specified) { - start_time = lf_time_physical(); - } else { - instant_t now = lf_time_physical(); - if (now < start_time) { - LF_PRINT_LOG("Sleeping " PRINTF_TIME " ns until start time", start_time - now); - lf_sleep(start_time - now); - } - } + // Set the start time of the program (if it is not set from the command line). Possibly wait for + // it to arrive also. + _lf_set_and_wait_for_start_time(); #ifndef FEDERATED lf_tracing_set_start_time(start_time); #endif diff --git a/include/core/reactor_common.h b/include/core/reactor_common.h index 1ff26733a..4c329a56d 100644 --- a/include/core/reactor_common.h +++ b/include/core/reactor_common.h @@ -66,6 +66,12 @@ extern struct allocation_record_t* _lf_reactors_to_free; ////////////////////// Functions ////////////////////// + +/** + * @brief Set the start time of the program and possibly wait for it to arrive. + */ +void _lf_set_and_wait_for_start_time(); + /** * @brief Combine a deadline and a level into a single index for sorting in the reaction queue. *