1 /* 2 Copyright (c) 2005-2021 Intel Corporation 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 #ifndef __TBB_detail__utils_H 18 #define __TBB_detail__utils_H 19 20 #include <type_traits> 21 #include <cstdint> 22 #include <atomic> 23 24 #include "_config.h" 25 #include "_assert.h" 26 #include "_machine.h" 27 28 namespace tbb { 29 namespace detail { 30 inline namespace d0 { 31 32 //! Utility template function to prevent "unused" warnings by various compilers. 33 template<typename... T> void suppress_unused_warning(T&&...) {} 34 35 //! Compile-time constant that is upper bound on cache line/sector size. 36 /** It should be used only in situations where having a compile-time upper 37 bound is more useful than a run-time exact answer. 38 @ingroup memory_allocation */ 39 constexpr size_t max_nfs_size = 128; 40 constexpr std::size_t max_nfs_size_exp = 7; 41 static_assert(1 << max_nfs_size_exp == max_nfs_size, "max_nfs_size_exp must be a log2(max_nfs_size)"); 42 43 //! Class that implements exponential backoff. 44 class atomic_backoff { 45 //! Time delay, in units of "pause" instructions. 46 /** Should be equal to approximately the number of "pause" instructions 47 that take the same time as an context switch. Must be a power of two.*/ 48 static constexpr std::int32_t LOOPS_BEFORE_YIELD = 16; 49 std::int32_t count; 50 51 public: 52 // In many cases, an object of this type is initialized eagerly on hot path, 53 // as in for(atomic_backoff b; ; b.pause()) { /*loop body*/ } 54 // For this reason, the construction cost must be very small! 55 atomic_backoff() : count(1) {} 56 // This constructor pauses immediately; do not use on hot paths! 57 atomic_backoff(bool) : count(1) { pause(); } 58 59 //! No Copy 60 atomic_backoff(const atomic_backoff&) = delete; 61 atomic_backoff& operator=(const atomic_backoff&) = delete; 62 63 //! Pause for a while. 64 void pause() { 65 if (count <= LOOPS_BEFORE_YIELD) { 66 machine_pause(count); 67 // Pause twice as long the next time. 68 count *= 2; 69 } else { 70 // Pause is so long that we might as well yield CPU to scheduler. 71 yield(); 72 } 73 } 74 75 //! Pause for a few times and return false if saturated. 76 bool bounded_pause() { 77 machine_pause(count); 78 if (count < LOOPS_BEFORE_YIELD) { 79 // Pause twice as long the next time. 80 count *= 2; 81 return true; 82 } else { 83 return false; 84 } 85 } 86 87 void reset() { 88 count = 1; 89 } 90 }; 91 92 //! Spin WHILE the condition is true. 93 /** T and U should be comparable types. */ 94 template <typename T, typename C> 95 T spin_wait_while(const std::atomic<T>& location, C comp, std::memory_order order) { 96 atomic_backoff backoff; 97 T snapshot = location.load(order); 98 while (comp(snapshot)) { 99 backoff.pause(); 100 snapshot = location.load(order); 101 } 102 return snapshot; 103 } 104 105 //! Spin WHILE the value of the variable is equal to a given value 106 /** T and U should be comparable types. */ 107 template <typename T, typename U> 108 T spin_wait_while_eq(const std::atomic<T>& location, const U value, std::memory_order order = std::memory_order_acquire) { 109 return spin_wait_while(location, [&value](T t) { return t == value; }, order); 110 } 111 112 //! Spin UNTIL the value of the variable is equal to a given value 113 /** T and U should be comparable types. */ 114 template<typename T, typename U> 115 T spin_wait_until_eq(const std::atomic<T>& location, const U value, std::memory_order order = std::memory_order_acquire) { 116 return spin_wait_while(location, [&value](T t) { return t != value; }, order); 117 } 118 119 //! Spin UNTIL the condition returns true or spinning time is up. 120 /** Returns what the passed functor returned last time it was invoked. */ 121 template <typename Condition> 122 bool timed_spin_wait_until(Condition condition) { 123 // 32 pauses + 32 yields are meausered as balanced spin time before sleep. 124 bool finish = condition(); 125 for (int i = 1; !finish && i < 32; finish = condition(), i *= 2) { 126 machine_pause(i); 127 } 128 for (int i = 32; !finish && i < 64; finish = condition(), ++i) { 129 yield(); 130 } 131 return finish; 132 } 133 134 template <typename T> 135 std::uintptr_t log2(T in) { 136 __TBB_ASSERT(in > 0, "The logarithm of a non-positive value is undefined."); 137 return machine_log2(in); 138 } 139 140 template<typename T> 141 T reverse_bits(T src) { 142 return machine_reverse_bits(src); 143 } 144 145 template<typename T> 146 T reverse_n_bits(T src, std::size_t n) { 147 __TBB_ASSERT(n != 0, "Reverse for 0 bits is undefined behavior."); 148 return reverse_bits(src) >> (number_of_bits<T>() - n); 149 } 150 151 // A function to check if passed integer is a power of two 152 template <typename IntegerType> 153 constexpr bool is_power_of_two( IntegerType arg ) { 154 static_assert(std::is_integral<IntegerType>::value, 155 "An argument for is_power_of_two should be integral type"); 156 return arg && (0 == (arg & (arg - 1))); 157 } 158 159 // A function to determine if passed integer is a power of two 160 // at least as big as another power of two, i.e. for strictly positive i and j, 161 // with j being a power of two, determines whether i==j<<k for some nonnegative k 162 template <typename ArgIntegerType, typename DivisorIntegerType> 163 constexpr bool is_power_of_two_at_least(ArgIntegerType arg, DivisorIntegerType divisor) { 164 // Divisor should be a power of two 165 static_assert(std::is_integral<ArgIntegerType>::value, 166 "An argument for is_power_of_two_at_least should be integral type"); 167 return 0 == (arg & (arg - divisor)); 168 } 169 170 // A function to compute arg modulo divisor where divisor is a power of 2. 171 template<typename ArgIntegerType, typename DivisorIntegerType> 172 inline ArgIntegerType modulo_power_of_two(ArgIntegerType arg, DivisorIntegerType divisor) { 173 __TBB_ASSERT( is_power_of_two(divisor), "Divisor should be a power of two" ); 174 return arg & (divisor - 1); 175 } 176 177 //! A function to check if passed in pointer is aligned on a specific border 178 template<typename T> 179 constexpr bool is_aligned(T* pointer, std::uintptr_t alignment) { 180 return 0 == ((std::uintptr_t)pointer & (alignment - 1)); 181 } 182 183 #if TBB_USE_ASSERT 184 static void* const poisoned_ptr = reinterpret_cast<void*>(-1); 185 186 //! Set p to invalid pointer value. 187 template<typename T> 188 inline void poison_pointer( T* &p ) { p = reinterpret_cast<T*>(poisoned_ptr); } 189 190 template<typename T> 191 inline void poison_pointer(std::atomic<T*>& p) { p.store(reinterpret_cast<T*>(poisoned_ptr), std::memory_order_relaxed); } 192 193 /** Expected to be used in assertions only, thus no empty form is defined. **/ 194 template<typename T> 195 inline bool is_poisoned( T* p ) { return p == reinterpret_cast<T*>(poisoned_ptr); } 196 197 template<typename T> 198 inline bool is_poisoned(const std::atomic<T*>& p) { return is_poisoned(p.load(std::memory_order_relaxed)); } 199 #else 200 template<typename T> 201 inline void poison_pointer(T* &) {/*do nothing*/} 202 203 template<typename T> 204 inline void poison_pointer(std::atomic<T*>&) { /* do nothing */} 205 #endif /* !TBB_USE_ASSERT */ 206 207 template <std::size_t alignment = 0, typename T> 208 bool assert_pointer_valid(T* p, const char* comment = nullptr) { 209 suppress_unused_warning(p, comment); 210 __TBB_ASSERT(p != nullptr, comment); 211 __TBB_ASSERT(!is_poisoned(p), comment); 212 #if !(_MSC_VER && _MSC_VER <= 1900 && !__INTEL_COMPILER) 213 __TBB_ASSERT(is_aligned(p, alignment == 0 ? alignof(T) : alignment), comment); 214 #endif 215 // Returns something to simplify assert_pointers_valid implementation. 216 return true; 217 } 218 219 template <typename... Args> 220 void assert_pointers_valid(Args*... p) { 221 // suppress_unused_warning is used as an evaluation context for the variadic pack. 222 suppress_unused_warning(assert_pointer_valid(p)...); 223 } 224 225 //! Base class for types that should not be assigned. 226 class no_assign { 227 public: 228 void operator=(const no_assign&) = delete; 229 no_assign(const no_assign&) = default; 230 no_assign() = default; 231 }; 232 233 //! Base class for types that should not be copied or assigned. 234 class no_copy: no_assign { 235 public: 236 no_copy(const no_copy&) = delete; 237 no_copy() = default; 238 }; 239 240 template <typename T> 241 void swap_atomics_relaxed(std::atomic<T>& lhs, std::atomic<T>& rhs){ 242 T tmp = lhs.load(std::memory_order_relaxed); 243 lhs.store(rhs.load(std::memory_order_relaxed), std::memory_order_relaxed); 244 rhs.store(tmp, std::memory_order_relaxed); 245 } 246 247 //! One-time initialization states 248 enum class do_once_state { 249 uninitialized = 0, ///< No execution attempts have been undertaken yet 250 pending, ///< A thread is executing associated do-once routine 251 executed, ///< Do-once routine has been executed 252 initialized = executed ///< Convenience alias 253 }; 254 255 //! One-time initialization function 256 /** /param initializer Pointer to function without arguments 257 The variant that returns bool is used for cases when initialization can fail 258 and it is OK to continue execution, but the state should be reset so that 259 the initialization attempt was repeated the next time. 260 /param state Shared state associated with initializer that specifies its 261 initialization state. Must be initially set to #uninitialized value 262 (e.g. by means of default static zero initialization). **/ 263 template <typename F> 264 void atomic_do_once( const F& initializer, std::atomic<do_once_state>& state ) { 265 // The loop in the implementation is necessary to avoid race when thread T2 266 // that arrived in the middle of initialization attempt by another thread T1 267 // has just made initialization possible. 268 // In such a case T2 has to rely on T1 to initialize, but T1 may already be past 269 // the point where it can recognize the changed conditions. 270 do_once_state expected_state; 271 while ( state.load( std::memory_order_acquire ) != do_once_state::executed ) { 272 if( state.load( std::memory_order_relaxed ) == do_once_state::uninitialized ) { 273 expected_state = do_once_state::uninitialized; 274 #if defined(__INTEL_COMPILER) && __INTEL_COMPILER <= 1910 275 using enum_type = typename std::underlying_type<do_once_state>::type; 276 if( ((std::atomic<enum_type>&)state).compare_exchange_strong( (enum_type&)expected_state, (enum_type)do_once_state::pending ) ) { 277 #else 278 if( state.compare_exchange_strong( expected_state, do_once_state::pending ) ) { 279 #endif 280 run_initializer( initializer, state ); 281 break; 282 } 283 } 284 spin_wait_while_eq( state, do_once_state::pending ); 285 } 286 } 287 288 // Run the initializer which can not fail 289 template<typename Functor> 290 void run_initializer(const Functor& f, std::atomic<do_once_state>& state ) { 291 f(); 292 state.store(do_once_state::executed, std::memory_order_release); 293 } 294 295 #if __TBB_CPP20_CONCEPTS_PRESENT 296 template <typename T> 297 concept boolean_testable_impl = std::convertible_to<T, bool>; 298 299 template <typename T> 300 concept boolean_testable = boolean_testable_impl<T> && requires( T&& t ) { 301 { !std::forward<T>(t) } -> boolean_testable_impl; 302 }; 303 304 #if __TBB_CPP20_COMPARISONS_PRESENT 305 struct synthesized_three_way_comparator { 306 template <typename T1, typename T2> 307 auto operator()( const T1& lhs, const T2& rhs ) const 308 requires requires { 309 { lhs < rhs } -> boolean_testable; 310 { rhs < lhs } -> boolean_testable; 311 } 312 { 313 if constexpr (std::three_way_comparable_with<T1, T2>) { 314 return lhs <=> rhs; 315 } else { 316 if (lhs < rhs) { 317 return std::weak_ordering::less; 318 } 319 if (rhs < lhs) { 320 return std::weak_ordering::greater; 321 } 322 return std::weak_ordering::equivalent; 323 } 324 } 325 }; // struct synthesized_three_way_comparator 326 327 template <typename T1, typename T2 = T1> 328 using synthesized_three_way_result = decltype(synthesized_three_way_comparator{}(std::declval<T1&>(), 329 std::declval<T2&>())); 330 331 #endif // __TBB_CPP20_COMPARISONS_PRESENT 332 333 // Check if the type T is implicitly OR explicitly convertible to U 334 template <typename T, typename U> 335 concept relaxed_convertible_to = std::constructible_from<U, T>; 336 337 template <typename T, typename U> 338 concept adaptive_same_as = 339 #if __TBB_STRICT_CONSTRAINTS 340 std::same_as<T, U>; 341 #else 342 std::convertible_to<T, U>; 343 #endif 344 #endif // __TBB_CPP20_CONCEPTS_PRESENT 345 346 } // namespace d0 347 348 namespace d1 { 349 350 class delegate_base { 351 public: 352 virtual bool operator()() const = 0; 353 virtual ~delegate_base() {} 354 }; 355 356 template <typename FuncType> 357 class delegated_function : public delegate_base { 358 public: 359 delegated_function(FuncType& f) : my_func(f) {} 360 361 bool operator()() const override { 362 return my_func(); 363 } 364 365 private: 366 FuncType &my_func; 367 }; 368 } // namespace d1 369 370 } // namespace detail 371 } // namespace tbb 372 373 #endif // __TBB_detail__utils_H 374