AceRoutine  1.2.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  // For v1.1 and earlier, calling Coroutine::suspend() caused the coroutine to
46  // be omitted from the linked list. But this can cause a cycle in the
47  // linked-list if the resume() is called immediately after a suspend. For
48  // v1.2, we keep all coroutines (even those in Suspended, Ending or
49  // Terminated) in the linked list. Since everything is always in the linked
50  // list, the following code for omitted suspended coroutines from the list
51  // is no longer needed.
52  /*
53  // Pre-scan to remove any coroutines whose suspend() was called before the
54  // CoroutineScheduler::setup(). This makes unit tests easier to write because
55  // they can just concentrate on only the active coroutines. And this is also
56  // more intuitive because we don't need to wait a complete runCoroutine()
57  // cycle to remove these from the queue.
58  Coroutine** current = Coroutine::getRoot();
59  while (*current != nullptr) {
60  if ((*current)->isSuspended()) {
61  *current = *((*current)->getNext());
62  } else {
63  current = (*current)->getNext();
64  }
65  }
66  */
67 }
68 
69 void CoroutineScheduler::runCoroutine() {
70  // If reached the end, start from the beginning again.
71  if (*mCurrent == nullptr) {
72  mCurrent = Coroutine::getRoot();
73  // Return if the list is empty. Checking for a null getRoot() inside the
74  // if-statement is deliberate, since it optimizes the common case where the
75  // linked list is not empty.
76  if (*mCurrent == nullptr) {
77  return;
78  }
79  }
80 
81 #if ACE_ROUTINE_DEBUG == 1
82  Serial.print(F("Processing "));
83  (*mCurrent)->getName().printTo(Serial);
84  Serial.println();
85 #endif
86 
87  // Handle the coroutine's dispatch back to the last known internal status.
88  switch ((*mCurrent)->getStatus()) {
90  (*mCurrent)->runCoroutine();
91  break;
92 
94  // Check isDelayExpired() here to optimize away an extra call into the
95  // Coroutine::runCoroutine(). Everything would still work if we just
96  // dispatched into the Coroutine::runCoroutine() because that method
97  // checks isDelayExpired() as well.
98  if ((*mCurrent)->isDelayExpired()) {
99  (*mCurrent)->runCoroutine();
100  }
101  break;
102 
104  // mark it terminated
105  (*mCurrent)->setTerminated();
106  break;
107 
108  default:
109  // For all other cases, just skip to the next coroutine.
110  break;
111  }
112 
113  // Go to the next coroutine
114  mCurrent = (*mCurrent)->getNext();
115 }
116 
117 void CoroutineScheduler::listCoroutines(Print& printer) {
118  for (Coroutine** p = Coroutine::getRoot(); (*p) != nullptr;
119  p = (*p)->getNext()) {
120  printer.print(F("Coroutine "));
121  (*p)->getName().printTo(printer);
122  printer.print(F("; status: "));
123  (*p)->statusPrintTo(printer);
124  printer.println();
125  }
126 }
127 
128 }
ace_routine::Coroutine::kStatusEnding
static const Status kStatusEnding
Coroutine executed the COROUTINE_END() statement.
Definition: Coroutine.h:491
ace_routine::Coroutine::getName
const ace_common::FCString & getName() const
Human-readable name of the coroutine.
Definition: Coroutine.h:272
ace_routine::Coroutine::kStatusDelaying
static const Status kStatusDelaying
Coroutine returned using the COROUTINE_DELAY() statement.
Definition: Coroutine.h:485
ace_routine::Coroutine::kStatusYielding
static const Status kStatusYielding
Coroutine returned using the COROUTINE_YIELD() statement.
Definition: Coroutine.h:482