AceRoutine  1.0
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 runCoroutine()
49  // cycle to 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()) {
81  (*mCurrent)->runCoroutine();
82  mCurrent = (*mCurrent)->getNext();
83  break;
85  // Check isDelayExpired() here to optimize away an extra call into the
86  // Coroutine::runCoroutine(). Everything would still work if we just
87  // dispatched into the Coroutine::runCoroutine() because that method
88  // checks isDelayExpired() as well.
89  if ((*mCurrent)->isDelayExpired()) {
90  (*mCurrent)->runCoroutine();
91  }
92  mCurrent = (*mCurrent)->getNext();
93  break;
94  }
96  // take the coroutine out of the list, and mark it terminated
97  (*mCurrent)->setTerminated();
98  *mCurrent = *((*mCurrent)->getNext());
99  break;
101  // take the coroutine out of the list
102  *mCurrent = *((*mCurrent)->getNext());
103  break;
104  default:
105  // Should never happen but skip to next coroutine to prevent infinite
106  // loop.
107  mCurrent = (*mCurrent)->getNext();
108  break;
109  }
110 }
111 
112 void CoroutineScheduler::listCoroutines(Print& printer) {
113  for (Coroutine** p = Coroutine::getRoot(); (*p) != nullptr;
114  p = (*p)->getNext()) {
115  printer.print(F("Coroutine "));
116  (*p)->getName().printTo(printer);
117  printer.print(F("; status: "));
118  (*p)->statusPrintTo(printer);
119  printer.println();
120  }
121 }
122 
123 }
static const Status kStatusEnding
Coroutine executed the COROUTINE_END() statement.
Definition: Coroutine.h:479
const FCString & getName() const
Human-readable name of the coroutine.
Definition: Coroutine.h:275
static const Status kStatusYielding
Coroutine returned using the COROUTINE_YIELD() statement.
Definition: Coroutine.h:470
static const Status kStatusSuspended
Coroutine has been suspended using suspend() and the scheduler should remove it from the queue upon t...
Definition: Coroutine.h:467
static const Status kStatusDelaying
Coroutine returned using the COROUTINE_DELAY() statement.
Definition: Coroutine.h:473
size_t printTo(Print &printer) const
Convenience method for printing an FCString to printer.
Definition: FCString.cpp:35
Coroutine ** getNext()
Return the next pointer as a pointer to the pointer, similar to getRoot().
Definition: Coroutine.h:272
static Coroutine ** getRoot()
Get the pointer to the root pointer.
Definition: Coroutine.cpp:34