[libcxx-commits] [libcxx] Fix bug in atomic_ref's calculation of lock_free-ness. (PR #93427)

via libcxx-commits libcxx-commits at lists.llvm.org
Tue Jun 25 07:03:48 PDT 2024


================
@@ -42,6 +42,19 @@ _LIBCPP_BEGIN_NAMESPACE_STD
 
 #if _LIBCPP_STD_VER >= 20
 
+// These types are required to make __atomic_is_always_lock_free work across GCC and Clang.
+// GCC won't allow the reinterpret_cast<_Tp*>(-required_alignment) trick initially used,
+// so we need to actually fake up an instance of a type with the correct alignment.
+template <size_t _Alignment>
+struct __alignment_checker_type {
+  alignas(_Alignment) char __data;
+};
+
+template <size_t _Alignment>
+struct __get_aligner_instance {
+  static constexpr __alignment_checker_type<_Alignment> __instance{};
----------------
EricWF wrote:

Yeah. It's so the alignment of the object is represented in the type.

I.e. `alignof(deltype(__instance)) == alignof(__instance)`

It took me a few passes to find a formulation that all compiler accepted.

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


More information about the libcxx-commits mailing list