Path: blob/main/contrib/llvm-project/libcxx/include/__atomic/atomic_sync.h
35262 views
//===----------------------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef _LIBCPP___ATOMIC_ATOMIC_SYNC_H9#define _LIBCPP___ATOMIC_ATOMIC_SYNC_H1011#include <__atomic/contention_t.h>12#include <__atomic/cxx_atomic_impl.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 <__thread/support.h>20#include <__type_traits/conjunction.h>21#include <__type_traits/decay.h>22#include <__type_traits/invoke.h>23#include <__type_traits/void_t.h>24#include <__utility/declval.h>25#include <cstring>2627#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)28# pragma GCC system_header29#endif3031_LIBCPP_BEGIN_NAMESPACE_STD3233// The customisation points to enable the following functions:34// - __atomic_wait35// - __atomic_wait_unless36// - __atomic_notify_one37// - __atomic_notify_all38// Note that std::atomic<T>::wait was back-ported to C++0339// The below implementations look ugly to support C++0340template <class _Tp, class = void>41struct __atomic_waitable_traits {42template <class _AtomicWaitable>43static void __atomic_load(_AtomicWaitable&&, memory_order) = delete;4445template <class _AtomicWaitable>46static void __atomic_contention_address(_AtomicWaitable&&) = delete;47};4849template <class _Tp, class = void>50struct __atomic_waitable : false_type {};5152template <class _Tp>53struct __atomic_waitable< _Tp,54__void_t<decltype(__atomic_waitable_traits<__decay_t<_Tp> >::__atomic_load(55std::declval<const _Tp&>(), std::declval<memory_order>())),56decltype(__atomic_waitable_traits<__decay_t<_Tp> >::__atomic_contention_address(57std::declval<const _Tp&>()))> > : true_type {};5859template <class _AtomicWaitable, class _Poll>60struct __atomic_wait_poll_impl {61const _AtomicWaitable& __a_;62_Poll __poll_;63memory_order __order_;6465_LIBCPP_HIDE_FROM_ABI bool operator()() const {66auto __current_val = __atomic_waitable_traits<__decay_t<_AtomicWaitable> >::__atomic_load(__a_, __order_);67return __poll_(__current_val);68}69};7071#ifndef _LIBCPP_HAS_NO_THREADS7273_LIBCPP_AVAILABILITY_SYNC _LIBCPP_EXPORTED_FROM_ABI void __cxx_atomic_notify_one(void const volatile*) _NOEXCEPT;74_LIBCPP_AVAILABILITY_SYNC _LIBCPP_EXPORTED_FROM_ABI void __cxx_atomic_notify_all(void const volatile*) _NOEXCEPT;75_LIBCPP_AVAILABILITY_SYNC _LIBCPP_EXPORTED_FROM_ABI __cxx_contention_t76__libcpp_atomic_monitor(void const volatile*) _NOEXCEPT;77_LIBCPP_AVAILABILITY_SYNC _LIBCPP_EXPORTED_FROM_ABI void78__libcpp_atomic_wait(void const volatile*, __cxx_contention_t) _NOEXCEPT;7980_LIBCPP_AVAILABILITY_SYNC _LIBCPP_EXPORTED_FROM_ABI void81__cxx_atomic_notify_one(__cxx_atomic_contention_t const volatile*) _NOEXCEPT;82_LIBCPP_AVAILABILITY_SYNC _LIBCPP_EXPORTED_FROM_ABI void83__cxx_atomic_notify_all(__cxx_atomic_contention_t const volatile*) _NOEXCEPT;84_LIBCPP_AVAILABILITY_SYNC _LIBCPP_EXPORTED_FROM_ABI __cxx_contention_t85__libcpp_atomic_monitor(__cxx_atomic_contention_t const volatile*) _NOEXCEPT;86_LIBCPP_AVAILABILITY_SYNC _LIBCPP_EXPORTED_FROM_ABI void87__libcpp_atomic_wait(__cxx_atomic_contention_t const volatile*, __cxx_contention_t) _NOEXCEPT;8889template <class _AtomicWaitable, class _Poll>90struct __atomic_wait_backoff_impl {91const _AtomicWaitable& __a_;92_Poll __poll_;93memory_order __order_;9495using __waitable_traits = __atomic_waitable_traits<__decay_t<_AtomicWaitable> >;9697_LIBCPP_AVAILABILITY_SYNC98_LIBCPP_HIDE_FROM_ABI bool99__update_monitor_val_and_poll(__cxx_atomic_contention_t const volatile*, __cxx_contention_t& __monitor_val) const {100// In case the contention type happens to be __cxx_atomic_contention_t, i.e. __cxx_atomic_impl<int64_t>,101// the platform wait is directly monitoring the atomic value itself.102// `__poll_` takes the current value of the atomic as an in-out argument103// to potentially modify it. After it returns, `__monitor` has a value104// which can be safely waited on by `std::__libcpp_atomic_wait` without any105// ABA style issues.106__monitor_val = __waitable_traits::__atomic_load(__a_, __order_);107return __poll_(__monitor_val);108}109110_LIBCPP_AVAILABILITY_SYNC111_LIBCPP_HIDE_FROM_ABI bool112__update_monitor_val_and_poll(void const volatile* __contention_address, __cxx_contention_t& __monitor_val) const {113// In case the contention type is anything else, platform wait is monitoring a __cxx_atomic_contention_t114// from the global pool, the monitor comes from __libcpp_atomic_monitor115__monitor_val = std::__libcpp_atomic_monitor(__contention_address);116auto __current_val = __waitable_traits::__atomic_load(__a_, __order_);117return __poll_(__current_val);118}119120_LIBCPP_AVAILABILITY_SYNC121_LIBCPP_HIDE_FROM_ABI bool operator()(chrono::nanoseconds __elapsed) const {122if (__elapsed > chrono::microseconds(64)) {123auto __contention_address = __waitable_traits::__atomic_contention_address(__a_);124__cxx_contention_t __monitor_val;125if (__update_monitor_val_and_poll(__contention_address, __monitor_val))126return true;127std::__libcpp_atomic_wait(__contention_address, __monitor_val);128} else if (__elapsed > chrono::microseconds(4))129__libcpp_thread_yield();130else {131} // poll132return false;133}134};135136// The semantics of this function are similar to `atomic`'s137// `.wait(T old, std::memory_order order)`, but instead of having a hardcoded138// predicate (is the loaded value unequal to `old`?), the predicate function is139// specified as an argument. The loaded value is given as an in-out argument to140// the predicate. If the predicate function returns `true`,141// `__atomic_wait_unless` will return. If the predicate function returns142// `false`, it must set the argument to its current understanding of the atomic143// value. The predicate function must not return `false` spuriously.144template <class _AtomicWaitable, class _Poll>145_LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void146__atomic_wait_unless(const _AtomicWaitable& __a, _Poll&& __poll, memory_order __order) {147static_assert(__atomic_waitable<_AtomicWaitable>::value, "");148__atomic_wait_poll_impl<_AtomicWaitable, __decay_t<_Poll> > __poll_impl = {__a, __poll, __order};149__atomic_wait_backoff_impl<_AtomicWaitable, __decay_t<_Poll> > __backoff_fn = {__a, __poll, __order};150std::__libcpp_thread_poll_with_backoff(__poll_impl, __backoff_fn);151}152153template <class _AtomicWaitable>154_LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void __atomic_notify_one(const _AtomicWaitable& __a) {155static_assert(__atomic_waitable<_AtomicWaitable>::value, "");156std::__cxx_atomic_notify_one(__atomic_waitable_traits<__decay_t<_AtomicWaitable> >::__atomic_contention_address(__a));157}158159template <class _AtomicWaitable>160_LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void __atomic_notify_all(const _AtomicWaitable& __a) {161static_assert(__atomic_waitable<_AtomicWaitable>::value, "");162std::__cxx_atomic_notify_all(__atomic_waitable_traits<__decay_t<_AtomicWaitable> >::__atomic_contention_address(__a));163}164165#else // _LIBCPP_HAS_NO_THREADS166167template <class _AtomicWaitable, class _Poll>168_LIBCPP_HIDE_FROM_ABI void __atomic_wait_unless(const _AtomicWaitable& __a, _Poll&& __poll, memory_order __order) {169__atomic_wait_poll_impl<_AtomicWaitable, __decay_t<_Poll> > __poll_fn = {__a, __poll, __order};170std::__libcpp_thread_poll_with_backoff(__poll_fn, __spinning_backoff_policy());171}172173template <class _AtomicWaitable>174_LIBCPP_HIDE_FROM_ABI void __atomic_notify_one(const _AtomicWaitable&) {}175176template <class _AtomicWaitable>177_LIBCPP_HIDE_FROM_ABI void __atomic_notify_all(const _AtomicWaitable&) {}178179#endif // _LIBCPP_HAS_NO_THREADS180181template <typename _Tp>182_LIBCPP_HIDE_FROM_ABI bool __cxx_nonatomic_compare_equal(_Tp const& __lhs, _Tp const& __rhs) {183return std::memcmp(std::addressof(__lhs), std::addressof(__rhs), sizeof(_Tp)) == 0;184}185186template <class _Tp>187struct __atomic_compare_unequal_to {188_Tp __val_;189_LIBCPP_HIDE_FROM_ABI bool operator()(const _Tp& __arg) const {190return !std::__cxx_nonatomic_compare_equal(__arg, __val_);191}192};193194template <class _AtomicWaitable, class _Up>195_LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void196__atomic_wait(_AtomicWaitable& __a, _Up __val, memory_order __order) {197static_assert(__atomic_waitable<_AtomicWaitable>::value, "");198__atomic_compare_unequal_to<_Up> __nonatomic_equal = {__val};199std::__atomic_wait_unless(__a, __nonatomic_equal, __order);200}201202_LIBCPP_END_NAMESPACE_STD203204#endif // _LIBCPP___ATOMIC_ATOMIC_SYNC_H205206207