[libcxx-commits] [libcxx] [libc++][test] add benchmarks for `std::atomic::wait` (PR #70571)
Louis Dionne via libcxx-commits
libcxx-commits at lists.llvm.org
Mon Nov 27 10:28:16 PST 2023
================
@@ -0,0 +1,96 @@
+//===----------------------------------------------------------------------===//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include <atomic>
+#include <mutex>
+#include <numeric>
+#include <thread>
+
+#include "benchmark/benchmark.h"
+#include "make_test_thread.h"
+
+#include <iostream>
+
+using namespace std::chrono_literals;
+
+struct AtomicLock {
+ std::atomic<bool>& locked_;
+
+ AtomicLock(const AtomicLock&) = delete;
+ AtomicLock& operator=(const AtomicLock&) = delete;
+
+ AtomicLock(std::atomic<bool>& l) : locked_(l) { lock(); }
+ ~AtomicLock() { unlock(); }
+
+ void lock() {
+ while (true) {
+ locked_.wait(true, std::memory_order_relaxed);
+ bool expected = false;
+ if (locked_.compare_exchange_weak(expected, true, std::memory_order_acquire, std::memory_order_relaxed))
+ break;
+ }
+ }
+
+ void unlock() {
+ locked_.store(false, std::memory_order_release);
+ locked_.notify_all();
+ }
+};
+
+using LockState = std::atomic<bool>;
+using Lock = AtomicLock;
+
+// using LockState = std::mutex;
----------------
ldionne wrote:
I would suggest refactoring this benchmark so that we run for both types of locks all the time, since the purpose of this benchmark is to measure the difference between both.
If this makes it impossible to play nice with `compare.py`, don't do this.
https://github.com/llvm/llvm-project/pull/70571
More information about the libcxx-commits
mailing list