1 //===------------------------- atomic.cpp ---------------------------------===//
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
7 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP_HAS_NO_THREADS
21 #include <linux/futex.h>
22 #include <sys/syscall.h>
24 #else // <- Add other operating systems here
26 // Baseline needs no new headers
30 _LIBCPP_BEGIN_NAMESPACE_STD
34 static void __libcpp_platform_wait_on_address(__cxx_atomic_contention_t const volatile* __ptr,
35 __cxx_contention_t __val)
37 static constexpr timespec __timeout = { 2, 0 };
38 syscall(SYS_futex, __ptr, FUTEX_WAIT_PRIVATE, __val, &__timeout, 0, 0);
41 static void __libcpp_platform_wake_by_address(__cxx_atomic_contention_t const volatile* __ptr,
44 syscall(SYS_futex, __ptr, FUTEX_WAKE_PRIVATE, __notify_one ? 1 : INT_MAX, 0, 0, 0);
47 #elif defined(__APPLE__) && defined(_LIBCPP_USE_ULOCK)
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);
53 #define UL_COMPARE_AND_WAIT 1
54 #define ULF_WAKE_ALL 0x00000100
56 static void __libcpp_platform_wait_on_address(__cxx_atomic_contention_t const volatile* __ptr,
57 __cxx_contention_t __val)
59 __ulock_wait(UL_COMPARE_AND_WAIT,
60 const_cast<__cxx_atomic_contention_t*>(__ptr), __val, 0);
63 static void __libcpp_platform_wake_by_address(__cxx_atomic_contention_t const volatile* __ptr,
66 __ulock_wake(UL_COMPARE_AND_WAIT | (__notify_one ? 0 : ULF_WAKE_ALL),
67 const_cast<__cxx_atomic_contention_t*>(__ptr), 0);
70 #else // <- Add other operating systems here
72 // Baseline is just a timed backoff
74 static void __libcpp_platform_wait_on_address(__cxx_atomic_contention_t const volatile* __ptr,
75 __cxx_contention_t __val)
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());
82 static void __libcpp_platform_wake_by_address(__cxx_atomic_contention_t const volatile*, bool) { }
86 static constexpr size_t __libcpp_contention_table_size = (1 << 8); /* < there's no magic in this number */
88 struct alignas(64) /* aim to avoid false sharing */ __libcpp_contention_table_entry
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) { }
96 static __libcpp_contention_table_entry __libcpp_contention_table[ __libcpp_contention_table_size ];
98 static hash<void const volatile*> __libcpp_contention_hasher;
100 static __libcpp_contention_table_entry* __libcpp_contention_state(void const volatile * p)
102 return &__libcpp_contention_table[__libcpp_contention_hasher(p) & (__libcpp_contention_table_size - 1)];
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. */
108 static void __libcpp_contention_notify(__cxx_atomic_contention_t volatile* __contention_state,
109 __cxx_atomic_contention_t const volatile* __platform_state,
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);
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)
119 // We will monitor this value.
120 return __cxx_atomic_load(__platform_state, memory_order_acquire);
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)
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);
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. */
135 static void __libcpp_atomic_notify(void const volatile* __location)
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 */);
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)
153 auto const __entry = __libcpp_contention_state(__location);
154 return __libcpp_contention_monitor_for_wait(&__entry->__contention_state, &__entry->__platform_state);
156 _LIBCPP_EXPORTED_FROM_ABI
157 void __libcpp_atomic_wait(void const volatile* __location, __cxx_contention_t __old_value)
159 auto const __entry = __libcpp_contention_state(__location);
160 __libcpp_contention_wait(&__entry->__contention_state, &__entry->__platform_state, __old_value);
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. */
166 _LIBCPP_EXPORTED_FROM_ABI
167 void __cxx_atomic_notify_one(__cxx_atomic_contention_t const volatile* __location)
169 __libcpp_contention_notify(&__libcpp_contention_state(__location)->__contention_state, __location, true);
171 _LIBCPP_EXPORTED_FROM_ABI
172 void __cxx_atomic_notify_all(__cxx_atomic_contention_t const volatile* __location)
174 __libcpp_contention_notify(&__libcpp_contention_state(__location)->__contention_state, __location, false);
176 _LIBCPP_EXPORTED_FROM_ABI
177 __cxx_contention_t __libcpp_atomic_monitor(__cxx_atomic_contention_t const volatile* __location)
179 return __libcpp_contention_monitor_for_wait(&__libcpp_contention_state(__location)->__contention_state, __location);
181 _LIBCPP_EXPORTED_FROM_ABI
182 void __libcpp_atomic_wait(__cxx_atomic_contention_t const volatile* __location, __cxx_contention_t __old_value)
184 __libcpp_contention_wait(&__libcpp_contention_state(__location)->__contention_state, __location, __old_value);
187 _LIBCPP_END_NAMESPACE_STD
189 #endif //_LIBCPP_HAS_NO_THREADS