diff --git a/libcxx/include/__atomic/atomic_ref.h b/libcxx/include/__atomic/atomic_ref.h index 2849b82e1a3dd70..b0180a37ab500c2 100644 --- a/libcxx/include/__atomic/atomic_ref.h +++ b/libcxx/include/__atomic/atomic_ref.h @@ -57,11 +57,6 @@ struct __get_aligner_instance { template struct __atomic_ref_base { -protected: - _Tp* __ptr_; - - _LIBCPP_HIDE_FROM_ABI __atomic_ref_base(_Tp& __obj) : __ptr_(std::addressof(__obj)) {} - private: _LIBCPP_HIDE_FROM_ABI static _Tp* __clear_padding(_Tp& __val) noexcept { _Tp* __ptr = std::addressof(__val); @@ -108,10 +103,14 @@ struct __atomic_ref_base { friend struct __atomic_waitable_traits<__atomic_ref_base<_Tp>>; + // require types that are 1, 2, 4, 8, or 16 bytes in length to be aligned to at least their size to be potentially + // used lock-free + static constexpr size_t __min_alignment = (sizeof(_Tp) & (sizeof(_Tp) - 1)) || (sizeof(_Tp) > 16) ? 0 : sizeof(_Tp); + public: using value_type = _Tp; - static constexpr size_t required_alignment = alignof(_Tp); + static constexpr size_t required_alignment = alignof(_Tp) > __min_alignment ? alignof(_Tp) : __min_alignment; // The __atomic_always_lock_free builtin takes into account the alignment of the pointer if provided, // so we create a fake pointer with a suitable alignment when querying it. Note that we are guaranteed @@ -218,6 +217,12 @@ struct __atomic_ref_base { } _LIBCPP_HIDE_FROM_ABI void notify_one() const noexcept { std::__atomic_notify_one(*this); } _LIBCPP_HIDE_FROM_ABI void notify_all() const noexcept { std::__atomic_notify_all(*this); } + +protected: + typedef _Tp _Aligned_Tp __attribute__((aligned(required_alignment))); + _Aligned_Tp* __ptr_; + + _LIBCPP_HIDE_FROM_ABI __atomic_ref_base(_Tp& __obj) : __ptr_(std::addressof(__obj)) {} }; template diff --git a/libcxx/test/std/atomics/atomics.ref/is_always_lock_free.pass.cpp b/libcxx/test/std/atomics/atomics.ref/is_always_lock_free.pass.cpp index acdbf63a24d8575..78e46c0397951af 100644 --- a/libcxx/test/std/atomics/atomics.ref/is_always_lock_free.pass.cpp +++ b/libcxx/test/std/atomics/atomics.ref/is_always_lock_free.pass.cpp @@ -54,7 +54,7 @@ void check_always_lock_free(std::atomic_ref const& a) { #define CHECK_ALWAYS_LOCK_FREE(T) \ do { \ typedef T type; \ - type obj{}; \ + alignas(std::atomic_ref::required_alignment) type obj{}; \ std::atomic_ref a(obj); \ check_always_lock_free(a); \ } while (0)