-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathLockingPolicies_c++20.cpp
220 lines (184 loc) · 5.05 KB
/
LockingPolicies_c++20.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Author: damir ljubic
// email: damirlj@yahoo.com
// All rights reserved.
#include <mutex>
#include <concepts>
#include <thread>
#include <iostream>
#include <array>
#include <chrono>
// https://godbolt.org/z/1bfcKfb83
template <typename Lock>
concept is_lockable = requires(std::remove_cvref_t<Lock>& lock)
{
lock.lock();
lock.unlock();
};
template <typename Host, typename Lock>
requires is_lockable<Lock>
struct ObjectLock
{
private: // data
mutable Lock lock_ {};
private: // methods
inline void lock() const { lock_.lock(); }
inline void unlock() const { lock_.unlock(); }
public:
/**
* This is requirement on enclosing class - lock level, to have the same (name) inner type that
* implements the actual locking strategy
*/
struct ScopeLock final
{
explicit ScopeLock(const ObjectLock& obj) noexcept : obj_{obj} { obj_.lock(); }
~ScopeLock() { obj_.unlock(); }
private:
const ObjectLock& obj_;
};
};
namespace locking
{
template <typename Lock>
using scope_lock = std::lock_guard<Lock>;
template <typename Host, typename Lock>
requires is_lockable<Lock>
struct ObjectLock
{
private: // data
mutable Lock lock_ {};
public:
/**
* Instead of having constraint on the inner (name) type, we
* can impose constraint on the behavioral aspect - that all lock-levels in our
* Locking policy have the same call-operator signature
*/
[[nodiscard]] inline auto operator()() const
{
return scope_lock<Lock> {lock_};
}
};
}
template <typename Host , typename Lock>
requires is_lockable<Lock>
struct ClassLock
{
private: // data
static inline Lock lock_{};
private: //methods
static void lock() { lock_.lock(); }
static void unlock() { lock_.unlock(); }
public:
struct ScopeLock final
{
explicit ScopeLock(const ClassLock& ) noexcept { lock(); }
~ScopeLock() { unlock(); }
};
};
namespace locking {
template <typename Host , typename Lock>
requires is_lockable<Lock>
struct ClassLock
{
private: // data
static inline Lock lock_{};
public:
[[nodiscard]] inline auto operator()() const
{
return scope_lock<Lock> {lock_};
}
};
}
template <typename Locker>
concept has_scope_lock = requires {
typename Locker::ScopeLock;
};
template <template <class, class> class Locker, typename Lock = std::mutex>
struct Counter
{
static_assert(has_scope_lock<Locker<Counter, Lock>>, "Invalid locking policy");
private:
int count_ = 0;
using lock_type = Locker<Counter, Lock>;
lock_type lock_ {};
public:
int operator()()
{
typename lock_type::ScopeLock lock {lock_};
std::cout << '[' << std::this_thread::get_id() << "] counter: " << ++count_ << '\n';
return count_;
}
void reset()
{
typename lock_type::ScopeLock lock {lock_};
count_ = 0;
}
};
namespace locking {
template <template <class, class> class Locker, typename Lock = std::mutex>
struct Counter
{
private:
int count_ = 0;
using lock_type = Locker<Counter, Lock>;
lock_type lock_ {};
public:
int operator()()
{
auto locker = lock_();
std::cout << '[' << std::this_thread::get_id() << "] counter: " << ++count_ << '\n';
return count_;
}
};
}
template <typename Counter>
void test_objectLevelLock()
{
std::cout << __func__ << '\n';
Counter counter;
auto thread_func = [&counter](int N) mutable
{
using namespace std::chrono_literals;
for (int i = 0; i < N; ++i )
{
counter();
std::this_thread::sleep_for(10ms);
}
};
std::array<std::jthread, 3> threads =
{
std::jthread{thread_func, 100},
std::jthread{thread_func, 50},
std::jthread{thread_func, 30}
};
}
template <typename Counter>
void test_classLevelLock()
{
std::cout << __func__ << '\n';
auto thread_func = [](int N)
{
using namespace std::chrono_literals;
Counter counter;
for (int i = 0; i < N; ++i )
{
counter();
std::this_thread::sleep_for(10ms);
}
};
std::array<std::jthread, 3> threads =
{
std::jthread{thread_func, 100},
std::jthread{thread_func, 50},
std::jthread{thread_func, 30}
};
}
int main()
{
// Object-level lock
test_objectLevelLock<locking::Counter<locking::ObjectLock>>();
test_objectLevelLock<Counter<ObjectLock>>();
// Class-level lock
test_classLevelLock<Counter<ClassLock>>();
test_classLevelLock<locking::Counter<locking::ClassLock>>();
return 0;
}