[RISCV] Fix mgather -> riscv.masked.strided.load combine not extending indices (...
[llvm-project.git] / libcxx / test / std / thread / thread.stoptoken / stoptoken / stop_requested.pass.cpp
blob4dbc3137ef2098c3f40d9b5c8e4d4e5051f57bcb
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
14 // [[nodiscard]] bool stop_requested() const noexcept;
15 // Returns: true if *this has ownership of a stop state that has received a stop request; otherwise, false.
17 #include <cassert>
18 #include <chrono>
19 #include <concepts>
20 #include <optional>
21 #include <stop_token>
22 #include <type_traits>
24 #include "make_test_thread.h"
25 #include "test_macros.h"
27 template <class T>
28 concept IsStopRequestedNoexcept = requires(const T& t) {
29 { t.stop_requested() } noexcept;
32 static_assert(IsStopRequestedNoexcept<std::stop_token>);
34 int main(int, char**) {
35 // no state
37 const std::stop_token st;
38 assert(!st.stop_requested());
41 // has state
43 std::stop_source ss;
44 const auto st = ss.get_token();
45 assert(!st.stop_requested());
47 ss.request_stop();
48 assert(st.stop_requested());
51 // already requested before constructor
53 std::stop_source ss;
54 ss.request_stop();
55 const auto st = ss.get_token();
56 assert(st.stop_requested());
59 // stop_token should share the state
61 std::optional<std::stop_source> ss{std::in_place};
62 ss->request_stop();
63 const auto st = ss->get_token();
65 ss.reset();
66 assert(st.stop_requested());
69 // single stop_source, multiple stop_token
71 std::stop_source ss;
72 const auto st1 = ss.get_token();
73 const auto st2 = ss.get_token();
74 assert(!st1.stop_requested());
75 assert(!st2.stop_requested());
77 ss.request_stop();
78 assert(st1.stop_requested());
79 assert(st2.stop_requested());
82 // multiple stop_source, multiple stop_token
84 std::stop_source ss1;
85 std::stop_source ss2;
87 const auto st1 = ss1.get_token();
88 const auto st2 = ss2.get_token();
89 assert(!st1.stop_requested());
90 assert(!st2.stop_requested());
92 ss1.request_stop();
93 assert(st1.stop_requested());
94 assert(!st2.stop_requested());
97 // multiple threads
99 std::stop_source ss;
100 const auto st = ss.get_token();
101 assert(!st.stop_requested());
103 std::thread t = support::make_test_thread([&]() { ss.request_stop(); });
105 t.join();
106 assert(st.stop_requested());
109 // maybe concurrent calls
111 std::stop_source ss;
112 const auto st = ss.get_token();
113 assert(!st.stop_requested());
115 std::thread t = support::make_test_thread([&]() { ss.request_stop(); });
117 while (!st.stop_requested()) {
118 // should eventually exit the loop
119 std::this_thread::yield();
122 t.join();
125 // [thread.stoptoken.intro] A call to request_stop that returns true
126 // synchronizes with a call to stop_requested on an associated stop_token
127 // or stop_source object that returns true.
129 std::stop_source ss;
130 const auto st = ss.get_token();
131 assert(!st.stop_requested());
133 bool flag = false;
135 std::thread t = support::make_test_thread([&]() {
136 using namespace std::chrono_literals;
137 std::this_thread::sleep_for(1ms);
139 // happens-before request_stop
140 flag = true;
141 auto b = ss.request_stop();
142 assert(b);
145 while (!st.stop_requested()) {
146 std::this_thread::yield();
149 // write should be visible to the current thread
150 assert(flag == true);
152 t.join();
155 return 0;