From 6af5b95a45bebfbcf87549d1bb593f1adf629c31 Mon Sep 17 00:00:00 2001 From: uchenily Date: Wed, 8 May 2024 09:16:29 +0800 Subject: [PATCH] runtime: spawn --- tests/meson.build | 1 + tests/test_spawn.cpp | 32 ++++++++++++++++++++++++++++++++ uvio/runtime.hpp | 7 +++++++ 3 files changed, 40 insertions(+) create mode 100644 tests/test_spawn.cpp diff --git a/tests/meson.build b/tests/meson.build index 1625b50..b540e9a 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -4,6 +4,7 @@ all_tests_sources = [ 'test_task.cpp', 'test_timer.cpp', 'coro_timer.cpp', + 'test_spawn.cpp', ] foreach source: all_tests_sources diff --git a/tests/test_spawn.cpp b/tests/test_spawn.cpp new file mode 100644 index 0000000..0a603dd --- /dev/null +++ b/tests/test_spawn.cpp @@ -0,0 +1,32 @@ +#include "uvio/log.hpp" +#include "uvio/runtime.hpp" +#include "uvio/task.hpp" +#include "uvio/time/sleep.hpp" + +using namespace uvio; +using namespace uvio::log; +using namespace uvio::time; + +auto task1() -> Task<> { + for (int i = 0; i < 6; i++) { + co_await sleep(1s); + console.warn("task1 i={}", i); + } +} + +auto task2() -> Task<> { + for (int i = 0; i < 3; i++) { + co_await sleep(2s); + console.info("task2 i={}", i); + } +} + +auto test() -> Task<> { + spawn(task1()); + spawn(task2()); + co_return; +} + +auto main() -> int { + block_on(test()); +} diff --git a/uvio/runtime.hpp b/uvio/runtime.hpp index 24f51f6..766a9df 100644 --- a/uvio/runtime.hpp +++ b/uvio/runtime.hpp @@ -26,4 +26,11 @@ static inline auto block_on(Task<> &&first_coro) { console.debug("loop end."); } +static inline auto spawn(Task<> &&task) { + auto handle = task.take(); + console.debug("spawn task ..."); + handle.resume(); + console.debug("spawn end."); +} + } // namespace uvio