MSTL 1.4.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
condition_variable.hpp
浏览该文件的文档.
1#ifndef MSTL_CORE_ASYNC_CONDITION_VARIABLE_HPP__
2#define MSTL_CORE_ASYNC_CONDITION_VARIABLE_HPP__
3
10
14
20
27enum class cv_status {
30};
31
32
35
42class MSTL_API condition_variable_base {
43public:
44#ifdef MSTL_PLATFORM_WINDOWS__
45 using native_handle_type = ::CONDITION_VARIABLE;
46#else
47 using native_handle_type = ::pthread_cond_t;
48#endif
49
50private:
51 native_handle_type cond_;
52
53public:
54 condition_variable_base();
55 condition_variable_base(const condition_variable_base&) = delete;
56 condition_variable_base& operator =(const condition_variable_base&) = delete;
57 ~condition_variable_base();
58
59 native_handle_type* native_handle() noexcept { return &cond_; }
60
69 void wait(mutex& mtx);
70
80 cv_status wait_until(mutex& mtx, int64_t sec, int64_t ns);
81
92 cv_status wait_until(mutex& mtx, bool is_monotonic, int64_t sec, int64_t ns);
93
94
100 void notify_one() noexcept;
101
107 void notify_all() noexcept;
108};
109
112
120public:
121 using base_type = _INNER condition_variable_base;
122 using native_handle_type = base_type::native_handle_type;
124
125private:
126 base_type cond_;
127
135 template <typename Dur>
136 cv_status __wait_until_impl(smart_lock<mutex>& lock,
137 const time_point<steady_clock, Dur>& util) {
138 auto s = util.to_sec();
139 const nanoseconds ns(util - s);
140 cond_.wait_until(*lock.mutex(), true, s.since_epoch().count(), ns.count());
141 return steady_clock::now() < util ?
144 }
145
153 template <typename Dur>
154 cv_status __wait_until_impl(smart_lock<mutex>& lock,
155 const time_point<system_clock, Dur>& util) {
156 auto sec = util.to_sec();
157 const nanoseconds nanosec(util - sec);
158 cond_.wait_until(*lock.mutex(), sec.since_epoch().count(), nanosec.count());
159 return system_clock::now() < util ?
162 }
163
164public:
169
174
175 condition_variable(const condition_variable&) = delete;
176 condition_variable& operator =(const condition_variable&) = delete;
177
183 return cond_.native_handle();
184 }
185
189 void notify_one() noexcept {
190 cond_.notify_one();
191 }
192
196 void notify_all() noexcept {
197 cond_.notify_all();
198 }
199
207 cond_.wait(*lock.mutex());
208 }
209
218 template <typename Pred>
219 void wait(smart_lock<mutex>& lock, Pred pred) {
220 while (!pred()) wait(lock);
221 }
222
230 template <typename Dur>
232 return this->__wait_until_impl(lock, util);
233 }
234
242 template <typename Dur>
244 return this->__wait_until_impl(lock, util);
245 }
246
257 template <typename Clock, typename Dur>
259 const typename Clock::time_point entry = Clock::now();
260 const auto atime = clock_type::now() + ceil<clock_type::duration>(util - entry);
261
262 if (this->__wait_until_impl(lock, atime) == cv_status::success) {
263 return cv_status::success;
264 }
265 if (Clock::now() < util) {
266 return cv_status::success;
267 }
268 return cv_status::timeout;
269 }
270
283 template <typename Clock, typename Dur, typename Pred>
285 while (!pred()) {
286 if (this->wait_until(lock, util) == cv_status::timeout) {
287 return pred();
288 }
289 }
290 return true;
291 }
292
301 template <typename Rep, typename Period>
303 const auto atime = steady_clock::now() + ceil<steady_clock::duration>(rest);
304 return this->wait_until(lock, atime);
305 }
306
317 template <typename Rep, typename Period, typename Pred>
318 bool wait_for(smart_lock<mutex>& lock, const duration<Rep, Period>& rest, Pred pred) {
319 const auto atime = steady_clock::now() + ceil<steady_clock::duration>(rest);
320 return this->wait_until(lock, atime, _MSTL move(pred));
321 }
322};
323 // ConditionVariables
325
327#endif // MSTL_CORE_ASYNC_CONDITION_VARIABLE_HPP__
cv_status wait_until(smart_lock< mutex > &lock, const time_point< steady_clock, Dur > &util)
等待直到稳定时钟时间点
bool wait_for(smart_lock< mutex > &lock, const duration< Rep, Period > &rest, Pred pred)
带谓词的等待指定持续时间
native_handle_type * native_handle() noexcept
获取原生句柄
cv_status wait_for(smart_lock< mutex > &lock, const duration< Rep, Period > &rest)
等待指定的持续时间
~condition_variable()=default
析构函数
condition_variable()=default
构造函数
void notify_all() noexcept
通知所有等待线程
void notify_one() noexcept
通知一个等待线程
void wait(smart_lock< mutex > &lock, Pred pred)
带谓词的无限期等待
cv_status wait_until(smart_lock< mutex > &lock, const time_point< system_clock, Dur > &util)
等待直到系统时钟时间点
bool wait_until(smart_lock< mutex > &lock, const time_point< Clock, Dur > &util, Pred pred)
带谓词的等待直到时间点
_MSTL steady_clock clock_type
默认时钟类型
base_type::native_handle_type native_handle_type
原生句柄类型
void wait(smart_lock< mutex > &lock)
无限期等待
_INNER condition_variable_base base_type
基类类型
cv_status wait_until(smart_lock< mutex > &lock, const time_point< Clock, Dur > &util)
等待直到任意时钟时间点
锁管理器模板
MSTL时钟类型
cv_status
条件变量等待状态
long long int64_t
64位有符号整数类型
duration< int64_t, nano > nanoseconds
纳秒持续时间
@ wait
等待操作
MSTL_CONST_FUNCTION MSTL_CONSTEXPR14 decimal_t ceil(const decimal_t x) noexcept
向上取整
lock< Mutex, true > smart_lock
智能锁管理器的便捷类型别名
#define _MSTL
全局命名空间MSTL前缀
#define MSTL_END_INNER__
结束inner命名空间
#define _INNER
inner命名空间前缀
#define MSTL_END_NAMESPACE__
结束全局命名空间MSTL
#define MSTL_BEGIN_NAMESPACE__
开始全局命名空间MSTL
#define MSTL_BEGIN_INNER__
开始inner命名空间
constexpr Iterator2 move(Iterator1 first, Iterator1 last, Iterator2 result)
移动范围元素
MSTL互斥锁
持续时间类模板
constexpr rep count() const noexcept
获取计数值
稳定时钟
static time_point now() noexcept
获取当前时间点
static time_point now() noexcept
获取当前时间点
时间点类模板