thread 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. // <thread> -*- 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 include/thread
  21. * This is a Standard C++ Library header.
  22. */
  23. #ifndef _GLIBCXX_THREAD
  24. #define _GLIBCXX_THREAD 1
  25. #pragma GCC system_header
  26. #if __cplusplus < 201103L
  27. # include <bits/c++0x_warning.h>
  28. #else
  29. #if __cplusplus > 201703L
  30. # include <compare> // std::strong_ordering
  31. # include <stop_token> // std::stop_source, std::stop_token, std::nostopstate
  32. #endif
  33. #include <bits/std_thread.h> // std::thread, get_id, yield
  34. #include <bits/this_thread_sleep.h> // std::this_thread::sleep_for, sleep_until
  35. namespace std _GLIBCXX_VISIBILITY(default)
  36. {
  37. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  38. /**
  39. * @defgroup threads Threads
  40. * @ingroup concurrency
  41. *
  42. * Classes for thread support.
  43. * @{
  44. */
  45. // std::thread is defined in <bits/std_thread.h>
  46. #if __cpp_lib_three_way_comparison
  47. inline strong_ordering
  48. operator<=>(thread::id __x, thread::id __y) noexcept
  49. { return __x._M_thread <=> __y._M_thread; }
  50. #else
  51. inline bool
  52. operator!=(thread::id __x, thread::id __y) noexcept
  53. { return !(__x == __y); }
  54. inline bool
  55. operator<(thread::id __x, thread::id __y) noexcept
  56. {
  57. // Pthreads doesn't define any way to do this, so we just have to
  58. // assume native_handle_type is LessThanComparable.
  59. return __x._M_thread < __y._M_thread;
  60. }
  61. inline bool
  62. operator<=(thread::id __x, thread::id __y) noexcept
  63. { return !(__y < __x); }
  64. inline bool
  65. operator>(thread::id __x, thread::id __y) noexcept
  66. { return __y < __x; }
  67. inline bool
  68. operator>=(thread::id __x, thread::id __y) noexcept
  69. { return !(__x < __y); }
  70. #endif // __cpp_lib_three_way_comparison
  71. template<class _CharT, class _Traits>
  72. inline basic_ostream<_CharT, _Traits>&
  73. operator<<(basic_ostream<_CharT, _Traits>& __out, thread::id __id)
  74. {
  75. if (__id == thread::id())
  76. return __out << "thread::id of a non-executing thread";
  77. else
  78. return __out << __id._M_thread;
  79. }
  80. #ifdef __cpp_lib_jthread
  81. #ifndef __STRICT_ANSI__
  82. template<typename _Callable, typename... _Args>
  83. constexpr bool __pmf_expects_stop_token = false;
  84. template<typename _Callable, typename _Obj, typename... _Args>
  85. constexpr bool __pmf_expects_stop_token<_Callable, _Obj, _Args...>
  86. = __and_<is_member_function_pointer<remove_reference_t<_Callable>>,
  87. is_invocable<_Callable, _Obj, stop_token, _Args...>>::value;
  88. #endif
  89. /// A thread that can be requested to stop and automatically joined.
  90. class jthread
  91. {
  92. public:
  93. using id = thread::id;
  94. using native_handle_type = thread::native_handle_type;
  95. jthread() noexcept
  96. : _M_stop_source{nostopstate}
  97. { }
  98. template<typename _Callable, typename... _Args,
  99. typename = enable_if_t<!is_same_v<remove_cvref_t<_Callable>,
  100. jthread>>>
  101. explicit
  102. jthread(_Callable&& __f, _Args&&... __args)
  103. : _M_thread{_S_create(_M_stop_source, std::forward<_Callable>(__f),
  104. std::forward<_Args>(__args)...)}
  105. { }
  106. jthread(const jthread&) = delete;
  107. jthread(jthread&&) noexcept = default;
  108. ~jthread()
  109. {
  110. if (joinable())
  111. {
  112. request_stop();
  113. join();
  114. }
  115. }
  116. jthread&
  117. operator=(const jthread&) = delete;
  118. jthread&
  119. operator=(jthread&& __other) noexcept
  120. {
  121. std::jthread(std::move(__other)).swap(*this);
  122. return *this;
  123. }
  124. void
  125. swap(jthread& __other) noexcept
  126. {
  127. std::swap(_M_stop_source, __other._M_stop_source);
  128. std::swap(_M_thread, __other._M_thread);
  129. }
  130. [[nodiscard]] bool
  131. joinable() const noexcept
  132. {
  133. return _M_thread.joinable();
  134. }
  135. void
  136. join()
  137. {
  138. _M_thread.join();
  139. }
  140. void
  141. detach()
  142. {
  143. _M_thread.detach();
  144. }
  145. [[nodiscard]] id
  146. get_id() const noexcept
  147. {
  148. return _M_thread.get_id();
  149. }
  150. [[nodiscard]] native_handle_type
  151. native_handle()
  152. {
  153. return _M_thread.native_handle();
  154. }
  155. [[nodiscard]] static unsigned
  156. hardware_concurrency() noexcept
  157. {
  158. return thread::hardware_concurrency();
  159. }
  160. [[nodiscard]] stop_source
  161. get_stop_source() noexcept
  162. {
  163. return _M_stop_source;
  164. }
  165. [[nodiscard]] stop_token
  166. get_stop_token() const noexcept
  167. {
  168. return _M_stop_source.get_token();
  169. }
  170. bool request_stop() noexcept
  171. {
  172. return _M_stop_source.request_stop();
  173. }
  174. friend void swap(jthread& __lhs, jthread& __rhs) noexcept
  175. {
  176. __lhs.swap(__rhs);
  177. }
  178. private:
  179. template<typename _Callable, typename... _Args>
  180. static thread
  181. _S_create(stop_source& __ssrc, _Callable&& __f, _Args&&... __args)
  182. {
  183. #ifndef __STRICT_ANSI__
  184. if constexpr (__pmf_expects_stop_token<_Callable, _Args...>)
  185. return _S_create_pmf(__ssrc, __f, std::forward<_Args>(__args)...);
  186. else
  187. #endif
  188. if constexpr(is_invocable_v<decay_t<_Callable>, stop_token,
  189. decay_t<_Args>...>)
  190. return thread{std::forward<_Callable>(__f), __ssrc.get_token(),
  191. std::forward<_Args>(__args)...};
  192. else
  193. {
  194. static_assert(is_invocable_v<decay_t<_Callable>,
  195. decay_t<_Args>...>,
  196. "std::jthread arguments must be invocable after"
  197. " conversion to rvalues");
  198. return thread{std::forward<_Callable>(__f),
  199. std::forward<_Args>(__args)...};
  200. }
  201. }
  202. #ifndef __STRICT_ANSI__
  203. template<typename _Callable, typename _Obj, typename... _Args>
  204. static thread
  205. _S_create_pmf(stop_source& __ssrc, _Callable __f, _Obj&& __obj,
  206. _Args&&... __args)
  207. {
  208. return thread{__f, std::forward<_Obj>(__obj), __ssrc.get_token(),
  209. std::forward<_Args>(__args)...};
  210. }
  211. #endif
  212. stop_source _M_stop_source;
  213. thread _M_thread;
  214. };
  215. #endif // __cpp_lib_jthread
  216. /// @} group threads
  217. _GLIBCXX_END_NAMESPACE_VERSION
  218. } // namespace
  219. #endif // C++11
  220. #endif // _GLIBCXX_THREAD