Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Fix indentation, add note that API is not stable
  • Loading branch information
igrr committed Jun 6, 2016
commit 1d0eb226c7b2c1d74fb02bc5678bae226fcf88c1
130 changes: 65 additions & 65 deletions cores/esp8266/Schedule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

struct scheduled_fn_t
{
scheduled_fn_t* mNext;
std::function<void(void)> mFunc;
scheduled_fn_t* mNext;
std::function<void(void)> mFunc;
};

static scheduled_fn_t* sFirst = 0;
Expand All @@ -16,82 +16,82 @@ static int sCount = 0;

static void init_lists()
{
if (sCount != 0) {
return;
}
while (sCount < SCHEDULED_FN_INITIAL_COUNT) {
scheduled_fn_t* it = new scheduled_fn_t;
if (sCount == 0) {
sFirstUnused = it;
}
else {
sLastUnused->mNext = it;
}
sLastUnused = it;
++sCount;
}
sLastUnused->mNext = NULL;
if (sCount != 0) {
return;
}
while (sCount < SCHEDULED_FN_INITIAL_COUNT) {
scheduled_fn_t* it = new scheduled_fn_t;
if (sCount == 0) {
sFirstUnused = it;
}
else {
sLastUnused->mNext = it;
}
sLastUnused = it;
++sCount;
}
sLastUnused->mNext = NULL;
}

static scheduled_fn_t* get_fn() {
scheduled_fn_t* result = NULL;
// try to get an item from unused items list
if (sFirstUnused) {
result = sFirstUnused;
sFirstUnused = result->mNext;
if (sFirstUnused == NULL) {
sLastUnused = NULL;
}
}
// if no unused items, and count not too high, allocate a new one
else if (sCount != SCHEDULED_FN_MAX_COUNT) {
result = new scheduled_fn_t;
result->mNext = NULL;
++sCount;
}
return result;
scheduled_fn_t* result = NULL;
// try to get an item from unused items list
if (sFirstUnused) {
result = sFirstUnused;
sFirstUnused = result->mNext;
if (sFirstUnused == NULL) {
sLastUnused = NULL;
}
}
// if no unused items, and count not too high, allocate a new one
else if (sCount != SCHEDULED_FN_MAX_COUNT) {
result = new scheduled_fn_t;
result->mNext = NULL;
++sCount;
}
return result;
}

static void recycle_fn(scheduled_fn_t* fn)
{
if (!sLastUnused) {
sFirstUnused = fn;
}
else {
sLastUnused->mNext = fn;
}
fn->mNext = NULL;
sLastUnused = fn;
if (!sLastUnused) {
sFirstUnused = fn;
}
else {
sLastUnused->mNext = fn;
}
fn->mNext = NULL;
sLastUnused = fn;
}

bool schedule_function(std::function<void(void)> fn)
{
scheduled_fn_t* item = get_fn();
if (!item) {
return false;
}
item->mFunc = fn;
item->mNext = NULL;
if (!sFirst) {
sFirst = item;
}
else {
sLast->mNext = item;
}
sLast = item;
return true;
scheduled_fn_t* item = get_fn();
if (!item) {
return false;
}
item->mFunc = fn;
item->mNext = NULL;
if (!sFirst) {
sFirst = item;
}
else {
sLast->mNext = item;
}
sLast = item;
return true;
}

void run_scheduled_functions()
{
while (sFirst) {
scheduled_fn_t* item = sFirst;
sFirst = item->mNext;
if (sFirst == NULL) {
sLast = NULL;
}
item->mFunc();
item->mFunc = std::function<void(void)>();
recycle_fn(item);
}
while (sFirst) {
scheduled_fn_t* item = sFirst;
sFirst = item->mNext;
if (sFirst == NULL) {
sLast = NULL;
}
item->mFunc();
item->mFunc = std::function<void(void)>();
recycle_fn(item);
}
}
8 changes: 8 additions & 0 deletions cores/esp8266/Schedule.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,17 @@
#define SCHEDULED_FN_MAX_COUNT 32
#define SCHEDULED_FN_INITIAL_COUNT 4

// Warning
// This API is not considered stable.
// Function signatures will change.
// You have been warned.

// Run given function next time `loop` function returns,
// or `run_scheduled_functions` is called.
// Use std::bind to pass arguments to a function, or call a class member function.
// Note: there is no mechanism for cancelling scheduled functions.
// Keep that in mind when binding functions to objects which may have short lifetime.
// Returns false if the number of scheduled functions exceeds SCHEDULED_FN_MAX_COUNT.
bool schedule_function(std::function<void(void)> fn);

// Run all scheduled functions.
Expand Down