Commit fef59129 authored by Maged Michael's avatar Maged Michael Committed by Facebook GitHub Bot

ConcurrentHashMap: Fix bug in try_emplace

Summary:
Add missing hazard pointer protection of the new node inserted by a successful try_emplace.

Add test that breaks without this fix.

Reviewed By: yfeldblum

Differential Revision: D28361211

fbshipit-source-id: 963527a9c6b24b0cb569d5b49da9ef9a0e8b58cd
parent 89b83873
...@@ -716,6 +716,7 @@ class alignas(64) BucketTable { ...@@ -716,6 +716,7 @@ class alignas(64) BucketTable {
cur->next_.store(headnode, std::memory_order_relaxed); cur->next_.store(headnode, std::memory_order_relaxed);
head->store(cur, std::memory_order_release); head->store(cur, std::memory_order_release);
it.setNode(cur, buckets, bcount, idx); it.setNode(cur, buckets, bcount, idx);
haznode.reset(cur);
return true; return true;
} }
......
...@@ -519,6 +519,27 @@ TYPED_TEST_P(ConcurrentHashMapTest, EraseStressTest) { ...@@ -519,6 +519,27 @@ TYPED_TEST_P(ConcurrentHashMapTest, EraseStressTest) {
} }
} }
TYPED_TEST_P(ConcurrentHashMapTest, TryEmplaceEraseStressTest) {
DeterministicSchedule sched(DeterministicSchedule::uniform(FLAGS_seed));
std::atomic<int> iterations{10000};
std::vector<std::thread> threads;
unsigned int num_threads = 32;
threads.reserve(num_threads);
folly::ConcurrentHashMap<int, int> map;
for (uint32_t t = 0; t < num_threads; t++) {
threads.push_back(lib::thread([&]() {
while (--iterations >= 0) {
auto it = map.try_emplace(1, 101);
map.erase(1);
EXPECT_EQ(it.first->second, 101);
}
}));
}
for (auto& t : threads) {
join;
}
}
TYPED_TEST_P(ConcurrentHashMapTest, IterateStressTest) { TYPED_TEST_P(ConcurrentHashMapTest, IterateStressTest) {
DeterministicSchedule sched(DeterministicSchedule::uniform(FLAGS_seed)); DeterministicSchedule sched(DeterministicSchedule::uniform(FLAGS_seed));
...@@ -993,6 +1014,7 @@ REGISTER_TYPED_TEST_CASE_P( ...@@ -993,6 +1014,7 @@ REGISTER_TYPED_TEST_CASE_P(
EraseStressTest, EraseStressTest,
EraseTest, EraseTest,
ForEachLoop, ForEachLoop,
TryEmplaceEraseStressTest,
IterateStressTest, IterateStressTest,
RefcountTest, RefcountTest,
UpdateStressTest, UpdateStressTest,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment