AceRoutine  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()
27 #include "Coroutine.h"
28 
29 namespace ace_routine {
30 
31 // Use a static variable inside a function to solve the static initialization
32 // ordering problem.
34  static Coroutine* root;
35  return &root;
36 }
37 
38 void Coroutine::insertSorted() {
39  Coroutine** p = getRoot();
40 
41  // O(N^2) insertion, good enough for small (O(100)?) number of coroutines.
42  while (*p != nullptr) {
43  if (getName().compareTo((*p)->getName()) <= 0) break;
44  p = &(*p)->mNext;
45  }
46 
47  mNext = *p;
48  *p = this;
49 }
50 
52  if (mStatus != kStatusSuspended) return;
53 
54  // We lost the original state of the coroutine when suspend() was called but
55  // the coroutine will automatically go back into the original state when
56  // Coroutine::run() is called because COROUTINE_YIELD(), COROUTINE_DELAY()
57  // and COROUTINE_AWAIT() are written to restore their status.
58  mStatus = kStatusYielding;
59 
60  // insert at the head of the linked list
61  Coroutine** p = getRoot();
62  mNext = *p;
63  *p = this;
64 }
65 
66 unsigned long Coroutine::millis() const {
67  return ::millis();
68 }
69 
70 }
void resume()
Add a Suspended coroutine into the head of the scheduler linked list, and change the state to Yieldin...
Definition: Coroutine.cpp:51
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
virtual unsigned long millis() const
Returns the current millisecond clock.
Definition: Coroutine.cpp:66
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
All coroutines are instances of the Coroutine base class.
Base class of all coroutines.
Definition: Coroutine.h:217
static Coroutine ** getRoot()
Get the pointer to the root pointer.
Definition: Coroutine.cpp:33