sp_thread_sleep.hpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #ifndef BOOST_CORE_DETAIL_SP_THREAD_SLEEP_HPP_INCLUDED
  2. #define BOOST_CORE_DETAIL_SP_THREAD_SLEEP_HPP_INCLUDED
  3. // MS compatible compilers support #pragma once
  4. #if defined(_MSC_VER) && (_MSC_VER >= 1020)
  5. # pragma once
  6. #endif
  7. // boost/core/detail/sp_thread_sleep.hpp
  8. //
  9. // inline void bost::core::sp_thread_sleep();
  10. //
  11. // Cease execution for a while to yield to other threads,
  12. // as if by calling nanosleep() with an appropriate interval.
  13. //
  14. // Copyright 2008, 2020, 2023 Peter Dimov
  15. // Distributed under the Boost Software License, Version 1.0
  16. // https://www.boost.org/LICENSE_1_0.txt
  17. #include <boost/config.hpp>
  18. #include <boost/config/pragma_message.hpp>
  19. #if defined( _WIN32 ) || defined( __WIN32__ ) || defined( __CYGWIN__ )
  20. #if defined(BOOST_SP_REPORT_IMPLEMENTATION)
  21. BOOST_PRAGMA_MESSAGE("Using Sleep(1) in sp_thread_sleep")
  22. #endif
  23. #include <boost/core/detail/sp_win32_sleep.hpp>
  24. namespace boost
  25. {
  26. namespace core
  27. {
  28. namespace detail
  29. {
  30. inline void sp_thread_sleep() BOOST_NOEXCEPT
  31. {
  32. Sleep( 1 );
  33. }
  34. } // namespace detail
  35. using boost::core::detail::sp_thread_sleep;
  36. } // namespace core
  37. } // namespace boost
  38. #elif defined(BOOST_HAS_NANOSLEEP)
  39. #if defined(BOOST_SP_REPORT_IMPLEMENTATION)
  40. BOOST_PRAGMA_MESSAGE("Using nanosleep() in sp_thread_sleep")
  41. #endif
  42. #include <time.h>
  43. #if defined(BOOST_HAS_PTHREADS) && !defined(__ANDROID__)
  44. # include <pthread.h>
  45. #endif
  46. namespace boost
  47. {
  48. namespace core
  49. {
  50. inline void sp_thread_sleep() BOOST_NOEXCEPT
  51. {
  52. #if defined(BOOST_HAS_PTHREADS) && !defined(__ANDROID__)
  53. int oldst;
  54. pthread_setcancelstate( PTHREAD_CANCEL_DISABLE, &oldst );
  55. #endif
  56. // g++ -Wextra warns on {} or {0}
  57. struct timespec rqtp = { 0, 0 };
  58. // POSIX says that timespec has tv_sec and tv_nsec
  59. // But it doesn't guarantee order or placement
  60. rqtp.tv_sec = 0;
  61. rqtp.tv_nsec = 1000;
  62. nanosleep( &rqtp, 0 );
  63. #if defined(BOOST_HAS_PTHREADS) && !defined(__ANDROID__)
  64. pthread_setcancelstate( oldst, &oldst );
  65. #endif
  66. }
  67. } // namespace core
  68. } // namespace boost
  69. #else
  70. #if defined(BOOST_SP_REPORT_IMPLEMENTATION)
  71. BOOST_PRAGMA_MESSAGE("Using sp_thread_yield() in sp_thread_sleep")
  72. #endif
  73. #include <boost/core/detail/sp_thread_yield.hpp>
  74. namespace boost
  75. {
  76. namespace core
  77. {
  78. inline void sp_thread_sleep() BOOST_NOEXCEPT
  79. {
  80. sp_thread_yield();
  81. }
  82. } // namespace core
  83. } // namespace boost
  84. #endif
  85. #endif // #ifndef BOOST_CORE_DETAIL_SP_THREAD_SLEEP_HPP_INCLUDED