summaryrefslogtreecommitdiff
path: root/async/scheduler.h
diff options
context:
space:
mode:
authorVegard Storheil Eriksen <zyp@jvnv.net>2022-04-16 20:59:36 +0200
committerVegard Storheil Eriksen <zyp@jvnv.net>2022-04-16 21:02:39 +0200
commitd6c95a111c0950757d75496af254e3427e3769b6 (patch)
tree049d63096e434a7dba49e018e6836faaa440ec01 /async/scheduler.h
parentc6b1f1112a3f4a5139700ef33da62c1ebdc3a7ba (diff)
async: Add preliminary time scheduler.
Diffstat (limited to 'async/scheduler.h')
-rw-r--r--async/scheduler.h43
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);
+ }
+ }
+};