From a2965453d84c0afcbd197de479f7738808a1c59a Mon Sep 17 00:00:00 2001 From: rusEfi Date: Sun, 14 Sep 2014 16:03:05 -0500 Subject: [PATCH] auto-sync --- .../system/SingleTimerExecutor.cpp | 23 +++++++++++++------ firmware/controllers/system/event_queue.cpp | 11 ++++++--- firmware/controllers/system/event_queue.h | 2 +- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/firmware/controllers/system/SingleTimerExecutor.cpp b/firmware/controllers/system/SingleTimerExecutor.cpp index 060c8dd5e4..b125bec626 100644 --- a/firmware/controllers/system/SingleTimerExecutor.cpp +++ b/firmware/controllers/system/SingleTimerExecutor.cpp @@ -43,7 +43,8 @@ void Executor::unlock(void) { unlockAnyContext(); } -void Executor::schedule2(const char *prefix, scheduling_s *scheduling, uint64_t timeUs, schfunc_t callback, void *param) { +void Executor::schedule2(const char *prefix, scheduling_s *scheduling, uint64_t timeUs, schfunc_t callback, + void *param) { // if (delayUs < 0) { // firmwareError("Negative delayUs %s: %d", prefix, delayUs); // return; @@ -63,8 +64,8 @@ void Executor::schedule2(const char *prefix, scheduling_s *scheduling, uint64_t } } - -void Executor::schedule(const char *prefix, scheduling_s *scheduling, uint64_t nowUs, int delayUs, schfunc_t callback, void *param) { +void Executor::schedule(const char *prefix, scheduling_s *scheduling, uint64_t nowUs, int delayUs, schfunc_t callback, + void *param) { schedule2(prefix, scheduling, nowUs + delayUs, callback, param); } @@ -84,15 +85,24 @@ void Executor::doExecute() { * further invocations */ reentrantLock = TRUE; + bool shouldExecute = true; /** - * It's worth noting that that the actions might be adding new actions into the queue + * in real life it could be that while we executing listeners time passes and it's already time to execute + * next listeners. + * TODO: add a counter & figure out a limit of iterations? */ - uint64_t nowUs = getTimeNowUs(); - queue.executeAll(nowUs); + while (shouldExecute) { + /** + * It's worth noting that that the actions might be adding new actions into the queue + */ + uint64_t nowUs = getTimeNowUs(); + shouldExecute = queue.executeAll(nowUs); + } if (!isLocked()) { firmwareError("Someone has stolen my lock"); return; } + uint64_t nowUs = getTimeNowUs(); reentrantLock = false; /** * 'executeAll' is potentially invoking heavy callbacks, let's grab fresh time value? @@ -124,7 +134,6 @@ void scheduleTask2(const char *prefix, scheduling_s *scheduling, uint64_t time, instance.schedule2(prefix, scheduling, time, callback, param); } - void initSignalExecutorImpl(void) { globalTimerCallback = executorCallback; #if EFI_PROD_CODE diff --git a/firmware/controllers/system/event_queue.cpp b/firmware/controllers/system/event_queue.cpp index f4384f47b9..410edd51c9 100644 --- a/firmware/controllers/system/event_queue.cpp +++ b/firmware/controllers/system/event_queue.cpp @@ -61,7 +61,7 @@ uint64_t EventQueue::getNextEventTime(uint64_t nowUs) { * looks like we end up here after 'writeconfig' (which freezes the firmware) - we are late * for the next scheduled event */ - uint64_t mock = nowUs + MS2US(10); + uint64_t mock = nowUs + 100; if (mock < result) result = mock; } else { @@ -74,8 +74,9 @@ uint64_t EventQueue::getNextEventTime(uint64_t nowUs) { /** * Invoke all pending actions prior to specified timestamp + * @return true if at least one action was executed */ -void EventQueue::executeAll(uint64_t now) { +bool EventQueue::executeAll(uint64_t now) { scheduling_s * current, *tmp; scheduling_s * executionList = NULL; @@ -87,7 +88,7 @@ void EventQueue::executeAll(uint64_t now) { { if (++counter > QUEUE_LENGTH_LIMIT) { firmwareError("Is this list looped?"); - return; + return false; } if (current->momentUs <= now) { LL_DELETE(head, current); @@ -99,8 +100,12 @@ void EventQueue::executeAll(uint64_t now) { * we need safe iteration here because 'callback' might change change 'current->next' * while re-inserting it into the queue from within the callback */ + bool result = (executionList != NULL); LL_FOREACH_SAFE(executionList, current, tmp) + { current->callback(current->param); + } + return result; } int EventQueue::size(void) { diff --git a/firmware/controllers/system/event_queue.h b/firmware/controllers/system/event_queue.h index 951493cbab..26393b549a 100644 --- a/firmware/controllers/system/event_queue.h +++ b/firmware/controllers/system/event_queue.h @@ -40,7 +40,7 @@ public: void insertTask(scheduling_s *scheduling, uint64_t timeUs, schfunc_t callback, void *param); - void executeAll(uint64_t now); + bool executeAll(uint64_t now); uint64_t getNextEventTime(uint64_t nowUs); void clear(void);