this_thread_sleep.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // std::this_thread::sleep_for/until declarations -*- C++ -*-
  2. // Copyright (C) 2008-2021 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/std_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/c++config.h>
  29. #include <chrono> // std::chrono::*
  30. #ifdef _GLIBCXX_USE_NANOSLEEP
  31. # include <cerrno> // errno, EINTR
  32. # include <time.h> // nanosleep
  33. #endif
  34. namespace std _GLIBCXX_VISIBILITY(default)
  35. {
  36. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  37. /** @addtogroup threads
  38. * @{
  39. */
  40. /** @namespace std::this_thread
  41. * @brief ISO C++ 2011 namespace for interacting with the current thread
  42. *
  43. * C++11 30.3.2 [thread.thread.this] Namespace this_thread.
  44. */
  45. namespace this_thread
  46. {
  47. #ifndef _GLIBCXX_NO_SLEEP
  48. #ifndef _GLIBCXX_USE_NANOSLEEP
  49. void
  50. __sleep_for(chrono::seconds, chrono::nanoseconds);
  51. #endif
  52. /// this_thread::sleep_for
  53. template<typename _Rep, typename _Period>
  54. inline void
  55. sleep_for(const chrono::duration<_Rep, _Period>& __rtime)
  56. {
  57. if (__rtime <= __rtime.zero())
  58. return;
  59. auto __s = chrono::duration_cast<chrono::seconds>(__rtime);
  60. auto __ns = chrono::duration_cast<chrono::nanoseconds>(__rtime - __s);
  61. #ifdef _GLIBCXX_USE_NANOSLEEP
  62. struct ::timespec __ts =
  63. {
  64. static_cast<std::time_t>(__s.count()),
  65. static_cast<long>(__ns.count())
  66. };
  67. while (::nanosleep(&__ts, &__ts) == -1 && errno == EINTR)
  68. { }
  69. #else
  70. __sleep_for(__s, __ns);
  71. #endif
  72. }
  73. /// this_thread::sleep_until
  74. template<typename _Clock, typename _Duration>
  75. inline void
  76. sleep_until(const chrono::time_point<_Clock, _Duration>& __atime)
  77. {
  78. #if __cplusplus > 201703L
  79. static_assert(chrono::is_clock_v<_Clock>);
  80. #endif
  81. auto __now = _Clock::now();
  82. if (_Clock::is_steady)
  83. {
  84. if (__now < __atime)
  85. sleep_for(__atime - __now);
  86. return;
  87. }
  88. while (__now < __atime)
  89. {
  90. sleep_for(__atime - __now);
  91. __now = _Clock::now();
  92. }
  93. }
  94. } // namespace this_thread
  95. #endif // ! NO_SLEEP
  96. /// @}
  97. _GLIBCXX_END_NAMESPACE_VERSION
  98. } // namespace
  99. #endif // C++11
  100. #endif // _GLIBCXX_THIS_THREAD_SLEEP_H