rusefi-1/firmware/controllers/scheduling/event_queue.cpp

231 lines
6.2 KiB
C++
Raw Normal View History

2015-07-10 06:01:56 -07:00
/**
* @file event_queue.cpp
* This is a data structure which keeps track of all pending events
* Implemented as a linked list, which is fine since the number of
* pending events is pretty low
* todo: MAYBE migrate to a better data structure, but that's low priority
*
* this data structure is NOT thread safe
*
* @date Apr 17, 2014
2018-01-20 17:55:31 -08:00
* @author Andrey Belomutskiy, (c) 2012-2018
2015-07-10 06:01:56 -07:00
*/
2018-09-16 19:26:57 -07:00
#include "global.h"
2015-07-10 06:01:56 -07:00
#include "event_queue.h"
#include "efitime.h"
#include "rfiutil.h"
2015-07-10 06:01:56 -07:00
2017-05-21 08:09:47 -07:00
uint32_t maxSchedulingPrecisionLoss = 0;
2015-07-10 06:01:56 -07:00
scheduling_s::scheduling_s() {
callback = NULL;
next = NULL;
param = NULL;
isScheduled = false;
momentX = 0;
}
EventQueue::EventQueue() {
head = NULL;
setLateDelay(100);
}
bool EventQueue::checkIfPending(scheduling_s *scheduling) {
return assertNotInList<scheduling_s>(head, scheduling);
}
/**
* @return true if inserted into the head of the list
*/
2017-05-15 06:21:40 -07:00
bool EventQueue::insertTask(scheduling_s *scheduling, efitime_t timeX, schfunc_t callback, void *param) {
2016-11-30 20:02:42 -08:00
#if EFI_UNIT_TEST || defined(__DOXYGEN__)
2015-07-10 06:01:56 -07:00
assertListIsSorted();
2016-08-27 07:03:11 -07:00
#endif /* EFI_UNIT_TEST */
2018-07-25 20:30:00 -07:00
efiAssert(CUSTOM_ERR_ASSERT, callback != NULL, "NULL callback", false);
2015-07-10 06:01:56 -07:00
2016-09-23 22:02:43 -07:00
// please note that simulator does not use this code at all - simulator uses signal_executor_sleep
2016-09-04 22:03:25 -07:00
if (scheduling->isScheduled) {
2016-09-23 22:02:43 -07:00
#if EFI_UNIT_TEST || defined(__DOXYGEN__)
2016-09-23 21:03:14 -07:00
printf("Already scheduled was %d\r\n", (int)scheduling->momentX);
printf("Already scheduled now %d\r\n", (int)timeX);
2017-06-03 20:36:34 -07:00
#endif /* EFI_UNIT_TEST */
2015-07-10 06:01:56 -07:00
return false;
2016-09-04 22:03:25 -07:00
}
2015-07-10 06:01:56 -07:00
scheduling->momentX = timeX;
scheduling->callback = callback;
scheduling->param = param;
scheduling->isScheduled = true;
if (head == NULL || timeX < head->momentX) {
2018-01-28 15:04:41 -08:00
// here we insert into head of the linked list
2015-07-10 06:01:56 -07:00
LL_PREPEND(head, scheduling);
2017-03-05 11:33:35 -08:00
#if EFI_UNIT_TEST || defined(__DOXYGEN__)
2015-07-10 06:01:56 -07:00
assertListIsSorted();
2016-08-27 07:03:11 -07:00
#endif /* EFI_UNIT_TEST */
2015-07-10 06:01:56 -07:00
return true;
} else {
2018-01-28 15:04:41 -08:00
// here we know we are not in the head of the list, let's find the position - linear search
2015-07-10 06:01:56 -07:00
scheduling_s *insertPosition = head;
while (insertPosition->next != NULL && insertPosition->next->momentX < timeX) {
insertPosition = insertPosition->next;
}
scheduling->next = insertPosition->next;
insertPosition->next = scheduling;
2016-11-30 20:02:42 -08:00
#if EFI_UNIT_TEST || defined(__DOXYGEN__)
2015-07-10 06:01:56 -07:00
assertListIsSorted();
2016-08-27 07:03:11 -07:00
#endif /* EFI_UNIT_TEST */
2015-07-10 06:01:56 -07:00
return false;
}
}
/**
* On this layer it does not matter which units are used - us, ms ot nt.
*
* This method is always invoked under a lock
* @return Get the timestamp of the soonest pending action, skipping all the actions in the past
*/
efitime_t EventQueue::getNextEventTime(efitime_t nowX) {
2016-12-17 06:02:59 -08:00
2015-07-10 06:01:56 -07:00
if (head != NULL) {
if (head->momentX <= nowX) {
/**
* We are here if action timestamp is in the past
*
* looks like we end up here after 'writeconfig' (which freezes the firmware) - we are late
* for the next scheduled event
*/
efitime_t aBitInTheFuture = nowX + lateDelay;
return aBitInTheFuture;
} else {
return head->momentX;
}
}
return EMPTY_QUEUE;
}
static scheduling_s * longScheduling;
2017-05-21 07:25:35 -07:00
/**
* See also maxPrecisionCallbackDuration for total hw callback time
*/
uint32_t maxEventCallbackDuration = 0;
static uint32_t lastEventCallbackDuration;
2015-07-10 06:01:56 -07:00
/**
* Invoke all pending actions prior to specified timestamp
* @return number of executed actions
*/
int EventQueue::executeAll(efitime_t now) {
scheduling_s * current, *tmp;
scheduling_s * executionList = NULL;
2016-09-03 19:01:51 -07:00
scheduling_s * lastInExecutionList = NULL;
2015-07-10 06:01:56 -07:00
int listIterationCounter = 0;
int executionCounter = 0;
// we need safe iteration because we are removing elements inside the loop
LL_FOREACH_SAFE(head, current, tmp)
{
2018-07-25 20:30:00 -07:00
efiAssert(CUSTOM_ERR_ASSERT, current->callback != NULL, "callback==null1", 0);
2015-07-10 06:01:56 -07:00
if (++listIterationCounter > QUEUE_LENGTH_LIMIT) {
2017-04-19 19:03:14 -07:00
firmwareError(CUSTOM_LIST_LOOP, "Is this list looped?");
2015-07-10 06:01:56 -07:00
return false;
}
if (current->momentX <= now) {
executionCounter++;
2018-07-25 20:30:00 -07:00
efiAssert(CUSTOM_ERR_ASSERT, head == current, "removing from head", -1);
2015-07-10 06:01:56 -07:00
//LL_DELETE(head, current);
head = head->next;
2016-09-03 19:01:51 -07:00
if (executionList == NULL) {
lastInExecutionList = executionList = current;
} else {
lastInExecutionList->next = current;
lastInExecutionList = current;
}
current->next = NULL;
2015-07-10 06:01:56 -07:00
} else {
/**
* The list is sorted. Once we find one action in the future, all the remaining ones
* are also in the future.
*/
break;
}
}
2017-03-05 11:33:35 -08:00
#if EFI_UNIT_TEST || defined(__DOXYGEN__)
2015-07-10 06:01:56 -07:00
assertListIsSorted();
#endif
/*
* we need safe iteration here because 'callback' might change change 'current->next'
* while re-inserting it into the queue from within the callback
*/
LL_FOREACH_SAFE(executionList, current, tmp)
{
2018-07-25 20:30:00 -07:00
efiAssert(CUSTOM_ERR_ASSERT, current->callback != NULL, "callback==null2", 0);
2015-07-10 06:01:56 -07:00
uint32_t before = GET_TIMESTAMP();
current->isScheduled = false;
2017-05-21 08:09:47 -07:00
uint32_t howFarOff = now - current->momentX;
maxSchedulingPrecisionLoss = maxI(maxSchedulingPrecisionLoss, howFarOff);
2016-09-23 22:02:43 -07:00
#if EFI_UNIT_TEST || defined(__DOXYGEN__)
2018-12-08 19:06:24 -08:00
printf("QUEUE: execute current=%d param=%d\r\n", (long)current, (long)current->param);
2016-09-23 22:02:43 -07:00
#endif
2015-07-10 06:01:56 -07:00
current->callback(current->param);
// even with overflow it's safe to subtract here
2017-05-21 07:25:35 -07:00
lastEventCallbackDuration = GET_TIMESTAMP() - before;
if (lastEventCallbackDuration > maxEventCallbackDuration)
maxEventCallbackDuration = lastEventCallbackDuration;
if (lastEventCallbackDuration > 2000) {
2015-07-10 06:01:56 -07:00
longScheduling = current;
2017-05-21 07:25:35 -07:00
// what is this line about? lastEventCallbackDuration++;
2015-07-10 06:01:56 -07:00
}
}
return executionCounter;
}
int EventQueue::size(void) {
scheduling_s *tmp;
int result;
LL_COUNT(head, tmp, result);
return result;
}
2016-11-30 20:02:42 -08:00
#if EFI_UNIT_TEST || defined(__DOXYGEN__)
2015-07-10 06:01:56 -07:00
void EventQueue::assertListIsSorted() {
scheduling_s *current = head;
while (current != NULL && current->next != NULL) {
2018-07-25 20:03:04 -07:00
efiAssertVoid(CUSTOM_ERR_6623, current->momentX <= current->next->momentX, "list order");
2015-07-10 06:01:56 -07:00
current = current->next;
}
}
#endif
void EventQueue::setLateDelay(int value) {
lateDelay = value;
}
scheduling_s * EventQueue::getHead() {
return head;
}
scheduling_s *EventQueue::getForUnitText(int index) {
scheduling_s * current;
LL_FOREACH(head, current)
{
if (index == 0)
return current;
index--;
}
2018-12-08 16:15:24 -08:00
#if EFI_UNIT_TEST
firmwareError(OBD_PCM_Processor_Fault, "getForUnitText: null");
#endif /* EFI_UNIT_TEST */
2015-07-10 06:01:56 -07:00
return NULL;
}
void EventQueue::clear(void) {
head = NULL;
}