Skip to content

Commit 7301b8e

Browse files
committed
1 parent 06f8277 commit 7301b8e

File tree

2 files changed

+57
-27
lines changed

2 files changed

+57
-27
lines changed

include/gtl/phmap.hpp

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3721,14 +3721,9 @@ class parallel_hash_set {
37213721
std::pair<iterator, bool> emplace_decomposable_with_hash(const K& key, size_t hashval, Args&&... args) {
37223722
Inner& inner = sets_[subidx(hashval)];
37233723
auto& set = inner.set_;
3724-
ReadWriteLock m(inner);
3724+
UniqueLock m(inner);
37253725

37263726
size_t offset = set._find_key(key, hashval);
3727-
if (offset == (size_t)-1 && m.switch_to_unique()) {
3728-
// we did an unlock/lock, and another thread could have inserted the same key, so we need to
3729-
// do a find() again.
3730-
offset = set._find_key(key, hashval);
3731-
}
37323727
if (offset == (size_t)-1) {
37333728
offset = set.prepare_insert(hashval);
37343729
set.emplace_at(offset, std::forward<Args>(args)...);
@@ -3807,13 +3802,8 @@ class parallel_hash_set {
38073802
iterator lazy_emplace_with_hash(const key_arg<K>& key, size_t hashval, F&& f) {
38083803
Inner& inner = sets_[subidx(hashval)];
38093804
auto& set = inner.set_;
3810-
ReadWriteLock m(inner);
3805+
UniqueLock m(inner);
38113806
size_t offset = set._find_key(key, hashval);
3812-
if (offset == (size_t)-1 && m.switch_to_unique()) {
3813-
// we did an unlock/lock, and another thread could have inserted the same key, so we need to
3814-
// do a find() again.
3815-
offset = set._find_key(key, hashval);
3816-
}
38173807
if (offset == (size_t)-1) {
38183808
offset = set.prepare_insert(hashval);
38193809
set.lazy_emplace_at(offset, std::forward<F>(f));
@@ -3929,7 +3919,7 @@ class parallel_hash_set {
39293919
template<class K = key_type, class FExists, class FEmplace>
39303920
bool lazy_emplace_l(const key_arg<K>& key, FExists&& fExists, FEmplace&& fEmplace) {
39313921
size_t hashval = this->hash(key);
3932-
ReadWriteLock m;
3922+
UniqueLock m;
39333923
auto res = this->find_or_prepare_insert_with_hash(hashval, key, m);
39343924
Inner* inner = std::get<0>(res);
39353925
if (std::get<2>(res)) {
@@ -4376,18 +4366,13 @@ class parallel_hash_set {
43764366
}
43774367

43784368
template<class K>
4379-
std::tuple<Inner*, size_t, bool> find_or_prepare_insert_with_hash(size_t hashval,
4380-
const K& key,
4381-
ReadWriteLock& mutexlock) {
4369+
std::tuple<Inner*, size_t, bool> find_or_prepare_insert_with_hash(size_t hashval,
4370+
const K& key,
4371+
UniqueLock& mutexlock) {
43824372
Inner& inner = sets_[subidx(hashval)];
43834373
auto& set = inner.set_;
4384-
mutexlock = std::move(ReadWriteLock(inner));
4374+
mutexlock = std::move(UniqueLock(inner));
43854375
size_t offset = set._find_key(key, hashval);
4386-
if (offset == (size_t)-1 && mutexlock.switch_to_unique()) {
4387-
// we did an unlock/lock, and another thread could have inserted the same key, so we need to
4388-
// do a find() again.
4389-
offset = set._find_key(key, hashval);
4390-
}
43914376
if (offset == (size_t)-1) {
43924377
offset = set.prepare_insert(hashval);
43934378
return std::make_tuple(&inner, offset, true);
@@ -4396,7 +4381,7 @@ class parallel_hash_set {
43964381
}
43974382

43984383
template<class K>
4399-
std::tuple<Inner*, size_t, bool> find_or_prepare_insert(const K& key, ReadWriteLock& mutexlock) {
4384+
std::tuple<Inner*, size_t, bool> find_or_prepare_insert(const K& key, UniqueLock& mutexlock) {
44004385
return find_or_prepare_insert_with_hash<K>(this->hash(key), key, mutexlock);
44014386
}
44024387

@@ -4607,7 +4592,7 @@ class parallel_hash_map : public parallel_hash_set<N, RefSet, Mtx_, AuxCont, Pol
46074592
template<class K = key_type, class F, class... Args>
46084593
bool try_emplace_l(K&& k, F&& f, Args&&... args) {
46094594
size_t hashval = this->hash(k);
4610-
ReadWriteLock m;
4595+
UniqueLock m;
46114596
auto res = this->find_or_prepare_insert_with_hash(hashval, k, m);
46124597
typename Base::Inner* inner = std::get<0>(res);
46134598

@@ -4631,7 +4616,7 @@ class parallel_hash_map : public parallel_hash_set<N, RefSet, Mtx_, AuxCont, Pol
46314616
template<class K = key_type, class... Args>
46324617
std::pair<typename parallel_hash_map::parallel_hash_set::pointer, bool> try_emplace_p(K&& k, Args&&... args) {
46334618
size_t hashval = this->hash(k);
4634-
ReadWriteLock m;
4619+
UniqueLock m;
46354620
auto res = this->find_or_prepare_insert_with_hash(hashval, k, m);
46364621
typename Base::Inner* inner = std::get<0>(res);
46374622
if (std::get<2>(res)) {
@@ -4661,7 +4646,7 @@ class parallel_hash_map : public parallel_hash_set<N, RefSet, Mtx_, AuxCont, Pol
46614646
template<class K, class V>
46624647
std::pair<iterator, bool> insert_or_assign_impl(K&& k, V&& v) {
46634648
size_t hashval = this->hash(k);
4664-
ReadWriteLock m;
4649+
UniqueLock m;
46654650
auto res = this->find_or_prepare_insert(k, m);
46664651
typename Base::Inner* inner = std::get<0>(res);
46674652
if (std::get<2>(res)) {
@@ -4679,7 +4664,7 @@ class parallel_hash_map : public parallel_hash_set<N, RefSet, Mtx_, AuxCont, Pol
46794664

46804665
template<class K = key_type, class... Args>
46814666
std::pair<iterator, bool> try_emplace_impl_with_hash(size_t hashval, K&& k, Args&&... args) {
4682-
ReadWriteLock m;
4667+
UniqueLock m;
46834668
auto res = this->find_or_prepare_insert_with_hash(hashval, k, m);
46844669
typename Base::Inner* inner = std::get<0>(res);
46854670
if (std::get<2>(res)) {
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,49 @@
11
#define THIS_HASH_MAP parallel_flat_hash_map
22
#define THIS_TEST_NAME ParallelFlatHashMap
33

4+
#include <thread>
5+
46
#include "parallel_hash_map_test.cpp"
7+
8+
#include <shared_mutex>
9+
10+
template <typename K> using HashEqual = gtl::priv::hash_default_eq<K>;
11+
template <typename V> using HashFn = gtl::priv::hash_default_hash<V>;
12+
template <typename K> using Allocator = gtl::priv::Allocator<K>;
13+
14+
template <typename K, typename V, size_t N>
15+
using parallel_flat_hash_map =
16+
gtl::parallel_flat_hash_map<K, V, HashFn<K>, HashEqual<K>,
17+
Allocator<gtl::priv::Pair<K, V>>, N,
18+
std::shared_mutex>;
19+
20+
using Table = parallel_flat_hash_map<int, int, 10>;
21+
22+
TEST(THIS_TEST_NAME, ConcurrencyCheck) {
23+
static constexpr int THREADS = 10;
24+
static constexpr int EPOCH = 1000;
25+
static constexpr int KEY = 12345;
26+
27+
auto Incr = [](Table *table) {
28+
auto exist_fn = [](typename Table::value_type &value) { value.second += 1; };
29+
auto emplace_fn = [](const typename Table::constructor &ctor) {
30+
ctor(KEY, 1);
31+
};
32+
for (int i = 0; i < EPOCH; ++i) {
33+
(void)table->lazy_emplace_l(KEY, exist_fn, emplace_fn);
34+
}
35+
};
36+
37+
Table table;
38+
std::vector<std::thread> threads;
39+
threads.reserve(THREADS);
40+
for (int i = 0; i < THREADS; ++i) {
41+
threads.emplace_back([&]() { Incr(&table); });
42+
}
43+
44+
for (auto &thread : threads) {
45+
thread.join();
46+
}
47+
48+
EXPECT_EQ(table[KEY], 10000);
49+
}

0 commit comments

Comments
 (0)