Skip to content

Commit 3b478a4

Browse files
committed
[libc++][atomic_ref] Enable atomic load/exchange for non-default constructible types
1 parent 8efcc01 commit 3b478a4

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

libcxx/include/__atomic/cxx_atomic_impl.h

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,11 @@ __cxx_atomic_store(_Tp* __a, typename __cxx_atomic_base_impl_traits<_Tp>::__unde
138138
template <typename _Tp>
139139
_LIBCPP_HIDE_FROM_ABI typename __cxx_atomic_base_impl_traits<_Tp>::__underlying_t
140140
__cxx_atomic_load(const _Tp* __a, memory_order __order) {
141-
typename __cxx_atomic_base_impl_traits<_Tp>::__underlying_t __ret;
142-
__atomic_load(std::addressof(__a->__a_value), std::addressof(__ret), __to_gcc_order(__order));
143-
return __ret;
141+
using _Ret = typename __cxx_atomic_base_impl_traits<_Tp>::__underlying_t;
142+
alignas(alignof(_Ret)) unsigned char __mem[sizeof(_Ret)];
143+
__atomic_load(
144+
std::addressof(__a->__a_value), std::addressof(*reinterpret_cast<_Ret*>(__mem)), __to_gcc_order(__order));
145+
return *reinterpret_cast<_Ret*>(__mem);
144146
}
145147

146148
template <typename _Tp>
@@ -152,10 +154,14 @@ _LIBCPP_HIDE_FROM_ABI void __cxx_atomic_load_inplace(
152154
template <typename _Tp>
153155
_LIBCPP_HIDE_FROM_ABI typename __cxx_atomic_base_impl_traits<_Tp>::__underlying_t __cxx_atomic_exchange(
154156
_Tp* __a, typename __cxx_atomic_base_impl_traits<_Tp>::__underlying_t __value, memory_order __order) {
155-
typename __cxx_atomic_base_impl_traits<_Tp>::__underlying_t __ret;
157+
using _Ret = typename __cxx_atomic_base_impl_traits<_Tp>::__underlying_t;
158+
alignas(alignof(_Ret)) unsigned char __mem[sizeof(_Ret)];
156159
__atomic_exchange(
157-
std::addressof(__a->__a_value), std::addressof(__value), std::addressof(__ret), __to_gcc_order(__order));
158-
return __ret;
160+
std::addressof(__a->__a_value),
161+
std::addressof(__value),
162+
std::addressof(*reinterpret_cast<_Ret*>(__mem)),
163+
__to_gcc_order(__order));
164+
return *reinterpret_cast<_Ret*>(__mem);
159165
}
160166

161167
template <typename _Tp>

0 commit comments

Comments
 (0)