Skip to content

Commit

Permalink
add LocalExecutor
Browse files Browse the repository at this point in the history
  • Loading branch information
chloro-pn committed Dec 25, 2024
1 parent 80730e1 commit 35f68b5
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 1 deletion.
75 changes: 75 additions & 0 deletions async_simple/coro/test/LocalExecutorTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright (c) 2022, Alibaba Group Holding Limited;
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <coroutine>
#include "async_simple/Try.h"
#include "async_simple/coro/Lazy.h"
#include "async_simple/executors/LocalExecutor.h"

#include "async_simple/test/unittest.h"

namespace async_simple {
namespace coro {

template <typename T>
struct CallbackAwaiter {
public:
using CallbackFunction =
std::function<void(std::coroutine_handle<>, std::function<void(T)>)>;

CallbackAwaiter(CallbackFunction callback_function)
: callback_function_(std::move(callback_function)) {}

bool await_ready() noexcept { return false; }

void await_suspend(std::coroutine_handle<> handle) {
callback_function_(handle, [this](T t) { result_ = std::move(t); });
}

T await_resume() noexcept { return std::move(result_); }

private:
CallbackFunction callback_function_;
T result_;
};

Lazy<int> async_compute(int i) noexcept {
int ret = co_await CallbackAwaiter<int>{
[&, i](std::coroutine_handle<> handle, auto set_resume_value) {
set_resume_value(i * 100);
handle.resume();
}};
co_return ret;
}

Lazy<int> coro_compute() {
int sum = 0;
for (auto i = 0; i < 1000; ++i) {
sum += co_await async_compute(i);
}
co_return sum;
}

TEST(LocalExecutorTest, testStackOverflow) {
executors::LocalExecutor ex;
int result{0};
coro_compute().via(&ex).start([&](Try<int> i) { result = i.value(); });
ex.Loop();
EXPECT_EQ(result, 49950000);
}

} // namespace coro
} // namespace async_simple
55 changes: 55 additions & 0 deletions async_simple/executors/LocalExecutor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (c) 2022, Alibaba Group Holding Limited;
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef ASYNC_LOCAL_EXECUTOR_H
#define ASYNC_LOCAL_EXECUTOR_H

#include <deque>
#include "async_simple/Executor.h"

namespace async_simple {

namespace executors {

class LocalExecutor : public Executor {
public:
LocalExecutor() : Executor("local executor") {}

bool schedule(Func func) override {
ready_queue_.push_back(std::move(func));
return true;
}

void Loop() {
while (true) {
if (ready_queue_.empty()) {
return;
}
Func func = std::move(ready_queue_.front());
ready_queue_.pop_front();
func();
}
}

private:
std::deque<Func> ready_queue_;
};

} // namespace executors

} // namespace async_simple

#endif
2 changes: 1 addition & 1 deletion bazel/config/copt.bzl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
ASYNC_SIMPLE_COPTS = select({
"@com_github_async_simple//bazel/config:msvc_compiler": [
"@async_simple//bazel/config:msvc_compiler": [
"/std:c++20",
"/await:strict",
"/EHa"
Expand Down

0 comments on commit 35f68b5

Please sign in to comment.