From fd7bd2223fa897a0212d2d93d263890e53309136 Mon Sep 17 00:00:00 2001 From: Byeonggil Jun Date: Tue, 28 Jan 2025 11:55:36 -0700 Subject: [PATCH 1/4] Perform busy wait when the wait duration is less than MIN_SLEEP_DURATION --- core/threaded/reactor_threaded.c | 7 +++++-- include/core/threaded/reactor_threaded.h | 4 ++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/core/threaded/reactor_threaded.c b/core/threaded/reactor_threaded.c index 493bd5a3e..61c771118 100644 --- a/core/threaded/reactor_threaded.c +++ b/core/threaded/reactor_threaded.c @@ -200,9 +200,12 @@ bool wait_until(instant_t wait_until_time, lf_cond_t* condition) { // Check whether we actually need to wait, or if we have already passed the timepoint. interval_t wait_duration = wait_until_time - lf_time_physical(); if (wait_duration < MIN_SLEEP_DURATION) { - LF_PRINT_DEBUG("Wait time " PRINTF_TIME " is less than MIN_SLEEP_DURATION " PRINTF_TIME ". Skipping wait.", + LF_PRINT_DEBUG("Wait time " PRINTF_TIME " is less than lf_min_sleep_duration " PRINTF_TIME + ". Performing busy wait.", wait_duration, MIN_SLEEP_DURATION); - return true; + while (lf_time_physical() < wait_until_time) { + // Busy wait + } } // We do the sleep on the cond var so we can be awakened by the diff --git a/include/core/threaded/reactor_threaded.h b/include/core/threaded/reactor_threaded.h index 2f5463165..e8b0cd0c6 100644 --- a/include/core/threaded/reactor_threaded.h +++ b/include/core/threaded/reactor_threaded.h @@ -94,8 +94,8 @@ void lf_synchronize_with_other_federates(void); * * The mutex lock associated with the condition argument is assumed to be held by * the calling thread. This mutex is released while waiting. If the wait time is - * too small to actually wait (less than MIN_SLEEP_DURATION), then this function - * immediately returns true and the mutex is not released. + * too small (less than MIN_SLEEP_DURATION) to wait using lf_clock_cond_timedwait, + * then this function performs busy wait and the mutex is not released. * * @param env Environment within which we are executing. * @param wait_until_time The time to wait until physical time matches it. From 184b55f0b5a846ae839f57bc4e3e319268eeb15a Mon Sep 17 00:00:00 2001 From: Byeonggil Jun Date: Wed, 29 Jan 2025 09:04:17 -0700 Subject: [PATCH 2/4] Do not check MIN_SLEEP_DURATION when waiting for the physical time to exceed the next tag --- core/threaded/reactor_threaded.c | 8 -------- include/core/threaded/reactor_threaded.h | 4 +--- 2 files changed, 1 insertion(+), 11 deletions(-) diff --git a/core/threaded/reactor_threaded.c b/core/threaded/reactor_threaded.c index 61c771118..9f6f8cd35 100644 --- a/core/threaded/reactor_threaded.c +++ b/core/threaded/reactor_threaded.c @@ -199,14 +199,6 @@ bool wait_until(instant_t wait_until_time, lf_cond_t* condition) { LF_PRINT_DEBUG("-------- Waiting until physical time " PRINTF_TIME, wait_until_time - start_time); // Check whether we actually need to wait, or if we have already passed the timepoint. interval_t wait_duration = wait_until_time - lf_time_physical(); - if (wait_duration < MIN_SLEEP_DURATION) { - LF_PRINT_DEBUG("Wait time " PRINTF_TIME " is less than lf_min_sleep_duration " PRINTF_TIME - ". Performing busy wait.", - wait_duration, MIN_SLEEP_DURATION); - while (lf_time_physical() < wait_until_time) { - // Busy wait - } - } // We do the sleep on the cond var so we can be awakened by the // asynchronous scheduling of a physical action. lf_clock_cond_timedwait diff --git a/include/core/threaded/reactor_threaded.h b/include/core/threaded/reactor_threaded.h index e8b0cd0c6..fe14beb8e 100644 --- a/include/core/threaded/reactor_threaded.h +++ b/include/core/threaded/reactor_threaded.h @@ -93,9 +93,7 @@ void lf_synchronize_with_other_federates(void); * if that event time matches or exceeds the specified time. * * The mutex lock associated with the condition argument is assumed to be held by - * the calling thread. This mutex is released while waiting. If the wait time is - * too small (less than MIN_SLEEP_DURATION) to wait using lf_clock_cond_timedwait, - * then this function performs busy wait and the mutex is not released. + * the calling thread. * * @param env Environment within which we are executing. * @param wait_until_time The time to wait until physical time matches it. From f9081b172cc47f8a0e35cf8bd0f46258bd599a5b Mon Sep 17 00:00:00 2001 From: Byeonggil Jun Date: Wed, 29 Jan 2025 16:30:54 -0700 Subject: [PATCH 3/4] Revert changes --- core/threaded/reactor_threaded.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/core/threaded/reactor_threaded.c b/core/threaded/reactor_threaded.c index 9f6f8cd35..493bd5a3e 100644 --- a/core/threaded/reactor_threaded.c +++ b/core/threaded/reactor_threaded.c @@ -199,6 +199,11 @@ bool wait_until(instant_t wait_until_time, lf_cond_t* condition) { LF_PRINT_DEBUG("-------- Waiting until physical time " PRINTF_TIME, wait_until_time - start_time); // Check whether we actually need to wait, or if we have already passed the timepoint. interval_t wait_duration = wait_until_time - lf_time_physical(); + if (wait_duration < MIN_SLEEP_DURATION) { + LF_PRINT_DEBUG("Wait time " PRINTF_TIME " is less than MIN_SLEEP_DURATION " PRINTF_TIME ". Skipping wait.", + wait_duration, MIN_SLEEP_DURATION); + return true; + } // We do the sleep on the cond var so we can be awakened by the // asynchronous scheduling of a physical action. lf_clock_cond_timedwait From 98b1090c0edd70abe2f7f257ebf3ec55f8843888 Mon Sep 17 00:00:00 2001 From: Byeonggil Jun Date: Thu, 30 Jan 2025 10:29:45 -0700 Subject: [PATCH 4/4] Make immediately return when we have already passed the target physical time --- core/threaded/reactor_threaded.c | 5 ++--- include/core/threaded/reactor_threaded.h | 4 +++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/core/threaded/reactor_threaded.c b/core/threaded/reactor_threaded.c index 493bd5a3e..7872bf9db 100644 --- a/core/threaded/reactor_threaded.c +++ b/core/threaded/reactor_threaded.c @@ -199,9 +199,8 @@ bool wait_until(instant_t wait_until_time, lf_cond_t* condition) { LF_PRINT_DEBUG("-------- Waiting until physical time " PRINTF_TIME, wait_until_time - start_time); // Check whether we actually need to wait, or if we have already passed the timepoint. interval_t wait_duration = wait_until_time - lf_time_physical(); - if (wait_duration < MIN_SLEEP_DURATION) { - LF_PRINT_DEBUG("Wait time " PRINTF_TIME " is less than MIN_SLEEP_DURATION " PRINTF_TIME ". Skipping wait.", - wait_duration, MIN_SLEEP_DURATION); + if (wait_duration < 0) { + LF_PRINT_DEBUG("We have already passed " PRINTF_TIME ". Skipping wait.", wait_until_time); return true; } diff --git a/include/core/threaded/reactor_threaded.h b/include/core/threaded/reactor_threaded.h index fe14beb8e..727b3839e 100644 --- a/include/core/threaded/reactor_threaded.h +++ b/include/core/threaded/reactor_threaded.h @@ -93,7 +93,9 @@ void lf_synchronize_with_other_federates(void); * if that event time matches or exceeds the specified time. * * The mutex lock associated with the condition argument is assumed to be held by - * the calling thread. + * the calling thread. This mutex is released while waiting. If the current physical + * time has already passed the specified time, then this function + * immediately returns true and the mutex is not released. * * @param env Environment within which we are executing. * @param wait_until_time The time to wait until physical time matches it.