-
Notifications
You must be signed in to change notification settings - Fork 50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
如何拓展lazy_io的范围 #93
Comments
你的设计方案包含一个完整的调度器,这是整个框架运行的核心逻辑。在 co_context 里,
awaiter 的一个 demo 是 |
我有没有可能在函数内部自定义调度呢?我看 task<> top1(){
co_await top2();
}
task<> top2(){
.... // 调度策略,如果接收到了用户态中断,选择被挂起的top3函数
co_await top3();
}
task<> top3(){
} 我在 |
想到了侵入比较小的方法:
|
不侵入的办法是有的,可以在 针对这个问题,一个办法是再启动一个 你可以试试运行这个 demo,留意 #include <co_context/all.hpp>
#include <queue>
using co_context::task;
using co_context::counting_semaphore;
struct scheduler {
counting_semaphore ready_count{0};
std::queue<std::coroutine_handle<>> ready_queue; // NOT thread-safe
bool poison{false}; // NOT thread-safe
void stop() noexcept {
poison = true;
ready_count.release();
}
void post(std::coroutine_handle<> job_handle) {
ready_queue.push(job_handle);
ready_count.release();
}
void post(task<> &&job) {
std::coroutine_handle<> job_handle = job.get_handle();
job.detach(); // prevent ~task() from killing the coroutine
post(job_handle);
}
task<> run() {
while (!poison) {
printf("scheduler::run(): acquire...\n");
co_await ready_count.acquire();
printf("scheduler::run(): task found\n");
if (poison) { // double check the poison
break;
}
std::coroutine_handle<> handle = ready_queue.front();
ready_queue.pop();
handle.resume();
}
}
};
struct my_awaiter {
constexpr bool await_ready() noexcept { return false; }
void await_suspend(std::coroutine_handle<> current) noexcept {
printf("my_awaiter: suspend\n");
// Do your job. When finished, re-post it anytime, anywhere.
my_context->post(current);
}
void await_resume() const noexcept { printf("my_awaiter: resume\n"); }
scheduler *my_context;
};
task<> my_job(scheduler &my_context) {
printf("my_job: begin\n");
co_await my_awaiter{&my_context};
printf("my_job: end\n");
}
int main() {
co_context::io_context ctx;
co_context::io_context background;
scheduler sch;
sch.post(my_job(sch)); // post the first job
ctx.co_spawn(sch.run()); // launch the scheduler
ctx.start();
background.start();
ctx.join();
return 0;
} |
我今天在学习
io_uring
时,发现其设计与我在用户态实现的异步IO几乎完全一致。进程间的异步IO是为了实现异步远程函数调用:请问我该如何适配co_context,时其支持我的设计方案呢?如果您方便的话,我想参考一下您的论文,如果不方便公开的话,可以发送到我的邮箱:
bitwangzhankun@gmail.com
The text was updated successfully, but these errors were encountered: