change cmp_xchg to work better
authorJacob Lifshay <programmerjake@gmail.com>
Fri, 22 Jul 2022 09:00:10 +0000 (02:00 -0700)
committerJacob Lifshay <programmerjake@gmail.com>
Fri, 22 Jul 2022 09:00:10 +0000 (02:00 -0700)
src/common/c11_atomics/c11_atomics_benchmarks.cpp

index 13dc5af6aa3c1b2d645148dd21f5a4ac7fff5667..01f831c94812b1455a2eb9ed8b752c2172e87e94 100644 (file)
@@ -65,7 +65,7 @@ static void push_atomic_bench(std::vector<Benchmark> &benches, Config config,
             index &= index_mask;
             std::atomic<T> *atomic = &(*buf)[index];
             input ^= static_cast<T>(iteration);
-            return fn(input, atomic);
+            return fn(input, atomic, iteration ^ thread_num);
         },
         T{}, name_parts...);
 }
@@ -76,37 +76,37 @@ static void rmw_benchmarks(std::vector<Benchmark> &benches, Config config,
 {
     push_atomic_bench(
         benches, config, buf,
-        [](T input, std::atomic<T> *atomic) {
+        [](T input, std::atomic<T> *atomic, std::uint64_t) {
             return std::atomic_exchange_explicit(atomic, input, order);
         },
         "atomic_exchange_", int_type_name<T>, "_", memory_order_name<order>);
     push_atomic_bench(
         benches, config, buf,
-        [](T input, std::atomic<T> *atomic) {
+        [](T input, std::atomic<T> *atomic, std::uint64_t) {
             return std::atomic_fetch_add_explicit(atomic, input, order);
         },
         "atomic_fetch_add_", int_type_name<T>, "_", memory_order_name<order>);
     push_atomic_bench(
         benches, config, buf,
-        [](T input, std::atomic<T> *atomic) {
+        [](T input, std::atomic<T> *atomic, std::uint64_t) {
             return std::atomic_fetch_sub_explicit(atomic, input, order);
         },
         "atomic_fetch_sub_", int_type_name<T>, "_", memory_order_name<order>);
     push_atomic_bench(
         benches, config, buf,
-        [](T input, std::atomic<T> *atomic) {
+        [](T input, std::atomic<T> *atomic, std::uint64_t) {
             return std::atomic_fetch_and_explicit(atomic, input, order);
         },
         "atomic_fetch_and_", int_type_name<T>, "_", memory_order_name<order>);
     push_atomic_bench(
         benches, config, buf,
-        [](T input, std::atomic<T> *atomic) {
+        [](T input, std::atomic<T> *atomic, std::uint64_t) {
             return std::atomic_fetch_or_explicit(atomic, input, order);
         },
         "atomic_fetch_or_", int_type_name<T>, "_", memory_order_name<order>);
     push_atomic_bench(
         benches, config, buf,
-        [](T input, std::atomic<T> *atomic) {
+        [](T input, std::atomic<T> *atomic, std::uint64_t) {
             return std::atomic_fetch_xor_explicit(atomic, input, order);
         },
         "atomic_fetch_xor_", int_type_name<T>, "_", memory_order_name<order>);
@@ -118,7 +118,7 @@ static void load_benchmarks(std::vector<Benchmark> &benches, Config config,
 {
     push_atomic_bench(
         benches, config, buf,
-        [](T, std::atomic<T> *atomic) {
+        [](T, std::atomic<T> *atomic, std::uint64_t) {
             return std::atomic_load_explicit(atomic, order);
         },
         "atomic_load_", int_type_name<T>, "_", memory_order_name<order>);
@@ -130,7 +130,7 @@ static void store_benchmarks(std::vector<Benchmark> &benches, Config config,
 {
     push_atomic_bench(
         benches, config, buf,
-        [](T input, std::atomic<T> *atomic) {
+        [](T input, std::atomic<T> *atomic, std::uint64_t) {
             return std::atomic_store_explicit(atomic, input, order);
         },
         "atomic_store_", int_type_name<T>, "_", memory_order_name<order>);
@@ -142,8 +142,8 @@ static void cmp_xchg_benchmarks(std::vector<Benchmark> &benches, Config config,
 {
     push_atomic_bench(
         benches, config, buf,
-        [](T input, std::atomic<T> *atomic) {
-            T expected = input >> 1;
+        [](T input, std::atomic<T> *atomic, std::uint64_t state) {
+            T expected = state;
             bool succeeded = std::atomic_compare_exchange_weak_explicit(
                 atomic, &expected, input, succ, fail);
             return std::pair(expected, succeeded);
@@ -152,8 +152,8 @@ static void cmp_xchg_benchmarks(std::vector<Benchmark> &benches, Config config,
         memory_order_name<succ>, "_", memory_order_name<fail>);
     push_atomic_bench(
         benches, config, buf,
-        [](T input, std::atomic<T> *atomic) {
-            T expected = input >> 1;
+        [](T input, std::atomic<T> *atomic, std::uint64_t state) {
+            T expected = state;
             bool succeeded = std::atomic_compare_exchange_strong_explicit(
                 atomic, &expected, input, succ, fail);
             return std::pair(expected, succeeded);
@@ -220,4 +220,4 @@ std::vector<Benchmark> c11_atomics_benchmarks(Config config)
     benchmarks<std::int32_t>(benches, config);
     benchmarks<std::int64_t>(benches, config);
     return benches;
-}
\ No newline at end of file
+}