Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix data race in BM_ThreadYieldSpinLockThrashing #1099

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 9 additions & 16 deletions api/test/common/spinlock_benchmark.cc
Original file line number Diff line number Diff line change
Expand Up @@ -100,32 +100,25 @@ static void BM_ProcYieldSpinLockThrashing(benchmark::State &s)
[](SpinLockMutex &m) { m.unlock(); });
}

// SpinLock thrashing with thread::yield() after N spins.
// SpinLock thrashing with thread::yield().
static void BM_ThreadYieldSpinLockThrashing(benchmark::State &s)
{
std::atomic<bool> mutex(false);
SpinThrash<std::atomic<bool>>(
std::atomic_flag mutex = ATOMIC_FLAG_INIT;
SpinThrash<std::atomic_flag>(
s, mutex,
[](std::atomic<bool> &l) {
if (!l.exchange(true, std::memory_order_acq_rel))
[](std::atomic_flag &l) {
uint32_t try_count = 0;
while (l.test_and_set(std::memory_order_acq_rel))
{
return;
}
for (std::size_t i = 0; i < 128; ++i)
{
if (!l.load(std::memory_order_acquire) && !l.exchange(true, std::memory_order_acq_rel))
{
return;
}

if (i % 32 == 0)
++try_count;
if (try_count % 32)
{
std::this_thread::yield();
}
}
std::this_thread::yield();
},
[](std::atomic<bool> &l) { l.store(false, std::memory_order_release); });
[](std::atomic_flag &l) { l.clear(std::memory_order_release); });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix. would you help understand this change (probably enrich PR description) how it is fixing the race condition.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the PR description. Please let me know if it needs more information is needed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update. Should we either

  • Change the test comment ( // SpinLock thrashing with thread::yield() after N spins ) as there is no more yield happening.
  • Add the yield after every few tries in the tight loop to be in sync with the benchmark comment.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, thanks. I updated the comment and added yield.

}

// Run the benchmarks at 2x thread/core and measure the amount of time to thrash around.
Expand Down