[libcxx-commits] [libcxx] [libc++][In progress] Floating Point Atomic (PR #67799)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Fri Oct 13 11:06:37 PDT 2023


================
@@ -0,0 +1,224 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+// UNSUPPORTED: no-threads
+// UNSUPPORTED: c++03, c++11, c++14, c++17
+
+// bool compare_exchange_strong(T& expected, T desired,
+//                            memory_order success, memory_order failure) volatile noexcept;
+// bool compare_exchange_strong(T& expected, T desired,
+//                            memory_order success, memory_order failure) noexcept;
+// bool compare_exchange_strong(T& expected, T desired,
+//                            memory_order order = memory_order::seq_cst) volatile noexcept;
+// bool compare_exchange_strong(T& expected, T desired,
+//                            memory_order order = memory_order::seq_cst) noexcept;
+
+#include <atomic>
+#include <cassert>
+#include <concepts>
+
+#include "test_helper.h"
+#include "test_macros.h"
+
+template <class T, class... Args>
+concept HasVolatileCompareExchangeStrong =
+    requires(volatile std::atomic<T> a, T t, Args... args) { a.compare_exchange_strong(t, t, args...); };
+
+template <class T, template <class> class MaybeVolatile, class... Args>
+concept HasNoexceptCompareExchangeStrong = requires(MaybeVolatile<std::atomic<T>> a, T t, Args... args) {
+  { a.compare_exchange_strong(t, t, args...) } noexcept;
+};
+
+template <class T, template <class> class MaybeVolatile = std::type_identity_t, class... MemoryOrder>
+void testBasic(MemoryOrder... memory_order) {
+  static_assert(HasVolatileCompareExchangeStrong<T, MemoryOrder...> == std::atomic<T>::is_always_lock_free);
+  static_assert(HasNoexceptCompareExchangeStrong<T, MaybeVolatile, MemoryOrder...>);
+
+  // compare pass
+  {
+    MaybeVolatile<std::atomic<T>> a(T(1.2));
+    T expected(1.2);
+    const T desired(2.3);
+    std::same_as<bool> decltype(auto) r = a.compare_exchange_strong(expected, desired, memory_order...);
+
+    assert(r);
+    assert(a.load() == desired);
+    assert(expected == T(1.2));
+  }
+
+  // compare fail
+  {
+    MaybeVolatile<std::atomic<T>> a(T(1.2));
+    T expected(1.5);
+    const T desired(2.3);
+    std::same_as<bool> decltype(auto) r = a.compare_exchange_strong(expected, desired, memory_order...);
+
+    assert(!r);
+    assert(a.load() == T(1.2));
+    assert(expected == T(1.2));
+  }
+}
+
+template <class T, template <class> class MaybeVolatile = std::type_identity_t>
+void testImpl() {
----------------
ldionne wrote:

Extreme nitpick, but we usually use snake_case for functions!

https://github.com/llvm/llvm-project/pull/67799


More information about the libcxx-commits mailing list