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 <__config>
10 #ifndef _LIBCPP_HAS_NO_THREADS
11 
12 #include <atomic>
13 #include <climits>
14 #include <functional>
15 #include <thread>
16 
17 #ifdef __linux__
18 
19 #include <unistd.h>
20 #include <linux/futex.h>
21 #include <sys/syscall.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 
44 static void __libcpp_platform_wait_on_address(__cxx_atomic_contention_t const volatile* __ptr,
45                                               __cxx_contention_t __val)
46 {
47     static constexpr timespec __timeout = { 2, 0 };
48     syscall(SYS_futex, __ptr, FUTEX_WAIT_PRIVATE, __val, &__timeout, 0, 0);
49 }
50 
51 static void __libcpp_platform_wake_by_address(__cxx_atomic_contention_t const volatile* __ptr,
52                                               bool __notify_one)
53 {
54     syscall(SYS_futex, __ptr, FUTEX_WAKE_PRIVATE, __notify_one ? 1 : INT_MAX, 0, 0, 0);
55 }
56 
57 #elif defined(__APPLE__) && defined(_LIBCPP_USE_ULOCK)
58 
59 extern "C" int __ulock_wait(uint32_t operation, void *addr, uint64_t value,
60 		uint32_t timeout); /* timeout is specified in microseconds */
61 extern "C" int __ulock_wake(uint32_t operation, void *addr, uint64_t wake_value);
62 
63 #define UL_COMPARE_AND_WAIT				1
64 #define ULF_WAKE_ALL					0x00000100
65 
66 static void __libcpp_platform_wait_on_address(__cxx_atomic_contention_t const volatile* __ptr,
67                                               __cxx_contention_t __val)
68 {
69     __ulock_wait(UL_COMPARE_AND_WAIT,
70                  const_cast<__cxx_atomic_contention_t*>(__ptr), __val, 0);
71 }
72 
73 static void __libcpp_platform_wake_by_address(__cxx_atomic_contention_t const volatile* __ptr,
74                                               bool __notify_one)
75 {
76     __ulock_wake(UL_COMPARE_AND_WAIT | (__notify_one ? 0 : ULF_WAKE_ALL),
77                  const_cast<__cxx_atomic_contention_t*>(__ptr), 0);
78 }
79 
80 #elif defined(__FreeBSD__) && __SIZEOF_LONG__ == 8
81 /*
82  * Since __cxx_contention_t is int64_t even on 32bit FreeBSD
83  * platforms, we have to use umtx ops that work on the long type, and
84  * limit its use to architectures where long and int64_t are synonyms.
85  */
86 
87 static void __libcpp_platform_wait_on_address(__cxx_atomic_contention_t const volatile* __ptr,
88                                               __cxx_contention_t __val)
89 {
90     _umtx_op(const_cast<__cxx_atomic_contention_t*>(__ptr),
91       UMTX_OP_WAIT, __val, NULL, NULL);
92 }
93 
94 static void __libcpp_platform_wake_by_address(__cxx_atomic_contention_t const volatile* __ptr,
95                                               bool __notify_one)
96 {
97     _umtx_op(const_cast<__cxx_atomic_contention_t*>(__ptr),
98       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 __libcpp_platform_wait_on_address(__cxx_atomic_contention_t const volatile* __ptr,
106                                               __cxx_contention_t __val)
107 {
108     __libcpp_thread_poll_with_backoff([=]() -> bool {
109         return !__cxx_nonatomic_compare_equal(__cxx_atomic_load(__ptr, memory_order_relaxed), __val);
110     }, __libcpp_timed_backoff_policy());
111 }
112 
113 static void __libcpp_platform_wake_by_address(__cxx_atomic_contention_t const volatile*, bool) { }
114 
115 #endif // __linux__
116 
117 static constexpr size_t __libcpp_contention_table_size = (1 << 8);  /* < there's no magic in this number */
118 
119 struct alignas(64) /*  aim to avoid false sharing */ __libcpp_contention_table_entry
120 {
121     __cxx_atomic_contention_t __contention_state;
122     __cxx_atomic_contention_t __platform_state;
123     inline constexpr __libcpp_contention_table_entry() :
124         __contention_state(0), __platform_state(0) { }
125 };
126 
127 static __libcpp_contention_table_entry __libcpp_contention_table[ __libcpp_contention_table_size ];
128 
129 static hash<void const volatile*> __libcpp_contention_hasher;
130 
131 static __libcpp_contention_table_entry* __libcpp_contention_state(void const volatile * p)
132 {
133     return &__libcpp_contention_table[__libcpp_contention_hasher(p) & (__libcpp_contention_table_size - 1)];
134 }
135 
136 /* Given an atomic to track contention and an atomic to actually wait on, which may be
137    the same atomic, we try to detect contention to avoid spuriously calling the platform. */
138 
139 static void __libcpp_contention_notify(__cxx_atomic_contention_t volatile* __contention_state,
140                                        __cxx_atomic_contention_t const volatile* __platform_state,
141                                        bool __notify_one)
142 {
143     if(0 != __cxx_atomic_load(__contention_state, memory_order_seq_cst))
144         // We only call 'wake' if we consumed a contention bit here.
145         __libcpp_platform_wake_by_address(__platform_state, __notify_one);
146 }
147 static __cxx_contention_t __libcpp_contention_monitor_for_wait(__cxx_atomic_contention_t volatile* __contention_state,
148                                                                __cxx_atomic_contention_t const volatile* __platform_state)
149 {
150     // We will monitor this value.
151     return __cxx_atomic_load(__platform_state, memory_order_acquire);
152 }
153 static void __libcpp_contention_wait(__cxx_atomic_contention_t volatile* __contention_state,
154                                      __cxx_atomic_contention_t const volatile* __platform_state,
155                                      __cxx_contention_t __old_value)
156 {
157     __cxx_atomic_fetch_add(__contention_state, __cxx_contention_t(1), 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 
166 static void __libcpp_atomic_notify(void const volatile* __location)
167 {
168     auto const __entry = __libcpp_contention_state(__location);
169     // The value sequence laundering happens on the next line below.
170     __cxx_atomic_fetch_add(&__entry->__platform_state, __cxx_contention_t(1), memory_order_release);
171     __libcpp_contention_notify(&__entry->__contention_state,
172                                &__entry->__platform_state,
173                                false /* when laundering, we can't handle notify_one */);
174 }
175 _LIBCPP_EXPORTED_FROM_ABI
176 void __cxx_atomic_notify_one(void const volatile* __location)
177     { __libcpp_atomic_notify(__location); }
178 _LIBCPP_EXPORTED_FROM_ABI
179 void __cxx_atomic_notify_all(void const volatile* __location)
180     { __libcpp_atomic_notify(__location); }
181 _LIBCPP_EXPORTED_FROM_ABI
182 __cxx_contention_t __libcpp_atomic_monitor(void const volatile* __location)
183 {
184     auto const __entry = __libcpp_contention_state(__location);
185     return __libcpp_contention_monitor_for_wait(&__entry->__contention_state, &__entry->__platform_state);
186 }
187 _LIBCPP_EXPORTED_FROM_ABI
188 void __libcpp_atomic_wait(void const volatile* __location, __cxx_contention_t __old_value)
189 {
190     auto const __entry = __libcpp_contention_state(__location);
191     __libcpp_contention_wait(&__entry->__contention_state, &__entry->__platform_state, __old_value);
192 }
193 
194 /* When the incoming atomic happens to be the platform wait size, we still need to use the
195    table for the contention detection, but we can use the atomic directly for the wait. */
196 
197 _LIBCPP_EXPORTED_FROM_ABI
198 void __cxx_atomic_notify_one(__cxx_atomic_contention_t const volatile* __location)
199 {
200     __libcpp_contention_notify(&__libcpp_contention_state(__location)->__contention_state, __location, true);
201 }
202 _LIBCPP_EXPORTED_FROM_ABI
203 void __cxx_atomic_notify_all(__cxx_atomic_contention_t const volatile* __location)
204 {
205     __libcpp_contention_notify(&__libcpp_contention_state(__location)->__contention_state, __location, false);
206 }
207 _LIBCPP_EXPORTED_FROM_ABI
208 __cxx_contention_t __libcpp_atomic_monitor(__cxx_atomic_contention_t const volatile* __location)
209 {
210     return __libcpp_contention_monitor_for_wait(&__libcpp_contention_state(__location)->__contention_state, __location);
211 }
212 _LIBCPP_EXPORTED_FROM_ABI
213 void __libcpp_atomic_wait(__cxx_atomic_contention_t const volatile* __location, __cxx_contention_t __old_value)
214 {
215     __libcpp_contention_wait(&__libcpp_contention_state(__location)->__contention_state, __location, __old_value);
216 }
217 
218 _LIBCPP_END_NAMESPACE_STD
219 
220 #endif //_LIBCPP_HAS_NO_THREADS
221