master
  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-exception
  6//
  7//===----------------------------------------------------------------------===//
  8
  9#include <__thread/timed_backoff_policy.h>
 10#include <atomic>
 11#include <climits>
 12#include <functional>
 13#include <thread>
 14
 15#include "include/apple_availability.h"
 16
 17#ifdef __linux__
 18
 19#  include <linux/futex.h>
 20#  include <sys/syscall.h>
 21#  include <unistd.h>
 22
 23// libc++ uses SYS_futex as a universal syscall name. However, on 32 bit architectures
 24// with a 64 bit time_t, we need to specify SYS_futex_time64.
 25#  if !defined(SYS_futex) && defined(SYS_futex_time64)
 26#    define SYS_futex SYS_futex_time64
 27#  endif
 28#  define _LIBCPP_FUTEX(...) syscall(SYS_futex, __VA_ARGS__)
 29
 30#elif defined(__FreeBSD__)
 31
 32#  include <sys/types.h>
 33#  include <sys/umtx.h>
 34
 35#  define _LIBCPP_FUTEX(...) syscall(SYS_futex, __VA_ARGS__)
 36
 37#elif defined(__OpenBSD__)
 38
 39#  include <sys/futex.h>
 40
 41// OpenBSD has no indirect syscalls
 42#  define _LIBCPP_FUTEX(...) futex(__VA_ARGS__)
 43
 44#else // <- Add other operating systems here
 45
 46// Baseline needs no new headers
 47
 48#  define _LIBCPP_FUTEX(...) syscall(SYS_futex, __VA_ARGS__)
 49
 50#endif
 51
 52_LIBCPP_BEGIN_NAMESPACE_STD
 53
 54#ifdef __linux__
 55
 56static void
 57__libcpp_platform_wait_on_address(__cxx_atomic_contention_t const volatile* __ptr, __cxx_contention_t __val) {
 58  static constexpr timespec __timeout = {2, 0};
 59  _LIBCPP_FUTEX(__ptr, FUTEX_WAIT_PRIVATE, __val, &__timeout, 0, 0);
 60}
 61
 62static void __libcpp_platform_wake_by_address(__cxx_atomic_contention_t const volatile* __ptr, bool __notify_one) {
 63  _LIBCPP_FUTEX(__ptr, FUTEX_WAKE_PRIVATE, __notify_one ? 1 : INT_MAX, 0, 0, 0);
 64}
 65
 66#elif defined(__APPLE__) && defined(_LIBCPP_USE_ULOCK)
 67
 68extern "C" int __ulock_wait(
 69    uint32_t operation, void* addr, uint64_t value, uint32_t timeout); /* timeout is specified in microseconds */
 70extern "C" int __ulock_wake(uint32_t operation, void* addr, uint64_t wake_value);
 71
 72// https://github.com/apple/darwin-xnu/blob/2ff845c2e033bd0ff64b5b6aa6063a1f8f65aa32/bsd/sys/ulock.h#L82
 73#  define UL_COMPARE_AND_WAIT64 5
 74#  define ULF_WAKE_ALL 0x00000100
 75
 76static void
 77__libcpp_platform_wait_on_address(__cxx_atomic_contention_t const volatile* __ptr, __cxx_contention_t __val) {
 78  static_assert(sizeof(__cxx_atomic_contention_t) == 8, "Waiting on 8 bytes value");
 79  __ulock_wait(UL_COMPARE_AND_WAIT64, const_cast<__cxx_atomic_contention_t*>(__ptr), __val, 0);
 80}
 81
 82static void __libcpp_platform_wake_by_address(__cxx_atomic_contention_t const volatile* __ptr, bool __notify_one) {
 83  static_assert(sizeof(__cxx_atomic_contention_t) == 8, "Waking up on 8 bytes value");
 84  __ulock_wake(
 85      UL_COMPARE_AND_WAIT64 | (__notify_one ? 0 : ULF_WAKE_ALL), const_cast<__cxx_atomic_contention_t*>(__ptr), 0);
 86}
 87
 88#elif defined(__FreeBSD__) && __SIZEOF_LONG__ == 8
 89/*
 90 * Since __cxx_contention_t is int64_t even on 32bit FreeBSD
 91 * platforms, we have to use umtx ops that work on the long type, and
 92 * limit its use to architectures where long and int64_t are synonyms.
 93 */
 94
 95static void
 96__libcpp_platform_wait_on_address(__cxx_atomic_contention_t const volatile* __ptr, __cxx_contention_t __val) {
 97  _umtx_op(const_cast<__cxx_atomic_contention_t*>(__ptr), UMTX_OP_WAIT, __val, nullptr, nullptr);
 98}
 99
