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