1 //===----------------------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: no-threads
10 // UNSUPPORTED: libcpp-has-no-experimental-stop_token
11 // UNSUPPORTED: c++03, c++11, c++14, c++17
12 // XFAIL: availability-synchronization_library-missing
14 // [[nodiscard]] bool stop_possible() const noexcept;
16 // - *this does not have ownership of a stop state, or
17 // - a stop request was not made and there are no associated stop_source objects;
24 #include <type_traits>
26 #include "make_test_thread.h"
27 #include "test_macros.h"
30 concept IsStopPossibleNoexcept
= requires(const T
& t
) {
31 { t
.stop_possible() } noexcept
;
34 static_assert(IsStopPossibleNoexcept
<std::stop_token
>);
36 int main(int, char**) {
39 const std::stop_token st
;
40 assert(!st
.stop_possible());
43 // a stop request was not made and there are no associated stop_source objects
45 std::optional
<std::stop_source
> ss
{std::in_place
};
46 const auto st
= ss
->get_token();
49 assert(!st
.stop_possible());
52 // a stop request was not made, but there is an associated stop_source objects
55 const auto st
= ss
.get_token();
56 assert(st
.stop_possible());
59 // a stop request was made and there are no associated stop_source objects
61 std::optional
<std::stop_source
> ss
{std::in_place
};
62 const auto st
= ss
->get_token();
66 assert(st
.stop_possible());
69 // a stop request was made and there is an associated stop_source objects
72 const auto st
= ss
.get_token();
74 assert(st
.stop_possible());
77 // a stop request was made on a different thread and
78 // there are no associated stop_source objects
80 std::optional
<std::stop_source
> ss
{std::in_place
};
81 const auto st
= ss
->get_token();
83 std::thread t
= support::make_test_thread([&]() {
88 assert(st
.stop_possible());
90 assert(st
.stop_possible());