100static void __libcpp_platform_wake_by_address(__cxx_atomic_contention_t const volatile* __ptr, bool __notify_one) {
101  _umtx_op(const_cast<__cxx_atomic_contention_t*>(__ptr), UMTX_OP_WAKE, __notify_one ? 1 : INT_MAX, nullptr, nullptr);
102}
103
104#else // <- Add other operating systems here
105
106// Baseline is just a timed backoff
107
108static void
109__libcpp_platform_wait_on_address(__cxx_atomic_contention_t const volatile* __ptr, __cxx_contention_t __val) {
110  __libcpp_thread_poll_with_backoff(
111      [=]() -> bool { return !__cxx_nonatomic_compare_equal(__cxx_atomic_load(__ptr, memory_order_relaxed), __val); },
112      __libcpp_timed_backoff_policy());
113}
114
115static void __libcpp_platform_wake_by_address(__cxx_atomic_contention_t const volatile*, bool) {}
116
117#endif // __linux__
118
119static constexpr size_t __libcpp_contention_table_size = (1 << 8); /* < there's no magic in this number */
120
121struct alignas(64) /*  aim to avoid false sharing */ __libcpp_contention_table_entry {
122  __cxx_atomic_contention_t __contention_state;
123  __cxx_atomic_contention_t __platform_state;
124  inline constexpr __libcpp_contention_table_entry() : __contention_state(0), __platform_state(0) {}
125};
126
127static __libcpp_contention_table_entry __libcpp_contention_table[__libcpp_contention_table_size];
128
129static hash<void const volatile*> __libcpp_contention_hasher;
130
131static __libcpp_contention_table_entry* __libcpp_contention_state(void const volatile* p) {
132  return &__libcpp_contention_table[__libcpp_contention_hasher(p) & (__libcpp_contention_table_size - 1)];
133}
134
135/* Given an atomic to track contention and an atomic to actually wait on, which may be
136   the same atomic, we try to detect contention to avoid spuriously calling the platform. */
137
138static void __libcpp_contention_notify(__cxx_atomic_contention_t volatile* __contention_state,
139                                       __cxx_atomic_contention_t const volatile* __platform_state,
140                                       bool __notify_one) {
141  if (0 != __cxx_atomic_load(__contention_state, memory_order_seq_cst))
142    // We only call 'wake' if we consumed a contention bit here.
143    __libcpp_platform_wake_by_address(__platform_state, __notify_one);
144}
145static __cxx_contention_t
146__libcpp_contention_monitor_for_wait(__cxx_atomic_contention_t volatile* /*__contention_state*/,
147                                     __cxx_atomic_contention_t const volatile* __platform_state) {
148  // We will monitor this value.
149  return __cxx_atomic_load(__platform_state, memory_order_acquire);
150}
151static void __libcpp_contention_wait(__cxx_atomic_contention_t volatile* __contention_state,
152                                     __cxx_atomic_contention_t const volatile* __platform_state,
153                                     __cxx_contention_t __old_value) {
154  __cxx_atomic_fetch_add(__contention_state, __cxx_contention_t(1), memory_order_relaxed);
155  // https://github.com/llvm/llvm-project/issues/109290
156  // There are no platform guarantees of a memory barrier in the platform wait implementation
157  __cxx_atomic_thread_fence(memory_order_seq_cst);
158  // We sleep as long as the monitored value hasn't changed.
159  __libcpp_platform_wait_on_address(__platform_state, __old_value);
160  __cxx_atomic_fetch_sub(__contention_state, __cxx_contention_t(1), memory_order_release);
161}
162
163/* When the incoming atomic is the wrong size for the platform wait size, need to
164   launder the value sequence through an atomic from our table. */
165
166static void __libcpp_atomic_notify(void const volatile* __location) {
167  auto const __entry = __libcpp_contention_state(__location);
168  // The value sequence laundering happens on the next line below.
169  __cxx_atomic_fetch_add(&__entry->__platform_state, __cxx_contention_t(1), memory_order_seq_cst);
170  __libcpp_contention_notify(
171      &__entry->__contention_state,
172      &__entry->__platform_state,
173      false /* when laundering, we can't handle notify_one */);
174}
175_LIBCPP_EXPORTED_FROM_ABI void __cxx_atomic_notify_one(void const volatile* __location) noexcept {
176  __libcpp_atomic_notify(__location);
177}
178_LIBCPP_EXPORTED_FROM_ABI void __cxx_atomic_notify_all(void const volatile* __location) noexcept {
179  __libcpp_atomic_notify(__location);
180}
181_LIBCPP_EXPORTED_FROM_ABI __cxx_contention_t __libcpp_atomic_monitor(void const volatile* __location) noexcept {
182  auto const __entry = __libcpp_contention_state(__location);
183  return __libcpp_contention_monitor_for_wait(&__entry->__contention_state, &__entry->__platform_state);
184}
185_LIBCPP_EXPORTED_FROM_ABI void
186__libcpp_atomic_wait(void const volatile* __location, __cxx_contention_t __old_value) noexcept {
187  auto const __entry = __libcpp_contention_state(__location);
188  __libcpp_contention_wait(&__entry->__contention_state, &__entry->__platform_state, __old_value);
189}
190
191/* When the incoming atomic happens to be the platform wait size, we still need to use the
192   table for the contention detection, but we can use the atomic directly for the wait. */
193
194_LIBCPP_EXPORTED_FROM_ABI void __cxx_atomic_notify_one(__cxx_atomic_contention_t const volatile* __location) noexcept {
195  __libcpp_contention_notify(&__libcpp_contention_state(__location)->__contention_state, __location, true);
196}
197_LIBCPP_EXPORTED_FROM_ABI void __cxx_atomic_notify_all(__cxx_atomic_contention_t const volatile* __location) noexcept {
198  __libcpp_contention_notify(&__libcpp_contention_state(__location)->__contention_state, __location, false);
199}
200// This function is never used, but still exported for ABI compatibility.
201_LIBCPP_EXPORTED_FROM_ABI __cxx_contention_t
202__libcpp_atomic_monitor(__cxx_atomic_contention_t const volatile* __location) noexcept {
203  return __libcpp_contention_monitor_for_wait(&__libcpp_contention_state(__location)->__contention_state, __location);
204}
205_LIBCPP_EXPORTED_FROM_ABI void
206__libcpp_atomic_wait(__cxx_atomic_contention_t const volatile* __location, __cxx_contention_t __old_value) noexcept {
207  __libcpp_contention_wait(&__libcpp_contention_state(__location)->__contention_state, __location, __old_value);
208}
209
210_LIBCPP_END_NAMESPACE_STD