MSTL 1.4.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
latch.hpp
浏览该文件的文档.
1#ifndef MSTL_CORE_ASYNC_LATCH_HPP__
2#define MSTL_CORE_ASYNC_LATCH_HPP__
3
11
12#include "atomic_base.hpp"
14
20
29class latch {
30private:
31 alignas(alignof(platform_wait_t)) platform_wait_t counter_;
32
33public:
38 static constexpr platform_wait_t max() noexcept {
40 }
41
48 constexpr explicit latch(const platform_wait_t expected) noexcept
49 : counter_(expected) {
50 MSTL_CONSTEXPR_ASSERT(expected >= 0);
51 }
52
53 ~latch() = default;
54 latch(const latch&) = delete;
55 latch& operator =(const latch&) = delete;
56
65 MSTL_ALWAYS_INLINE void count_down(const platform_wait_t update = 1) {
66 auto const old_value = _MSTL atomic_fetch_sub(&counter_, update, memory_order_release);
67 if (old_value == update) {
68 _MSTL atomic_notify_address(&counter_, true);
69 }
70 }
71
78 MSTL_ALWAYS_INLINE bool try_wait() const noexcept {
79 return _MSTL atomic_load(&counter_, memory_order_acquire) == 0;
80 }
81
89 MSTL_ALWAYS_INLINE void wait() const noexcept {
90 auto const predicate = [this] { return this->try_wait(); };
91 _MSTL atomic_wait_address(&counter_, predicate);
92 }
93
103 MSTL_ALWAYS_INLINE void arrive_and_wait(const platform_wait_t update = 1) noexcept {
104 count_down(update);
105 wait();
106 }
107};
108 // Latches
110
112#endif // MSTL_CORE_ASYNC_LATCH_HPP__
MSTL原子操作基本工具
latch(const latch &)=delete
禁止拷贝构造
latch & operator=(const latch &)=delete
禁止拷贝赋值
MSTL_ALWAYS_INLINE void arrive_and_wait(const platform_wait_t update=1) noexcept
减少计数器并等待
constexpr latch(const platform_wait_t expected) noexcept
构造函数
~latch()=default
析构函数
MSTL_ALWAYS_INLINE void count_down(const platform_wait_t update=1)
减少计数器
MSTL_ALWAYS_INLINE bool try_wait() const noexcept
尝试等待
MSTL_ALWAYS_INLINE void wait() const noexcept
等待计数器为零
static constexpr platform_wait_t max() noexcept
获取闩锁的最大计数值
static MSTL_NODISCARD constexpr T max() noexcept
获取类型的最大值
void atomic_wait_address(const T *addr, Pred pred) noexcept
基于谓词的原子等待
MSTL_ALWAYS_INLINE_INLINE remove_volatile_t< T > atomic_load(const volatile T *ptr, const memory_order mo) noexcept
原子加载操作
MSTL_ALWAYS_INLINE_INLINE remove_volatile_t< T > atomic_fetch_sub(volatile T *ptr, atomic_diff_t< T > value, const memory_order mo) noexcept
原子获取并减去操作
void atomic_notify_address(const T *addr, const bool all) noexcept
原子通知
int platform_wait_t
平台等待类型别名
MSTL_INLINE17 constexpr auto memory_order_release
释放内存顺序常量
MSTL_INLINE17 constexpr auto memory_order_acquire
获取内存顺序常量
#define _MSTL
全局命名空间MSTL前缀
#define MSTL_END_NAMESPACE__
结束全局命名空间MSTL
#define MSTL_BEGIN_NAMESPACE__
开始全局命名空间MSTL