stop_token.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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___STOP_TOKEN_STOP_TOKEN_H
  10. #define _LIBCPP___STOP_TOKEN_STOP_TOKEN_H
  11. #include <__availability>
  12. #include <__config>
  13. #include <__stop_token/intrusive_shared_ptr.h>
  14. #include <__stop_token/stop_state.h>
  15. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  16. # pragma GCC system_header
  17. #endif
  18. _LIBCPP_BEGIN_NAMESPACE_STD
  19. #if _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_STOP_TOKEN) && !defined(_LIBCPP_HAS_NO_THREADS)
  20. class _LIBCPP_AVAILABILITY_SYNC stop_token {
  21. public:
  22. _LIBCPP_HIDE_FROM_ABI stop_token() noexcept = default;
  23. _LIBCPP_HIDE_FROM_ABI stop_token(const stop_token&) noexcept = default;
  24. _LIBCPP_HIDE_FROM_ABI stop_token(stop_token&&) noexcept = default;
  25. _LIBCPP_HIDE_FROM_ABI stop_token& operator=(const stop_token&) noexcept = default;
  26. _LIBCPP_HIDE_FROM_ABI stop_token& operator=(stop_token&&) noexcept = default;
  27. _LIBCPP_HIDE_FROM_ABI ~stop_token() = default;
  28. _LIBCPP_HIDE_FROM_ABI void swap(stop_token& __other) noexcept { __state_.swap(__other.__state_); }
  29. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI bool stop_requested() const noexcept {
  30. return __state_ != nullptr && __state_->__stop_requested();
  31. }
  32. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI bool stop_possible() const noexcept {
  33. return __state_ != nullptr && __state_->__stop_possible_for_stop_token();
  34. }
  35. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI friend bool operator==(const stop_token&, const stop_token&) noexcept = default;
  36. _LIBCPP_HIDE_FROM_ABI friend void swap(stop_token& __lhs, stop_token& __rhs) noexcept { __lhs.swap(__rhs); }
  37. private:
  38. __intrusive_shared_ptr<__stop_state> __state_;
  39. friend class stop_source;
  40. template <class _Tp>
  41. friend class stop_callback;
  42. _LIBCPP_HIDE_FROM_ABI explicit stop_token(const __intrusive_shared_ptr<__stop_state>& __state) : __state_(__state) {}
  43. };
  44. #endif // _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_STOP_TOKEN) && !defined(_LIBCPP_HAS_NO_THREADS)
  45. _LIBCPP_END_NAMESPACE_STD
  46. #endif // _LIBCPP___STOP_TOKEN_STOP_TOKEN_H