Skip to content

Commit c9520f1

Browse files
committed
Fix issue #255 - Incorrect concurrent increment of value with std::shared_mutex.
1 parent 4817a6d commit c9520f1

File tree

2 files changed

+58
-26
lines changed

2 files changed

+58
-26
lines changed

parallel_hashmap/phmap.h

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2616,7 +2616,6 @@ class parallel_hash_set
26162616
using UniqueLock = typename Lockable::UniqueLock;
26172617
using SharedLock = typename Lockable::SharedLock;
26182618
using ReadWriteLock = typename Lockable::ReadWriteLock;
2619-
26202619

26212620
// --------------------------------------------------------------------
26222621
struct Inner : public Lockable
@@ -3178,14 +3177,9 @@ class parallel_hash_set
31783177
{
31793178
Inner& inner = sets_[subidx(hashval)];
31803179
auto& set = inner.set_;
3181-
ReadWriteLock m(inner);
3180+
UniqueLock m(inner);
31823181

31833182
size_t offset = set._find_key(key, hashval);
3184-
if (offset == (size_t)-1 && m.switch_to_unique()) {
3185-
// we did an unlock/lock, and another thread could have inserted the same key, so we need to
3186-
// do a find() again.
3187-
offset = set._find_key(key, hashval);
3188-
}
31893183
if (offset == (size_t)-1) {
31903184
offset = set.prepare_insert(hashval);
31913185
set.emplace_at(offset, std::forward<Args>(args)...);
@@ -3268,13 +3262,8 @@ class parallel_hash_set
32683262
iterator lazy_emplace_with_hash(const key_arg<K>& key, size_t hashval, F&& f) {
32693263
Inner& inner = sets_[subidx(hashval)];
32703264
auto& set = inner.set_;
3271-
ReadWriteLock m(inner);
3265+
UniqueLock m(inner);
32723266
size_t offset = set._find_key(key, hashval);
3273-
if (offset == (size_t)-1 && m.switch_to_unique()) {
3274-
// we did an unlock/lock, and another thread could have inserted the same key, so we need to
3275-
// do a find() again.
3276-
offset = set._find_key(key, hashval);
3277-
}
32783267
if (offset == (size_t)-1) {
32793268
offset = set.prepare_insert(hashval);
32803269
set.lazy_emplace_at(offset, std::forward<F>(f));
@@ -3389,7 +3378,7 @@ class parallel_hash_set
33893378
template <class K = key_type, class FExists, class FEmplace>
33903379
bool lazy_emplace_l(const key_arg<K>& key, FExists&& fExists, FEmplace&& fEmplace) {
33913380
size_t hashval = this->hash(key);
3392-
ReadWriteLock m;
3381+
UniqueLock m;
33933382
auto res = this->find_or_prepare_insert_with_hash(hashval, key, m);
33943383
Inner* inner = std::get<0>(res);
33953384
if (std::get<2>(res)) {
@@ -3843,16 +3832,11 @@ class parallel_hash_set
38433832

38443833
template <class K>
38453834
std::tuple<Inner*, size_t, bool>
3846-
find_or_prepare_insert_with_hash(size_t hashval, const K& key, ReadWriteLock &mutexlock) {
3835+
find_or_prepare_insert_with_hash(size_t hashval, const K& key, UniqueLock &mutexlock) {
38473836
Inner& inner = sets_[subidx(hashval)];
38483837
auto& set = inner.set_;
3849-
mutexlock = std::move(ReadWriteLock(inner));
3838+
mutexlock = std::move(UniqueLock(inner));
38503839
size_t offset = set._find_key(key, hashval);
3851-
if (offset == (size_t)-1 && mutexlock.switch_to_unique()) {
3852-
// we did an unlock/lock, and another thread could have inserted the same key, so we need to
3853-
// do a find() again.
3854-
offset = set._find_key(key, hashval);
3855-
}
38563840
if (offset == (size_t)-1) {
38573841
offset = set.prepare_insert(hashval);
38583842
return std::make_tuple(&inner, offset, true);
@@ -3862,7 +3846,7 @@ class parallel_hash_set
38623846

38633847
template <class K>
38643848
std::tuple<Inner*, size_t, bool>
3865-
find_or_prepare_insert(const K& key, ReadWriteLock &mutexlock) {
3849+
find_or_prepare_insert(const K& key, UniqueLock &mutexlock) {
38663850
return find_or_prepare_insert_with_hash<K>(this->hash(key), key, mutexlock);
38673851
}
38683852

@@ -4084,7 +4068,7 @@ class parallel_hash_map : public parallel_hash_set<N, RefSet, Mtx_, Policy, Hash
40844068
template <class K = key_type, class F, class... Args>
40854069
bool try_emplace_l(K&& k, F&& f, Args&&... args) {
40864070
size_t hashval = this->hash(k);
4087-
ReadWriteLock m;
4071+
UniqueLock m;
40884072
auto res = this->find_or_prepare_insert_with_hash(hashval, k, m);
40894073
typename Base::Inner *inner = std::get<0>(res);
40904074
if (std::get<2>(res)) {
@@ -4105,7 +4089,7 @@ class parallel_hash_map : public parallel_hash_set<N, RefSet, Mtx_, Policy, Hash
41054089
template <class K = key_type, class... Args>
41064090
std::pair<typename parallel_hash_map::parallel_hash_set::pointer, bool> try_emplace_p(K&& k, Args&&... args) {
41074091
size_t hashval = this->hash(k);
4108-
ReadWriteLock m;
4092+
UniqueLock m;
41094093
auto res = this->find_or_prepare_insert_with_hash(hashval, k, m);
41104094
typename Base::Inner *inner = std::get<0>(res);
41114095
if (std::get<2>(res)) {
@@ -4135,7 +4119,7 @@ class parallel_hash_map : public parallel_hash_set<N, RefSet, Mtx_, Policy, Hash
41354119
template <class K, class V>
41364120
std::pair<iterator, bool> insert_or_assign_impl(K&& k, V&& v) {
41374121
size_t hashval = this->hash(k);
4138-
ReadWriteLock m;
4122+
UniqueLock m;
41394123
auto res = this->find_or_prepare_insert_with_hash(hashval, k, m);
41404124
typename Base::Inner *inner = std::get<0>(res);
41414125
if (std::get<2>(res)) {
@@ -4155,7 +4139,7 @@ class parallel_hash_map : public parallel_hash_set<N, RefSet, Mtx_, Policy, Hash
41554139

41564140
template <class K = key_type, class... Args>
41574141
std::pair<iterator, bool> try_emplace_impl_with_hash(size_t hashval, K&& k, Args&&... args) {
4158-
ReadWriteLock m;
4142+
UniqueLock m;
41594143
auto res = this->find_or_prepare_insert_with_hash(hashval, k, m);
41604144
typename Base::Inner *inner = std::get<0>(res);
41614145
if (std::get<2>(res)) {

tests/parallel_flat_hash_map_test.cc

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,52 @@
11
#define THIS_HASH_MAP parallel_flat_hash_map
22
#define THIS_TEST_NAME ParallelFlatHashMap
3+
#include <thread>
34

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

0 commit comments

Comments
 (0)