Skip to content

Commit

Permalink
runtime: spawn
Browse files Browse the repository at this point in the history
  • Loading branch information
uchenily committed May 8, 2024
1 parent 403a35a commit 6af5b95
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ all_tests_sources = [
'test_task.cpp',
'test_timer.cpp',
'coro_timer.cpp',
'test_spawn.cpp',
]

foreach source: all_tests_sources
Expand Down
32 changes: 32 additions & 0 deletions tests/test_spawn.cpp
Original file line number Diff line number Diff line change
@@ -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());
}
7 changes: 7 additions & 0 deletions uvio/runtime.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 6af5b95

Please sign in to comment.