Skip to content

Commit 5a47cab

Browse files
authored
add documentation to scheduled functions (#6234)
* add documentation to scheduled functions
1 parent f9009b8 commit 5a47cab

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

cores/esp8266/Schedule.cpp

+2-5
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,9 @@ bool schedule_recurrent_function_us (const std::function<bool(void)>& fn, uint32
9898
item->mFunc = fn;
9999

100100
if (rFirst)
101-
{
102101
item->mNext = rFirst;
103-
rFirst = item;
104-
}
105-
else
106-
rFirst = item;
102+
103+
rFirst = item;
107104

108105
return true;
109106
}

cores/esp8266/Schedule.h

+28-6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,21 @@
55

66
#define SCHEDULED_FN_MAX_COUNT 32
77

8+
// The purpose of scheduled functions is to trigger, from SYS stack (like in
9+
// an interrupt or a system event), registration of user code to be executed
10+
// in user stack (called CONT stack) without the common restrictions from
11+
// system context. Details are below.
12+
13+
// The purpose of recurrent scheduled function is to independantly execute
14+
// user code in CONT stack on a regular basis.
15+
// It has been introduced with ethernet service in mind, it can also be used
16+
// for all libraries in the need of a regular `libdaemon_handlestuff()`.
17+
// It allows these services to be run even from a user loop not going back
18+
// to `loop()`.
19+
// Ticker + scheduled function offer the same service but recurrent
20+
// scheduled function happen more often: every yield() (vs every loop()),
21+
// and time resolution is microsecond (vs millisecond). Details are below.
22+
823
// scheduled functions called once:
924
//
1025
// * internal queue is FIFO.
@@ -17,27 +32,34 @@
1732
// * There is no mechanism for cancelling scheduled functions.
1833
// * `yield` can be called from inside lambdas.
1934
// * Returns false if the number of scheduled functions exceeds
20-
// SCHEDULED_FN_MAX_COUNT.
35+
// SCHEDULED_FN_MAX_COUNT (or memory shortage).
2136
// * Run the lambda only once next time.
37+
// * A scheduled function can schedule a function.
2238

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

2541
// Run all scheduled functions.
26-
// Use this function if your are not using `loop`, or `loop` does not return
27-
// on a regular basis.
42+
// Use this function if your are not using `loop`,
43+
// or `loop` does not return on a regular basis.
2844

2945
void run_scheduled_functions();
3046

3147
// recurrent scheduled function:
3248
//
33-
// * internal queue if not FIFO.
49+
// * Internal queue may not be a FIFO.
3450
// * Run the lambda periodically about every <repeat_us> microseconds until
3551
// it returns false.
3652
// * Note that it may be more than <repeat_us> microseconds between calls if
3753
// `yield` is not called frequently, and therefore should not be used for
3854
// timing critical operations.
39-
// * There is no mechanism for cancelling recurrent scheduled functions.
40-
// * long running operations or yield() or delay() are not allowed in the lambda.
55+
// * Please ensure variables or instances used from inside lambda will exist
56+
// when lambda is later called.
57+
// * There is no mechanism for externally cancelling recurrent scheduled
58+
// functions. However a user function returning false will cancel itself.
59+
// * Long running operations or yield() or delay() are not allowed in the
60+
// recurrent function.
61+
// * A recurrent function currently must not schedule another recurrent
62+
// functions.
4163

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

0 commit comments

Comments
 (0)