AceRoutine  1.5.0
A low-memory, fast-switching, cooperative multitasking library using stackless coroutines on Arduino platforms.
CoroutineScheduler.h
1 /*
2 MIT License
3 
4 Copyright (c) 2018 Brian T. Park
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12 
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23 */
24 
25 #ifndef ACE_ROUTINE_COROUTINE_SCHEDULER_H
26 #define ACE_ROUTINE_COROUTINE_SCHEDULER_H
27 
28 #if ACE_ROUTINE_DEBUG == 1
29  #include <Arduino.h> // Serial, Print
30 #endif
31 #include "Coroutine.h"
32 #include "CoroutineProfiler.h"
33 
34 class Print;
35 
36 namespace ace_routine {
37 
81 template <typename T_COROUTINE>
82 class CoroutineSchedulerTemplate {
83  public:
85  static void setup() { getScheduler()->setupScheduler(); }
86 
88  static void setupCoroutines() {
89  getScheduler()->setupCoroutinesInternal();
90  }
91 
98  static void loop() { getScheduler()->runCoroutine(); }
99 
106  static void loopWithProfiler() {
107  getScheduler()->runCoroutineWithProfiler();
108  }
109 
116  static void list(Print& printer) {
117  getScheduler()->listCoroutines(printer);
118  }
119 
120  private:
121  // Disable copy-constructor and assignment operator
124  delete;
125 
127  static CoroutineSchedulerTemplate* getScheduler() {
128  static CoroutineSchedulerTemplate singletonScheduler;
129  return &singletonScheduler;
130  }
131 
133  CoroutineSchedulerTemplate() = default;
134 
145  void setupScheduler() {
146  mCurrent = T_COROUTINE::getRoot();
147  }
148 
150  void setupCoroutinesInternal() {
151  for (T_COROUTINE** p = T_COROUTINE::getRoot();
152  (*p) != nullptr;
153  p = (*p)->getNext()) {
154 
155  (*p)->setupCoroutine();
156  }
157  }
158 
163  void runCoroutine() {
164  // If reached the end, start from the beginning again.
165  if (*mCurrent == nullptr) {
166  mCurrent = T_COROUTINE::getRoot();
167  // Return if the list is empty. Checking for a null getRoot() inside the
168  // if-statement is deliberate, since it optimizes the common case where
169  // the linked list is not empty.
170  if (*mCurrent == nullptr) {
171  return;
172  }
173  }
174 
175  // Handle the coroutine's dispatch back to the last known internal status.
176  switch ((*mCurrent)->getStatus()) {
177  case T_COROUTINE::kStatusYielding:
178  case T_COROUTINE::kStatusDelaying:
179  // The coroutine itself knows whether it is yielding or delaying, and
180  // its continuation context determines whether to call
181  // Coroutine::isDelayExpired(), Coroutine::isDelayMicrosExpired(), or
182  // Coroutine::isDelaySecondsExpired().
183  (*mCurrent)->runCoroutine();
184  break;
185 
186  case T_COROUTINE::kStatusEnding:
187  // mark it terminated
188  (*mCurrent)->setTerminated();
189  break;
190 
191  default:
192  // For all other cases, just skip to the next coroutine.
193  break;
194  }
195 
196  // Go to the next coroutine
197  mCurrent = (*mCurrent)->getNext();
198  }
199 
200  /*
201  * Run the current coroutine with profiling enabled by calling
202  * Coroutine::runCoroutineWithProfiler().
203  */
204  void runCoroutineWithProfiler() {
205  // If reached the end, start from the beginning again.
206  if (*mCurrent == nullptr) {
207  mCurrent = T_COROUTINE::getRoot();
208  // Return if the list is empty. Checking for a null getRoot() inside the
209  // if-statement is deliberate, since it optimizes the common case where
210  // the linked list is not empty.
211  if (*mCurrent == nullptr) {
212  return;
213  }
214  }
215 
216  // Handle the coroutine's dispatch back to the last known internal status.
217  switch ((*mCurrent)->getStatus()) {
218  case T_COROUTINE::kStatusYielding:
219  case T_COROUTINE::kStatusDelaying:
220  // The coroutine itself knows whether it is yielding or delaying, and
221  // its continuation context determines whether to call
222  // Coroutine::isDelayExpired(), Coroutine::isDelayMicrosExpired(), or
223  // Coroutine::isDelaySecondsExpired().
224  //
225  // This version calls `Coroutine::runCoroutineWithProfiler()` to
226  // enable the profiler.
227  (*mCurrent)->runCoroutineWithProfiler();
228  break;
229 
230  case T_COROUTINE::kStatusEnding:
231  // mark it terminated
232  (*mCurrent)->setTerminated();
233  break;
234 
235  default:
236  // For all other cases, just skip to the next coroutine.
237  break;
238  }
239 
240  // Go to the next coroutine
241  mCurrent = (*mCurrent)->getNext();
242  }
243 
245  void listCoroutines(Print& printer) {
246  for (T_COROUTINE** p = T_COROUTINE::getRoot(); (*p) != nullptr;
247  p = (*p)->getNext()) {
248  printer.print(F("Coroutine "));
249  (*p)->printNameTo(printer);
250  printer.print(F("; status: "));
251  (*p)->statusPrintTo(printer);
252  printer.println();
253  }
254  }
255 
256  // The current coroutine is represented by a pointer to a pointer. This
257  // allows the root node to be treated the same as all the other nodes, and
258  // simplifies the code that traverses the singly-linked list.
259  T_COROUTINE** mCurrent = nullptr;
260 };
261 
262 using CoroutineScheduler = CoroutineSchedulerTemplate<Coroutine>;
263 
264 }
265 
266 #endif
ace_routine::CoroutineSchedulerTemplate::loop
static void loop()
Run the current coroutine using the current scheduler.
Definition: CoroutineScheduler.h:98
ace_routine::CoroutineSchedulerTemplate::setupCoroutines
static void setupCoroutines()
Set up the coroutines by calling their setupCoroutine() methods.
Definition: CoroutineScheduler.h:88
ace_routine::CoroutineSchedulerTemplate::loopWithProfiler
static void loopWithProfiler()
Run the current coroutine using the current scheduler with the coroutine profiler enabled.
Definition: CoroutineScheduler.h:106
ace_routine::CoroutineSchedulerTemplate::setup
static void setup()
Set up the scheduler.
Definition: CoroutineScheduler.h:85
ace_routine::CoroutineSchedulerTemplate::list
static void list(Print &printer)
Print out the known coroutines to the printer (usually Serial).
Definition: CoroutineScheduler.h:116
ace_routine::CoroutineSchedulerTemplate
Class that manages instances of the Coroutine class, and executes them in a round-robin fashion.
Definition: Coroutine.h:274
Coroutine.h