Skip to content

Commit 1d0eb22

Browse files
committed
Fix indentation, add note that API is not stable
1 parent 23a03a7 commit 1d0eb22

File tree

2 files changed

+73
-65
lines changed

2 files changed

+73
-65
lines changed

cores/esp8266/Schedule.cpp

Lines changed: 65 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
struct scheduled_fn_t
44
{
5-
scheduled_fn_t* mNext;
6-
std::function<void(void)> mFunc;
5+
scheduled_fn_t* mNext;
6+
std::function<void(void)> mFunc;
77
};
88

99
static scheduled_fn_t* sFirst = 0;
@@ -16,82 +16,82 @@ static int sCount = 0;
1616

1717
static void init_lists()
1818
{
19-
if (sCount != 0) {
20-
return;
21-
}
22-
while (sCount < SCHEDULED_FN_INITIAL_COUNT) {
23-
scheduled_fn_t* it = new scheduled_fn_t;
24-
if (sCount == 0) {
25-
sFirstUnused = it;
26-
}
27-
else {
28-
sLastUnused->mNext = it;
29-
}
30-
sLastUnused = it;
31-
++sCount;
32-
}
33-
sLastUnused->mNext = NULL;
19+
if (sCount != 0) {
20+
return;
21+
}
22+
while (sCount < SCHEDULED_FN_INITIAL_COUNT) {
23+
scheduled_fn_t* it = new scheduled_fn_t;
24+
if (sCount == 0) {
25+
sFirstUnused = it;
26+
}
27+
else {
28+
sLastUnused->mNext = it;
29+
}
30+
sLastUnused = it;
31+
++sCount;
32+
}
33+
sLastUnused->mNext = NULL;
3434
}
3535

3636
static scheduled_fn_t* get_fn() {
37-
scheduled_fn_t* result = NULL;
38-
// try to get an item from unused items list
39-
if (sFirstUnused) {
40-
result = sFirstUnused;
41-
sFirstUnused = result->mNext;
42-
if (sFirstUnused == NULL) {
43-
sLastUnused = NULL;
44-
}
45-
}
46-
// if no unused items, and count not too high, allocate a new one
47-
else if (sCount != SCHEDULED_FN_MAX_COUNT) {
48-
result = new scheduled_fn_t;
49-
result->mNext = NULL;
50-
++sCount;
51-
}
52-
return result;
37+
scheduled_fn_t* result = NULL;
38+
// try to get an item from unused items list
39+
if (sFirstUnused) {
40+
result = sFirstUnused;
41+
sFirstUnused = result->mNext;
42+
if (sFirstUnused == NULL) {
43+
sLastUnused = NULL;
44+
}
45+
}
46+
// if no unused items, and count not too high, allocate a new one
47+
else if (sCount != SCHEDULED_FN_MAX_COUNT) {
48+
result = new scheduled_fn_t;
49+
result->mNext = NULL;
50+
++sCount;
51+
}
52+
return result;
5353
}
5454

5555
static void recycle_fn(scheduled_fn_t* fn)
5656
{
57-
if (!sLastUnused) {
58-
sFirstUnused = fn;
59-
}
60-
else {
61-
sLastUnused->mNext = fn;
62-
}
63-
fn->mNext = NULL;
64-
sLastUnused = fn;
57+
if (!sLastUnused) {
58+
sFirstUnused = fn;
59+
}
60+
else {
61+
sLastUnused->mNext = fn;
62+
}
63+
fn->mNext = NULL;
64+
sLastUnused = fn;
6565
}
6666

6767
bool schedule_function(std::function<void(void)> fn)
6868
{
69-
scheduled_fn_t* item = get_fn();
70-
if (!item) {
71-
return false;
72-
}
73-
item->mFunc = fn;
74-
item->mNext = NULL;
75-
if (!sFirst) {
76-
sFirst = item;
77-
}
78-
else {
79-
sLast->mNext = item;
80-
}
81-
sLast = item;
82-
return true;
69+
scheduled_fn_t* item = get_fn();
70+
if (!item) {
71+
return false;
72+
}
73+
item->mFunc = fn;
74+
item->mNext = NULL;
75+
if (!sFirst) {
76+
sFirst = item;
77+
}
78+
else {
79+
sLast->mNext = item;
80+
}
81+
sLast = item;
82+
return true;
8383
}
8484

8585
void run_scheduled_functions()
8686
{
87-
while (sFirst) {
88-
scheduled_fn_t* item = sFirst;
89-
sFirst = item->mNext;
90-
if (sFirst == NULL) {
91-
sLast = NULL;
92-
}
93-
item->mFunc();
94-
item->mFunc = std::function<void(void)>();
95-
recycle_fn(item);
96-
}
87+
while (sFirst) {
88+
scheduled_fn_t* item = sFirst;
89+
sFirst = item->mNext;
90+
if (sFirst == NULL) {
91+
sLast = NULL;
92+
}
93+
item->mFunc();
94+
item->mFunc = std::function<void(void)>();
95+
recycle_fn(item);
96+
}
9797
}

cores/esp8266/Schedule.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,17 @@
66
#define SCHEDULED_FN_MAX_COUNT 32
77
#define SCHEDULED_FN_INITIAL_COUNT 4
88

9+
// Warning
10+
// This API is not considered stable.
11+
// Function signatures will change.
12+
// You have been warned.
13+
914
// Run given function next time `loop` function returns,
1015
// or `run_scheduled_functions` is called.
1116
// Use std::bind to pass arguments to a function, or call a class member function.
17+
// Note: there is no mechanism for cancelling scheduled functions.
18+
// Keep that in mind when binding functions to objects which may have short lifetime.
19+
// Returns false if the number of scheduled functions exceeds SCHEDULED_FN_MAX_COUNT.
1220
bool schedule_function(std::function<void(void)> fn);
1321

1422
// Run all scheduled functions.

0 commit comments

Comments
 (0)