CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
237 lines
7.0 KiB
C++
237 lines
7.0 KiB
C++
//===----------------------------------------------------------------------===//
|
|
//
|
|
// 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: 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 <hip/std/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<hip::wthread> 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<hip::wthread> 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;
|
|
hip::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;
|
|
}
|