AceRoutine  0.1
A low-memory, fast-switching, cooperative multitasking library using stackless coroutines on Arduino platforms.
CoroutineScheduler.cpp
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 // Set this to 1 to send debugging from CoroutineScheduler::runCoroutine() to
26 // Serial. You must run Serial.begin(speed) in setup().
27 #define ACE_ROUTINE_DEBUG 0
28 
29 #include "CoroutineScheduler.h"
30 
31 #if ACE_ROUTINE_DEBUG == 1
32  #include <Arduino.h> // Serial, Print
33 #endif
34 
35 namespace ace_routine {
36 
37 CoroutineScheduler* CoroutineScheduler::getScheduler() {
38  static CoroutineScheduler singletonScheduler;
39  return &singletonScheduler;
40 }
41 
42 void CoroutineScheduler::setupScheduler() {
43  mCurrent = Coroutine::getRoot();
44 
45  // Pre-scan to remove any coroutines whose suspend() was called before the
46  // CoroutineScheduler::setup(). This makes unit tests easier to write because
47  // they can just concentrate on only the active coroutines. And this is also
48  // more intuitive because we don't need to wait a complete run() cycle to
49  // remove these from the queue.
50  Coroutine** current = Coroutine::getRoot();
51  while (*current != nullptr) {
52  if ((*current)->isSuspended()) {
53  *current = *((*current)->getNext());
54  } else {
55  current = (*current)->getNext();
56  }
57  }
58 }
59 
60 void CoroutineScheduler::runCoroutine() {
61  // If reached the end, start from the beginning again.
62  if (*mCurrent == nullptr) {
63  mCurrent = Coroutine::getRoot();
64  // Return if the list is empty. Checking for a null getRoot() inside the
65  // if-statement is deliberate, since it optimizes the common case where the
66  // linked list is not empty.
67  if (*mCurrent == nullptr) {
68  return;
69  }
70  }
71 
72 #if ACE_ROUTINE_DEBUG == 1
73  Serial.print(F("Processing "));
74  (*mCurrent)->printeName(Serial);
75  Serial.println();
76 #endif
77 
78  // Handle the coroutine's dispatch back to the last known internal status.
79  switch ((*mCurrent)->getStatus()) {
82  (*mCurrent)->run();
83  mCurrent = (*mCurrent)->getNext();
84  break;
86  // Check isDelayExpired() here to optimize away an extra call into the
87  // Coroutine::run(). Everything would still work if we just dispatched
88  // into the Coroutine::run() because that method checks isDelayExpired()
89  // as well.
90  if ((*mCurrent)->isDelayExpired()) {
91  (*mCurrent)->run();
92  }
93  mCurrent = (*mCurrent)->getNext();
94  break;
95  }
97  // take the coroutine out of the list, and mark it terminated
98  (*mCurrent)->setTerminated();
99  *mCurrent = *((*mCurrent)->getNext());
100  break;
102  // take the coroutine out of the list
103  *mCurrent = *((*mCurrent)->getNext());
104  break;
105  default:
106  // Should never happen but skip to next coroutine to prevent infinite
107  // loop.
108  mCurrent = (*mCurrent)->getNext();
109  break;
110  }
111 }
112 
113 void CoroutineScheduler::listCoroutines(Print& printer) {
114  for (Coroutine** p = Coroutine::getRoot(); (*p) != nullptr;
115  p = (*p)->getNext()) {
116  printer.print(F("Coroutine "));
117  (*p)->getName().print(printer);
118  printer.print(F("; status: "));
119  printer.println((*p)->getStatus());
120  }
121 }
122 
123 }
static const Status kStatusEnding
Coroutine executed the COROUTINE_END() statement.
Definition: Coroutine.h:378
const FCString & getName() const
Human-readable name of the coroutine.
Definition: Coroutine.h:236
static const Status kStatusYielding
Coroutine returned using the COROUTINE_YIELD() statement.
Definition: Coroutine.h:366
static const Status kStatusSuspended
Coroutine has been suspended using suspend() and the scheduler should remove it from the queue upon t...
Definition: Coroutine.h:363
void print(Print &printer) const
Convenience method for printing an FCString.
Definition: FCString.cpp:35
static const Status kStatusDelaying
Coroutine returned using the COROUTINE_DELAY() statement.
Definition: Coroutine.h:372
static const Status kStatusAwaiting
Coroutine returned using the COROUTINE_AWAIT() statement.
Definition: Coroutine.h:369
Coroutine ** getNext()
Return the next pointer as a pointer to the pointer, similar to getRoot().
Definition: Coroutine.h:233
static Coroutine ** getRoot()
Get the pointer to the root pointer.
Definition: Coroutine.cpp:33