19#ifndef _UTILS_TIMEDCONDITIONVARIABLE_HPP_
20#define _UTILS_TIMEDCONDITIONVARIABLE_HPP_
21#include <fastrtps/config.h>
42#if HAVE_STRICT_REALTIME && defined(__unix__)
45#define CV_INIT_(x) pthread_condattr_init(&cv_attr_); \
46 pthread_condattr_setclock(&cv_attr_, CLOCK_MONOTONIC); \
47 pthread_cond_init(x, &cv_attr_);
48#define CV_WAIT_(cv, x) pthread_cond_wait(&cv, x)
49#define CV_TIMEDWAIT_(cv, x, y) pthread_cond_timedwait(&cv, x, y)
50#define CV_SIGNAL_(cv) pthread_cond_signal(&cv)
51#define CV_BROADCAST_(cv) pthread_cond_broadcast(&cv)
52#define CV_T_ pthread_condattr_t cv_attr_; pthread_cond_t
54#include <condition_variable>
64#if HAVE_STRICT_REALTIME && ( defined(__unix__))
75 template<
typename Mutex>
77 std::unique_lock<Mutex>& lock,
78 std::function<
bool()> predicate)
82 CV_WAIT_(cv_, lock.mutex()->native_handle());
86 template<
typename Mutex>
88 std::unique_lock<Mutex>& lock)
90 CV_WAIT_(cv_, lock.mutex()->native_handle());
93 template<
typename Mutex>
95 std::unique_lock<Mutex>& lock,
96 const std::chrono::nanoseconds& max_blocking_time,
97 std::function<
bool()> predicate)
99 bool ret_value =
true;
100 auto nsecs = max_blocking_time;
101 struct timespec max_wait = {
104 clock_gettime(CLOCK_MONOTONIC, &max_wait);
105 nsecs = nsecs + std::chrono::nanoseconds(max_wait.tv_nsec);
106 auto secs = std::chrono::duration_cast<std::chrono::seconds>(nsecs);
108 max_wait.tv_sec += secs.count();
109 max_wait.tv_nsec = (long)nsecs.count();
110 while (ret_value &&
false == (ret_value = predicate()))
112 ret_value = (0 == CV_TIMEDWAIT_(cv_, lock.mutex()->native_handle(), &max_wait));
118 template<
typename Mutex>
120 std::unique_lock<Mutex>& lock,
121 const std::chrono::steady_clock::time_point& max_blocking_time,
122 std::function<
bool()> predicate)
124 auto secs = std::chrono::time_point_cast<std::chrono::seconds>(max_blocking_time);
125 auto ns = std::chrono::time_point_cast<std::chrono::nanoseconds>(max_blocking_time) -
126 std::chrono::time_point_cast<std::chrono::nanoseconds>(secs);
127 struct timespec max_wait = {
128 secs.time_since_epoch().count(), ns.count()
130 bool ret_value =
true;
131 while (ret_value &&
false == (ret_value = predicate()))
133 ret_value = (CV_TIMEDWAIT_(cv_, lock.mutex()->native_handle(), &max_wait) == 0);
139 template<
typename Mutex>
141 std::unique_lock<Mutex>& lock,
142 const std::chrono::steady_clock::time_point& max_blocking_time)
144 auto secs = std::chrono::time_point_cast<std::chrono::seconds>(max_blocking_time);
145 auto ns = std::chrono::time_point_cast<std::chrono::nanoseconds>(max_blocking_time) -
146 std::chrono::time_point_cast<std::chrono::nanoseconds>(secs);
147 struct timespec max_wait = {
148 secs.time_since_epoch().count(), ns.count()
150 return (CV_TIMEDWAIT_(cv_, lock.mutex()->native_handle(), &max_wait) == 0);
std::condition_variable_any TimedConditionVariable
Definition: TimedConditionVariable.hpp:168
eProsima namespace.
Definition: LibrarySettingsAttributes.h:23