|
| 1 | +#include <format> |
| 2 | +#include <string_view> |
| 3 | + |
| 4 | +#include "CoroutineTests/alien/get_scheduler.hpp" |
| 5 | +#include "CoroutineTests/alien/schedule_on.hpp" |
| 6 | +#include "CoroutineTests/alien/sync_wait.hpp" |
| 7 | +#include "CoroutineTests/alien/tool.hpp" |
| 8 | +#include "CoroutineTests/threadpool.hpp" |
| 9 | +#include "logging_utils.hpp" |
| 10 | + |
| 11 | +using namespace CoroutineTests::alien; |
| 12 | +using scheduler_t = std::function<void(std::coroutine_handle<>)>; |
| 13 | +tool::Task<void> innermost_task(std::string_view parent) { |
| 14 | + const auto self = format_name(parent, "innermost_task"); |
| 15 | + log(self) << "Running innermost task" << std::endl; |
| 16 | + co_return; |
| 17 | +} |
| 18 | + |
| 19 | +tool::Task<void> inner_task(scheduler_t innermost_scheduler, |
| 20 | + std::string_view parent) { |
| 21 | + const auto self = format_name(parent, "inner_task"); |
| 22 | + log(self) << "Running inner task, scheduling innermost task on innermost " |
| 23 | + "scheduler" |
| 24 | + << std::endl; |
| 25 | + co_await schedule_on(innermost_scheduler, innermost_task(self)); |
| 26 | + log(self) << "Innermost task completed, resuming inner task" << std::endl; |
| 27 | + co_return; |
| 28 | +} |
| 29 | + |
| 30 | +tool::Task<void> outer_task(scheduler_t inner_scheduler, |
| 31 | + std::string_view parent) { |
| 32 | + const auto self = format_name(parent, "outer_task"); |
| 33 | + log(self) << "Running outer task" << std::endl; |
| 34 | + auto current_scheduler = co_await GetScheduler{}; |
| 35 | + log(self) << "Got current scheduler in outer task, scheduling inner task " |
| 36 | + "on inner scheduler" |
| 37 | + << std::endl; |
| 38 | + co_await schedule_on(inner_scheduler, inner_task(current_scheduler, self)); |
| 39 | + log(self) << "Inner task completed, resuming outer task" << std::endl; |
| 40 | + co_return; |
| 41 | +} |
| 42 | + |
| 43 | +int main() { |
| 44 | + log() << "Starting main, launching task..." << std::endl; |
| 45 | + auto threadpool_A = CoroutineTests::Threadpool(1); |
| 46 | + auto scheduler_A = [&](std::coroutine_handle<> h) { |
| 47 | + threadpool_A.enqueue_task(h); |
| 48 | + }; |
| 49 | + |
| 50 | + auto threadpool_B = CoroutineTests::Threadpool(1); |
| 51 | + auto scheduler_B = [&](std::coroutine_handle<> h) { |
| 52 | + threadpool_B.enqueue_task(h); |
| 53 | + }; |
| 54 | + |
| 55 | + sync_wait(scheduler_A, outer_task(scheduler_B, "main")); |
| 56 | + log() << "Finished waiting for task" << std::endl; |
| 57 | + return 0; |
| 58 | +} |
0 commit comments