5#include "webcface/common/webcface-config.h"
10#include <condition_variable>
19template <
typename Proxy,
typename Lock>
25 [[nodiscard]]
auto &
get() {
26 assert(this->owns_lock());
30 assert(this->owns_lock());
33 [[nodiscard]]
auto &
cond() {
return p->cond; }
37 inline static constexpr std::chrono::microseconds poll_interval{1};
38 std::atomic<int> notified;
45 void wait(std::unique_lock<std::mutex> &lock){
46 int notify_target = notified.load();
48 while(notified.load() == notify_target){
49 std::this_thread::sleep_for(poll_interval);
52 template <
typename Pred>
53 void wait(std::unique_lock<std::mutex> &lock, Pred pred){
56 std::this_thread::sleep_for(poll_interval);
59 template <
typename Clock,
typename Duration>
60 std::cv_status
wait_until(std::unique_lock<std::mutex> &lock,
const std::chrono::time_point<Clock, Duration>&abs){
61 int notify_target = notified.load();
63 while(notified.load() == notify_target){
64 if(Clock::now() >= abs){
65 return std::cv_status::timeout;
67 std::this_thread::sleep_for(poll_interval);
69 return std::cv_status::no_timeout;
71 template <
typename Clock,
typename Duration,
typename Pred>
72 bool wait_until(std::unique_lock<std::mutex> &lock,
const std::chrono::time_point<Clock, Duration>&abs, Pred pred){
74 if(Clock::now() >= abs){
78 std::this_thread::sleep_for(poll_interval);
82 template <
typename Rep,
typename Period>
83 auto wait_for(std::unique_lock<std::mutex> &lock,
const std::chrono::duration<Rep, Period>&rel){
84 return wait_until(lock, std::chrono::steady_clock::now() + rel);
86 template <
typename Rep,
typename Period,
typename Pred>
87 auto wait_for(std::unique_lock<std::mutex> &lock,
const std::chrono::duration<Rep, Period>&rel, Pred pred){
88 return wait_until(lock, std::chrono::steady_clock::now() + rel, pred);
111 mutable std::mutex m;
115 template <
typename Proxy,
typename Lock>
129 mutable std::shared_mutex m;
132 template <
typename Proxy,
typename Lock>
136 std::unique_lock<std::shared_mutex>>
141 std::shared_lock<std::shared_mutex>>
RustのMutexのようにデータを保護するクラス
Definition mutex.h:109
ScopedLock< const MutexProxy, std::unique_lock< std::mutex > > const_lock() const
Definition mutex.h:122
ScopedLock< MutexProxy, std::unique_lock< std::mutex > > lock()
Definition mutex.h:118
void wait(std::unique_lock< std::mutex > &lock, Pred pred)
Definition mutex.h:53
void wait(std::unique_lock< std::mutex > &lock)
Definition mutex.h:45
bool wait_until(std::unique_lock< std::mutex > &lock, const std::chrono::time_point< Clock, Duration > &abs, Pred pred)
Definition mutex.h:72
auto wait_for(std::unique_lock< std::mutex > &lock, const std::chrono::duration< Rep, Period > &rel)
Definition mutex.h:83
std::cv_status wait_until(std::unique_lock< std::mutex > &lock, const std::chrono::time_point< Clock, Duration > &abs)
Definition mutex.h:60
auto wait_for(std::unique_lock< std::mutex > &lock, const std::chrono::duration< Rep, Period > &rel, Pred pred)
Definition mutex.h:87
PollingConditionVariable()
Definition mutex.h:41
void notify_all()
Definition mutex.h:42
auto * operator->()
Definition mutex.h:29
auto & cond()
Definition mutex.h:33
auto & get()
Definition mutex.h:25
ScopedLock(Proxy *p)
Definition mutex.h:24
ScopedLock< const SharedMutexProxy, std::shared_lock< std::shared_mutex > > shared_lock() const
Definition mutex.h:142
ScopedLock< SharedMutexProxy, std::unique_lock< std::shared_mutex > > lock()
Definition mutex.h:137
#define WEBCFACE_NS_END
Definition webcface-config.h:118
#define WEBCFACE_NS_BEGIN
Definition webcface-config.h:117