123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- #ifndef _UTILS_TIMEDCONDITIONVARIABLE_HPP_
- #define _UTILS_TIMEDCONDITIONVARIABLE_HPP_
- #if HAVE_STRICT_REALTIME && defined(__linux__)
- #include <pthread.h>
- #define CV_INIT_(x) pthread_cond_init(x, NULL);
- #define CV_WAIT_(cv, x) pthread_cond_wait(&cv, x)
- #define CV_TIMEDWAIT_(cv, x, y) pthread_cond_timedwait(&cv, x, y)
- #define CV_SIGNAL_(cv) pthread_cond_signal(&cv)
- #define CV_BROADCAST_(cv) pthread_cond_broadcast(&cv)
- #define CV_T_ pthread_cond_t
- #else
- #include <condition_variable>
- #endif
- #include <mutex>
- #include <chrono>
- #include <functional>
- namespace eprosima {
- namespace fastrtps {
- #if HAVE_STRICT_REALTIME && ( defined(__linux__))
- class TimedConditionVariable
- {
- public:
- TimedConditionVariable()
- {
- CV_INIT_(&cv_);
- }
- template<typename Mutex>
- void wait(
- std::unique_lock<Mutex>& lock,
- std::function<bool()> predicate)
- {
- while (!predicate())
- {
- CV_WAIT_(cv_, lock.mutex()->native_handle());
- }
- }
- template<typename Mutex>
- void wait(
- std::unique_lock<Mutex>& lock)
- {
- CV_WAIT_(cv_, lock.mutex()->native_handle());
- }
- template<typename Mutex>
- bool wait_for(
- std::unique_lock<Mutex>& lock,
- const std::chrono::nanoseconds& max_blocking_time,
- std::function<bool()> predicate)
- {
- bool ret_value = true;
- auto nsecs = max_blocking_time;
- 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 = (0 == CV_TIMEDWAIT_(cv_, lock.mutex()->native_handle(), &max_wait));
- }
- return ret_value;
- }
- template<typename Mutex>
- bool wait_until(
- std::unique_lock<Mutex>& lock,
- 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;
- while (ret_value && false == (ret_value = predicate()))
- {
- ret_value = (CV_TIMEDWAIT_(cv_, lock.mutex()->native_handle(), &max_wait) == 0);
- }
- return ret_value;
- }
- template<typename Mutex>
- bool wait_until(
- std::unique_lock<Mutex>& lock,
- const std::chrono::steady_clock::time_point& max_blocking_time)
- {
- 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);
- }
- void notify_one()
- {
- CV_SIGNAL_(cv_);
- }
- void notify_all()
- {
- CV_BROADCAST_(cv_);
- }
- private:
- CV_T_ cv_;
- };
- #else
- using TimedConditionVariable = std::condition_variable_any;
- #endif
- }
- }
- #endif
|