Fast RTPS  Version 2.9.0
Fast RTPS
TimedConditionVariable.hpp
1 // Copyright 2018 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
19 #ifndef _UTILS_TIMEDCONDITIONVARIABLE_HPP_
20 #define _UTILS_TIMEDCONDITIONVARIABLE_HPP_
21 #include <fastrtps/config.h>
22 
23 /*
24  NOTE: Windows implementation temporary disabled due to aleatory high CPU consumption when
25  calling _Cnd_timedwait function, making some tests to fail and very poor performance.
26  Related task: #6274
27 
28  #if HAVE_STRICT_REALTIME && defined(_WIN32)
29  #include <thr/xthreads.h>
30 
31  #define CLOCK_REALTIME 0
32  #define CV_INIT_(x) _Cnd_init(x)
33  #define CV_WAIT_(cv, x) _Cnd_wait(cv, (_Mtx_t)x)
34  #define CV_TIMEDWAIT_(cv, x, y) _Cnd_timedwait(cv, (_Mtx_t)x, (xtime*)y)
35  #define CV_SIGNAL_(cv) _Cnd_signal(cv)
36  #define CV_BROADCAST_(cv) _Cnd_broadcast(cv)
37  #define CV_T_ _Cnd_t
38 
39  extern int clock_gettime(int, struct timespec* tv);
40  #elif HAVE_STRICT_REALTIME && defined(__unix__)
41  */
42 #if HAVE_STRICT_REALTIME && defined(__unix__)
43 #include <pthread.h>
44 
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
53 #else
54 #include <condition_variable>
55 #endif // if HAVE_STRICT_REALTIME && defined(__unix__)
56 
57 #include <mutex>
58 #include <chrono>
59 #include <functional>
60 
61 namespace eprosima {
62 namespace fastrtps {
63 
64 #if HAVE_STRICT_REALTIME && (/*defined(_WIN32) ||*/ defined(__unix__))
65 
67 {
68 public:
69 
71  {
72  CV_INIT_(&cv_);
73  }
74 
75  template<typename Mutex>
76  void wait(
77  std::unique_lock<Mutex>& lock,
78  std::function<bool()> predicate)
79  {
80  while (!predicate())
81  {
82  CV_WAIT_(cv_, lock.mutex()->native_handle());
83  }
84  }
85 
86  template<typename Mutex>
87  void wait(
88  std::unique_lock<Mutex>& lock)
89  {
90  CV_WAIT_(cv_, lock.mutex()->native_handle());
91  }
92 
93  template<typename Mutex>
94  bool wait_for(
95  std::unique_lock<Mutex>& lock,
96  const std::chrono::nanoseconds& max_blocking_time,
97  std::function<bool()> predicate)
98  {
99  bool ret_value = true;
100  auto nsecs = max_blocking_time;
101  struct timespec max_wait = {
102  0, 0
103  };
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);
107  nsecs -= secs;
108  max_wait.tv_sec += secs.count();
109  max_wait.tv_nsec = (long)nsecs.count();
110  while (ret_value && false == (ret_value = predicate()))
111  {
112  ret_value = (0 == CV_TIMEDWAIT_(cv_, lock.mutex()->native_handle(), &max_wait));
113  }
114 
115  return ret_value;
116  }
117 
118  template<typename Mutex>
119  bool wait_until(
120  std::unique_lock<Mutex>& lock,
121  const std::chrono::steady_clock::time_point& max_blocking_time,
122  std::function<bool()> predicate)
123  {
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()
129  };
130  bool ret_value = true;
131  while (ret_value && false == (ret_value = predicate()))
132  {
133  ret_value = (CV_TIMEDWAIT_(cv_, lock.mutex()->native_handle(), &max_wait) == 0);
134  }
135 
136  return ret_value;
137  }
138 
139  template<typename Mutex>
140  bool wait_until(
141  std::unique_lock<Mutex>& lock,
142  const std::chrono::steady_clock::time_point& max_blocking_time)
143  {
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()
149  };
150  return (CV_TIMEDWAIT_(cv_, lock.mutex()->native_handle(), &max_wait) == 0);
151  }
152 
153  void notify_one()
154  {
155  CV_SIGNAL_(cv_);
156  }
157 
158  void notify_all()
159  {
160  CV_BROADCAST_(cv_);
161  }
162 
163 private:
164 
165  CV_T_ cv_;
166 };
167 #else
168 using TimedConditionVariable = std::condition_variable_any;
169 #endif // HAVE_STRICT_REALTIME && (/*defined(_WIN32)*/ || defined(__unix__))
170 
171 } // namespace fastrtps
172 } // namespace eprosima
173 
174 #endif // _UTILS_TIMEDCONDITIONVARIABLE_HPP_
std::condition_variable_any TimedConditionVariable
Definition: TimedConditionVariable.hpp:168
eProsima namespace.
Definition: LibrarySettingsAttributes.h:23