Commit 02447777 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

Fix crash in SingletonRelaxedCounter

Summary: [Folly] Fix crash in `SingletonRelaxedCounter` when recreating threads.

Reviewed By: andriigrynenko

Differential Revision: D13645448

fbshipit-source-id: dea09925f1943ebf2141d69b791a9259524c19e1
parent 700f1926
...@@ -132,6 +132,7 @@ class SingletonRelaxedCounter { ...@@ -132,6 +132,7 @@ class SingletonRelaxedCounter {
global.fallback.fetch_add(current, std::memory_order_relaxed); global.fallback.fetch_add(current, std::memory_order_relaxed);
} }
} }
tracking->lifetimes.erase(this);
} }
void track(CounterAndCache& state) { void track(CounterAndCache& state) {
......
...@@ -44,7 +44,22 @@ TEST_F(SingletonRelaxedCounterTest, Basic) { ...@@ -44,7 +44,22 @@ TEST_F(SingletonRelaxedCounterTest, Basic) {
TEST_F(SingletonRelaxedCounterTest, MultithreadCorrectness) { TEST_F(SingletonRelaxedCounterTest, MultithreadCorrectness) {
static constexpr size_t const kPerThreadIncrements = 1000000; static constexpr size_t const kPerThreadIncrements = 1000000;
static constexpr size_t const kNumThreads = 24; static constexpr size_t const kNumThreads = 24;
static constexpr size_t const kNumCounters = 6;
static constexpr size_t const kNumIters = 2;
std::atomic<bool> stop{false};
std::vector<std::thread> counters(kNumCounters);
for (auto& counter : counters) {
counter = std::thread([&] {
while (!stop.load(std::memory_order_relaxed)) {
std::this_thread::yield();
Counter::count();
}
});
}
for (size_t j = 0; j < kNumIters; ++j) {
std::vector<std::thread> threads(kNumThreads); std::vector<std::thread> threads(kNumThreads);
boost::barrier barrier{kNumThreads + 1}; boost::barrier barrier{kNumThreads + 1};
...@@ -82,5 +97,13 @@ TEST_F(SingletonRelaxedCounterTest, MultithreadCorrectness) { ...@@ -82,5 +97,13 @@ TEST_F(SingletonRelaxedCounterTest, MultithreadCorrectness) {
for (auto& thread : threads) { for (auto& thread : threads) {
thread.join(); thread.join();
} }
}
stop.store(true, std::memory_order_relaxed);
for (auto& counter : counters) {
counter.join();
}
Counter::count();
} }
} // namespace folly } // namespace folly
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