jthread.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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/thread.h>
  17. #include <__threading_support>
  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. #if _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_STOP_TOKEN)
  28. _LIBCPP_BEGIN_NAMESPACE_STD
  29. class _LIBCPP_AVAILABILITY_SYNC jthread {
  30. public:
  31. // types
  32. using id = thread::id;
  33. using native_handle_type = thread::native_handle_type;
  34. // [thread.jthread.cons], constructors, move, and assignment
  35. _LIBCPP_HIDE_FROM_ABI jthread() noexcept : __stop_source_(std::nostopstate) {}
  36. template <class _Fun, class... _Args>
  37. _LIBCPP_HIDE_FROM_ABI explicit jthread(_Fun&& __fun, _Args&&... __args)
  38. requires(!std::is_same_v<remove_cvref_t<_Fun>, jthread>)
  39. : __stop_source_(),
  40. __thread_(__init_thread(__stop_source_, std::forward<_Fun>(__fun), std::forward<_Args>(__args)...)) {
  41. static_assert(is_constructible_v<decay_t<_Fun>, _Fun>);
  42. static_assert((is_constructible_v<decay_t<_Args>, _Args> && ...));
  43. static_assert(is_invocable_v<decay_t<_Fun>, decay_t<_Args>...> ||
  44. is_invocable_v<decay_t<_Fun>, stop_token, decay_t<_Args>...>);
  45. }
  46. _LIBCPP_HIDE_FROM_ABI ~jthread() {
  47. if (joinable()) {
  48. request_stop();
  49. join();
  50. }
  51. }
  52. jthread(const jthread&) = delete;
  53. _LIBCPP_HIDE_FROM_ABI jthread(jthread&&) noexcept = default;
  54. jthread& operator=(const jthread&) = delete;
  55. _LIBCPP_HIDE_FROM_ABI jthread& operator=(jthread&& __other) noexcept {
  56. if (this != &__other) {
  57. if (joinable()) {
  58. request_stop();
  59. join();
  60. }
  61. __stop_source_ = std::move(__other.__stop_source_);
  62. __thread_ = std::move(__other.__thread_);
  63. }
  64. return *this;
  65. }
  66. // [thread.jthread.mem], members
  67. _LIBCPP_HIDE_FROM_ABI void swap(jthread& __other) noexcept {
  68. std::swap(__stop_source_, __other.__stop_source_);
  69. std::swap(__thread_, __other.__thread_);
  70. }
  71. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI bool joinable() const noexcept { return get_id() != id(); }
  72. _LIBCPP_HIDE_FROM_ABI void join() { __thread_.join(); }
  73. _LIBCPP_HIDE_FROM_ABI void detach() { __thread_.detach(); }
  74. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI id get_id() const noexcept { return __thread_.get_id(); }
  75. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() { return __thread_.native_handle(); }
  76. // [thread.jthread.stop], stop token handling
  77. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI stop_source get_stop_source() noexcept { return __stop_source_; }
  78. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI stop_token get_stop_token() const noexcept { return __stop_source_.get_token(); }
  79. _LIBCPP_HIDE_FROM_ABI bool request_stop() noexcept { return __stop_source_.request_stop(); }
  80. // [thread.jthread.special], specialized algorithms
  81. _LIBCPP_HIDE_FROM_ABI friend void swap(jthread& __lhs, jthread& __rhs) noexcept { __lhs.swap(__rhs); }
  82. // [thread.jthread.static], static members
  83. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static unsigned int hardware_concurrency() noexcept {
  84. return thread::hardware_concurrency();
  85. }
  86. private:
  87. template <class _Fun, class... _Args>
  88. _LIBCPP_HIDE_FROM_ABI static thread __init_thread(const stop_source& __ss, _Fun&& __fun, _Args&&... __args) {
  89. if constexpr (is_invocable_v<decay_t<_Fun>, stop_token, decay_t<_Args>...>) {
  90. return thread(std::forward<_Fun>(__fun), __ss.get_token(), std::forward<_Args>(__args)...);
  91. } else {
  92. return thread(std::forward<_Fun>(__fun), std::forward<_Args>(__args)...);
  93. }
  94. }
  95. stop_source __stop_source_;
  96. thread __thread_;
  97. };
  98. _LIBCPP_END_NAMESPACE_STD
  99. #endif // _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_STOP_TOKEN)
  100. #endif // _LIBCPP___THREAD_JTHREAD_H