避免wait_if的counter因为ABA问题导致计数错误

This commit is contained in:
mutouyun 2021-09-17 22:25:53 +08:00
parent 91385d727a
commit 843770442c

View File

@ -33,9 +33,10 @@ struct waiter_helper {
auto & counter = ctrl.counter(); auto & counter = ctrl.counter();
counter.waiting_.fetch_add(1, std::memory_order_release); counter.waiting_.fetch_add(1, std::memory_order_release);
flags.is_waiting_.store(true, std::memory_order_relaxed); flags.is_waiting_.store(true, std::memory_order_relaxed);
auto finally = ipc::guard([&counter, &flags] { auto finally = ipc::guard([&ctrl, &counter, &flags] {
for (auto curr_wait = counter.waiting_.load(std::memory_order_acquire); curr_wait > 0;) { ctrl.get_lock(); // barrier
if (counter.waiting_.compare_exchange_weak(curr_wait, curr_wait - 1, std::memory_order_release)) { for (auto curr_wait = counter.waiting_.load(std::memory_order_relaxed); curr_wait > 0;) {
if (counter.waiting_.compare_exchange_weak(curr_wait, curr_wait - 1, std::memory_order_acq_rel)) {
break; break;
} }
} }