WebCFace 2.9.0
Web-based Communication Framework & Dashboard-like UI
Loading...
Searching...
No Matches
mutex.h
Go to the documentation of this file.
1#pragma once
2#ifdef WEBCFACE_MESON
3#include "webcface-config.h"
4#else
5#include "webcface/common/webcface-config.h"
6#endif
7#include <shared_mutex>
8#include <mutex>
9#include <cassert>
10#include <condition_variable>
11#include <atomic>
12#include <thread>
13#include <chrono>
14#include "./unlock.h"
15
17namespace internal {
18
19template <typename Proxy, typename Lock>
20class ScopedLock : public Lock {
21 Proxy *p;
22
23 public:
24 ScopedLock(Proxy *p) : Lock(p->m), p(p) {}
25 [[nodiscard]] auto &get() {
26 assert(this->owns_lock());
27 return p->data;
28 }
29 auto *operator->() {
30 assert(this->owns_lock());
31 return &p->data;
32 }
33 [[nodiscard]] auto &cond() { return p->cond; }
34};
35
37 inline static constexpr std::chrono::microseconds poll_interval{1};
38 std::atomic<int> notified;
39
40public:
41 PollingConditionVariable(): notified(0){}
42 void notify_all(){
43 ++notified;
44 }
45 void wait(std::unique_lock<std::mutex> &lock){
46 int notify_target = notified.load();
47 ScopedUnlock un(lock);
48 while(notified.load() == notify_target){
49 std::this_thread::sleep_for(poll_interval);
50 }
51 }
52 template <typename Pred>
53 void wait(std::unique_lock<std::mutex> &lock, Pred pred){
54 while(!pred()){
55 ScopedUnlock un(lock);
56 std::this_thread::sleep_for(poll_interval);
57 }
58 }
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();
62 ScopedUnlock un(lock);
63 while(notified.load() == notify_target){
64 if(Clock::now() >= abs){
65 return std::cv_status::timeout;
66 }
67 std::this_thread::sleep_for(poll_interval);
68 }
69 return std::cv_status::no_timeout;
70 }
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){
73 while(!pred()){
74 if(Clock::now() >= abs){
75 return false;
76 }
77 ScopedUnlock un(lock);
78 std::this_thread::sleep_for(poll_interval);
79 }
80 return true;
81 }
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);
85 }
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);
89 }
90};
91
108template <typename T>
110 T data;
111 mutable std::mutex m;
112 mutable PollingConditionVariable cond;
113
114 public:
115 template <typename Proxy, typename Lock>
116 friend class ScopedLock;
117
119 return this;
120 }
122 const_lock() const {
123 return this;
124 }
125};
126template <typename T>
128 T data;
129 mutable std::shared_mutex m;
130
131 public:
132 template <typename Proxy, typename Lock>
133 friend class ScopedLock;
134
135 [[nodiscard]] ScopedLock<SharedMutexProxy,
136 std::unique_lock<std::shared_mutex>>
138 return this;
139 }
140 [[nodiscard]] ScopedLock<const SharedMutexProxy,
141 std::shared_lock<std::shared_mutex>>
142 shared_lock() const {
143 return this;
144 }
145};
146
147} // namespace internal
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
Definition mutex.h:20
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
Definition unlock.h:11
#define WEBCFACE_NS_END
Definition webcface-config.h:118
#define WEBCFACE_NS_BEGIN
Definition webcface-config.h:117