xref: /llvm-project/libcxx/test/std/thread/thread.stoptoken/stoptoken/stop_possible.pass.cpp (revision 121ed5c1985356436d0040dbe81bca26992b1fae)
1*b77e50e6SHui //===----------------------------------------------------------------------===//
2*b77e50e6SHui //
3*b77e50e6SHui // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*b77e50e6SHui // See https://llvm.org/LICENSE.txt for license information.
5*b77e50e6SHui // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*b77e50e6SHui //
7*b77e50e6SHui //===----------------------------------------------------------------------===//
8*b77e50e6SHui //
9*b77e50e6SHui // UNSUPPORTED: no-threads
10*b77e50e6SHui // UNSUPPORTED: c++03, c++11, c++14, c++17
11*b77e50e6SHui // XFAIL: availability-synchronization_library-missing
12*b77e50e6SHui 
13*b77e50e6SHui // [[nodiscard]] bool stop_possible() const noexcept;
14*b77e50e6SHui // Returns: false if:
15*b77e50e6SHui //    - *this does not have ownership of a stop state, or
16*b77e50e6SHui //    - a stop request was not made and there are no associated stop_source objects;
17*b77e50e6SHui // otherwise, true.
18*b77e50e6SHui 
19*b77e50e6SHui #include <cassert>
20*b77e50e6SHui #include <concepts>
21*b77e50e6SHui #include <optional>
22*b77e50e6SHui #include <stop_token>
23*b77e50e6SHui #include <type_traits>
24*b77e50e6SHui 
25*b77e50e6SHui #include "make_test_thread.h"
26*b77e50e6SHui #include "test_macros.h"
27*b77e50e6SHui 
28*b77e50e6SHui template <class T>
29*b77e50e6SHui concept IsStopPossibleNoexcept = requires(const T& t) {
30*b77e50e6SHui   { t.stop_possible() } noexcept;
31*b77e50e6SHui };
32*b77e50e6SHui 
33*b77e50e6SHui static_assert(IsStopPossibleNoexcept<std::stop_token>);
34*b77e50e6SHui 
35*b77e50e6SHui int main(int, char**) {
36*b77e50e6SHui   // no state
37*b77e50e6SHui   {
38*b77e50e6SHui     const std::stop_token st;
39*b77e50e6SHui     assert(!st.stop_possible());
40*b77e50e6SHui   }
41*b77e50e6SHui 
42*b77e50e6SHui   // a stop request was not made and there are no associated stop_source objects
43*b77e50e6SHui   {
44*b77e50e6SHui     std::optional<std::stop_source> ss{std::in_place};
45*b77e50e6SHui     const auto st = ss->get_token();
46*b77e50e6SHui     ss.reset();
47*b77e50e6SHui 
48*b77e50e6SHui     assert(!st.stop_possible());
49*b77e50e6SHui   }
50*b77e50e6SHui 
51*b77e50e6SHui   // a stop request was not made, but there is an associated stop_source objects
52*b77e50e6SHui   {
53*b77e50e6SHui     std::stop_source ss;
54*b77e50e6SHui     const auto st = ss.get_token();
55*b77e50e6SHui     assert(st.stop_possible());
56*b77e50e6SHui   }
57*b77e50e6SHui 
58*b77e50e6SHui   // a stop request was made and there are no associated stop_source objects
59*b77e50e6SHui   {
60*b77e50e6SHui     std::optional<std::stop_source> ss{std::in_place};
61*b77e50e6SHui     const auto st = ss->get_token();
62*b77e50e6SHui     ss->request_stop();
63*b77e50e6SHui     ss.reset();
64*b77e50e6SHui 
65*b77e50e6SHui     assert(st.stop_possible());
66*b77e50e6SHui   }
67*b77e50e6SHui 
68*b77e50e6SHui   // a stop request was made and there is an associated stop_source objects
69*b77e50e6SHui   {
70*b77e50e6SHui     std::stop_source ss;
71*b77e50e6SHui     const auto st = ss.get_token();
72*b77e50e6SHui     ss.request_stop();
73*b77e50e6SHui     assert(st.stop_possible());
74*b77e50e6SHui   }
75*b77e50e6SHui 
76*b77e50e6SHui   // a stop request was made on a different thread and
77*b77e50e6SHui   // there are no associated stop_source objects
78*b77e50e6SHui   {
79*b77e50e6SHui     std::optional<std::stop_source> ss{std::in_place};
80*b77e50e6SHui     const auto st = ss->get_token();
81*b77e50e6SHui 
82*b77e50e6SHui     std::thread t = support::make_test_thread([&]() {
83*b77e50e6SHui       ss->request_stop();
84*b77e50e6SHui       ss.reset();
85*b77e50e6SHui     });
86*b77e50e6SHui 
87*b77e50e6SHui     assert(st.stop_possible());
88*b77e50e6SHui     t.join();
89*b77e50e6SHui     assert(st.stop_possible());
90*b77e50e6SHui 
91*b77e50e6SHui   }
92*b77e50e6SHui 
93*b77e50e6SHui   return 0;
94*b77e50e6SHui }
95