spinlock_ttas.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright Oliver Kowalke 2016.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_FIBERS_SPINLOCK_TTAS_H
  6. #define BOOST_FIBERS_SPINLOCK_TTAS_H
  7. #include <atomic>
  8. #include <chrono>
  9. #include <cmath>
  10. #include <random>
  11. #include <thread>
  12. #include <boost/fiber/detail/config.hpp>
  13. #include <boost/fiber/detail/cpu_relax.hpp>
  14. #include <boost/fiber/detail/spinlock_status.hpp>
  15. // based on informations from:
  16. // https://software.intel.com/en-us/articles/benefitting-power-and-performance-sleep-loops
  17. // https://software.intel.com/en-us/articles/long-duration-spin-wait-loops-on-hyper-threading-technology-enabled-intel-processors
  18. namespace boost {
  19. namespace fibers {
  20. namespace detail {
  21. class spinlock_ttas {
  22. private:
  23. template< typename FBSplk >
  24. friend class spinlock_rtm;
  25. std::atomic< spinlock_status > state_{ spinlock_status::unlocked };
  26. public:
  27. spinlock_ttas() = default;
  28. spinlock_ttas( spinlock_ttas const&) = delete;
  29. spinlock_ttas & operator=( spinlock_ttas const&) = delete;
  30. void lock() noexcept {
  31. static thread_local std::minstd_rand generator{ std::random_device{}() };
  32. std::size_t collisions = 0 ;
  33. for (;;) {
  34. // avoid using multiple pause instructions for a delay of a specific cycle count
  35. // the delay of cpu_relax() (pause on Intel) depends on the processor family
  36. // the cycle count can not guaranteed from one system to the next
  37. // -> check the shared variable 'state_' in between each cpu_relax() to prevent
  38. // unnecessarily long delays on some systems
  39. std::size_t retries = 0;
  40. // test shared variable 'status_'
  41. // first access to 'state_' -> chache miss
  42. // sucessive acccess to 'state_' -> cache hit
  43. // if 'state_' was released by other fiber
  44. // cached 'state_' is invalidated -> cache miss
  45. while ( spinlock_status::locked == state_.load( std::memory_order_relaxed) ) {
  46. #if !defined(BOOST_FIBERS_SPIN_SINGLE_CORE)
  47. if ( BOOST_FIBERS_SPIN_BEFORE_SLEEP0 > retries) {
  48. ++retries;
  49. // give CPU a hint that this thread is in a "spin-wait" loop
  50. // delays the next instruction's execution for a finite period of time (depends on processor family)
  51. // the CPU is not under demand, parts of the pipeline are no longer being used
  52. // -> reduces the power consumed by the CPU
  53. // -> prevent pipeline stalls
  54. cpu_relax();
  55. } else if ( BOOST_FIBERS_SPIN_BEFORE_YIELD > retries) {
  56. // std::this_thread::sleep_for( 0us) has a fairly long instruction path length,
  57. // combined with an expensive ring3 to ring 0 transition costing about 1000 cycles
  58. // std::this_thread::sleep_for( 0us) lets give up this_thread the remaining part of its time slice
  59. // if and only if a thread of equal or greater priority is ready to run
  60. static constexpr std::chrono::microseconds us0{ 0 };
  61. std::this_thread::sleep_for( us0);
  62. } else {
  63. // std::this_thread::yield() allows this_thread to give up the remaining part of its time slice,
  64. // but only to another thread on the same processor
  65. // instead of constant checking, a thread only checks if no other useful work is pending
  66. std::this_thread::yield();
  67. }
  68. #else
  69. std::this_thread::yield();
  70. #endif
  71. }
  72. // test-and-set shared variable 'status_'
  73. // everytime 'status_' is signaled over the bus, even if the test failes
  74. if ( spinlock_status::locked == state_.exchange( spinlock_status::locked, std::memory_order_acquire) ) {
  75. // spinlock now contended
  76. // utilize 'Binary Exponential Backoff' algorithm
  77. // linear_congruential_engine is a random number engine based on Linear congruential generator (LCG)
  78. std::uniform_int_distribution< std::size_t > distribution{
  79. 0, static_cast< std::size_t >( 1) << (std::min)(collisions, static_cast< std::size_t >( BOOST_FIBERS_CONTENTION_WINDOW_THRESHOLD)) };
  80. const std::size_t z = distribution( generator);
  81. ++collisions;
  82. for ( std::size_t i = 0; i < z; ++i) {
  83. // -> reduces the power consumed by the CPU
  84. // -> prevent pipeline stalls
  85. cpu_relax();
  86. }
  87. } else {
  88. // success, thread has acquired the lock
  89. break;
  90. }
  91. }
  92. }
  93. bool try_lock() noexcept {
  94. return spinlock_status::unlocked == state_.exchange( spinlock_status::locked, std::memory_order_acquire);
  95. }
  96. void unlock() noexcept {
  97. state_.store( spinlock_status::unlocked, std::memory_order_release);
  98. }
  99. };
  100. }}}
  101. #endif // BOOST_FIBERS_SPINLOCK_TTAS_H