187 lines · c
1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef _LIBCPP___ATOMIC_ATOMIC_SYNC_H10#define _LIBCPP___ATOMIC_ATOMIC_SYNC_H11 12#include <__atomic/contention_t.h>13#include <__atomic/memory_order.h>14#include <__atomic/to_gcc_order.h>15#include <__chrono/duration.h>16#include <__config>17#include <__memory/addressof.h>18#include <__thread/poll_with_backoff.h>19#include <__type_traits/conjunction.h>20#include <__type_traits/decay.h>21#include <__type_traits/invoke.h>22#include <__type_traits/void_t.h>23#include <__utility/declval.h>24#include <cstring>25 26#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)27# pragma GCC system_header28#endif29 30_LIBCPP_BEGIN_NAMESPACE_STD31 32// The customisation points to enable the following functions:33// - __atomic_wait34// - __atomic_wait_unless35// - __atomic_notify_one36// - __atomic_notify_all37// Note that std::atomic<T>::wait was back-ported to C++0338// The below implementations look ugly to support C++0339template <class _Tp, class = void>40struct __atomic_waitable_traits {41 template <class _AtomicWaitable>42 static void __atomic_load(_AtomicWaitable&&, memory_order) = delete;43 44 template <class _AtomicWaitable>45 static void __atomic_contention_address(_AtomicWaitable&&) = delete;46};47 48template <class _Tp, class = void>49struct __atomic_waitable : false_type {};50 51template <class _Tp>52struct __atomic_waitable< _Tp,53 __void_t<decltype(__atomic_waitable_traits<__decay_t<_Tp> >::__atomic_load(54 std::declval<const _Tp&>(), std::declval<memory_order>())),55 decltype(__atomic_waitable_traits<__decay_t<_Tp> >::__atomic_contention_address(56 std::declval<const _Tp&>()))> > : true_type {};57 58#if _LIBCPP_STD_VER >= 2059# if _LIBCPP_HAS_THREADS60 61_LIBCPP_EXPORTED_FROM_ABI void __cxx_atomic_notify_one(void const volatile*) _NOEXCEPT;62_LIBCPP_EXPORTED_FROM_ABI void __cxx_atomic_notify_all(void const volatile*) _NOEXCEPT;63_LIBCPP_EXPORTED_FROM_ABI __cxx_contention_t __libcpp_atomic_monitor(void const volatile*) _NOEXCEPT;64_LIBCPP_EXPORTED_FROM_ABI void __libcpp_atomic_wait(void const volatile*, __cxx_contention_t) _NOEXCEPT;65 66_LIBCPP_EXPORTED_FROM_ABI void __cxx_atomic_notify_one(__cxx_atomic_contention_t const volatile*) _NOEXCEPT;67_LIBCPP_EXPORTED_FROM_ABI void __cxx_atomic_notify_all(__cxx_atomic_contention_t const volatile*) _NOEXCEPT;68_LIBCPP_EXPORTED_FROM_ABI __cxx_contention_t69__libcpp_atomic_monitor(__cxx_atomic_contention_t const volatile*) _NOEXCEPT;70_LIBCPP_EXPORTED_FROM_ABI void71__libcpp_atomic_wait(__cxx_atomic_contention_t const volatile*, __cxx_contention_t) _NOEXCEPT;72 73template <class _AtomicWaitable, class _Poll>74struct __atomic_wait_backoff_impl {75 const _AtomicWaitable& __a_;76 _Poll __poll_;77 memory_order __order_;78 79 using __waitable_traits _LIBCPP_NODEBUG = __atomic_waitable_traits<__decay_t<_AtomicWaitable> >;80 81 _LIBCPP_HIDE_FROM_ABI bool82 __update_monitor_val_and_poll(__cxx_atomic_contention_t const volatile*, __cxx_contention_t& __monitor_val) const {83 // In case the contention type happens to be __cxx_atomic_contention_t, i.e. __cxx_atomic_impl<int64_t>,84 // the platform wait is directly monitoring the atomic value itself.85 // `__poll_` takes the current value of the atomic as an in-out argument86 // to potentially modify it. After it returns, `__monitor` has a value87 // which can be safely waited on by `std::__libcpp_atomic_wait` without any88 // ABA style issues.89 __monitor_val = __waitable_traits::__atomic_load(__a_, __order_);90 return __poll_(__monitor_val);91 }92 93 _LIBCPP_HIDE_FROM_ABI bool94 __update_monitor_val_and_poll(void const volatile* __contention_address, __cxx_contention_t& __monitor_val) const {95 // In case the contention type is anything else, platform wait is monitoring a __cxx_atomic_contention_t96 // from the global pool, the monitor comes from __libcpp_atomic_monitor97 __monitor_val = std::__libcpp_atomic_monitor(__contention_address);98 auto __current_val = __waitable_traits::__atomic_load(__a_, __order_);99 return __poll_(__current_val);100 }101 102 _LIBCPP_HIDE_FROM_ABI bool operator()(chrono::nanoseconds __elapsed) const {103 if (__elapsed > chrono::microseconds(4)) {104 auto __contention_address = __waitable_traits::__atomic_contention_address(__a_);105 __cxx_contention_t __monitor_val;106 if (__update_monitor_val_and_poll(__contention_address, __monitor_val))107 return true;108 std::__libcpp_atomic_wait(__contention_address, __monitor_val);109 } else {110 } // poll111 return false;112 }113};114 115// The semantics of this function are similar to `atomic`'s116// `.wait(T old, std::memory_order order)`, but instead of having a hardcoded117// predicate (is the loaded value unequal to `old`?), the predicate function is118// specified as an argument. The loaded value is given as an in-out argument to119// the predicate. If the predicate function returns `true`,120// `__atomic_wait_unless` will return. If the predicate function returns121// `false`, it must set the argument to its current understanding of the atomic122// value. The predicate function must not return `false` spuriously.123template <class _AtomicWaitable, class _Poll>124_LIBCPP_HIDE_FROM_ABI void __atomic_wait_unless(const _AtomicWaitable& __a, memory_order __order, _Poll&& __poll) {125 static_assert(__atomic_waitable<_AtomicWaitable>::value, "");126 __atomic_wait_backoff_impl<_AtomicWaitable, __decay_t<_Poll> > __backoff_fn = {__a, __poll, __order};127 std::__libcpp_thread_poll_with_backoff(128 /* poll */129 [&]() {130 auto __current_val = __atomic_waitable_traits<__decay_t<_AtomicWaitable> >::__atomic_load(__a, __order);131 return __poll(__current_val);132 },133 /* backoff */ __backoff_fn);134}135 136template <class _AtomicWaitable>137_LIBCPP_HIDE_FROM_ABI void __atomic_notify_one(const _AtomicWaitable& __a) {138 static_assert(__atomic_waitable<_AtomicWaitable>::value, "");139 std::__cxx_atomic_notify_one(__atomic_waitable_traits<__decay_t<_AtomicWaitable> >::__atomic_contention_address(__a));140}141 142template <class _AtomicWaitable>143_LIBCPP_HIDE_FROM_ABI void __atomic_notify_all(const _AtomicWaitable& __a) {144 static_assert(__atomic_waitable<_AtomicWaitable>::value, "");145 std::__cxx_atomic_notify_all(__atomic_waitable_traits<__decay_t<_AtomicWaitable> >::__atomic_contention_address(__a));146}147 148# else // _LIBCPP_HAS_THREADS149 150template <class _AtomicWaitable, class _Poll>151_LIBCPP_HIDE_FROM_ABI void __atomic_wait_unless(const _AtomicWaitable& __a, memory_order __order, _Poll&& __poll) {152 std::__libcpp_thread_poll_with_backoff(153 /* poll */154 [&]() {155 auto __current_val = __atomic_waitable_traits<__decay_t<_AtomicWaitable> >::__atomic_load(__a, __order);156 return __poll(__current_val);157 },158 /* backoff */ __spinning_backoff_policy());159}160 161template <class _AtomicWaitable>162_LIBCPP_HIDE_FROM_ABI void __atomic_notify_one(const _AtomicWaitable&) {}163 164template <class _AtomicWaitable>165_LIBCPP_HIDE_FROM_ABI void __atomic_notify_all(const _AtomicWaitable&) {}166 167# endif // _LIBCPP_HAS_THREADS168 169template <typename _Tp>170_LIBCPP_HIDE_FROM_ABI bool __cxx_nonatomic_compare_equal(_Tp const& __lhs, _Tp const& __rhs) {171 return std::memcmp(std::addressof(__lhs), std::addressof(__rhs), sizeof(_Tp)) == 0;172}173 174template <class _AtomicWaitable, class _Tp>175_LIBCPP_HIDE_FROM_ABI void __atomic_wait(_AtomicWaitable& __a, _Tp __val, memory_order __order) {176 static_assert(__atomic_waitable<_AtomicWaitable>::value, "");177 std::__atomic_wait_unless(__a, __order, [&](_Tp const& __current) {178 return !std::__cxx_nonatomic_compare_equal(__current, __val);179 });180}181 182#endif // C++20183 184_LIBCPP_END_NAMESPACE_STD185 186#endif // _LIBCPP___ATOMIC_ATOMIC_SYNC_H187