yield_k.hpp 1023 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #ifndef BOOST_SMART_PTR_DETAIL_YIELD_K_HPP_INCLUDED
  2. #define BOOST_SMART_PTR_DETAIL_YIELD_K_HPP_INCLUDED
  3. // MS compatible compilers support #pragma once
  4. #if defined(_MSC_VER) && (_MSC_VER >= 1020)
  5. # pragma once
  6. #endif
  7. // boost/smart_ptr/detail/yield_k.hpp
  8. //
  9. // Copyright 2008, 2020 Peter Dimov
  10. //
  11. // inline void boost::detail::yield( unsigned k );
  12. //
  13. // Typical use:
  14. // for( unsigned k = 0; !try_lock(); ++k ) yield( k );
  15. //
  16. // Distributed under the Boost Software License, Version 1.0.
  17. // https://www.boost.org/LICENSE_1_0.txt
  18. #include <boost/core/yield_primitives.hpp>
  19. namespace boost
  20. {
  21. namespace detail
  22. {
  23. inline void yield( unsigned k )
  24. {
  25. // Experiments on Windows and Fedora 32 show that a single pause,
  26. // followed by an immediate sp_thread_sleep(), is best.
  27. if( k & 1 )
  28. {
  29. boost::core::sp_thread_sleep();
  30. }
  31. else
  32. {
  33. boost::core::sp_thread_pause();
  34. }
  35. }
  36. } // namespace detail
  37. } // namespace boost
  38. #endif // #ifndef BOOST_SMART_PTR_DETAIL_YIELD_K_HPP_INCLUDED