31#include <gtest/gtest.h>
39#if ALEPH_HAS_EXPERIMENTAL_ASYNC
46#if ALEPH_HAS_EXPERIMENTAL_ASYNC
54 detached_task get_return_object()
57 std::coroutine_handle<promise_type>::from_promise(*
this));
60 std::suspend_never initial_suspend() noexcept {
return {}; }
61 std::suspend_always final_suspend() noexcept {
return {}; }
62 void return_void() noexcept {}
63 void unhandled_exception() { std::terminate(); }
67 std::coroutine_handle<promise_type> handle_;
69 explicit detached_task(std::coroutine_handle<promise_type> handle) noexcept
73 detached_task(
const detached_task &) =
delete;
74 detached_task & operator = (
const detached_task &) =
delete;
76 detached_task(detached_task && other) noexcept
77 : handle_(other.handle_)
82 detached_task & operator = (detached_task && other)
noexcept
88 handle_ = other.handle_;
100 template <
typename T>
103 std::future<T> future_;
108 std::promise<T> promise_;
110 sync_task get_return_object()
112 return sync_task(promise_.get_future(),
113 std::coroutine_handle<promise_type>::from_promise(*
this));
116 std::suspend_never initial_suspend() noexcept {
return {}; }
117 std::suspend_always final_suspend() noexcept {
return {}; }
119 void return_value(
T value)
121 promise_.set_value(std::move(
value));
124 void unhandled_exception()
126 promise_.set_exception(std::current_exception());
131 std::coroutine_handle<promise_type> handle_;
133 sync_task(std::future<T> future,
134 std::coroutine_handle<promise_type> handle) noexcept
135 : future_(std::move(future)), handle_(handle) {}
138 sync_task(
const sync_task &) =
delete;
139 sync_task & operator = (
const sync_task &) =
delete;
141 sync_task(sync_task && other) noexcept
142 : future_(std::move(other.future_)), handle_(other.handle_)
147 sync_task & operator = (sync_task && other)
noexcept
153 future_ = std::move(other.future_);
154 handle_ = other.handle_;
167 return future_.get();
173 const int first =
co_await experimental::schedule(pool, [] {
return 20; });
174 const int second =
co_await experimental::schedule(pool, [first] {
180 detached_task await_and_mark(
ThreadPool & pool,
181 std::shared_future<void> gate,
182 std::promise<void> started,
183 std::atomic<bool> & resumed)
185 co_await experimental::schedule(pool, [gate, started = std::move(started)]()
mutable {
190 resumed.store(
true, std::memory_order_release);
194TEST(ExperimentalAsync, ScheduleGetReturnsValue)
197 auto op = experimental::schedule(pool, [] {
return 42; });
199 EXPECT_EQ(op.get(), 42);
202TEST(ExperimentalAsync, SchedulePropagatesExceptions)
205 auto op = experimental::schedule(pool, []() ->
int {
206 throw std::runtime_error(
"boom");
209 EXPECT_THROW(op.get(), std::runtime_error);
212TEST(ExperimentalAsync, ScheduleSupportsMoveOnlyResults)
215 auto op = experimental::schedule(pool, [] {
216 return std::make_unique<int>(17);
219 auto value = op.get();
220 ASSERT_TRUE(
value !=
nullptr);
221 EXPECT_EQ(*
value, 17);
224TEST(ExperimentalAsync, ScheduleCanBeAwaitedInsideCoroutine)
228 EXPECT_EQ(chained_sum(pool).get(), 42);
231TEST(ExperimentalAsync, ScheduleHasSingleConsumerSemantics)
234 auto op = experimental::schedule(pool, [] {
return 5; });
236 EXPECT_TRUE(op.valid());
237 EXPECT_EQ(op.get(), 5);
238 EXPECT_FALSE(op.valid());
239 EXPECT_THROW((
void) op.get(), std::runtime_error);
242TEST(ExperimentalAsync, ScheduleVoidInvalidAfterConsumption)
245 auto op = experimental::schedule(pool, [] {});
247 EXPECT_TRUE(op.valid());
249 EXPECT_FALSE(op.valid());
250 EXPECT_THROW(op.get(), std::runtime_error);
253TEST(ExperimentalAsync, DestroyedAwaiterUnregistersContinuation)
256 std::promise<void> gate_promise;
257 auto gate = gate_promise.get_future().share();
258 std::promise<void> started;
259 auto started_future = started.get_future();
260 std::atomic<bool> resumed{
false};
263 auto task = await_and_mark(pool, gate, std::move(started), resumed);
264 started_future.wait();
267 gate_promise.set_value();
268 std::this_thread::sleep_for(std::chrono::milliseconds(20));
270 EXPECT_FALSE(resumed.load(std::memory_order_acquire));
273TEST(ExperimentalAsync, DisabledWithoutOptIn)
size_t size_t int32_t value
A reusable thread pool for efficient parallel task execution.
Opt-in experimental coroutine-friendly bridge over ThreadPool.
#define ALEPH_HAS_EXPERIMENTAL_ASYNC
Main namespace for Aleph-w library functions.
std::decay_t< typename HeadC::Item_Type > T
A modern, efficient thread pool for parallel task execution.