this_thread_sleep.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // std::this_thread::sleep_for/until declarations -*- C++ -*-
  2. // Copyright (C) 2008-2022 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file bits/this_thread_sleep.h
  21. * This is an internal header file, included by other library headers.
  22. * Do not attempt to use it directly. @headername{thread}
  23. */
  24. #ifndef _GLIBCXX_THIS_THREAD_SLEEP_H
  25. #define _GLIBCXX_THIS_THREAD_SLEEP_H 1
  26. #pragma GCC system_header
  27. #if __cplusplus >= 201103L
  28. #include <bits/chrono.h> // std::chrono::*
  29. #ifdef _GLIBCXX_USE_NANOSLEEP
  30. # include <cerrno> // errno, EINTR
  31. # include <time.h> // nanosleep
  32. #endif
  33. namespace std _GLIBCXX_VISIBILITY(default)
  34. {
  35. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  36. /** @addtogroup threads
  37. * @{
  38. */
  39. /** @namespace std::this_thread
  40. * @brief ISO C++ 2011 namespace for interacting with the current thread
  41. *
  42. * C++11 30.3.2 [thread.thread.this] Namespace this_thread.
  43. */
  44. namespace this_thread
  45. {
  46. #ifndef _GLIBCXX_NO_SLEEP
  47. #ifndef _GLIBCXX_USE_NANOSLEEP
  48. void
  49. __sleep_for(chrono::seconds, chrono::nanoseconds);
  50. #endif
  51. /// this_thread::sleep_for
  52. template<typename _Rep, typename _Period>
  53. inline void
  54. sleep_for(const chrono::duration<_Rep, _Period>& __rtime)
  55. {
  56. if (__rtime <= __rtime.zero())
  57. return;
  58. auto __s = chrono::duration_cast<chrono::seconds>(__rtime);
  59. auto __ns = chrono::duration_cast<chrono::nanoseconds>(__rtime - __s);
  60. #ifdef _GLIBCXX_USE_NANOSLEEP
  61. struct ::timespec __ts =
  62. {
  63. static_cast<std::time_t>(__s.count()),
  64. static_cast<long>(__ns.count())
  65. };
  66. while (::nanosleep(&__ts, &__ts) == -1 && errno == EINTR)
  67. { }
  68. #else
  69. __sleep_for(__s, __ns);
  70. #endif
  71. }
  72. /// this_thread::sleep_until
  73. template<typename _Clock, typename _Duration>
  74. inline void
  75. sleep_until(const chrono::time_point<_Clock, _Duration>& __atime)
  76. {
  77. #if __cplusplus > 201703L
  78. static_assert(chrono::is_clock_v<_Clock>);
  79. #endif
  80. auto __now = _Clock::now();
  81. if (_Clock::is_steady)
  82. {
  83. if (__now < __atime)
  84. sleep_for(__atime - __now);
  85. return;
  86. }
  87. while (__now < __atime)
  88. {
  89. sleep_for(__atime - __now);
  90. __now = _Clock::now();
  91. }
  92. }
  93. #endif // ! NO_SLEEP
  94. } // namespace this_thread
  95. /// @}
  96. _GLIBCXX_END_NAMESPACE_VERSION
  97. } // namespace
  98. #endif // C++11
  99. #endif // _GLIBCXX_THIS_THREAD_SLEEP_H