[libcxx-commits] [libcxx] [libc++] Implement C++20 atomic_ref (PR #76647)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Mon Feb 5 11:30:46 PST 2024


================
@@ -0,0 +1,47 @@
+//===----------------------------------------------------------------------===//
+//
+// 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: c++03, c++11, c++14, c++17
+
+// <atomic>
+
+// static constexpr bool is_always_lock_free;
+// bool is_lock_free() const noexcept;
+
+#include <atomic>
+#include <cassert>
+
+#include "test_macros.h"
+
+template <typename T>
+void checkAlwaysLockFree(std::atomic_ref<T> a) {
+  if (std::atomic_ref<T>::is_always_lock_free) {
+    assert(a.is_lock_free());
+  }
+  ASSERT_NOEXCEPT(a.is_lock_free());
+}
+
+void test() {
+  int i = 0;
+  checkAlwaysLockFree(std::atomic_ref<int>(i));
+
+  float f = 0.f;
+  checkAlwaysLockFree(std::atomic_ref<float>(f));
+
+  int* p = &i;
+  checkAlwaysLockFree(std::atomic_ref<int*>(p));
+
+  struct X {
----------------
ldionne wrote:

Maybe also check with a user-defined struct of a certain size?

And maybe a few types with funky alignment requirements?

You can find some inspiration in `libcxx/test/std/atomics/atomics.lockfree/isalwayslockfree.pass.cpp` -- you may not have to go all the way like that file does, but we can increase this coverage a bit pretty easily.

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


More information about the libcxx-commits mailing list