Skip to content

Commit

Permalink
Fixing wait_until when time is in the past <1.9.x> [7210] (#944)
Browse files Browse the repository at this point in the history
* Refs #7209. Fixing TimedConditionVariable::wait_until when time is in the past.

* Refs #7209. Only using custom implementation with HAVE_STRICT_REALTIME
  • Loading branch information
MiguelCompany authored Feb 6, 2020
1 parent 66a5356 commit c7b6d93
Showing 1 changed file with 14 additions and 22 deletions.
36 changes: 14 additions & 22 deletions include/fastrtps/utils/TimedConditionVariable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <thr/xthreads.h>
#define CLOCK_REALTIME 0
Expand All @@ -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 <pthread.h>

#define CV_INIT_(x) pthread_cond_init(x, NULL);
Expand All @@ -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
{
Expand Down Expand Up @@ -116,15 +116,11 @@ class TimedConditionVariable
const std::chrono::steady_clock::time_point& max_blocking_time,
std::function<bool()> predicate)
{
auto secs = std::chrono::time_point_cast<std::chrono::seconds>(max_blocking_time);
auto ns = std::chrono::time_point_cast<std::chrono::nanoseconds>(max_blocking_time) -
std::chrono::time_point_cast<std::chrono::nanoseconds>(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<std::chrono::seconds>(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);
Expand All @@ -138,14 +134,10 @@ class TimedConditionVariable
std::unique_lock<Mutex>& 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<std::chrono::seconds>(nsecs);
nsecs -= secs;
max_wait.tv_sec += secs.count();
max_wait.tv_nsec = (long)nsecs.count();
auto secs = std::chrono::time_point_cast<std::chrono::seconds>(max_blocking_time);
auto ns = std::chrono::time_point_cast<std::chrono::nanoseconds>(max_blocking_time) -
std::chrono::time_point_cast<std::chrono::nanoseconds>(secs);
struct timespec max_wait = { secs.time_since_epoch().count(), ns.count() };
return (CV_TIMEDWAIT_(cv_, lock.mutex()->native_handle(), &max_wait) == 0);
}

Expand All @@ -167,7 +159,7 @@ class TimedConditionVariable
using TimedConditionVariable = std::condition_variable_any;
#endif // /*defined(_WIN32)*/ || defined(__linux__)

}
}
} // namsepace fastrtps
} // namespace eprosima

#endif // _UTILS_TIMEDCONDITIONVARIABLE_HPP_

0 comments on commit c7b6d93

Please sign in to comment.