From aecffc468e61a32580d7d7613d22f7b5e215388c Mon Sep 17 00:00:00 2001 From: Miguel Company Date: Tue, 7 Jan 2020 14:44:16 +0100 Subject: [PATCH 1/2] Refs #7209. Fixing TimedConditionVariable::wait_until when time is in the past. --- .../fastrtps/utils/TimedConditionVariable.hpp | 24 +++++++------------ 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/include/fastrtps/utils/TimedConditionVariable.hpp b/include/fastrtps/utils/TimedConditionVariable.hpp index cea801f51de..55d28aa8f40 100644 --- a/include/fastrtps/utils/TimedConditionVariable.hpp +++ b/include/fastrtps/utils/TimedConditionVariable.hpp @@ -116,15 +116,11 @@ class TimedConditionVariable const std::chrono::steady_clock::time_point& max_blocking_time, std::function predicate) { + auto secs = std::chrono::time_point_cast(max_blocking_time); + auto ns = std::chrono::time_point_cast(max_blocking_time) - + std::chrono::time_point_cast(secs); + struct timespec max_wait = { secs.time_since_epoch().count(), ns.count() }; bool ret_value = true; - std::chrono::nanoseconds nsecs = max_blocking_time - std::chrono::steady_clock::now(); - struct timespec max_wait = { 0, 0 }; - clock_gettime(CLOCK_REALTIME, &max_wait); - nsecs = nsecs + std::chrono::nanoseconds(max_wait.tv_nsec); - auto secs = std::chrono::duration_cast(nsecs); - nsecs -= secs; - max_wait.tv_sec += secs.count(); - max_wait.tv_nsec = (long)nsecs.count(); while (ret_value && false == (ret_value = predicate())) { ret_value = (CV_TIMEDWAIT_(cv_, lock.mutex()->native_handle(), &max_wait) == 0); @@ -138,14 +134,10 @@ class TimedConditionVariable std::unique_lock& lock, const std::chrono::steady_clock::time_point& max_blocking_time) { - std::chrono::nanoseconds nsecs = max_blocking_time - std::chrono::steady_clock::now(); - struct timespec max_wait = { 0, 0 }; - clock_gettime(CLOCK_REALTIME, &max_wait); - nsecs = nsecs + std::chrono::nanoseconds(max_wait.tv_nsec); - auto secs = std::chrono::duration_cast(nsecs); - nsecs -= secs; - max_wait.tv_sec += secs.count(); - max_wait.tv_nsec = (long)nsecs.count(); + auto secs = std::chrono::time_point_cast(max_blocking_time); + auto ns = std::chrono::time_point_cast(max_blocking_time) - + std::chrono::time_point_cast(secs); + struct timespec max_wait = { secs.time_since_epoch().count(), ns.count() }; return (CV_TIMEDWAIT_(cv_, lock.mutex()->native_handle(), &max_wait) == 0); } From 46a64d58374cac7f89154b92f79b66c4be0e5c6e Mon Sep 17 00:00:00 2001 From: Miguel Company Date: Tue, 4 Feb 2020 17:25:24 +0100 Subject: [PATCH 2/2] Refs #7209. Only using custom implementation with HAVE_STRICT_REALTIME --- include/fastrtps/utils/TimedConditionVariable.hpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/include/fastrtps/utils/TimedConditionVariable.hpp b/include/fastrtps/utils/TimedConditionVariable.hpp index 55d28aa8f40..e48f95906cf 100644 --- a/include/fastrtps/utils/TimedConditionVariable.hpp +++ b/include/fastrtps/utils/TimedConditionVariable.hpp @@ -24,7 +24,7 @@ NOTE: Windows implementation temporary disabled due to aleatory high CPU consump calling _Cnd_timedwait function, making some tests to fail and very poor performance. Related task: #6274 -#if defined(_WIN32) +#if HAVE_STRICT_REALTIME && defined(_WIN32) #include #define CLOCK_REALTIME 0 @@ -36,9 +36,9 @@ Related task: #6274 #define CV_T_ _Cnd_t extern int clock_gettime(int, struct timespec* tv); -#elif defined(__linux__) +#elif HAVE_STRICT_REALTIME && defined(__linux__) */ -#if defined(__linux__) +#if HAVE_STRICT_REALTIME && defined(__linux__) #include #define CV_INIT_(x) pthread_cond_init(x, NULL); @@ -58,7 +58,7 @@ extern int clock_gettime(int, struct timespec* tv); namespace eprosima { namespace fastrtps { -#if /*defined(_WIN32) ||*/ defined(__linux__) +#if HAVE_STRICT_REALTIME && (/*defined(_WIN32) ||*/ defined(__linux__)) class TimedConditionVariable { @@ -159,7 +159,7 @@ class TimedConditionVariable using TimedConditionVariable = std::condition_variable_any; #endif // /*defined(_WIN32)*/ || defined(__linux__) -} -} +} // namsepace fastrtps +} // namespace eprosima #endif // _UTILS_TIMEDCONDITIONVARIABLE_HPP_