AceRoutine
1.5.0
A low-memory, fast-switching, cooperative multitasking library using stackless coroutines on Arduino platforms.
|
Base class of all coroutines. More...
#include <Coroutine.h>
Public Member Functions | |
virtual int | runCoroutine ()=0 |
The body of the coroutine. More... | |
virtual void | setupCoroutine () |
Perform coroutine initialization. More... | |
int | runCoroutineWithProfiler () |
This is a variant of runCoroutine() which measures the execution time of runCoroutine() and updates the attached profiler if it exists. More... | |
uint8_t | getNameType () const |
Return the type of the name string, either kNameTypeCString or kNameTypeFString. | |
void | setName (const char *name) |
Set the name of the coroutine to the given c-string. | |
void | setName (const __FlashStringHelper *name) |
Set the name of the coroutine to the given f-string. | |
const char * | getCName () const |
Get name of the coroutine assuming it's a c-string. More... | |
const __FlashStringHelper * | getFName () const |
Get name of the coroutine assuming it's an f-string. More... | |
void | printNameTo (Print &printer, uint8_t maxLen=0) const |
Print name to the given Printer. More... | |
void | suspend () |
Suspend the coroutine at the next scheduler iteration. More... | |
void | resume () |
Add a Suspended coroutine into the head of the scheduler linked list, and change the state to Yielding. More... | |
void | reset () |
Reset the coroutine to its initial state. More... | |
bool | isDelayExpired () const |
Check if delay millis time is over. | |
bool | isDelayMicrosExpired () const |
Check if delay micros time is over. | |
bool | isDelaySecondsExpired () const |
Check if delay seconds time is over. | |
bool | isSuspended () const |
The coroutine was suspended with a call to suspend(). | |
bool | isYielding () const |
The coroutine returned using COROUTINE_YIELD(). | |
bool | isDelaying () const |
The coroutine returned using COROUTINE_DELAY(). | |
bool | isRunning () const |
The coroutine is currently running. More... | |
bool | isEnding () const |
The coroutine returned using COROUTINE_END(). More... | |
bool | isTerminated () const |
The coroutine was terminated by the scheduler with a call to setTerminated(). More... | |
bool | isDone () const |
The coroutine is either Ending or Terminated. More... | |
void | setupCoroutine (const char *) ACE_ROUTINE_DEPRECATED |
Deprecated method that does nothing. More... | |
void | setupCoroutine (const __FlashStringHelper *) ACE_ROUTINE_DEPRECATED |
Deprecated method that does nothing. More... | |
void | setProfiler (CoroutineProfiler *profiler) |
Set the profiler. | |
CoroutineProfiler * | getProfiler () const |
Get the profiler. More... | |
CoroutineTemplate ** | getNext () |
Return the next pointer as a pointer to the pointer, similar to getRoot(). More... | |
Static Public Member Functions | |
static CoroutineTemplate ** | getRoot () |
Get the pointer to the root pointer. More... | |
Static Public Attributes | |
static const uint8_t | kNameTypeCString = 0 |
Coroutine name is a const char* c-string. | |
static const uint8_t | kNameTypeFString = 1 |
Coroutine name is a const __FlashStringHelper* f-string. | |
Protected Types | |
typedef uint8_t | Status |
The execution status of the coroutine, corresponding to the COROUTINE_YIELD(), COROUTINE_DELAY(), COROUTINE_AWAIT() and COROUTINE_END() macros. More... | |
Protected Member Functions | |
CoroutineTemplate () | |
Constructor. More... | |
~CoroutineTemplate ()=default | |
Destructor. More... | |
Status | getStatus () const |
Return the status of the coroutine. More... | |
void | statusPrintTo (Print &printer) |
Print the human-readable string of the Status. | |
void | setJump (void *jumpPoint) |
Pointer to label where execute will start on the next call to runCoroutine(). | |
void * | getJump () const |
Pointer to label where execute will start on the next call to runCoroutine(). | |
void | setRunning () |
Set the kStatusRunning state. | |
void | setYielding () |
Set the kStatusDelaying state. | |
void | setDelaying () |
Set the kStatusDelaying state. | |
void | setEnding () |
Set the kStatusEnding state. | |
void | setTerminated () |
Set status to indicate that the Coroutine has been removed from the Scheduler queue. More... | |
void | setDelayMillis (T_DELAY delayMillis) |
Configure the delay timer for delayMillis. More... | |
void | setDelayMicros (T_DELAY delayMicros) |
Configure the delay timer for delayMicros. More... | |
void | setDelaySeconds (T_DELAY delaySeconds) |
Configure the delay timer for delaySeconds. More... | |
Static Protected Member Functions | |
static unsigned long | coroutineMillis () |
Returns the current millisecond clock. More... | |
static unsigned long | coroutineMicros () |
Returns the current microseconds clock. More... | |
static unsigned long | coroutineSeconds () |
Returns the current clock in unit of seconds, truncated to the lower 16-bits. More... | |
Protected Attributes | |
CoroutineTemplate * | mNext = nullptr |
Pointer to the next coroutine in a singly-linked list. | |
void * | mJumpPoint = nullptr |
Address of the label used by the computed-goto. | |
const void * | mName = nullptr |
Name of the coroutine, either (const char*) or (const __FlashStringHelper*). More... | |
uint8_t | mNameType = kNameTypeCString |
String type of the coroutine mName. | |
Status | mStatus = kStatusYielding |
Run-state of the coroutine. | |
T_DELAY | mDelayStart |
Start time provided by COROUTINE_DELAY(), COROUTINE_DELAY_MICROS(), or COROUTINE_DELAY_SECONDS(). More... | |
T_DELAY | mDelayDuration |
Delay time specified by COROUTINE_DELAY(), COROUTINE_DELAY_MICROS() or, COROUTINE_DELAY_SECONDS(). More... | |
CoroutineProfiler * | mProfiler = nullptr |
Pointer to a profiler instance, either static or on the heap. | |
Static Protected Attributes | |
static const Status | kStatusSuspended = 0 |
Coroutine has been suspended using suspend() and the scheduler should remove it from the queue upon the next iteration. More... | |
static const Status | kStatusYielding = 1 |
Coroutine returned using the COROUTINE_YIELD() statement. | |
static const Status | kStatusDelaying = 2 |
Coroutine returned using the COROUTINE_DELAY() statement. | |
static const Status | kStatusRunning = 3 |
Coroutine is currenly running. More... | |
static const Status | kStatusEnding = 4 |
Coroutine executed the COROUTINE_END() statement. | |
static const Status | kStatusTerminated = 5 |
Coroutine has ended and no longer in the scheduler queue. | |
Friends | |
class | CoroutineSchedulerTemplate< CoroutineTemplate< T_CLOCK, T_DELAY > > |
class | ::AceRoutineTest_statusStrings |
class | ::SuspendTest_suspendAndResume |
Base class of all coroutines.
The actual coroutine code is an implementation of the virtual runCoroutine() method.
T_CLOCK | class that provides micros(), millis() and seconds() functions, usually ClockInterface but can be something else for testing purposes. |
T_DELAY | type used to store the mDelayStart and mDelayDuration, usually uint16_t . It may be possible to create a variant set of Coroutine32 and CoroutineScheduler32 classes which use a uint32_t instead of a uint16_t . This would probably allow the COROUTINE_DELAY() , COROUTINE_DELAY_MICROS() and COROUTINE_DELAY_SECONDS() macros to accept 32-bit integers instead of 16-bits. I have not tested this possibility at all. |
Definition at line 292 of file Coroutine.h.
|
protected |
The execution status of the coroutine, corresponding to the COROUTINE_YIELD(), COROUTINE_DELAY(), COROUTINE_AWAIT() and COROUTINE_END() macros.
The finite state diagram looks like this:
* Suspended * ^ ^ * / \ * / \ * v \ * Yielding Delaying * ^ ^ * \ / * \ / * \ / * v v * Running * | * | * v * Ending * | * | * v * Terminated *
Definition at line 603 of file Coroutine.h.
|
inlineprotected |
Constructor.
Automatically insert self into singly-linked list.
Definition at line 629 of file Coroutine.h.
|
protecteddefault |
Destructor.
Non-virtual.
A virtual destructor increases the flash memory consumption on 8-bit AVR processors by 500-600 bytes because it pulls in the free() and malloc() functions. On the 32-bit SAMD21, the flash memory increases by by about 350 bytes. On other 32-bit processors (STM32, ESP8266, ESP32, Teensy 3.2), the flash memory increase is modest, about 50-150 bytes.
Since a Coroutine is expected to be created statically, instead of the heap, a non-virtual destructor is good enough.
|
inlinestaticprotected |
Returns the current microseconds clock.
By default it returns the global micros() function from Arduino but can be overridden by providing a different T_CLOCK template parameter.
Definition at line 751 of file Coroutine.h.
|
inlinestaticprotected |
Returns the current millisecond clock.
By default it returns the global millis() function from Arduino but can be overridden by providing a different T_CLOCK template parameter.
Definition at line 742 of file Coroutine.h.
|
inlinestaticprotected |
Returns the current clock in unit of seconds, truncated to the lower 16-bits.
This is an approximation of (millis / 1000). It does not need to be perfectly accurate because COROUTINE_DELAY_SECONDS() is not guaranteed to be precise.
Definition at line 761 of file Coroutine.h.
|
inline |
Get name of the coroutine assuming it's a c-string.
Nullable.
Definition at line 375 of file Coroutine.h.
|
inline |
Get name of the coroutine assuming it's an f-string.
Nullable.
Definition at line 378 of file Coroutine.h.
|
inline |
Return the next pointer as a pointer to the pointer, similar to getRoot().
This makes it much easier to manipulate a singly-linked list. Also makes setNext() method unnecessary. Should be used only by CoroutineScheduler.
Definition at line 570 of file Coroutine.h.
|
inline |
|
inlinestatic |
Get the pointer to the root pointer.
Implemented as a function static to fix the C++ static initialization problem, making it safe to use this in other static contexts.
Definition at line 557 of file Coroutine.h.
|
inlineprotected |
Return the status of the coroutine.
Used by the CoroutineScheduler.
Definition at line 648 of file Coroutine.h.
|
inline |
The coroutine is either Ending or Terminated.
This method is recommended over isEnding() or isTerminated() because it works when the coroutine is executed either manually or through the CoroutineScheduler.
Definition at line 525 of file Coroutine.h.
|
inline |
The coroutine returned using COROUTINE_END().
In most cases, isDone() is recommended instead because it works when coroutines are executed manually or through the CoroutineScheduler.
Definition at line 510 of file Coroutine.h.
|
inline |
The coroutine is currently running.
True only within the coroutine.
Definition at line 503 of file Coroutine.h.
|
inline |
The coroutine was terminated by the scheduler with a call to setTerminated().
In most cases, isDone() should be used instead because it works when coroutines are executed manually or through the CoroutineScheudler.
Definition at line 518 of file Coroutine.h.
|
inline |
Print name to the given Printer.
If the name is null, then print the hexadecimal representation of the pointer to the coroutine.
printer | destination of output, usually Serial |
maxLen | truncate or pad to maxLen if given |
Definition at line 389 of file Coroutine.h.
|
inline |
Reset the coroutine to its initial state.
Only the Coroutine base-class state is reset to the original state. If the subclass runCoroutine() uses any static variables (for example, a loop counter), you must reset those variables manually as well, since this library does not have any knowledge about them.
It is expected that this method will be called from outside the runCoroutine() method. If it is called within the method, I'm not sure what will happen. I think the coroutine will abandon the current continuation point, and start executing from the beginning of the Coroutine upon the next iteration.
Definition at line 467 of file Coroutine.h.
|
inline |
Add a Suspended coroutine into the head of the scheduler linked list, and change the state to Yielding.
If the coroutine is in any other state, this method does nothing. This method works only if the CoroutineScheduler::loop() is used.
Definition at line 443 of file Coroutine.h.
|
pure virtual |
The body of the coroutine.
The COROUTINE macro creates a subclass of this class and puts the body of the coroutine into this method.
|
inline |
This is a variant of runCoroutine() which measures the execution time of runCoroutine() and updates the attached profiler if it exists.
On 8-bit processors, memory consumption can be reduced by calling the Coroutine::runCoroutine()
method directly in the global loop()
function, instead of using the CoroutineScheduler
. In such an environment, the end-user can enable profiling by manually changing the calls to Coroutine::runCoroutine()
to Coroutine::runCoroutineWithProfiler()
, and recompiling the program.
Definition at line 344 of file Coroutine.h.
|
inlineprotected |
Configure the delay timer for delayMicros.
Similar to seDelayMillis(), the maximum delay is 32767 micros.
Definition at line 713 of file Coroutine.h.
|
inlineprotected |
Configure the delay timer for delayMillis.
The maximum duration is set to (UINT16_MAX / 2) (i.e. 32767 milliseconds) if given a larger value. This makes the longest allowable time between two successive calls to isDelayExpired() for a given coroutine to be 32767 (UINT16_MAX - UINT16_MAX / 2 - 1) milliseconds, which should be long enough for all practical use-cases. (The '- 1' comes from an edge case where isDelayExpired() evaluates to be true in the CoroutineScheduler::runCoroutine() but becomes to be false in the COROUTINE_DELAY() macro inside Coroutine::runCoroutine()) because the clock increments by 1 millisecond.)
Definition at line 699 of file Coroutine.h.
|
inlineprotected |
Configure the delay timer for delaySeconds.
Similar to seDelayMillis(), the maximum delay is 32767 seconds.
Definition at line 727 of file Coroutine.h.
|
inlineprotected |
Set status to indicate that the Coroutine has been removed from the Scheduler queue.
Should be used only by the CoroutineScheduler.
Definition at line 684 of file Coroutine.h.
|
inlinevirtual |
Perform coroutine initialization.
This is intended to be called directly from the global setup()
function, or through the CoroutineScheduler::setupCoroutines()
method which should also be called from the global setup()
function.
If your coroutines do not override this method, hence do not need to perform any setup, then you should not call CoroutineScheduler::setupCoroutines()
to avoid consuming unnecessary flash memory. On AVR processors, each Coroutine::setupCoroutine()
seems to consume at least 50-60 bytes of flash memory overhead per coroutine. On 32-bit processors, the overhead seems to be only about 30-40 bytes per coroutine.
Definition at line 331 of file Coroutine.h.
|
inline |
Deprecated method that does nothing.
Starting v1.3, the setup into the singly-linked list is automatically performed by the constructor and this method no longer needs to be called manually. This method is retained for backwards compatibility.
Definition at line 543 of file Coroutine.h.
|
inline |
Deprecated method that does nothing.
Starting v1.3, the setup into the singly-linked list is automatically performed by the constructor and this method no longer needs to be called manually. This method is retained for backwards compatibility.
Definition at line 535 of file Coroutine.h.
|
inline |
Suspend the coroutine at the next scheduler iteration.
If the coroutine is already in the process of ending or is already terminated, then this method does nothing. A coroutine cannot use this method to suspend itself, it can only suspend some other coroutine. Currently, there is no ability for a coroutine to suspend itself. I think that would require the addition of a COROUTINE_SUSPEND() macro. Also, this method works only if the CoroutineScheduler::loop() is used because the suspend functionality is implemented by the CoroutineScheduler.
Definition at line 432 of file Coroutine.h.
|
staticprotected |
Coroutine is currenly running.
True only within the coroutine itself.
Definition at line 620 of file Coroutine.h.
|
staticprotected |
Coroutine has been suspended using suspend() and the scheduler should remove it from the queue upon the next iteration.
We don't distinguish whether the coroutine is still in the queue or not with this status. We can add that later if we need to.
Definition at line 611 of file Coroutine.h.
|
protected |
Delay time specified by COROUTINE_DELAY(), COROUTINE_DELAY_MICROS() or, COROUTINE_DELAY_SECONDS().
The unit of this number is context dependent, milliseconds, microseconds, or seconds.
Definition at line 813 of file Coroutine.h.
|
protected |
Start time provided by COROUTINE_DELAY(), COROUTINE_DELAY_MICROS(), or COROUTINE_DELAY_SECONDS().
The unit of this number is context dependent, milliseconds, microseconds, or seconds.
Definition at line 806 of file Coroutine.h.
|
protected |
Name of the coroutine, either (const char*) or (const __FlashStringHelper*).
(Optional)
Definition at line 793 of file Coroutine.h.