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