Path: blob/main/contrib/llvm-project/libcxx/src/atomic.cpp
35154 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#include <__thread/timed_backoff_policy.h>9#include <atomic>10#include <climits>11#include <functional>12#include <thread>1314#include "include/apple_availability.h"1516#ifdef __linux__1718# include <linux/futex.h>19# include <sys/syscall.h>20# include <unistd.h>2122// libc++ uses SYS_futex as a universal syscall name. However, on 32 bit architectures23// with a 64 bit time_t, we need to specify SYS_futex_time64.24# if !defined(SYS_futex) && defined(SYS_futex_time64)25# define SYS_futex SYS_futex_time6426# endif27# define _LIBCPP_FUTEX(...) syscall(SYS_futex, __VA_ARGS__)2829#elif defined(__FreeBSD__)3031# include <sys/types.h>32# include <sys/umtx.h>3334# define _LIBCPP_FUTEX(...) syscall(SYS_futex, __VA_ARGS__)3536#elif defined(__OpenBSD__)3738# include <sys/futex.h>3940// OpenBSD has no indirect syscalls41# define _LIBCPP_FUTEX(...) futex(__VA_ARGS__)4243#else // <- Add other operating systems here4445// Baseline needs no new headers4647# define _LIBCPP_FUTEX(...) syscall(SYS_futex, __VA_ARGS__)4849#endif5051_LIBCPP_BEGIN_NAMESPACE_STD5253#ifdef __linux__5455static void56__libcpp_platform_wait_on_address(__cxx_atomic_contention_t const volatile* __ptr, __cxx_contention_t __val) {57static constexpr timespec __timeout = {2, 0};58_LIBCPP_FUTEX(__ptr, FUTEX_WAIT_PRIVATE, __val, &__timeout, 0, 0);59}6061static void __libcpp_platform_wake_by_address(__cxx_atomic_contention_t const volatile* __ptr, bool __notify_one) {62_LIBCPP_FUTEX(__ptr, FUTEX_WAKE_PRIVATE, __notify_one ? 1 : INT_MAX, 0, 0, 0);63}6465#elif defined(__APPLE__) && defined(_LIBCPP_USE_ULOCK)6667extern "C" int __ulock_wait(68uint32_t operation, void* addr, uint64_t value, uint32_t timeout); /* timeout is specified in microseconds */69extern "C" int __ulock_wake(uint32_t operation, void* addr, uint64_t wake_value);7071// https://github.com/apple/darwin-xnu/blob/2ff845c2e033bd0ff64b5b6aa6063a1f8f65aa32/bsd/sys/ulock.h#L8272# define UL_COMPARE_AND_WAIT64 573# define ULF_WAKE_ALL 0x000001007475static void76__libcpp_platform_wait_on_address(__cxx_atomic_contention_t const volatile* __ptr, __cxx_contention_t __val) {77static_assert(sizeof(__cxx_atomic_contention_t) == 8, "Waiting on 8 bytes value");78__ulock_wait(UL_COMPARE_AND_WAIT64, const_cast<__cxx_atomic_contention_t*>(__ptr), __val, 0);79}8081static void __libcpp_platform_wake_by_address(__cxx_atomic_contention_t const volatile* __ptr, bool __notify_one) {82static_assert(sizeof(__cxx_atomic_contention_t) == 8, "Waking up on 8 bytes value");83__ulock_wake(84UL_COMPARE_AND_WAIT64 | (__notify_one ? 0 : ULF_WAKE_ALL), const_cast<__cxx_atomic_contention_t*>(__ptr), 0);85}8687#elif defined(__FreeBSD__) && __SIZEOF_LONG__ == 888/*89* Since __cxx_contention_t is int64_t even on 32bit FreeBSD90* platforms, we have to use umtx ops that work on the long type, and91* limit its use to architectures where long and int64_t are synonyms.92*/9394static void95__libcpp_platform_wait_on_address(__cxx_atomic_contention_t const volatile* __ptr, __cxx_contention_t __val) {96_umtx_op(const_cast<__cxx_atomic_contention_t*>(__ptr), UMTX_OP_WAIT, __val, NULL, NULL);97}9899static void __libcpp_platform_wake_by_address(__cxx_atomic_contention_t const volatile* __ptr, bool __notify_one) {100_umtx_op(const_cast<__cxx_atomic_contention_t*>(__ptr), UMTX_OP_WAKE, __notify_one ? 1 : INT_MAX, NULL, NULL);101}102103#else // <- Add other operating systems here104105// Baseline is just a timed backoff106107static void108__libcpp_platform_wait_on_address(__cxx_atomic_contention_t const volatile* __ptr, __cxx_contention_t __val) {109__libcpp_thread_poll_with_backoff(110[=]() -> bool { return !__cxx_nonatomic_compare_equal(__cxx_atomic_load(__ptr, memory_order_relaxed), __val); },111__libcpp_timed_backoff_policy());112}113114static void __libcpp_platform_wake_by_address(__cxx_atomic_contention_t const volatile*, bool) {}115116#endif // __linux__117118static constexpr size_t __libcpp_contention_table_size = (1 << 8); /* < there's no magic in this number */119120struct alignas(64) /* aim to avoid false sharing */ __libcpp_contention_table_entry {121__cxx_atomic_contention_t __contention_state;122__cxx_atomic_contention_t __platform_state;123inline constexpr __libcpp_contention_table_entry() : __contention_state(0), __platform_state(0) {}124};125126static __libcpp_contention_table_entry __libcpp_contention_table[__libcpp_contention_table_size];127128static hash<void const volatile*> __libcpp_contention_hasher;129130static __libcpp_contention_table_entry* __libcpp_contention_state(void const volatile* p) {131return &__libcpp_contention_table[__libcpp_contention_hasher(p) & (__libcpp_contention_table_size - 1)];132}133134/* Given an atomic to track contention and an atomic to actually wait on, which may be135the same atomic, we try to detect contention to avoid spuriously calling the platform. */136137static void __libcpp_contention_notify(__cxx_atomic_contention_t volatile* __contention_state,138__cxx_atomic_contention_t const volatile* __platform_state,139bool __notify_one) {140if (0 != __cxx_atomic_load(__contention_state, memory_order_seq_cst))141// We only call 'wake' if we consumed a contention bit here.142__libcpp_platform_wake_by_address(__platform_state, __notify_one);143}144static __cxx_contention_t145__libcpp_contention_monitor_for_wait(__cxx_atomic_contention_t volatile* /*__contention_state*/,146__cxx_atomic_contention_t const volatile* __platform_state) {147// We will monitor this value.148return __cxx_atomic_load(__platform_state, memory_order_acquire);149}150static void __libcpp_contention_wait(__cxx_atomic_contention_t volatile* __contention_state,151__cxx_atomic_contention_t const volatile* __platform_state,152__cxx_contention_t __old_value) {153__cxx_atomic_fetch_add(__contention_state, __cxx_contention_t(1), memory_order_seq_cst);154// We sleep as long as the monitored value hasn't changed.155__libcpp_platform_wait_on_address(__platform_state, __old_value);156__cxx_atomic_fetch_sub(__contention_state, __cxx_contention_t(1), memory_order_release);157}158159/* When the incoming atomic is the wrong size for the platform wait size, need to160launder the value sequence through an atomic from our table. */161162static void __libcpp_atomic_notify(void const volatile* __location) {163auto const __entry = __libcpp_contention_state(__location);164// The value sequence laundering happens on the next line below.165__cxx_atomic_fetch_add(&__entry->__platform_state, __cxx_contention_t(1), memory_order_release);166__libcpp_contention_notify(167&__entry->__contention_state,168&__entry->__platform_state,169false /* when laundering, we can't handle notify_one */);170}171_LIBCPP_EXPORTED_FROM_ABI void __cxx_atomic_notify_one(void const volatile* __location) noexcept {172__libcpp_atomic_notify(__location);173}174_LIBCPP_EXPORTED_FROM_ABI void __cxx_atomic_notify_all(void const volatile* __location) noexcept {175__libcpp_atomic_notify(__location);176}177_LIBCPP_EXPORTED_FROM_ABI __cxx_contention_t __libcpp_atomic_monitor(void const volatile* __location) noexcept {178auto const __entry = __libcpp_contention_state(__location);179return __libcpp_contention_monitor_for_wait(&__entry->__contention_state, &__entry->__platform_state);180}181_LIBCPP_EXPORTED_FROM_ABI void182__libcpp_atomic_wait(void const volatile* __location, __cxx_contention_t __old_value) noexcept {183auto const __entry = __libcpp_contention_state(__location);184__libcpp_contention_wait(&__entry->__contention_state, &__entry->__platform_state, __old_value);185}186187/* When the incoming atomic happens to be the platform wait size, we still need to use the188table for the contention detection, but we can use the atomic directly for the wait. */189190_LIBCPP_EXPORTED_FROM_ABI void __cxx_atomic_notify_one(__cxx_atomic_contention_t const volatile* __location) noexcept {191__libcpp_contention_notify(&__libcpp_contention_state(__location)->__contention_state, __location, true);192}193_LIBCPP_EXPORTED_FROM_ABI void __cxx_atomic_notify_all(__cxx_atomic_contention_t const volatile* __location) noexcept {194__libcpp_contention_notify(&__libcpp_contention_state(__location)->__contention_state, __location, false);195}196// This function is never used, but still exported for ABI compatibility.197_LIBCPP_EXPORTED_FROM_ABI __cxx_contention_t198__libcpp_atomic_monitor(__cxx_atomic_contention_t const volatile* __location) noexcept {199return __libcpp_contention_monitor_for_wait(&__libcpp_contention_state(__location)->__contention_state, __location);200}201_LIBCPP_EXPORTED_FROM_ABI void202__libcpp_atomic_wait(__cxx_atomic_contention_t const volatile* __location, __cxx_contention_t __old_value) noexcept {203__libcpp_contention_wait(&__libcpp_contention_state(__location)->__contention_state, __location, __old_value);204}205206_LIBCPP_END_NAMESPACE_STD207208209