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
29#elif defined(__FreeBSD__)
30
31# include <sys/types.h>
32# include <sys/umtx.h>
33
34#else // <- Add other operating systems here
35
36// Baseline needs no new headers
37
38#endif
39
40_LIBCPP_BEGIN_NAMESPACE_STD
41
42#ifdef __linux__
43
44static void
45__libcpp_platform_wait_on_address(__cxx_atomic_contention_t const volatile* __ptr, __cxx_contention_t __val) {
46 static constexpr timespec __timeout = {.tv_sec: 2, .tv_nsec: 0};
47 syscall(SYS_futex, __ptr, FUTEX_WAIT_PRIVATE, __val, &__timeout, 0, 0);
48}
49
50static void __libcpp_platform_wake_by_address(__cxx_atomic_contention_t const volatile* __ptr, bool __notify_one) {
51 syscall(SYS_futex, __ptr, FUTEX_WAKE_PRIVATE, __notify_one ? 1 : INT_MAX, 0, 0, 0);
52}
53
54#elif defined(__APPLE__) && defined(_LIBCPP_USE_ULOCK)
55
56extern "C" int __ulock_wait(
57 uint32_t operation, void* addr, uint64_t value, uint32_t timeout); /* timeout is specified in microseconds */
58extern "C" int __ulock_wake(uint32_t operation, void* addr, uint64_t wake_value);
59
60# define UL_COMPARE_AND_WAIT 1
61# define ULF_WAKE_ALL 0x00000100
62
63static void
64__libcpp_platform_wait_on_address(__cxx_atomic_contention_t const volatile* __ptr, __cxx_contention_t __val) {
65 __ulock_wait(UL_COMPARE_AND_WAIT, const_cast<__cxx_atomic_contention_t*>(__ptr), __val, 0);
66}
67
68static void __libcpp_platform_wake_by_address(__cxx_atomic_contention_t const volatile* __ptr, bool __notify_one) {
69 __ulock_wake(
70 UL_COMPARE_AND_WAIT | (__notify_one ? 0 : ULF_WAKE_ALL), const_cast<__cxx_atomic_contention_t*>(__ptr), 0);
71}
72
73#elif defined(__FreeBSD__) && __SIZEOF_LONG__ == 8
74/*
75 * Since __cxx_contention_t is int64_t even on 32bit FreeBSD
76 * platforms, we have to use umtx ops that work on the long type, and
77 * limit its use to architectures where long and int64_t are synonyms.
78 */
79
80static void
81__libcpp_platform_wait_on_address(__cxx_atomic_contention_t const volatile* __ptr, __cxx_contention_t __val) {
82 _umtx_op(const_cast<__cxx_atomic_contention_t*>(__ptr), UMTX_OP_WAIT, __val, NULL, NULL);
83}
84
85static void __libcpp_platform_wake_by_address(__cxx_atomic_contention_t const volatile* __ptr, bool __notify_one) {
86 _umtx_op(const_cast<__cxx_atomic_contention_t*>(__ptr), UMTX_OP_WAKE, __notify_one ? 1 : INT_MAX, NULL, NULL);
87}
88
89#else // <- Add other operating systems here
90
91// Baseline is just a timed backoff
92
93static void
94__libcpp_platform_wait_on_address(__cxx_atomic_contention_t const volatile* __ptr, __cxx_contention_t __val) {
95 __libcpp_thread_poll_with_backoff(
96 [=]() -> bool { return !__cxx_nonatomic_compare_equal(__cxx_atomic_load(__ptr, memory_order_relaxed), __val); },
97 __libcpp_timed_backoff_policy());
98}
99
100static void __libcpp_platform_wake_by_address(__cxx_atomic_contention_t const volatile*, bool) {}
101
102#endif // __linux__
103
104static constexpr size_t __libcpp_contention_table_size = (1 << 8); /* < there's no magic in this number */
105
106struct alignas(64) /* aim to avoid false sharing */ __libcpp_contention_table_entry {
107 __cxx_atomic_contention_t __contention_state;
108 __cxx_atomic_contention_t __platform_state;
109 inline constexpr __libcpp_contention_table_entry() : __contention_state(0), __platform_state(0) {}
110};
111
112static __libcpp_contention_table_entry __libcpp_contention_table[__libcpp_contention_table_size];
113
114static hash<void const volatile*> __libcpp_contention_hasher;
115
116static __libcpp_contention_table_entry* __libcpp_contention_state(void const volatile* p) {
117 return &__libcpp_contention_table[__libcpp_contention_hasher(p) & (__libcpp_contention_table_size - 1)];
118}
119
120/* Given an atomic to track contention and an atomic to actually wait on, which may be
121 the same atomic, we try to detect contention to avoid spuriously calling the platform. */
122
123static void __libcpp_contention_notify(__cxx_atomic_contention_t volatile* __contention_state,
124 __cxx_atomic_contention_t const volatile* __platform_state,
125 bool __notify_one) {
126 if (0 != __cxx_atomic_load(__contention_state, memory_order_seq_cst))
127 // We only call 'wake' if we consumed a contention bit here.
128 __libcpp_platform_wake_by_address(__platform_state, __notify_one);
129}
130static __cxx_contention_t
131__libcpp_contention_monitor_for_wait(__cxx_atomic_contention_t volatile* /*__contention_state*/,
132 __cxx_atomic_contention_t const volatile* __platform_state) {
133 // We will monitor this value.
134 return __cxx_atomic_load(__platform_state, memory_order_acquire);
135}
136static void __libcpp_contention_wait(__cxx_atomic_contention_t volatile* __contention_state,
137 __cxx_atomic_contention_t const volatile* __platform_state,
138 __cxx_contention_t __old_value) {
139 __cxx_atomic_fetch_add(__contention_state, __cxx_contention_t(1), memory_order_seq_cst);
140 // We sleep as long as the monitored value hasn't changed.
141 __libcpp_platform_wait_on_address(__platform_state, __old_value);
142 __cxx_atomic_fetch_sub(__contention_state, __cxx_contention_t(1), memory_order_release);
143}
144
145/* When the incoming atomic is the wrong size for the platform wait size, need to
146 launder the value sequence through an atomic from our table. */
147
148static void __libcpp_atomic_notify(void const volatile* __location) {
149 auto const __entry = __libcpp_contention_state(p: __location);
150 // The value sequence laundering happens on the next line below.
151 __cxx_atomic_fetch_add(&__entry->__platform_state, __cxx_contention_t(1), memory_order_release);
152 __libcpp_contention_notify(
153 &__entry->__contention_state,
154 &__entry->__platform_state,
155 false /* when laundering, we can't handle notify_one */);
156}
157_LIBCPP_EXPORTED_FROM_ABI void __cxx_atomic_notify_one(void const volatile* __location) {
158 __libcpp_atomic_notify(__location);
159}
160_LIBCPP_EXPORTED_FROM_ABI void __cxx_atomic_notify_all(void const volatile* __location) {
161 __libcpp_atomic_notify(__location);
162}
163_LIBCPP_EXPORTED_FROM_ABI __cxx_contention_t __libcpp_atomic_monitor(void const volatile* __location) {
164 auto const __entry = __libcpp_contention_state(__location);
165 return __libcpp_contention_monitor_for_wait(&__entry->__contention_state, &__entry->__platform_state);
166}
167_LIBCPP_EXPORTED_FROM_ABI void __libcpp_atomic_wait(void const volatile* __location, __cxx_contention_t __old_value) {
168 auto const __entry = __libcpp_contention_state(p: __location);
169 __libcpp_contention_wait(&__entry->__contention_state, &__entry->__platform_state, __old_value);
170}
171
172/* When the incoming atomic happens to be the platform wait size, we still need to use the
173 table for the contention detection, but we can use the atomic directly for the wait. */
174
175_LIBCPP_EXPORTED_FROM_ABI void __cxx_atomic_notify_one(__cxx_atomic_contention_t const volatile* __location) {
176 __libcpp_contention_notify(&__libcpp_contention_state(__location)->__contention_state, __location, true);
177}
178_LIBCPP_EXPORTED_FROM_ABI void __cxx_atomic_notify_all(__cxx_atomic_contention_t const volatile* __location) {
179 __libcpp_contention_notify(&__libcpp_contention_state(__location)->__contention_state, __location, false);
180}
181// This function is never used, but still exported for ABI compatibility.
182_LIBCPP_EXPORTED_FROM_ABI __cxx_contention_t
183__libcpp_atomic_monitor(__cxx_atomic_contention_t const volatile* __location) {
184 return __libcpp_contention_monitor_for_wait(&__libcpp_contention_state(__location)->__contention_state, __location);
185}
186_LIBCPP_EXPORTED_FROM_ABI void
187__libcpp_atomic_wait(__cxx_atomic_contention_t const volatile* __location, __cxx_contention_t __old_value) {
188 __libcpp_contention_wait(&__libcpp_contention_state(__location)->__contention_state, __location, __old_value);
189}
190
191_LIBCPP_END_NAMESPACE_STD
192

source code of libcxx/src/atomic.cpp