diff options
Diffstat (limited to 'async/scheduler.h')
-rw-r--r-- | async/scheduler.h | 43 |
1 files changed, 43 insertions, 0 deletions
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 <coroutine> +#include <optional> + +#include <cortex_m/critical_section.h> struct schedulable { schedulable* next = nullptr; @@ -161,3 +164,43 @@ struct task : public schedulable { task(std::coroutine_handle<promise_type> 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); + } + } +}; |