From b10f2509455f75ed1321be03075faf56a70a3454 Mon Sep 17 00:00:00 2001 From: Patrick Wuttke Date: Sun, 23 Jun 2024 12:19:57 +0200 Subject: [PATCH] Added simple (albeit in many cases suboptiomal) c_sleep() method for coroutines. --- source/mijin/async/coroutine_sleep.hpp | 27 ++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 source/mijin/async/coroutine_sleep.hpp diff --git a/source/mijin/async/coroutine_sleep.hpp b/source/mijin/async/coroutine_sleep.hpp new file mode 100644 index 0000000..829e9e7 --- /dev/null +++ b/source/mijin/async/coroutine_sleep.hpp @@ -0,0 +1,27 @@ + + +#pragma once + +#if !defined(MIJIN_ASYNC_COROUTINE_SLEEP_HPP_INCLUDED) +#define MIJIN_ASYNC_COROUTINE_SLEEP_HPP_INCLUDED 1 + +#include +#include "./coroutine.hpp" + +namespace mijin +{ +template +Task<> c_sleep(std::chrono::duration duration) +{ + auto now = std::chrono::high_resolution_clock::now(); + const auto end = now + duration; + while (now < end) + { + co_await c_suspend(); + now = std::chrono::high_resolution_clock::now(); + } + co_return; +} +} // namespace mijin + +#endif // !defined(MIJIN_ASYNC_COROUTINE_SLEEP_HPP_INCLUDED)