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
13namespace internal {
14
15template <typename Proxy, typename Lock>
16class ScopedLock : public Lock {
17 Proxy *p;
18
19 public:
20 ScopedLock(Proxy *p) : Lock(p->m), p(p) {}
21 [[nodiscard]] auto &get() {
22 assert(this->owns_lock());
23 return p->data;
24 }
25 auto *operator->() {
26 assert(this->owns_lock());
27 return &p->data;
28 }
29 [[nodiscard]] auto &cond() { return p->cond; }
30};
31
48template <typename T>
50 T data;
51 mutable std::mutex m;
52 mutable std::condition_variable cond;
53
54 public:
55 template <typename Proxy, typename Lock>
56 friend class ScopedLock;
57
59 return this;
60 }
62 const_lock() const {
63 return this;
64 }
65};
66template <typename T>
68 T data;
69 mutable std::shared_mutex m;
70
71 public:
72 template <typename Proxy, typename Lock>
73 friend class ScopedLock;
74
75 [[nodiscard]] ScopedLock<SharedMutexProxy,
76 std::unique_lock<std::shared_mutex>>
77 lock() {
78 return this;
79 }
80 [[nodiscard]] ScopedLock<const SharedMutexProxy,
81 std::shared_lock<std::shared_mutex>>
82 shared_lock() const {
83 return this;
84 }
85};
86
87} // namespace internal
RustのMutexのようにデータを保護するクラス
Definition mutex.h:49
ScopedLock< const MutexProxy, std::unique_lock< std::mutex > > const_lock() const
Definition mutex.h:62
ScopedLock< MutexProxy, std::unique_lock< std::mutex > > lock()
Definition mutex.h:58
Definition mutex.h:16
auto * operator->()
Definition mutex.h:25
auto & cond()
Definition mutex.h:29
auto & get()
Definition mutex.h:21
ScopedLock(Proxy *p)
Definition mutex.h:20
ScopedLock< const SharedMutexProxy, std::shared_lock< std::shared_mutex > > shared_lock() const
Definition mutex.h:82
ScopedLock< SharedMutexProxy, std::unique_lock< std::shared_mutex > > lock()
Definition mutex.h:77
#define WEBCFACE_NS_END
Definition webcface-config.h:118
#define WEBCFACE_NS_BEGIN
Definition webcface-config.h:117