jthread.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP___THREAD_JTHREAD_H
  10. #define _LIBCPP___THREAD_JTHREAD_H
  11. #include <__availability>
  12. #include <__config>
  13. #include <__functional/invoke.h>
  14. #include <__stop_token/stop_source.h>
  15. #include <__stop_token/stop_token.h>
  16. #include <__thread/support.h>
  17. #include <__thread/thread.h>
  18. #include <__type_traits/decay.h>
  19. #include <__type_traits/is_constructible.h>
  20. #include <__type_traits/is_same.h>
  21. #include <__type_traits/remove_cvref.h>
  22. #include <__utility/forward.h>
  23. #include <__utility/move.h>
  24. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  25. # pragma GCC system_header
  26. #endif
  27. _LIBCPP_PUSH_MACROS
  28. #include <__undef_macros>
  29. #if _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_STOP_TOKEN)
  30. _LIBCPP_BEGIN_NAMESPACE_STD
  31. class _LIBCPP_AVAILABILITY_SYNC jthread {
  32. public:
  33. // types
  34. using id = thread::id;
  35. using native_handle_type = thread::native_handle_type;
  36. // [thread.jthread.cons], constructors, move, and assignment
  37. _LIBCPP_HIDE_FROM_ABI jthread() noexcept : __stop_source_(std::nostopstate) {}
  38. template <class _Fun, class... _Args>
  39. _LIBCPP_HIDE_FROM_ABI explicit jthread(_Fun&& __fun, _Args&&... __args)
  40. requires(!std::is_same_v<remove_cvref_t<_Fun>, jthread>)
  41. : __stop_source_(),
  42. __thread_(__init_thread(__stop_source_, std::forward<_Fun>(__fun), std::forward<_Args>(__args)...)) {
  43. static_assert(is_constructible_v<decay_t<_Fun>, _Fun>);
  44. static_assert((is_constructible_v<decay_t<_Args>, _Args> && ...));
  45. static_assert(is_invocable_v<decay_t<_Fun>, decay_t<_Args>...> ||
  46. is_invocable_v<decay_t<_Fun>, stop_token, decay_t<_Args>...>);
  47. }
  48. _LIBCPP_HIDE_FROM_ABI ~jthread() {
  49. if (joinable()) {
  50. request_stop();
  51. join();
  52. }
  53. }
  54. jthread(const jthread&) = delete;
  55. _LIBCPP_HIDE_FROM_ABI jthread(jthread&&) noexcept = default;
  56. jthread& operator=(const jthread&) = delete;
  57. _LIBCPP_HIDE_FROM_ABI jthread& operator=(jthread&& __other) noexcept {
  58. if (this != &__other) {
  59. if (joinable()) {
  60. request_stop();
  61. join();
  62. }
  63. __stop_source_ = std::move(__other.__stop_source_);
  64. __thread_ = std::move(__other.__thread_);
  65. }
  66. return *this;
  67. }
  68. // [thread.jthread.mem], members
  69. _LIBCPP_HIDE_FROM_ABI void swap(jthread& __other) noexcept {
  70. std::swap(__stop_source_, __other.__stop_source_);
  71. std::swap(__thread_, __other.__thread_);
  72. }
  73. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI bool joinable() const noexcept { return get_id() != id(); }
  74. _LIBCPP_HIDE_FROM_ABI void join() { __thread_.join(); }
  75. _LIBCPP_HIDE_FROM_ABI void detach() { __thread_.detach(); }
  76. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI id get_id() const noexcept { return __thread_.get_id(); }
  77. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() { return __thread_.native_handle(); }
  78. // [thread.jthread.stop], stop token handling
  79. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI stop_source get_stop_source() noexcept { return __stop_source_; }
  80. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI stop_token get_stop_token() const noexcept { return __stop_source_.get_token(); }
  81. _LIBCPP_HIDE_FROM_ABI bool request_stop() noexcept { return __stop_source_.request_stop(); }
  82. // [thread.jthread.special], specialized algorithms
  83. _LIBCPP_HIDE_FROM_ABI friend void swap(jthread& __lhs, jthread& __rhs) noexcept { __lhs.swap(__rhs); }
  84. // [thread.jthread.static], static members
  85. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static unsigned int hardware_concurrency() noexcept {
  86. return thread::hardware_concurrency();
  87. }
  88. private:
  89. template <class _Fun, class... _Args>
  90. _LIBCPP_HIDE_FROM_ABI static thread __init_thread(const stop_source& __ss, _Fun&& __fun, _Args&&... __args) {
  91. if constexpr (is_invocable_v<decay_t<_Fun>, stop_token, decay_t<_Args>...>) {
  92. return thread(std::forward<_Fun>(__fun), __ss.get_token(), std::forward<_Args>(__args)...);
  93. } else {
  94. return thread(std::forward<_Fun>(__fun), std::forward<_Args>(__args)...);
  95. }
  96. }
  97. stop_source __stop_source_;
  98. thread __thread_;
  99. };
  100. _LIBCPP_END_NAMESPACE_STD
  101. #endif // _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_STOP_TOKEN)
  102. _LIBCPP_POP_MACROS
  103. #endif // _LIBCPP___THREAD_JTHREAD_H