Skip to content

Commit

Permalink
rename to directlyStart
Browse files Browse the repository at this point in the history
  • Loading branch information
poor-circle committed Nov 7, 2024
1 parent 633aff9 commit bd9f241
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 8 deletions.
5 changes: 3 additions & 2 deletions async_simple/coro/Lazy.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <typename F>
void start(F&& callback,
Executor* executor) requires(std::is_invocable_v<F&&, Try<T>>) {
void directlyStart(F&& callback, Executor* executor) requires(
std::is_invocable_v<F&&, Try<T>>) {
this->_coro.promise()._executor = executor;
return start(std::forward<F>(callback));
}
Expand Down
2 changes: 1 addition & 1 deletion async_simple/coro/test/LazyTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ TEST_F(LazyTest, testStartWithExecutor) {
co_return;
};
std::promise<void> f;
test().start([&f](auto&&) { f.set_value(); }, &_executor);
test().directlyStart([&f](auto&&) { f.set_value(); }, &_executor);
f.get_future().wait();
}

Expand Down
2 changes: 1 addition & 1 deletion async_simple/uthread/Await.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ decltype(auto) await(Executor* ex, Fn&& fn, Args&&... args) requires
co_return;
};
lazy(std::forward<Fn>(fn), std::forward<Args>(args)...)
.start([](auto&&) {}, ex);
.directlyStart([](auto&&) {}, ex);
return await(std::move(f));
}

Expand Down
2 changes: 1 addition & 1 deletion docs/docs.cn/Executor.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
24 changes: 23 additions & 1 deletion docs/docs.cn/Lazy.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Lazy<int> task2(int x) {

## 启动方式

一个 Lazy 应该以 `co_await``syncAwait` 以及 `.start(callback)` 方式启动。
一个 Lazy 应该以 `co_await``syncAwait`, `.start(callback)` 以及 `directlyStart(callback, executor)`方式启动。

### co_await 启动

Expand Down Expand Up @@ -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<executors::SimpleExecutor>(1);
task().directlyStart([executor](Try<void> Result){},executor.get());
}
```

### syncAwait 启动

例如:
Expand Down
2 changes: 1 addition & 1 deletion docs/docs.en/Executor.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
24 changes: 23 additions & 1 deletion docs/docs.en/Lazy.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ of async_simple itself, we requrie the alignment of `T` in `Lazy<T>` 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

Expand Down Expand Up @@ -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<executors::SimpleExecutor>(1);
task().directlyStart([executor](Try<void> Result){},executor.get());
}
```

### syncAwait

For example:
Expand Down

0 comments on commit bd9f241

Please sign in to comment.