| 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 |
// [[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. |
| 16 |
|
| 17 |
#include <cassert> |
| 18 |
#include <chrono> |
| 19 |
#include <concepts> |
| 20 |
#include <optional> |
| 21 |
#include <stop_token> |
| 22 |
#include <type_traits> |
| 23 |
|
| 24 |
#include "make_test_thread.h" |
| 25 |
#include "test_macros.h" |
| 26 |
|
| 27 |
template <class T> |
| 28 |
concept IsStopRequestedNoexcept = requires(const T& t) { |
| 29 |
{ t.stop_requested() } noexcept; |
| 30 |
}; |
| 31 |
|
| 32 |
static_assert(IsStopRequestedNoexcept<std::stop_token>); |
| 33 |
|
| 34 |
int main(int, char**) { |
| 35 |
// no state |
| 36 |
{ |
| 37 |
const std::stop_token st; |
| 38 |
assert(!st.stop_requested()); |
| 39 |
} |
| 40 |
|
| 41 |
// has state |
| 42 |
{ |
| 43 |
std::stop_source ss; |
| 44 |
const auto st = ss.get_token(); |
| 45 |
assert(!st.stop_requested()); |
| 46 |
|
| 47 |
ss.request_stop(); |
| 48 |
assert(st.stop_requested()); |
| 49 |
} |
| 50 |
|
| 51 |
// already requested before constructor |
| 52 |
{ |
| 53 |
std::stop_source ss; |
| 54 |
ss.request_stop(); |
| 55 |
const auto st = ss.get_token(); |
| 56 |
assert(st.stop_requested()); |
| 57 |
} |
| 58 |
|
| 59 |
// stop_token should share the state |
| 60 |
{ |
| 61 |
std::optional<std::stop_source> ss{std::in_place}; |
| 62 |
ss->request_stop(); |
| 63 |
const auto st = ss->get_token(); |
| 64 |
|
| 65 |
ss.reset(); |
| 66 |
assert(st.stop_requested()); |
| 67 |
} |
| 68 |
|
| 69 |
// single stop_source, multiple stop_token |
| 70 |
{ |
| 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()); |
| 76 |
|
| 77 |
ss.request_stop(); |
| 78 |
assert(st1.stop_requested()); |
| 79 |
assert(st2.stop_requested()); |
| 80 |
} |
| 81 |
|
| 82 |
// multiple stop_source, multiple stop_token |
| 83 |
{ |
| 84 |
std::stop_source ss1; |
| 85 |
std::stop_source ss2; |
| 86 |
|
| 87 |
const auto st1 = ss1.get_token(); |
| 88 |
const auto st2 = ss2.get_token(); |
| 89 |
assert(!st1.stop_requested()); |
| 90 |
assert(!st2.stop_requested()); |
| 91 |
|
| 92 |
ss1.request_stop(); |
| 93 |
assert(st1.stop_requested()); |
| 94 |
assert(!st2.stop_requested()); |
| 95 |
} |
| 96 |
|
| 97 |
// multiple threads |
| 98 |
{ |
| 99 |
std::stop_source ss; |
| 100 |
const auto st = ss.get_token(); |
| 101 |
assert(!st.stop_requested()); |
| 102 |
|
| 103 |
std::thread t = support::make_test_thread([&]() { ss.request_stop(); }); |
| 104 |
|
| 105 |
t.join(); |
| 106 |
assert(st.stop_requested()); |
| 107 |
} |
| 108 |
|
| 109 |
// maybe concurrent calls |
| 110 |
{ |
| 111 |
std::stop_source ss; |
| 112 |
const auto st = ss.get_token(); |
| 113 |
assert(!st.stop_requested()); |
| 114 |
|
| 115 |
std::thread t = support::make_test_thread([&]() { ss.request_stop(); }); |
| 116 |
|
| 117 |
while (!st.stop_requested()) { |
| 118 |
// should eventually exit the loop |
| 119 |
std::this_thread::yield(); |
| 120 |
} |
| 121 |
|
| 122 |
t.join(); |
| 123 |
} |
| 124 |
|
| 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. |
| 128 |
{ |
| 129 |
std::stop_source ss; |
| 130 |
const auto st = ss.get_token(); |
| 131 |
assert(!st.stop_requested()); |
| 132 |
|
| 133 |
bool flag = false; |
| 134 |
|
| 135 |
std::thread t = support::make_test_thread([&]() { |
| 136 |
using namespace std::chrono_literals; |
| 137 |
std::this_thread::sleep_for(1ms); |
| 138 |
|
| 139 |
// happens-before request_stop |
| 140 |
flag = true; |
| 141 |
auto b = ss.request_stop(); |
| 142 |
assert(b); |
| 143 |
}); |
| 144 |
|
| 145 |
while (!st.stop_requested()) { |
| 146 |
std::this_thread::yield(); |
| 147 |
} |
| 148 |
|
| 149 |
// write should be visible to the current thread |
| 150 |
assert(flag == true); |
| 151 |
|
| 152 |
t.join(); |
| 153 |
} |
| 154 |
|
| 155 |
return 0; |
| 156 |
} |