NexusForce 1.0.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
call_once.hpp
浏览该文件的文档.
1#ifndef NEFORCE_CORE_ASYNC_CALL_ONCE_HPP__
2#define NEFORCE_CORE_ASYNC_CALL_ONCE_HPP__
3
10
13NEFORCE_BEGIN_NAMESPACE__
14
20
26
38class once_flag {
39private:
40 atomic_futex<> state_{0};
41
42 template <typename Callable, typename... Args>
43 friend void call_once(once_flag& flag, Callable&& func, Args&&... args);
44
45public:
46 constexpr once_flag() noexcept = default;
47 once_flag(const once_flag&) = delete;
48 once_flag& operator=(const once_flag&) = delete;
49 once_flag(once_flag&&) = delete;
50 once_flag& operator=(once_flag&&) = delete;
51};
52
68template <typename Callable, typename... Args>
69void call_once(once_flag& flag, Callable&& func, Args&&... args) {
70 if (flag.state_.load(memory_order_acquire) == 2) {
71 return;
72 }
73
74 while (true) {
75 const uint32_t state = flag.state_.load(memory_order_acquire);
76 if (state == 2) {
77 return;
78 }
79
80 if (state == 0) {
81 uint32_t expected = 0;
83 try {
84 _NEFORCE invoke<Callable, Args...>(_NEFORCE forward<Callable>(func),
85 _NEFORCE forward<Args>(args)...);
87 return;
88 } catch (...) {
90 throw;
91 }
92 }
93 continue;
94 }
95
96 if (flag.state_.load_when_not_equal(1, memory_order_acquire) == 2) {
97 return;
98 }
99 }
100}
101 // CallOnce
103 // AsyncComponents
105
106NEFORCE_END_NAMESPACE__
107#endif // NEFORCE_CORE_ASYNC_CALL_ONCE_HPP__
原子快速用户态互斥锁实现
原子快速用户态互斥锁类模板
uint32_t load(const memory_order mo) const noexcept
原子加载数据
void store_notify_all(const uint32_t value, const memory_order mo) noexcept
存储新值并通知所有等待线程
uint32_t load_when_not_equal(const uint32_t value, const memory_order mo)
等待直到值不等于指定值
bool compare_exchange_strong(uint32_t &expected, const uint32_t desired, const memory_order success, const memory_order failure) noexcept
原子比较并交换
friend void call_once(once_flag &flag, Callable &&func, Args &&... args)
单次调用函数
constexpr T && forward(remove_reference_t< T > &x) noexcept
完美转发左值
void call_once(once_flag &flag, Callable &&func, Args &&... args)
单次调用函数
unsigned int uint32_t
32位无符号整数类型
constexpr inner::__invoke_result_aux< Callable, Args... >::type invoke(Callable &&f, Args &&... args) noexcept(is_nothrow_invocable< Callable, Args... >::value)
统一调用接口
constexpr auto memory_order_release
释放内存顺序常量
constexpr auto memory_order_relaxed
宽松内存顺序常量
constexpr auto memory_order_acquire
获取内存顺序常量
统一调用接口