-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathconcurrent_map.h
More file actions
209 lines (168 loc) · 4.8 KB
/
concurrent_map.h
File metadata and controls
209 lines (168 loc) · 4.8 KB
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
// Copyright (C) 2017-2018 Egor Pugin <[email protected]>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#pragma once
#include <junction/ConcurrentMap_Leapfrog.h>
#include <primitives/exceptions.h>
#include <primitives/templates.h>
#include <memory>
namespace sw
{
using ConcurrentContext = junction::QSBR::Context;
template <class K, class V>
struct ConcurrentMap
{
using MapType = junction::ConcurrentMap_Leapfrog<K, V*>;
//using MapType = std::unordered_map<K, V*>;
using value_type = std::pair<K, V>;
using insert_type = std::pair<V*, bool>;
ConcurrentMap()
{
clear();
}
~ConcurrentMap()
{
// causes crashes
/*if (!map)
return;
for (auto i = getIterator(); i.isValid(); i.next())
delete i.getValue();
map.reset();*/
}
void clear()
{
map = std::make_unique<MapType>();
}
insert_type insert(const value_type &v)
{
return insert(v.first, v.second);
}
template <class Deleter>
insert_type insert(K k, const V &v, Deleter &&d)
{
if (k == 0)
throw SW_RUNTIME_ERROR("ConcurrentMap: zero key");
//std::unique_lock lk(m);
return insert_no_lock(k, v, d);
}
template <class Deleter>
insert_type insert_no_lock(K k, const V &v, Deleter &&d)
{
if (k == 0)
throw SW_RUNTIME_ERROR("ConcurrentMap: zero key");
/*auto i = map->find(k);
if (i == map->end())
{
auto value = new V(v);
map->emplace(k, value);
return { value, true };
}
return { i->second, false };*/
auto i = map->insertOrFind(k);
auto value = i.getValue();
if (!value)
{
value = new V(v);
auto oldValue = i.exchangeValue(value);
if (oldValue)
{
*value = *oldValue;
std::forward<Deleter>(d)(oldValue);
return { value, false };
}
return { value, true };
}
return { value, false };
}
insert_type insert(K k, const V &v = V())
{
if constexpr (std::is_pointer_v<V>)
return insert(k, v, [](auto *v)
{
junction::DefaultQSBR.enqueue(&V::destroy, v);
});
else
return insert(k, v, [](auto *v) {});
}
insert_type insert_ptr(K k, const V &v = V())
{
if constexpr (std::is_pointer_v<V>)
return insert(k, v, [](auto *v) { delete *v; });
else
return insert(k, v, [](auto *v) {});
}
V &operator[](K k)
{
return *insert(k).first;
}
const V &operator[](K k) const
{
return *insert(k).first;
}
auto getIterator()
{
return typename MapType::Iterator(*map);
}
struct end_iterator {};
struct iterator
{
typename MapType::Iterator i;
iterator(typename MapType::Iterator i) : i(i) {}
bool operator!=(const end_iterator &e) const { return i.isValid(); }
std::pair<K, V&> operator*() { return { i.getKey(), *i.getValue() }; }
void operator++() { i.next(); }
};
iterator begin() { return getIterator(); }
end_iterator end() { return {}; }
// for usual maps
//iterator begin() { return map->begin(); }
//iterator end() { return map->end(); }
//auto begin() const { return map->begin(); }
//auto end() const { return map->end(); }
/*for (auto i = getIterator(); i.isValid(); i.next())
{
b.write(i.getKey());
b.write(*i.getValue()->mtime);
}*/
private:
std::unique_ptr<MapType> map;
//std::mutex m;
};
template <class V>
using ConcurrentMapSimple = ConcurrentMap<size_t, V>;
template <class K, class V>
struct ConcurrentHashMap : ConcurrentMapSimple<V>
{
using Base = ConcurrentMapSimple<V>;
using Base::operator[];
using value_type = std::pair<K, V>;
using insert_type = typename Base::insert_type;
using Base::insert;
insert_type insert(const K &k, const V &v = V())
{
return insert(value_type{ k, v });
}
insert_type insert(const value_type &v)
{
return Base::insert(std::hash<K>()(v.first), v.second);
}
V &operator[](const K &k)
{
return *insert(k).first;
}
const V &operator[](const K &k) const
{
return *insert(k).first;
}
};
SW_BUILDER_API
SW_DECLARE_GLOBAL_STATIC_FUNCTION(ConcurrentContext, getConcurrentContext);
SW_BUILDER_API
ConcurrentContext createConcurrentContext();
SW_BUILDER_API
void destroyConcurrentContext(ConcurrentContext ctx);
SW_BUILDER_API
void updateConcurrentContext();
}