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