25 #ifndef ACE_ROUTINE_COROUTINE_SCHEDULER_H
26 #define ACE_ROUTINE_COROUTINE_SCHEDULER_H
28 #if ACE_ROUTINE_DEBUG == 1
35 namespace ace_routine {
41 template <
typename T_COROUTINE>
42 class CoroutineSchedulerTemplate {
45 static void setup() { getScheduler()->setupScheduler(); }
53 static void loop() { getScheduler()->runCoroutine(); }
61 static void list(Print& printer) {
62 getScheduler()->listCoroutines(printer);
74 return &singletonScheduler;
78 CoroutineSchedulerTemplate() =
default;
90 void setupScheduler() {
91 mCurrent = T_COROUTINE::getRoot();
97 if (*mCurrent ==
nullptr) {
98 mCurrent = T_COROUTINE::getRoot();
102 if (*mCurrent ==
nullptr) {
107 #if ACE_ROUTINE_DEBUG == 1
108 Serial.print(F(
"Processing "));
109 Serial.print((uintptr_t) (*mCurrent));
114 switch ((*mCurrent)->getStatus()) {
115 case T_COROUTINE::kStatusYielding:
116 case T_COROUTINE::kStatusDelaying:
121 (*mCurrent)->runCoroutine();
124 case T_COROUTINE::kStatusEnding:
126 (*mCurrent)->setTerminated();
135 mCurrent = (*mCurrent)->getNext();
140 void listCoroutines(Print& printer) {
141 for (T_COROUTINE** p = T_COROUTINE::getRoot(); (*p) !=
nullptr;
142 p = (*p)->getNext()) {
143 printer.print(F(
"Coroutine "));
144 printer.print((uintptr_t) *p);
145 printer.print(F(
"; status: "));
146 (*p)->statusPrintTo(printer);
154 T_COROUTINE** mCurrent =
nullptr;
157 using CoroutineScheduler = CoroutineSchedulerTemplate<Coroutine>;