AceRoutine  1.4.1
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 
33 class Print;
34 
35 namespace ace_routine {
36 
80 template <typename T_COROUTINE>
81 class CoroutineSchedulerTemplate {
82  public:
84  static void setup() { getScheduler()->setupScheduler(); }
85 
87  static void setupCoroutines() {
88  getScheduler()->setupCoroutinesInternal();
89  }
90 
97  static void loop() { getScheduler()->runCoroutine(); }
98 
105  static void list(Print& printer) {
106  getScheduler()->listCoroutines(printer);
107  }
108 
109  private:
110  // Disable copy-constructor and assignment operator
113  delete;
114 
116  static CoroutineSchedulerTemplate* getScheduler() {
117  static CoroutineSchedulerTemplate singletonScheduler;
118  return &singletonScheduler;
119  }
120 
122  CoroutineSchedulerTemplate() = default;
123 
134  void setupScheduler() {
135  mCurrent = T_COROUTINE::getRoot();
136  }
137 
139  void setupCoroutinesInternal() {
140  for (T_COROUTINE** p = T_COROUTINE::getRoot();
141  (*p) != nullptr;
142  p = (*p)->getNext()) {
143 
144  (*p)->setupCoroutine();
145  }
146  }
147 
149  void runCoroutine() {
150  // If reached the end, start from the beginning again.
151  if (*mCurrent == nullptr) {
152  mCurrent = T_COROUTINE::getRoot();
153  // Return if the list is empty. Checking for a null getRoot() inside the
154  // if-statement is deliberate, since it optimizes the common case where
155  // the linked list is not empty.
156  if (*mCurrent == nullptr) {
157  return;
158  }
159  }
160 
161  #if ACE_ROUTINE_DEBUG == 1
162  Serial.print(F("Processing "));
163  Serial.print((uintptr_t) (*mCurrent));
164  Serial.println();
165  #endif
166 
167  // Handle the coroutine's dispatch back to the last known internal status.
168  switch ((*mCurrent)->getStatus()) {
169  case T_COROUTINE::kStatusYielding:
170  case T_COROUTINE::kStatusDelaying:
171  // The coroutine itself knows whether it is yielding or delaying, and
172  // its continuation context determines whether to call
173  // Coroutine::isDelayExpired(), Coroutine::isDelayMicrosExpired(), or
174  // Coroutine::isDelaySecondsExpired().
175  (*mCurrent)->runCoroutine();
176  break;
177 
178  case T_COROUTINE::kStatusEnding:
179  // mark it terminated
180  (*mCurrent)->setTerminated();
181  break;
182 
183  default:
184  // For all other cases, just skip to the next coroutine.
185  break;
186  }
187 
188  // Go to the next coroutine
189  mCurrent = (*mCurrent)->getNext();
190  }
191 
192 
194  void listCoroutines(Print& printer) {
195  for (T_COROUTINE** p = T_COROUTINE::getRoot(); (*p) != nullptr;
196  p = (*p)->getNext()) {
197  printer.print(F("Coroutine "));
198  printer.print((uintptr_t) *p);
199  printer.print(F("; status: "));
200  (*p)->statusPrintTo(printer);
201  printer.println();
202  }
203  }
204 
205  // The current coroutine is represented by a pointer to a pointer. This
206  // allows the root node to be treated the same as all the other nodes, and
207  // simplifies the code that traverses the singly-linked list.
208  T_COROUTINE** mCurrent = nullptr;
209 };
210 
211 using CoroutineScheduler = CoroutineSchedulerTemplate<Coroutine>;
212 
213 }
214 
215 #endif
ace_routine::CoroutineSchedulerTemplate::loop
static void loop()
Run the current coroutine using the current scheduler.
Definition: CoroutineScheduler.h:97
ace_routine::CoroutineSchedulerTemplate::setupCoroutines
static void setupCoroutines()
Set up the coroutines by calling their setupCoroutine() methods.
Definition: CoroutineScheduler.h:87
ace_routine::CoroutineSchedulerTemplate::setup
static void setup()
Set up the scheduler.
Definition: CoroutineScheduler.h:84
ace_routine::CoroutineSchedulerTemplate::list
static void list(Print &printer)
Print out the known coroutines to the printer (usually Serial).
Definition: CoroutineScheduler.h:105
ace_routine::CoroutineSchedulerTemplate
Class that manages instances of the Coroutine class, and executes them in a round-robin fashion.
Definition: Coroutine.h:271
Coroutine.h