AceRoutine  1.0.1
A low-memory, fast-switching, cooperative multitasking library using stackless coroutines on Arduino platforms.
Coroutine.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 #include <stdint.h> // uintptr_t
26 #include <Arduino.h> // millis(), micros()
27 #include "Coroutine.h"
28 #include "compat.h" // FPSTR()
29 
30 namespace ace_routine {
31 
32 // Use a static variable inside a function to solve the static initialization
33 // ordering problem.
35  static Coroutine* root;
36  return &root;
37 }
38 
39 void Coroutine::insertSorted() {
40  Coroutine** p = getRoot();
41 
42  // O(N^2) insertion, good enough for small (O(100)?) number of coroutines.
43  while (*p != nullptr) {
44  if (getName().compareTo((*p)->getName()) <= 0) break;
45  p = &(*p)->mNext;
46  }
47 
48  mNext = *p;
49  *p = this;
50 }
51 
53  if (mStatus != kStatusSuspended) return;
54 
55  // We lost the original state of the coroutine when suspend() was called but
56  // the coroutine will automatically go back into the original state when
57  // Coroutine::runCoroutine() is called because COROUTINE_YIELD(),
58  // COROUTINE_DELAY() and COROUTINE_AWAIT() are written to restore their
59  // status.
60  mStatus = kStatusYielding;
61 
62  // insert at the head of the linked list
63  Coroutine** p = getRoot();
64  mNext = *p;
65  *p = this;
66 }
67 
68 unsigned long Coroutine::coroutineMillis() const {
69  return ::millis();
70 }
71 
72 unsigned long Coroutine::coroutineMicros() const {
73  return ::micros();
74 }
75 
76 unsigned long Coroutine::coroutineSeconds() const {
77  unsigned long m = ::millis();
78 #if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_SAMD) || defined(ESP8266)
79  // No hardware division so the udiv1000() approximation is faster
80  return internal::udiv1000(m);
81 #else
82  return m / 1000;
83 #endif
84 }
85 
86 // Create the sStatusStrings lookup table to translate Status integer to a
87 // human-readable string. When it is used, it increases flash memory by 86
88 // bytes, and static RAM by 14 bytes. It is currently only used by
89 // CoroutineScheduler::list() but I think it's worth it to make debugging
90 // easier.
91 
92 static const char kStatusSuspendedString[] PROGMEM = "Suspended";
93 static const char kStatusYieldingString[] PROGMEM = "Yielding";
94 static const char kStatusDelayingString[] PROGMEM = "Delaying";
95 static const char kStatusRunningString[] PROGMEM = "Running";
96 static const char kStatusEndingString[] PROGMEM = "Ending";
97 static const char kStatusTerminatedString[] PROGMEM = "Terminated";
98 
99 const __FlashStringHelper* const Coroutine::sStatusStrings[] = {
100  FPSTR(kStatusSuspendedString),
101  FPSTR(kStatusYieldingString),
102  FPSTR(kStatusDelayingString),
103  FPSTR(kStatusRunningString),
104  FPSTR(kStatusEndingString),
105  FPSTR(kStatusTerminatedString),
106 };
107 
108 }
ace_routine::Coroutine::getName
const FCString & getName() const
Human-readable name of the coroutine.
Definition: Coroutine.h:275
ace_routine::Coroutine::resume
void resume()
Add a Suspended coroutine into the head of the scheduler linked list, and change the state to Yieldin...
Definition: Coroutine.cpp:52
ace_routine::Coroutine::coroutineMicros
virtual unsigned long coroutineMicros() const
Returns the current millisecond clock.
Definition: Coroutine.cpp:72
ace_routine::Coroutine::kStatusSuspended
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
compat.h
ace_routine::Coroutine::coroutineSeconds
virtual unsigned long coroutineSeconds() const
Returns the current clock in unit of seconds, truncated to the lower 16-bits.
Definition: Coroutine.cpp:76
ace_routine::Coroutine
Base class of all coroutines.
Definition: Coroutine.h:255
ace_routine::Coroutine::kStatusYielding
static const Status kStatusYielding
Coroutine returned using the COROUTINE_YIELD() statement.
Definition: Coroutine.h:470
Coroutine.h
ace_routine::Coroutine::getRoot
static Coroutine ** getRoot()
Get the pointer to the root pointer.
Definition: Coroutine.cpp:34
ace_routine::Coroutine::coroutineMillis
virtual unsigned long coroutineMillis() const
Returns the current millisecond clock.
Definition: Coroutine.cpp:68