-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConcurrentHashMap.h
240 lines (236 loc) · 7.69 KB
/
ConcurrentHashMap.h
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#pragma once
#include <shared_mutex>
#include <algorithm>
#include <unordered_map>
template <typename F, typename Mtx>
auto DefenceScopeReadLock(F f, Mtx &mtx, bool withLock) -> typename std::enable_if<std::is_same<decltype(f()), void>::value, void>::type {
if (withLock) {
mtx.lock_shared();
}
try {
f();
} catch (...) {
if (withLock) {
mtx.unlock_shared();
}
throw;
}
if (withLock) {
mtx.unlock_shared();
}
}
template <typename F, typename Mtx>
auto DefenceScopeWriteLock(F f, Mtx &mtx, bool withLock) -> typename std::enable_if<std::is_same<decltype(f()), void>::value, void>::type {
if (withLock) {
mtx.lock();
}
try {
f();
} catch (...) {
if (withLock) {
mtx.unlock();
}
throw;
}
if (withLock) {
mtx.unlock();
}
}
template <typename F, typename Mtx>
auto DefenceScopeReadLock(F f, Mtx &mtx, bool withLock) -> typename std::enable_if<!std::is_same<decltype(f()), void>::value, decltype(f())>::type {
decltype(f()) result;
if (withLock) {
mtx.lock_shared();
}
try {
result = f();
} catch (...) {
if (withLock) {
mtx.unlock_shared();
}
throw;
}
if (withLock) {
mtx.unlock_shared();
}
return result;
}
template <typename F, typename Mtx>
auto DefenceScopeWriteLock(F f, Mtx &mtx, bool withLock) -> typename std::enable_if<!std::is_same<decltype(f()), void>::value, decltype(f())>::type {
decltype(f()) result;
if (withLock) {
mtx.lock();
}
try {
result = f();
} catch (...) {
if (withLock) {
mtx.unlock();
}
throw;
}
if (withLock) {
mtx.unlock();
}
return result;
}
template <typename Key, typename Value>
class ConcurrentHashMap {
mutable std::unordered_map<Key, Value> _map;
mutable std::shared_timed_mutex _rwLock;
public:
enum class OperationMode { WITH_LOCK, FORCE_NO_LOCK };
using ValueType = typename std::unordered_map<Key, Value>::value_type;
ConcurrentHashMap() = default;
explicit ConcurrentHashMap(size_t n) : _map(n) {}
ConcurrentHashMap(std::initializer_list<ValueType> list, size_t n = 0) : _map(list, n) {}
ConcurrentHashMap(const ConcurrentHashMap &o) {
std::shared_lock<std::shared_timed_mutex> readRWLock(o._rwLock);
_map = o._map;
}
ConcurrentHashMap(ConcurrentHashMap &&o) noexcept : _rwLock() {
std::unique_lock<std::shared_timed_mutex> writeRWLock(o._rwLock);
_map = std::move(o._map);
}
const std::shared_timed_mutex &rwLock() const { return _rwLock; }
std::shared_timed_mutex &rwLock() { return _rwLock; }
void insert(const Key &key, const Value &value, OperationMode mode = OperationMode::WITH_LOCK) {
DefenceScopeWriteLock([&]() { _map.insert(std::make_pair(key, value)); }, _rwLock, mode == OperationMode::WITH_LOCK);
}
void emplace(Key &&key, Value &&value, OperationMode mode = OperationMode::WITH_LOCK) {
DefenceScopeWriteLock([&]() { _map.emplace(std::forward<Key>(key), std::forward<Value>(value)); }, _rwLock, mode == OperationMode::WITH_LOCK);
}
bool empty(OperationMode mode = OperationMode::WITH_LOCK) const {
return DefenceScopeReadLock([&]() -> bool { return _map.empty(); }, _rwLock, mode == OperationMode::WITH_LOCK);
}
size_t size(OperationMode mode = OperationMode::WITH_LOCK) const {
return DefenceScopeReadLock([&]() -> size_t { return _map.size(); }, _rwLock, mode == OperationMode::WITH_LOCK);
}
bool contains(const Key &k, OperationMode mode = OperationMode::WITH_LOCK) const {
return DefenceScopeReadLock([&]() -> bool { _map.find(k) != _map.end(); }, _rwLock, mode == OperationMode::WITH_LOCK);
}
void erase(const Key &key, OperationMode mode = OperationMode::WITH_LOCK) {
DefenceScopeWriteLock([&]() { _map.erase(key); }, _rwLock, mode == OperationMode::WITH_LOCK);
}
void eraseKeys(const std::vector<Key> &keys, OperationMode mode = OperationMode::WITH_LOCK) {
DefenceScopeWriteLock(
[&]() {
for (const auto &k : keys) {
_map.erase(k);
}
},
_rwLock,
mode == OperationMode::WITH_LOCK);
}
template <typename Predicate>
void eraseIf(Predicate predicate, OperationMode mode = OperationMode::WITH_LOCK) {
DefenceScopeWriteLock(
[&]() {
for (auto it = _map.begin(); it != _map.end();) {
if (predicate(*it)) {
it = _map.erase(it);
} else {
++it;
}
}
},
_rwLock,
mode == OperationMode::WITH_LOCK);
}
template <typename Predicate>
void eraseKeyIf(Predicate predicate, const Key &key, OperationMode mode = OperationMode::WITH_LOCK) {
DefenceScopeWriteLock(
[&]() {
auto item = _map.find(key);
if (item != _map.end() && predicate(*item)) {
_map.erase(item);
}
},
_rwLock,
mode == OperationMode::WITH_LOCK);
}
template <typename Function>
void doForEeach(Function function, OperationMode mode = OperationMode::WITH_LOCK) const {
DefenceScopeReadLock([&]() { std::for_each(_map.begin(), _map.end(), function); }, _rwLock, mode == OperationMode::WITH_LOCK);
}
template <typename Function>
void doForEeach(Function function, OperationMode mode = OperationMode::WITH_LOCK) {
DefenceScopeWriteLock([&]() { std::for_each(_map.begin(), _map.end(), function); }, _rwLock, mode == OperationMode::WITH_LOCK);
}
template <typename Function, typename Predicate>
void doForEeachIf(Function function, Predicate predicate, OperationMode mode = OperationMode::WITH_LOCK) const {
DefenceScopeReadLock(
[&]() {
for (const auto &item : _map) {
if (predicate(item)) {
function(item);
}
}
},
_rwLock,
mode == OperationMode::WITH_LOCK);
}
template <typename Function, typename Predicate>
void doForEeachIf(Function function, Predicate predicate, OperationMode mode = OperationMode::WITH_LOCK) {
DefenceScopeWriteLock(
[&]() {
for (auto &item : _map) {
if (predicate(item)) {
function(item);
}
}
},
_rwLock,
mode == OperationMode::WITH_LOCK);
}
template <typename Function>
void doForKey(Function function, const Key &key, OperationMode mode = OperationMode::WITH_LOCK) const {
DefenceScopeReadLock(
[&]() {
auto item = _map.find(key);
if (item != _map.end()) {
function(*item);
}
},
_rwLock,
mode == OperationMode::WITH_LOCK);
}
template <typename Function>
void doForKey(Function function, const Key &key, OperationMode mode = OperationMode::WITH_LOCK) {
DefenceScopeWriteLock(
[&]() {
auto item = _map.find(key);
if (item != _map.end()) {
function(*item);
}
},
_rwLock,
mode == OperationMode::WITH_LOCK);
}
template <typename Function>
void insertOrDoIfExists(const Key &key, const Value &value, Function f) {
if (contains(key)) {
doForKey(f, key);
} else {
std::unique_lock<std::shared_timed_mutex> writeRWLock(_rwLock);
if (contains(key, OperationMode::FORCE_NO_LOCK)) {
doForKey(f, key, OperationMode::FORCE_NO_LOCK);
} else {
insert(key, value, OperationMode::FORCE_NO_LOCK);
}
}
}
template <typename Function>
void emplaceOrDoIfExists(Key &&key, Value &&value, Function f) {
if (contains(key)) {
doForKey(f, key);
} else {
std::unique_lock<std::shared_timed_mutex> writeRWLock(_rwLock);
if (contains(key, OperationMode::FORCE_NO_LOCK)) {
doForKey(f, key, OperationMode::FORCE_NO_LOCK);
} else {
emplace(std::move(key), std::move(value), OperationMode::FORCE_NO_LOCK);
}
}
}
};