NexusForce 1.0.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
latch.hpp
浏览该文件的文档.
1#ifndef NEFORCE_CORE_ASYNC_LATCH_HPP__
2#define NEFORCE_CORE_ASYNC_LATCH_HPP__
3
11
12#include "atomic_base.hpp"
13NEFORCE_BEGIN_NAMESPACE__
14
20
26
35class latch {
36private:
37 alignas(alignof(platform_wait_t)) platform_wait_t counter_;
38
39public:
44 static constexpr platform_wait_t max() noexcept { return numeric_traits<platform_wait_t>::max(); }
45
52 constexpr explicit latch(const platform_wait_t expected) noexcept :
53 counter_(expected) {
54 NEFORCE_CONSTEXPR_ASSERT(expected >= 0);
55 }
56
57 ~latch() = default;
58 latch(const latch&) = delete;
59 latch& operator=(const latch&) = delete;
60
69 NEFORCE_ALWAYS_INLINE void count_down(const platform_wait_t update = 1) {
70 auto const old_value = _NEFORCE atomic_fetch_sub(&counter_, update, memory_order_release);
71 if (old_value == update) {
72 _NEFORCE atomic_notify_address(&counter_, true);
73 }
74 }
75
82 NEFORCE_ALWAYS_INLINE bool try_wait() const noexcept {
83 return _NEFORCE atomic_load(&counter_, memory_order_acquire) == 0;
84 }
85
93 NEFORCE_ALWAYS_INLINE void wait() const noexcept {
94 auto const predicate = [this] { return this->try_wait(); };
95 _NEFORCE atomic_wait_address(&counter_, predicate);
96 }
97
107 NEFORCE_ALWAYS_INLINE void arrive_and_wait(const platform_wait_t update = 1) noexcept {
108 count_down(update);
109 wait();
110 }
111};
112 // ThreadSync
114 // AsyncComponents
116
117NEFORCE_END_NAMESPACE__
118#endif // NEFORCE_CORE_ASYNC_LATCH_HPP__
原子操作基本工具
latch(const latch &)=delete
禁止拷贝构造
NEFORCE_ALWAYS_INLINE void wait() const noexcept
等待计数器为零
latch & operator=(const latch &)=delete
禁止拷贝赋值
constexpr latch(const platform_wait_t expected) noexcept
构造函数
~latch()=default
析构函数
NEFORCE_ALWAYS_INLINE bool try_wait() const noexcept
尝试等待
NEFORCE_ALWAYS_INLINE void arrive_and_wait(const platform_wait_t update=1) noexcept
减少计数器并等待
static constexpr platform_wait_t max() noexcept
获取闩锁的最大计数值
NEFORCE_ALWAYS_INLINE void count_down(const platform_wait_t update=1)
减少计数器
static NEFORCE_NODISCARD constexpr T max() noexcept
获取类型的最大值
NEFORCE_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_wait_address(const T *addr, Pred pred) noexcept
基于谓词的原子等待
void atomic_notify_address(const T *addr, const bool all) noexcept
原子通知
NEFORCE_ALWAYS_INLINE_INLINE remove_volatile_t< T > atomic_load(const volatile T *ptr, const memory_order mo) noexcept
原子加载操作
#define NEFORCE_CONSTEXPR_ASSERT(COND)
编译时常量断言
int platform_wait_t
平台等待类型别名
NEFORCE_INLINE17 constexpr auto memory_order_release
释放内存顺序常量
NEFORCE_INLINE17 constexpr auto memory_order_acquire
获取内存顺序常量