Commit 839b3f31 authored by Maged Michael's avatar Maged Michael Committed by Facebook Github Bot

Detect data race in IndexedMemPool

Summary: IndexedMemPool uses regular memory for the global and local next links. There is a data race as there can be concurrent reads and writes of such links. Added a test for data races.

Reviewed By: nbronson

Differential Revision: D4680286

fbshipit-source-id: 9ef3ed439b9df07d69afe570e516c146caa53a6c
parent fb9776fc
......@@ -216,3 +216,29 @@ TEST(IndexedMemPool, late_recycle) {
}
EXPECT_EQ(NonTrivialStruct::count, 0);
}
TEST(IndexedMemPool, no_data_races) {
const int count = 1000;
const uint32_t poolSize = 100;
const int nthreads = 10;
using Pool = IndexedMemPool<int, 8, 8>;
Pool pool(poolSize);
std::vector<std::thread> thr(nthreads);
for (auto i = 0; i < nthreads; ++i) {
thr[i] = std::thread([&]() {
for (auto j = 0; j < count; ++j) {
uint32_t idx = pool.allocIndex();
EXPECT_NE(idx, 0u);
EXPECT_LE(
idx, poolSize + (pool.NumLocalLists - 1) * pool.LocalListLimit);
pool[idx] = j;
pool.recycleIndex(idx);
}
});
}
for (auto& t : thr) {
t.join();
}
}
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