Skip to content

add documentation to scheduled functions #6234

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions cores/esp8266/Schedule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,9 @@ bool schedule_recurrent_function_us (const std::function<bool(void)>& fn, uint32
item->mFunc = fn;

if (rFirst)
{
item->mNext = rFirst;
rFirst = item;
}
else
rFirst = item;

rFirst = item;

return true;
}
Expand Down
34 changes: 28 additions & 6 deletions cores/esp8266/Schedule.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,21 @@

#define SCHEDULED_FN_MAX_COUNT 32

// The purpose of scheduled functions is to trigger, from SYS stack (like in
// an interrupt or a system event), registration of user code to be executed
// in user stack (called CONT stack) without the common restrictions from
// system context. Details are below.

// The purpose of recurrent scheduled function is to independantly execute
// user code in CONT stack on a regular basis.
// It has been introduced with ethernet service in mind, it can also be used
// for all libraries in the need of a regular `libdaemon_handlestuff()`.
// It allows these services to be run even from a user loop not going back
// to `loop()`.
// Ticker + scheduled function offer the same service but recurrent
// scheduled function happen more often: every yield() (vs every loop()),
// and time resolution is microsecond (vs millisecond). Details are below.

// scheduled functions called once:
//
// * internal queue is FIFO.
Expand All @@ -17,27 +32,34 @@
// * There is no mechanism for cancelling scheduled functions.
// * `yield` can be called from inside lambdas.
// * Returns false if the number of scheduled functions exceeds
// SCHEDULED_FN_MAX_COUNT.
// SCHEDULED_FN_MAX_COUNT (or memory shortage).
// * Run the lambda only once next time.
// * A scheduled function can schedule a function.

bool schedule_function (const std::function<void(void)>& fn);

// Run all scheduled functions.
// Use this function if your are not using `loop`, or `loop` does not return
// on a regular basis.
// Use this function if your are not using `loop`,
// or `loop` does not return on a regular basis.

void run_scheduled_functions();

// recurrent scheduled function:
//
// * internal queue if not FIFO.
// * Internal queue may not be a FIFO.
// * Run the lambda periodically about every <repeat_us> microseconds until
// it returns false.
// * Note that it may be more than <repeat_us> microseconds between calls if
// `yield` is not called frequently, and therefore should not be used for
// timing critical operations.
// * There is no mechanism for cancelling recurrent scheduled functions.
// * long running operations or yield() or delay() are not allowed in the lambda.
// * Please ensure variables or instances used from inside lambda will exist
// when lambda is later called.
// * There is no mechanism for externally cancelling recurrent scheduled
// functions. However a user function returning false will cancel itself.
// * Long running operations or yield() or delay() are not allowed in the
// recurrent function.
// * A recurrent function currently must not schedule another recurrent
// functions.

bool schedule_recurrent_function_us (const std::function<bool(void)>& fn, uint32_t repeat_us);

Expand Down