From bd9f2416182eb2f854e7b846d0186c64c190b4c9 Mon Sep 17 00:00:00 2001 From: saipubw Date: Thu, 7 Nov 2024 16:41:06 +0800 Subject: [PATCH] rename to directlyStart --- async_simple/coro/Lazy.h | 5 +++-- async_simple/coro/test/LazyTest.cpp | 2 +- async_simple/uthread/Await.h | 2 +- docs/docs.cn/Executor.md | 2 +- docs/docs.cn/Lazy.md | 24 +++++++++++++++++++++++- docs/docs.en/Executor.md | 2 +- docs/docs.en/Lazy.md | 24 +++++++++++++++++++++++- 7 files changed, 53 insertions(+), 8 deletions(-) diff --git a/async_simple/coro/Lazy.h b/async_simple/coro/Lazy.h index 65b0a4b71..13cd008af 100644 --- a/async_simple/coro/Lazy.h +++ b/async_simple/coro/Lazy.h @@ -518,9 +518,10 @@ class [[nodiscard]] CORO_ONLY_DESTROY_WHEN_DONE ELIDEABLE_AFTER_AWAIT Lazy using Base::start; + // Bind an executor and start coroutine without scheduling immediately. template - void start(F&& callback, - Executor* executor) requires(std::is_invocable_v>) { + void directlyStart(F&& callback, Executor* executor) requires( + std::is_invocable_v>) { this->_coro.promise()._executor = executor; return start(std::forward(callback)); } diff --git a/async_simple/coro/test/LazyTest.cpp b/async_simple/coro/test/LazyTest.cpp index f45118197..b975c3529 100644 --- a/async_simple/coro/test/LazyTest.cpp +++ b/async_simple/coro/test/LazyTest.cpp @@ -269,7 +269,7 @@ TEST_F(LazyTest, testStartWithExecutor) { co_return; }; std::promise f; - test().start([&f](auto&&) { f.set_value(); }, &_executor); + test().directlyStart([&f](auto&&) { f.set_value(); }, &_executor); f.get_future().wait(); } diff --git a/async_simple/uthread/Await.h b/async_simple/uthread/Await.h index 695a58cb0..27dc126a6 100644 --- a/async_simple/uthread/Await.h +++ b/async_simple/uthread/Await.h @@ -98,7 +98,7 @@ decltype(auto) await(Executor* ex, Fn&& fn, Args&&... args) requires co_return; }; lazy(std::forward(fn), std::forward(args)...) - .start([](auto&&) {}, ex); + .directlyStart([](auto&&) {}, ex); return await(std::move(f)); } diff --git a/docs/docs.cn/Executor.md b/docs/docs.cn/Executor.md index 06537bc3a..bc5100aa5 100644 --- a/docs/docs.cn/Executor.md +++ b/docs/docs.cn/Executor.md @@ -4,7 +4,7 @@ Executor是调度协程的关键组件。绝大多数开源协程框架提供内 ## 使用Executor -让协程运行在指定的调度器中非常简单,只需要创建协程时传递Executor给协程即可。在Lazy中通过`via()`可以传递Executor。或者可以在start()接口中传递executor实现惰性调度,也可以在Uthread中设置`async()`的Executor参数传递。 +让协程运行在指定的调度器中非常简单,只需要创建协程时传递Executor给协程即可。在Lazy中通过`via()`可以传递Executor。或者可以使用`directlyStart(callback, executor)`接口启动协程并惰性调度,也可以在Uthread中设置`async()`的Executor参数传递。 ```cpp Executor e; diff --git a/docs/docs.cn/Lazy.md b/docs/docs.cn/Lazy.md index 2cd131b18..6f2843cdf 100644 --- a/docs/docs.cn/Lazy.md +++ b/docs/docs.cn/Lazy.md @@ -30,7 +30,7 @@ Lazy task2(int x) { ## 启动方式 -一个 Lazy 应该以 `co_await`、 `syncAwait` 以及 `.start(callback)` 方式启动。 +一个 Lazy 应该以 `co_await`、 `syncAwait`, `.start(callback)` 以及 `directlyStart(callback, executor)`方式启动。 ### co_await 启动 @@ -93,6 +93,28 @@ void func() { task().start([](auto&&){}); ``` +### directlyStart(callback, executor) 启动 + +和`start`类似,但是提供了调度器接口,用于在启动协程时绑定调度器。需要注意的是,`directlyStart`不会在启动时立即调度协程。 + +```cpp +Lazy<> task() { + + auto e = co_await currentExecutor{}; + // 已经成功绑定调度器 + assert(e!=nullptr); + // 惰性调度,此时任务还未被提交给调度器运行 + assert(e.currentThreadInExecutor()==false); + co_await coro::Sleep(1s); + // Sleep函数需要使用调度器,因此任务已被提交给调度器运行。 + assert(e.currentThreadInExecutor()==true); +} +void func() { + auto executor=std::make_shared(1); + task().directlyStart([executor](Try Result){},executor.get()); +} +``` + ### syncAwait 启动 例如: diff --git a/docs/docs.en/Executor.md b/docs/docs.en/Executor.md index ff55e785f..4cdb68cc4 100644 --- a/docs/docs.en/Executor.md +++ b/docs/docs.en/Executor.md @@ -4,7 +4,7 @@ Executor is the key component for shceduling coroutine. A lot of the open-source ### Use Executor -It is easy to assign a coroutine instance to an executor. The user need to pass the executor to coroutine only. The users could assign an executor to a Lazy then schedule immediately by `via`. User could also use start(cb, executor) to assign an executor wihtout schedul immediately. They could use `async` to pass Executor to Uthread. +It is easy to assign a coroutine instance to an executor. The user need to pass the executor to coroutine only. The users could assign an executor to a Lazy then schedule immediately by `via`. User could also use `directlyStart(callback, executor)` to assign an executor wihtout schedul immediately. They could use `async` to pass Executor to Uthread. ```cpp Executor e; diff --git a/docs/docs.en/Lazy.md b/docs/docs.en/Lazy.md index fa7a23775..48d2cad45 100644 --- a/docs/docs.en/Lazy.md +++ b/docs/docs.en/Lazy.md @@ -30,7 +30,7 @@ of async_simple itself, we requrie the alignment of `T` in `Lazy` can exceed ## Start Lazy -We could start a Lazy by `co_await`, `syncAwait` and `.start(callback)`. +We could start a Lazy by `co_await`, `syncAwait`, `.start(callback)` or `directlyStart(callback, executor)`. ### co_await @@ -93,6 +93,28 @@ In case the `callback` isn't needed, we could write: task().start([](auto&&){}); ``` + +### directlyStart(callback, executor) + +Similar to `start`, but provides a paramter for binding a scheduler when starting a coroutine. It is important to note that `directlyStart` does not immediately schedule the task when coroutine start. + +```cpp +Lazy<> task() { + auto e = co_await currentExecutor{}; + // binding executor successfully. + assert(e!=nullptr); + // lazy schedule, work doesn't run in executor. + assert(e->currentThreadInExecutor()==false); + co_await coro::Sleep(1s); + // Sleep function need executor schedule, now work runs in executor. + assert(e->currentThreadInExecutor()==true); +} +void func() { + auto executor=std::make_shared(1); + task().directlyStart([executor](Try Result){},executor.get()); +} +``` + ### syncAwait For example: