1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
|
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// UNSUPPORTED: no-threads
// UNSUPPORTED: libcpp-has-no-experimental-stop_token
// UNSUPPORTED: c++03, c++11, c++14, c++17
// XFAIL: availability-synchronization_library-missing
// template<class C>
// explicit stop_callback(const stop_token& st, C&& cb)
// noexcept(is_nothrow_constructible_v<Callback, C>);
#include <atomic>
#include <cassert>
#include <chrono>
#include <stop_token>
#include <type_traits>
#include <utility>
#include <vector>
#include "make_test_thread.h"
#include "test_macros.h"
struct Cb {
void operator()() const;
};
// Constraints: Callback and C satisfy constructible_from<Callback, C>.
static_assert(std::is_constructible_v<std::stop_callback<void (*)()>, const std::stop_token&, void (*)()>);
static_assert(!std::is_constructible_v<std::stop_callback<void (*)()>, const std::stop_token&, void (*)(int)>);
static_assert(std::is_constructible_v<std::stop_callback<Cb>, const std::stop_token&, Cb&>);
static_assert(std::is_constructible_v<std::stop_callback<Cb&>, const std::stop_token&, Cb&>);
static_assert(!std::is_constructible_v<std::stop_callback<Cb>, const std::stop_token&, int>);
// explicit
template <class T>
void conversion_test(T);
template <class T, class... Args>
concept ImplicitlyConstructible = requires(Args&&... args) { conversion_test<T>({std::forward<Args>(args)...}); };
static_assert(ImplicitlyConstructible<int, int>);
static_assert(!ImplicitlyConstructible<std::stop_callback<Cb>, const std::stop_token&, Cb>);
// noexcept
template <bool NoExceptCtor>
struct CbNoExcept {
CbNoExcept(int) noexcept(NoExceptCtor);
void operator()() const;
};
static_assert(std::is_nothrow_constructible_v<std::stop_callback<CbNoExcept<true>>, const std::stop_token&, int>);
static_assert(!std::is_nothrow_constructible_v<std::stop_callback<CbNoExcept<false>>, const std::stop_token&, int>);
int main(int, char**) {
// was requested
{
std::stop_source ss;
const std::stop_token st = ss.get_token();
ss.request_stop();
bool called = false;
std::stop_callback sc(st, [&] { called = true; });
assert(called);
}
// was not requested
{
std::stop_source ss;
const std::stop_token st = ss.get_token();
bool called = false;
std::stop_callback sc(st, [&] { called = true; });
assert(!called);
ss.request_stop();
assert(called);
}
// token has no state
{
std::stop_token st;
bool called = false;
std::stop_callback sc(st, [&] { called = true; });
assert(!called);
}
// should not be called multiple times
{
std::stop_source ss;
const std::stop_token st = ss.get_token();
int calledTimes = 0;
std::stop_callback sc(st, [&] { ++calledTimes; });
std::vector<std::thread> threads;
for (auto i = 0; i < 10; ++i) {
threads.emplace_back(support::make_test_thread([&] { ss.request_stop(); }));
}
for (auto& thread : threads) {
thread.join();
}
assert(calledTimes == 1);
}
// adding more callbacks during invoking other callbacks
{
std::stop_source ss;
const std::stop_token st = ss.get_token();
std::atomic<bool> startedFlag = false;
std::atomic<bool> finishFlag = false;
std::stop_callback sc(st, [&] {
startedFlag = true;
startedFlag.notify_all();
finishFlag.wait(false);
});
auto thread = support::make_test_thread([&] { ss.request_stop(); });
startedFlag.wait(false);
// first callback is still running, adding another one;
bool secondCallbackCalled = false;
std::stop_callback sc2(st, [&] { secondCallbackCalled = true; });
finishFlag = true;
finishFlag.notify_all();
thread.join();
assert(secondCallbackCalled);
}
// adding callbacks on different threads
{
std::stop_source ss;
const std::stop_token st = ss.get_token();
std::vector<std::thread> threads;
std::atomic<int> callbackCalledTimes = 0;
std::atomic<bool> done = false;
for (auto i = 0; i < 10; ++i) {
threads.emplace_back(support::make_test_thread([&] {
std::stop_callback sc{st, [&] { callbackCalledTimes.fetch_add(1, std::memory_order_relaxed); }};
done.wait(false);
}));
}
using namespace std::chrono_literals;
std::this_thread::sleep_for(1ms);
ss.request_stop();
done = true;
done.notify_all();
for (auto& thread : threads) {
thread.join();
}
assert(callbackCalledTimes.load(std::memory_order_relaxed) == 10);
}
// correct overload
{
struct CBWithTracking {
bool& lvalueCalled;
bool& lvalueConstCalled;
bool& rvalueCalled;
bool& rvalueConstCalled;
void operator()() & { lvalueCalled = true; }
void operator()() const& { lvalueConstCalled = true; }
void operator()() && { rvalueCalled = true; }
void operator()() const&& { rvalueConstCalled = true; }
};
// RValue
{
bool lvalueCalled = false;
bool lvalueConstCalled = false;
bool rvalueCalled = false;
bool rvalueConstCalled = false;
std::stop_source ss;
const std::stop_token st = ss.get_token();
ss.request_stop();
std::stop_callback<CBWithTracking> sc(
st, CBWithTracking{lvalueCalled, lvalueConstCalled, rvalueCalled, rvalueConstCalled});
assert(rvalueCalled);
}
// RValue
{
bool lvalueCalled = false;
bool lvalueConstCalled = false;
bool rvalueCalled = false;
bool rvalueConstCalled = false;
std::stop_source ss;
const std::stop_token st = ss.get_token();
ss.request_stop();
std::stop_callback<const CBWithTracking> sc(
st, CBWithTracking{lvalueCalled, lvalueConstCalled, rvalueCalled, rvalueConstCalled});
assert(rvalueConstCalled);
}
// LValue
{
bool lvalueCalled = false;
bool lvalueConstCalled = false;
bool rvalueCalled = false;
bool rvalueConstCalled = false;
std::stop_source ss;
const std::stop_token st = ss.get_token();
ss.request_stop();
CBWithTracking cb{lvalueCalled, lvalueConstCalled, rvalueCalled, rvalueConstCalled};
std::stop_callback<CBWithTracking&> sc(st, cb);
assert(lvalueCalled);
}
// const LValue
{
bool lvalueCalled = false;
bool lvalueConstCalled = false;
bool rvalueCalled = false;
bool rvalueConstCalled = false;
std::stop_source ss;
const std::stop_token st = ss.get_token();
ss.request_stop();
CBWithTracking cb{lvalueCalled, lvalueConstCalled, rvalueCalled, rvalueConstCalled};
std::stop_callback<const CBWithTracking&> sc(st, cb);
assert(lvalueConstCalled);
}
}
return 0;
}
|