From d6c95a111c0950757d75496af254e3427e3769b6 Mon Sep 17 00:00:00 2001 From: Vegard Storheil Eriksen Date: Sat, 16 Apr 2022 20:59:36 +0200 Subject: async: Add preliminary time scheduler. --- async/scheduler.h | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'async/scheduler.h') diff --git a/async/scheduler.h b/async/scheduler.h index 0196aa5..bae4890 100644 --- a/async/scheduler.h +++ b/async/scheduler.h @@ -1,6 +1,9 @@ #pragma once #include +#include + +#include struct schedulable { schedulable* next = nullptr; @@ -161,3 +164,43 @@ struct task : public schedulable { task(std::coroutine_handle h) : schedulable(h) {} }; + +struct async_flag : public schedulable { + bool ready; + + async_flag() : ready(false) {} + + bool await_ready() { + return ready; + } + + bool await_suspend(std::coroutine_handle<> h) { + critical_section lock; + + if(ready) { + return false; + } else { + awaiter = h; + return true; + } + } + + void await_resume() { + critical_section lock; + + awaiter = nullptr; + ready = false; + } + + void set() { + if(ready) { + return; + } + + ready = true; + + if(awaiter) { + scheduler.schedule(*this); + } + } +}; -- cgit v1.2.3