auto-sync

This commit is contained in:
rusEfi 2014-09-14 16:03:05 -05:00
parent eb8fea9bcd
commit a2965453d8
3 changed files with 25 additions and 11 deletions

View File

@ -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

View File

@ -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) {

View File

@ -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);