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_requested() const noexcept;
15 // Returns: true if *this has ownership of a stop state that has received a stop request; otherwise, false.
22 #include <type_traits>
24 #include "make_test_thread.h"
25 #include "test_macros.h"
28 concept IsStopRequestedNoexcept
= requires(const T
& t
) {
29 { t
.stop_requested() } noexcept
;
32 static_assert(IsStopRequestedNoexcept
<std::stop_token
>);
34 int main(int, char**) {
37 const std::stop_token st
;
38 assert(!st
.stop_requested());
44 const auto st
= ss
.get_token();
45 assert(!st
.stop_requested());
48 assert(st
.stop_requested());
51 // already requested before constructor
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
};
63 const auto st
= ss
->get_token();
66 assert(st
.stop_requested());
69 // single stop_source, multiple stop_token
72 const auto st1
= ss
.get_token();
73 const auto st2
= ss
.get_token();
74 assert(!st1
.stop_requested());
75 assert(!st2
.stop_requested());
78 assert(st1
.stop_requested());
79 assert(st2
.stop_requested());
82 // multiple stop_source, multiple stop_token
87 const auto st1
= ss1
.get_token();
88 const auto st2
= ss2
.get_token();
89 assert(!st1
.stop_requested());
90 assert(!st2
.stop_requested());
93 assert(st1
.stop_requested());
94 assert(!st2
.stop_requested());
100 const auto st
= ss
.get_token();
101 assert(!st
.stop_requested());
103 std::thread t
= support::make_test_thread([&]() { ss
.request_stop(); });
106 assert(st
.stop_requested());
109 // maybe concurrent calls
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();
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.
130 const auto st
= ss
.get_token();
131 assert(!st
.stop_requested());
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
141 auto b
= ss
.request_stop();
145 while (!st
.stop_requested()) {
146 std::this_thread::yield();
149 // write should be visible to the current thread
150 assert(flag
== true);