AceRoutine  0.3
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 "Flash.h"
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  ACE_ROUTINE_FPSTR(kStatusSuspendedString),
101  ACE_ROUTINE_FPSTR(kStatusYieldingString),
102  ACE_ROUTINE_FPSTR(kStatusDelayingString),
103  ACE_ROUTINE_FPSTR(kStatusRunningString),
104  ACE_ROUTINE_FPSTR(kStatusEndingString),
105  ACE_ROUTINE_FPSTR(kStatusTerminatedString),
106 };
107 
108 }
void resume()
Add a Suspended coroutine into the head of the scheduler linked list, and change the state to Yieldin...
Definition: Coroutine.cpp:52
#define ACE_ROUTINE_FPSTR(pstr_pointer)
The FPSTR() macro is defined on ESP8266, not defined on Teensy and AVR, and broken on ESP32...
Definition: Flash.h:51
const FCString & getName() const
Human-readable name of the coroutine.
Definition: Coroutine.h:280
static const Status kStatusYielding
Coroutine returned using the COROUTINE_YIELD() statement.
Definition: Coroutine.h:473
static const Status kStatusSuspended
Coroutine has been suspended using suspend() and the scheduler should remove it from the queue upon t...
Definition: Coroutine.h:470
All coroutines are instances of the Coroutine base class.
virtual unsigned long coroutineMillis() const
Returns the current millisecond clock.
Definition: Coroutine.cpp:68
virtual unsigned long coroutineSeconds() const
Returns the current clock in unit of seconds, truncated to the lower 16-bits.
Definition: Coroutine.cpp:76
Base class of all coroutines.
Definition: Coroutine.h:260
static Coroutine ** getRoot()
Get the pointer to the root pointer.
Definition: Coroutine.cpp:34
Various macros to smooth over the differences among the various platforms with regards to their suppo...
virtual unsigned long coroutineMicros() const
Returns the current millisecond clock.
Definition: Coroutine.cpp:72