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