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