2 //===----------------------------------------------------------------------===//
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
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP___CXX03___STOP_TOKEN_STOP_SOURCE_H
11 #define _LIBCPP___CXX03___STOP_TOKEN_STOP_SOURCE_H
13 #include <__cxx03/__config>
14 #include <__cxx03/__stop_token/intrusive_shared_ptr.h>
15 #include <__cxx03/__stop_token/stop_state.h>
16 #include <__cxx03/__stop_token/stop_token.h>
17 #include <__cxx03/__utility/move.h>
19 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
20 # pragma GCC system_header
23 _LIBCPP_BEGIN_NAMESPACE_STD
25 #if _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_STOP_TOKEN) && !defined(_LIBCPP_HAS_NO_THREADS)
27 struct nostopstate_t
{
28 explicit nostopstate_t() = default;
31 inline constexpr nostopstate_t nostopstate
{};
33 class _LIBCPP_AVAILABILITY_SYNC stop_source
{
35 _LIBCPP_HIDE_FROM_ABI
stop_source() : __state_(new __stop_state()) { __state_
->__increment_stop_source_counter(); }
37 _LIBCPP_HIDE_FROM_ABI
explicit stop_source(nostopstate_t
) noexcept
: __state_(nullptr) {}
39 _LIBCPP_HIDE_FROM_ABI
stop_source(const stop_source
& __other
) noexcept
: __state_(__other
.__state_
) {
41 __state_
->__increment_stop_source_counter();
45 _LIBCPP_HIDE_FROM_ABI
stop_source(stop_source
&& __other
) noexcept
= default;
47 _LIBCPP_HIDE_FROM_ABI stop_source
& operator=(const stop_source
& __other
) noexcept
{
48 // increment `__other` first so that we don't hit 0 in case of self-assignment
49 if (__other
.__state_
) {
50 __other
.__state_
->__increment_stop_source_counter();
53 __state_
->__decrement_stop_source_counter();
55 __state_
= __other
.__state_
;
59 _LIBCPP_HIDE_FROM_ABI stop_source
& operator=(stop_source
&&) noexcept
= default;
61 _LIBCPP_HIDE_FROM_ABI
~stop_source() {
63 __state_
->__decrement_stop_source_counter();
67 _LIBCPP_HIDE_FROM_ABI
void swap(stop_source
& __other
) noexcept
{ __state_
.swap(__other
.__state_
); }
69 [[nodiscard
]] _LIBCPP_HIDE_FROM_ABI stop_token
get_token() const noexcept
{ return stop_token(__state_
); }
71 [[nodiscard
]] _LIBCPP_HIDE_FROM_ABI
bool stop_possible() const noexcept
{ return __state_
!= nullptr; }
73 [[nodiscard
]] _LIBCPP_HIDE_FROM_ABI
bool stop_requested() const noexcept
{
74 return __state_
!= nullptr && __state_
->__stop_requested();
77 _LIBCPP_HIDE_FROM_ABI
bool request_stop() noexcept
{ return __state_
&& __state_
->__request_stop(); }
79 [[nodiscard
]] _LIBCPP_HIDE_FROM_ABI
friend bool operator==(const stop_source
&, const stop_source
&) noexcept
= default;
81 _LIBCPP_HIDE_FROM_ABI
friend void swap(stop_source
& __lhs
, stop_source
& __rhs
) noexcept
{ __lhs
.swap(__rhs
); }
84 __intrusive_shared_ptr
<__stop_state
> __state_
;
87 #endif // _LIBCPP_STD_VER >= 20
89 _LIBCPP_END_NAMESPACE_STD
91 #endif // _LIBCPP___CXX03___STOP_TOKEN_STOP_SOURCE_H