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