xref: /llvm-project/libcxx/test/std/thread/thread.stoptoken/stopsource/request_stop.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 // bool request_stop() noexcept;
14*b77e50e6SHui 
15*b77e50e6SHui #include <cassert>
16*b77e50e6SHui #include <chrono>
17*b77e50e6SHui #include <concepts>
18*b77e50e6SHui #include <optional>
19*b77e50e6SHui #include <stop_token>
20*b77e50e6SHui #include <type_traits>
21*b77e50e6SHui 
22*b77e50e6SHui #include "make_test_thread.h"
23*b77e50e6SHui #include "test_macros.h"
24*b77e50e6SHui 
25*b77e50e6SHui template <class T>
26*b77e50e6SHui concept IsRequestStopNoexcept = requires(T& t) {
27*b77e50e6SHui   { t.request_stop() } noexcept;
28*b77e50e6SHui };
29*b77e50e6SHui 
30*b77e50e6SHui static_assert(IsRequestStopNoexcept<std::stop_source>);
31*b77e50e6SHui 
32*b77e50e6SHui int main(int, char**) {
33*b77e50e6SHui   // If *this does not have ownership of a stop state, returns false
34*b77e50e6SHui   {
35*b77e50e6SHui     std::stop_source ss{std::nostopstate};
36*b77e50e6SHui     auto ret = ss.request_stop();
37*b77e50e6SHui     assert(!ret);
38*b77e50e6SHui     assert(!ss.stop_requested());
39*b77e50e6SHui   }
40*b77e50e6SHui 
41*b77e50e6SHui   // Otherwise, atomically determines whether the owned stop state has received
42*b77e50e6SHui   // a stop request, and if not, makes a stop request
43*b77e50e6SHui   {
44*b77e50e6SHui     std::stop_source ss;
45*b77e50e6SHui 
46*b77e50e6SHui     auto ret = ss.request_stop();
47*b77e50e6SHui     assert(ret);
48*b77e50e6SHui     assert(ss.stop_requested());
49*b77e50e6SHui   }
50*b77e50e6SHui 
51*b77e50e6SHui   // already requested
52*b77e50e6SHui   {
53*b77e50e6SHui     std::stop_source ss;
54*b77e50e6SHui     ss.request_stop();
55*b77e50e6SHui     assert(ss.stop_requested());
56*b77e50e6SHui 
57*b77e50e6SHui     auto ret = ss.request_stop();
58*b77e50e6SHui     assert(!ret);
59*b77e50e6SHui     assert(ss.stop_requested());
60*b77e50e6SHui   }
61*b77e50e6SHui 
62*b77e50e6SHui   // If the request was made, the callbacks registered by
63*b77e50e6SHui   // associated stop_callback objects are synchronously called.
64*b77e50e6SHui   {
65*b77e50e6SHui     std::stop_source ss;
66*b77e50e6SHui     auto st = ss.get_token();
67*b77e50e6SHui 
68*b77e50e6SHui     bool cb1Called = false;
69*b77e50e6SHui     bool cb2Called = false;
70*b77e50e6SHui     std::stop_callback sc1(st, [&] { cb1Called = true; });
71*b77e50e6SHui     std::stop_callback sc2(st, [&] { cb2Called = true; });
72*b77e50e6SHui 
73*b77e50e6SHui     ss.request_stop();
74*b77e50e6SHui     assert(cb1Called);
75*b77e50e6SHui     assert(cb2Called);
76*b77e50e6SHui   }
77*b77e50e6SHui 
78*b77e50e6SHui   return 0;
79*b77e50e6SHui }
80