From e22300ab3db213a01da0f57128e50e4519cd45f3 Mon Sep 17 00:00:00 2001 From: esigo Date: Thu, 25 Nov 2021 19:05:13 +0100 Subject: [PATCH 1/2] fix lock --- api/test/common/spinlock_benchmark.cc | 24 ++++++------------------ 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/api/test/common/spinlock_benchmark.cc b/api/test/common/spinlock_benchmark.cc index 95c3f7c7af..2b4e2d52de 100644 --- a/api/test/common/spinlock_benchmark.cc +++ b/api/test/common/spinlock_benchmark.cc @@ -103,29 +103,17 @@ static void BM_ProcYieldSpinLockThrashing(benchmark::State &s) // SpinLock thrashing with thread::yield() after N spins. static void BM_ThreadYieldSpinLockThrashing(benchmark::State &s) { - std::atomic mutex(false); - SpinThrash>( + std::atomic_flag mutex = ATOMIC_FLAG_INIT; + SpinThrash( s, mutex, - [](std::atomic &l) { - if (!l.exchange(true, std::memory_order_acq_rel)) + [](std::atomic_flag &l) { + 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) - { - std::this_thread::yield(); - } + ; } std::this_thread::yield(); }, - [](std::atomic &l) { l.store(false, std::memory_order_release); }); + [](std::atomic_flag &l) { l.clear(std::memory_order_release); }); } // Run the benchmarks at 2x thread/core and measure the amount of time to thrash around. From 1b8b635fd3fb980423a14a66036a82ce55a7a27c Mon Sep 17 00:00:00 2001 From: Oblivion Date: Wed, 1 Dec 2021 20:04:14 +0000 Subject: [PATCH 2/2] add yield --- api/test/common/spinlock_benchmark.cc | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/api/test/common/spinlock_benchmark.cc b/api/test/common/spinlock_benchmark.cc index 2b4e2d52de..28692bee56 100644 --- a/api/test/common/spinlock_benchmark.cc +++ b/api/test/common/spinlock_benchmark.cc @@ -100,16 +100,21 @@ 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_flag mutex = ATOMIC_FLAG_INIT; SpinThrash( s, mutex, [](std::atomic_flag &l) { + uint32_t try_count = 0; while (l.test_and_set(std::memory_order_acq_rel)) { - ; + ++try_count; + if (try_count % 32) + { + std::this_thread::yield(); + } } std::this_thread::yield(); },