MSTL 1.4.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
semaphore.hpp
浏览该文件的文档.
1#ifndef MSTL_CORE_ASYNC_SEMAPHORE_HPP__
2#define MSTL_CORE_ASYNC_SEMAPHORE_HPP__
3
10
11#include "atomic_timed_wait.hpp"
13
19
30template <platform_wait_t LeastMaxValue = numeric_traits<platform_wait_t>::max()>
32 static_assert(LeastMaxValue >= 0, "LeastMaxValue should be upper than zero.");
33
34 alignas(alignof(platform_wait_t)) platform_wait_t counter_;
35
43 MSTL_ALWAYS_INLINE bool do_try_acquire() noexcept {
44 auto old_value = _MSTL atomic_load(&counter_, memory_order_acquire);
45 if (old_value == 0) {
46 return false;
47 }
49 &counter_, &old_value, old_value - 1,
51 }
52
53public:
60 explicit counting_semaphore(const platform_wait_t desired) noexcept
61 : counter_(desired) {
62 MSTL_CONSTEXPR_ASSERT(desired >= 0);
63 }
64
65 ~counting_semaphore() = default;
66
68 counting_semaphore& operator =(const counting_semaphore&) = delete;
69
74 static constexpr platform_wait_t max() noexcept {
75 return LeastMaxValue;
76 }
77
84 void release(const platform_wait_t update = 1) noexcept {
85 if (0 < _MSTL atomic_fetch_add(&counter_, update, memory_order_release)) {
86 return;
87 }
88 _MSTL atomic_notify_address(&counter_, true);
89 }
90
96 void acquire() noexcept {
97 auto const pred = [this] {
98 return this->do_try_acquire();
99 };
100 _MSTL atomic_wait_address(&counter_, pred);
101 }
102
109 bool try_acquire() noexcept {
110 auto const pred = [this] {
111 return this->do_try_acquire();
112 };
113 return _MSTL atomic_spin(pred, [] { return false; });
114 }
115
125 template <typename Rep, typename Period>
126 bool try_acquire_for(const duration<Rep, Period>& relative) noexcept {
127 auto const pred = [this] {
128 return this->do_try_acquire();
129 };
130 return _MSTL atomic_wait_address_for(&counter_, pred, relative);
131 }
132
142 template <typename Clock, typename Dur>
144 auto const pred = [this] {
145 return this->do_try_acquire();
146 };
147 return _MSTL atomic_wait_address_until(&counter_, pred, timeout);
148 }
149};
150
158 // Semaphores
160
162#endif // MSTL_CORE_ASYNC_SEMAPHORE_HPP__
MSTL带超时的原子等待机制
计数信号量类模板
void release(const platform_wait_t update=1) noexcept
释放信号量
bool try_acquire_for(const duration< Rep, Period > &relative) noexcept
在指定时间内尝试获取信号量
bool try_acquire_until(const time_point< Clock, Dur > &timeout) noexcept
在指定时间点前尝试获取信号量
counting_semaphore(const platform_wait_t desired) noexcept
构造函数
void acquire() noexcept
获取信号量
~counting_semaphore()=default
析构函数
static constexpr platform_wait_t max() noexcept
获取信号量的最大可能值
bool try_acquire() 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
原子加载操作
bool atomic_spin(Pred &pred, Spin spin=Spin{}) noexcept
原子自旋等待
MSTL_ALWAYS_INLINE_INLINE bool atomic_cmpexch_strong(volatile T *ptr, remove_volatile_t< T > *expected, remove_volatile_t< T > desired, const memory_order success, const memory_order failure) noexcept
强比较交换操作
void atomic_notify_address(const T *addr, const bool all) noexcept
原子通知
bool atomic_wait_address_until(const T *addr, Pred pred, const time_point< Clock, Dur > &timeout) noexcept
基于谓词的原子定时等待(绝对时间)
MSTL_ALWAYS_INLINE_INLINE remove_volatile_t< T > atomic_fetch_add(volatile T *ptr, atomic_diff_t< T > value, const memory_order mo) noexcept
原子获取并添加操作
bool atomic_wait_address_for(const T *addr, Pred pred, const duration< Rep, Period > &rt) noexcept
基于谓词的原子定时等待(相对时间)
int platform_wait_t
平台等待类型别名
MSTL_INLINE17 constexpr auto memory_order_release
释放内存顺序常量
MSTL_INLINE17 constexpr auto memory_order_relaxed
宽松内存顺序常量
MSTL_INLINE17 constexpr auto memory_order_acquire
获取内存顺序常量
#define _MSTL
全局命名空间MSTL前缀
#define MSTL_END_NAMESPACE__
结束全局命名空间MSTL
#define MSTL_BEGIN_NAMESPACE__
开始全局命名空间MSTL
counting_semaphore< 1 > binary_semaphore
二元信号量
持续时间类模板
时间点类模板